2008-10-21 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini-sparc.c
1 /*
2  * mini-sparc.c: Sparc backend for the Mono code generator
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Dietmar Maurer (dietmar@ximian.com)
7  *
8  * Modified for SPARC:
9  *   Christopher Taylor (ct@gentoo.org)
10  *   Mark Crichton (crichton@gimp.org)
11  *   Zoltan Varga (vargaz@freemail.hu)
12  *
13  * (C) 2003 Ximian, Inc.
14  */
15 #include "mini.h"
16 #include <string.h>
17 #include <pthread.h>
18 #include <unistd.h>
19
20 #ifndef __linux__
21 #include <sys/systeminfo.h>
22 #include <thread.h>
23 #endif
24
25 #include <unistd.h>
26 #include <sys/mman.h>
27
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>
32
33 #include "mini-sparc.h"
34 #include "inssel.h"
35 #include "trace.h"
36 #include "cpu-sparc.h"
37 #include "jit-icalls.h"
38
39 /*
40  * Sparc V9 means two things:
41  * - the instruction set
42  * - the ABI
43  *
44  * V9 instructions are only usable if the underlying processor is 64 bit. Most Sparc 
45  * processors in use are 64 bit processors. The V9 ABI is only usable if the 
46  * mono executable is a 64 bit executable. So it would make sense to use the 64 bit
47  * instructions without using the 64 bit ABI.
48  */
49
50 /*
51  * Register usage:
52  * - %i0..%i<n> hold the incoming arguments, these are never written by JITted 
53  * code. Unused input registers are used for global register allocation.
54  * - %o0..%o5 and %l7 is used for local register allocation and passing arguments
55  * - %l0..%l6 is used for global register allocation
56  * - %o7 and %g1 is used as scratch registers in opcodes
57  * - all floating point registers are used for local register allocation except %f0. 
58  *   Only double precision registers are used.
59  * In 64 bit mode:
60  * - fp registers %d0..%d30 are used for parameter passing, and %d32..%d62 are
61  *   used for local allocation.
62  */
63
64 /*
65  * Alignment:
66  * - doubles and longs must be stored in dword aligned locations
67  */
68
69 /*
70  * The following things are not implemented or do not work:
71  *  - some fp arithmetic corner cases
72  * The following tests in mono/mini are expected to fail:
73  *  - test_0_simple_double_casts
74  *      This test casts (guint64)-1 to double and then back to guint64 again.
75  *    Under x86, it returns 0, while under sparc it returns -1.
76  *
77  * In addition to this, the runtime requires the trunc function, or its 
78  * solaris counterpart, aintl, to do some double->int conversions. If this 
79  * function is not available, it is emulated somewhat, but the results can be
80  * strange.
81  */
82
83 /*
84  * SPARCV9 FIXME:
85  * - optimize sparc_set according to the memory model
86  * - when non-AOT compiling, compute patch targets immediately so we don't
87  *   have to emit the 6 byte template.
88  * - varags
89  * - struct arguments/returns
90  */
91
92 /*
93  * SPARCV9 ISSUES:
94  * - sparc_call_simple can't be used in a lot of places since the displacement
95  *   might not fit into an imm30.
96  * - g1 can't be used in a lot of places since it is used as a scratch reg in
97  *   sparc_set.
98  * - sparc_f0 can't be used as a scratch register on V9
99  * - the %d34..%d62 fp registers are encoded as: %dx = %f(x - 32 + 1), ie.
100  *   %d36 = %f5.
101  * - ldind.i4/u4 needs to sign extend/clear out upper word -> slows things down
102  * - ins->dreg can't be used as a scatch register in r4 opcodes since it might
103  *   be a double precision register which has no single precision part.
104  * - passing/returning structs is hard to implement, because:
105  *   - the spec is very hard to understand
106  *   - it requires knowledge about the fields of structure, needs to handle
107  *     nested structures etc.
108  */
109
110 /*
111  * Possible optimizations:
112  * - delay slot scheduling
113  * - allocate large constants to registers
114  * - add more mul/div/rem optimizations
115  */
116
117 #ifndef __linux__
118 #define MONO_SPARC_THR_TLS 1
119 #endif
120
121 /*
122  * There was a 64 bit bug in glib-2.2: g_bit_nth_msf (0, -1) would return 32,
123  * causing infinite loops in dominator computation. So glib-2.4 is required.
124  */
125 #ifdef SPARCV9
126 #if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 4
127 #error "glib 2.4 or later is required for 64 bit mode."
128 #endif
129 #endif
130
131 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
132
133 #define SIGNAL_STACK_SIZE (64 * 1024)
134
135 #define STACK_BIAS MONO_SPARC_STACK_BIAS
136
137 #ifdef SPARCV9
138
139 /* %g1 is used by sparc_set */
140 #define GP_SCRATCH_REG sparc_g4
141 /* %f0 is used for parameter passing */
142 #define FP_SCRATCH_REG sparc_f30
143 #define ARGS_OFFSET (STACK_BIAS + 128)
144
145 #else
146
147 #define FP_SCRATCH_REG sparc_f0
148 #define ARGS_OFFSET 68
149 #define GP_SCRATCH_REG sparc_g1
150
151 #endif
152
153 /* Whenever the CPU supports v9 instructions */
154 static gboolean sparcv9 = FALSE;
155
156 /* Whenever this is a 64bit executable */
157 #if SPARCV9
158 static gboolean v64 = TRUE;
159 #else
160 static gboolean v64 = FALSE;
161 #endif
162
163 static gpointer mono_arch_get_lmf_addr (void);
164
165 const char*
166 mono_arch_regname (int reg) {
167         static const char * rnames[] = {
168                 "sparc_g0", "sparc_g1", "sparc_g2", "sparc_g3", "sparc_g4",
169                 "sparc_g5", "sparc_g6", "sparc_g7", "sparc_o0", "sparc_o1",
170                 "sparc_o2", "sparc_o3", "sparc_o4", "sparc_o5", "sparc_sp",
171                 "sparc_call", "sparc_l0", "sparc_l1", "sparc_l2", "sparc_l3",
172                 "sparc_l4", "sparc_l5", "sparc_l6", "sparc_l7", "sparc_i0",
173                 "sparc_i1", "sparc_i2", "sparc_i3", "sparc_i4", "sparc_i5",
174                 "sparc_fp", "sparc_retadr"
175         };
176         if (reg >= 0 && reg < 32)
177                 return rnames [reg];
178         return "unknown";
179 }
180
181 const char*
182 mono_arch_fregname (int reg) {
183         static const char *rnames [] = {
184                 "sparc_f0", "sparc_f1", "sparc_f2", "sparc_f3", "sparc_f4", 
185                 "sparc_f5", "sparc_f6", "sparc_f7", "sparc_f8", "sparc_f9",
186                 "sparc_f10", "sparc_f11", "sparc_f12", "sparc_f13", "sparc_f14", 
187                 "sparc_f15", "sparc_f16", "sparc_f17", "sparc_f18", "sparc_f19",
188                 "sparc_f20", "sparc_f21", "sparc_f22", "sparc_f23", "sparc_f24", 
189                 "sparc_f25", "sparc_f26", "sparc_f27", "sparc_f28", "sparc_f29",
190                 "sparc_f30", "sparc_f31"
191         };
192
193         if (reg >= 0 && reg < 32)
194                 return rnames [reg];
195         else
196                 return "unknown";
197 }
198
199 /*
200  * Initialize the cpu to execute managed code.
201  */
202 void
203 mono_arch_cpu_init (void)
204 {
205         guint32 dummy;
206         /* make sure sparcv9 is initialized for embedded use */
207         mono_arch_cpu_optimizazions(&dummy);
208 }
209
210 /*
211  * Initialize architecture specific code.
212  */
213 void
214 mono_arch_init (void)
215 {
216 }
217
218 /*
219  * Cleanup architecture specific code.
220  */
221 void
222 mono_arch_cleanup (void)
223 {
224 }
225
226 /*
227  * This function returns the optimizations supported on this cpu.
228  */
229 guint32
230 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
231 {
232         char buf [1024];
233         guint32 opts = 0;
234
235         *exclude_mask = 0;
236
237 #ifndef __linux__
238         if (!sysinfo (SI_ISALIST, buf, 1024))
239                 g_assert_not_reached ();
240 #else
241         /* From glibc.  If the getpagesize is 8192, we're on sparc64, which
242          * (in)directly implies that we're a v9 or better.
243          * Improvements to this are greatly accepted...
244          * Also, we don't differentiate between v7 and v8.  I sense SIGILL
245          * sniffing in my future.  
246          */
247         if (getpagesize() == 8192)
248                 strcpy (buf, "sparcv9");
249         else
250                 strcpy (buf, "sparcv8");
251 #endif
252
253         /* 
254          * On some processors, the cmov instructions are even slower than the
255          * normal ones...
256          */
257         if (strstr (buf, "sparcv9")) {
258                 opts |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
259                 sparcv9 = TRUE;
260         }
261         else
262                 *exclude_mask |= MONO_OPT_CMOV | MONO_OPT_FCMOV;
263
264         return opts;
265 }
266
267 #ifdef __GNUC__
268 #define flushi(addr)    __asm__ __volatile__ ("iflush %0"::"r"(addr):"memory")
269 #else /* assume Sun's compiler */
270 static void flushi(void *addr)
271 {
272     asm("flush %i0");
273 }
274 #endif
275
276 #ifndef __linux__
277 void sync_instruction_memory(caddr_t addr, int len);
278 #endif
279
280 void
281 mono_arch_flush_icache (guint8 *code, gint size)
282 {
283 #ifndef __linux__
284         /* Hopefully this is optimized based on the actual CPU */
285         sync_instruction_memory (code, size);
286 #else
287         gulong start = (gulong) code;
288         gulong end = start + size;
289         gulong align;
290
291         /* Sparcv9 chips only need flushes on 32 byte
292          * cacheline boundaries.
293          *
294          * Sparcv8 needs a flush every 8 bytes.
295          */
296         align = (sparcv9 ? 32 : 8);
297
298         start &= ~(align - 1);
299         end = (end + (align - 1)) & ~(align - 1);
300
301         while (start < end) {
302 #ifdef __GNUC__
303                 __asm__ __volatile__ ("iflush %0"::"r"(start));
304 #else
305                 flushi (start);
306 #endif
307                 start += align;
308         }
309 #endif
310 }
311
312 /*
313  * mono_sparc_flushw:
314  *
315  * Flush all register windows to memory. Every register window is saved to
316  * a 16 word area on the stack pointed to by its %sp register.
317  */
318 void
319 mono_sparc_flushw (void)
320 {
321         static guint32 start [64];
322         static int inited = 0;
323         guint32 *code;
324         static void (*flushw) (void);
325
326         if (!inited) {
327                 code = start;
328
329                 sparc_save_imm (code, sparc_sp, -160, sparc_sp);
330                 sparc_flushw (code);
331                 sparc_ret (code);
332                 sparc_restore_simple (code);
333
334                 g_assert ((code - start) < 64);
335
336                 mono_arch_flush_icache ((guint8*)start, (guint8*)code - (guint8*)start);
337
338                 flushw = (gpointer)start;
339
340                 inited = 1;
341         }
342
343         flushw ();
344 }
345
346 void
347 mono_arch_flush_register_windows (void)
348 {
349         mono_sparc_flushw ();
350 }
351
352 gboolean 
353 mono_arch_is_inst_imm (gint64 imm)
354 {
355         return sparc_is_imm13 (imm);
356 }
357
358 gboolean 
359 mono_sparc_is_v9 (void) {
360         return sparcv9;
361 }
362
363 gboolean 
364 mono_sparc_is_sparc64 (void) {
365         return v64;
366 }
367
368 typedef enum {
369         ArgInIReg,
370         ArgInIRegPair,
371         ArgInSplitRegStack,
372         ArgInFReg,
373         ArgInFRegPair,
374         ArgOnStack,
375         ArgOnStackPair,
376         ArgInFloatReg,  /* V9 only */
377         ArgInDoubleReg  /* V9 only */
378 } ArgStorage;
379
380 typedef struct {
381         gint16 offset;
382         /* This needs to be offset by %i0 or %o0 depending on caller/callee */
383         gint8  reg;
384         ArgStorage storage;
385         guint32 vt_offset; /* for valuetypes */
386 } ArgInfo;
387
388 typedef struct {
389         int nargs;
390         guint32 stack_usage;
391         guint32 reg_usage;
392         ArgInfo ret;
393         ArgInfo sig_cookie;
394         ArgInfo args [1];
395 } CallInfo;
396
397 #define DEBUG(a)
398
399 /* %o0..%o5 */
400 #define PARAM_REGS 6
401
402 static void inline
403 add_general (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean pair)
404 {
405         ainfo->offset = *stack_size;
406
407         if (!pair) {
408                 if (*gr >= PARAM_REGS) {
409                         ainfo->storage = ArgOnStack;
410                 }
411                 else {
412                         ainfo->storage = ArgInIReg;
413                         ainfo->reg = *gr;
414                         (*gr) ++;
415                 }
416
417                 /* Allways reserve stack space for parameters passed in registers */
418                 (*stack_size) += sizeof (gpointer);
419         }
420         else {
421                 if (*gr < PARAM_REGS - 1) {
422                         /* A pair of registers */
423                         ainfo->storage = ArgInIRegPair;
424                         ainfo->reg = *gr;
425                         (*gr) += 2;
426                 }
427                 else if (*gr >= PARAM_REGS) {
428                         /* A pair of stack locations */
429                         ainfo->storage = ArgOnStackPair;
430                 }
431                 else {
432                         ainfo->storage = ArgInSplitRegStack;
433                         ainfo->reg = *gr;
434                         (*gr) ++;
435                 }
436
437                 (*stack_size) += 2 * sizeof (gpointer);
438         }
439 }
440
441 #ifdef SPARCV9
442
443 #define FLOAT_PARAM_REGS 32
444
445 static void inline
446 add_float (guint32 *gr, guint32 *stack_size, ArgInfo *ainfo, gboolean single)
447 {
448         ainfo->offset = *stack_size;
449
450         if (single) {
451                 if (*gr >= FLOAT_PARAM_REGS) {
452                         ainfo->storage = ArgOnStack;
453                 }
454                 else {
455                         /* A single is passed in an even numbered fp register */
456                         ainfo->storage = ArgInFloatReg;
457                         ainfo->reg = *gr + 1;
458                         (*gr) += 2;
459                 }
460         }
461         else {
462                 if (*gr < FLOAT_PARAM_REGS) {
463                         /* A double register */
464                         ainfo->storage = ArgInDoubleReg;
465                         ainfo->reg = *gr;
466                         (*gr) += 2;
467                 }
468                 else {
469                         ainfo->storage = ArgOnStack;
470                 }
471         }
472
473         (*stack_size) += sizeof (gpointer);
474 }
475
476 #endif
477
478 /*
479  * get_call_info:
480  *
481  *  Obtain information about a call according to the calling convention.
482  * For V8, see the "System V ABI, Sparc Processor Supplement" Sparc V8 version 
483  * document for more information.
484  * For V9, see the "Low Level System Information (64-bit psABI)" chapter in
485  * the 'Sparc Compliance Definition 2.4' document.
486  */
487 static CallInfo*
488 get_call_info (MonoCompile *cfg, MonoMethodSignature *sig, gboolean is_pinvoke)
489 {
490         guint32 i, gr, fr;
491         int n = sig->hasthis + sig->param_count;
492         guint32 stack_size = 0;
493         CallInfo *cinfo;
494         MonoType *ret_type;
495         MonoGenericSharingContext *gsctx = cfg ? cfg->generic_sharing_context : NULL;
496
497         cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
498
499         gr = 0;
500         fr = 0;
501
502 #ifdef SPARCV9
503         if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
504                 /* The address of the return value is passed in %o0 */
505                 add_general (&gr, &stack_size, &cinfo->ret, FALSE);
506                 cinfo->ret.reg += sparc_i0;
507         }
508 #endif
509
510         /* this */
511         if (sig->hasthis)
512                 add_general (&gr, &stack_size, cinfo->args + 0, FALSE);
513
514         if ((sig->call_convention == MONO_CALL_VARARG) && (n == 0)) {
515                 gr = PARAM_REGS;
516
517                 /* Emit the signature cookie just before the implicit arguments */
518                 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
519         }
520
521         for (i = 0; i < sig->param_count; ++i) {
522                 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
523                 MonoType *ptype;
524
525                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
526                         gr = PARAM_REGS;
527
528                         /* Emit the signature cookie just before the implicit arguments */
529                         add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
530                 }
531
532                 DEBUG(printf("param %d: ", i));
533                 if (sig->params [i]->byref) {
534                         DEBUG(printf("byref\n"));
535                         
536                         add_general (&gr, &stack_size, ainfo, FALSE);
537                         continue;
538                 }
539                 ptype = mono_type_get_underlying_type (sig->params [i]);
540                 ptype = mini_get_basic_type_from_generic (gsctx, ptype);
541                 switch (ptype->type) {
542                 case MONO_TYPE_BOOLEAN:
543                 case MONO_TYPE_I1:
544                 case MONO_TYPE_U1:
545                         add_general (&gr, &stack_size, ainfo, FALSE);
546                         /* the value is in the ls byte */
547                         ainfo->offset += sizeof (gpointer) - 1;
548                         break;
549                 case MONO_TYPE_I2:
550                 case MONO_TYPE_U2:
551                 case MONO_TYPE_CHAR:
552                         add_general (&gr, &stack_size, ainfo, FALSE);
553                         /* the value is in the ls word */
554                         ainfo->offset += sizeof (gpointer) - 2;
555                         break;
556                 case MONO_TYPE_I4:
557                 case MONO_TYPE_U4:
558                         add_general (&gr, &stack_size, ainfo, FALSE);
559                         /* the value is in the ls dword */
560                         ainfo->offset += sizeof (gpointer) - 4;
561                         break;
562                 case MONO_TYPE_I:
563                 case MONO_TYPE_U:
564                 case MONO_TYPE_PTR:
565                 case MONO_TYPE_FNPTR:
566                 case MONO_TYPE_CLASS:
567                 case MONO_TYPE_OBJECT:
568                 case MONO_TYPE_STRING:
569                 case MONO_TYPE_SZARRAY:
570                 case MONO_TYPE_ARRAY:
571                         add_general (&gr, &stack_size, ainfo, FALSE);
572                         break;
573                 case MONO_TYPE_GENERICINST:
574                         if (!mono_type_generic_inst_is_valuetype (sig->params [i])) {
575                                 add_general (&gr, &stack_size, ainfo, FALSE);
576                                 break;
577                         }
578                         /* Fall through */
579                 case MONO_TYPE_VALUETYPE:
580 #ifdef SPARCV9
581                         if (sig->pinvoke)
582                                 NOT_IMPLEMENTED;
583 #endif
584                         add_general (&gr, &stack_size, ainfo, FALSE);
585                         break;
586                 case MONO_TYPE_TYPEDBYREF:
587                         add_general (&gr, &stack_size, ainfo, FALSE);
588                         break;
589                 case MONO_TYPE_U8:
590                 case MONO_TYPE_I8:
591 #ifdef SPARCV9
592                         add_general (&gr, &stack_size, ainfo, FALSE);
593 #else
594                         add_general (&gr, &stack_size, ainfo, TRUE);
595 #endif
596                         break;
597                 case MONO_TYPE_R4:
598 #ifdef SPARCV9
599                         add_float (&fr, &stack_size, ainfo, TRUE);
600                         gr ++;
601 #else
602                         /* single precision values are passed in integer registers */
603                         add_general (&gr, &stack_size, ainfo, FALSE);
604 #endif
605                         break;
606                 case MONO_TYPE_R8:
607 #ifdef SPARCV9
608                         add_float (&fr, &stack_size, ainfo, FALSE);
609                         gr ++;
610 #else
611                         /* double precision values are passed in a pair of registers */
612                         add_general (&gr, &stack_size, ainfo, TRUE);
613 #endif
614                         break;
615                 default:
616                         g_assert_not_reached ();
617                 }
618         }
619
620         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n > 0) && (sig->sentinelpos == sig->param_count)) {
621                 gr = PARAM_REGS;
622
623                 /* Emit the signature cookie just before the implicit arguments */
624                 add_general (&gr, &stack_size, &cinfo->sig_cookie, FALSE);
625         }
626
627         /* return value */
628         ret_type = mono_type_get_underlying_type (sig->ret);
629         ret_type = mini_get_basic_type_from_generic (gsctx, ret_type);
630         switch (ret_type->type) {
631         case MONO_TYPE_BOOLEAN:
632         case MONO_TYPE_I1:
633         case MONO_TYPE_U1:
634         case MONO_TYPE_I2:
635         case MONO_TYPE_U2:
636         case MONO_TYPE_CHAR:
637         case MONO_TYPE_I4:
638         case MONO_TYPE_U4:
639         case MONO_TYPE_I:
640         case MONO_TYPE_U:
641         case MONO_TYPE_PTR:
642         case MONO_TYPE_FNPTR:
643         case MONO_TYPE_CLASS:
644         case MONO_TYPE_OBJECT:
645         case MONO_TYPE_SZARRAY:
646         case MONO_TYPE_ARRAY:
647         case MONO_TYPE_STRING:
648                 cinfo->ret.storage = ArgInIReg;
649                 cinfo->ret.reg = sparc_i0;
650                 if (gr < 1)
651                         gr = 1;
652                 break;
653         case MONO_TYPE_U8:
654         case MONO_TYPE_I8:
655 #ifdef SPARCV9
656                 cinfo->ret.storage = ArgInIReg;
657                 cinfo->ret.reg = sparc_i0;
658                 if (gr < 1)
659                         gr = 1;
660 #else
661                 cinfo->ret.storage = ArgInIRegPair;
662                 cinfo->ret.reg = sparc_i0;
663                 if (gr < 2)
664                         gr = 2;
665 #endif
666                 break;
667         case MONO_TYPE_R4:
668         case MONO_TYPE_R8:
669                 cinfo->ret.storage = ArgInFReg;
670                 cinfo->ret.reg = sparc_f0;
671                 break;
672         case MONO_TYPE_GENERICINST:
673                 if (!mono_type_generic_inst_is_valuetype (sig->ret)) {
674                         cinfo->ret.storage = ArgInIReg;
675                         cinfo->ret.reg = sparc_i0;
676                         if (gr < 1)
677                                 gr = 1;
678                         break;
679                 }
680                 /* Fall through */
681         case MONO_TYPE_VALUETYPE:
682                 if (v64) {
683                         if (sig->pinvoke)
684                                 NOT_IMPLEMENTED;
685                         else
686                                 /* Already done */
687                                 ;
688                 }
689                 else
690                         cinfo->ret.storage = ArgOnStack;
691                 break;
692         case MONO_TYPE_TYPEDBYREF:
693                 if (v64) {
694                         if (sig->pinvoke)
695                                 /* Same as a valuetype with size 24 */
696                                 NOT_IMPLEMENTED;
697                         else
698                                 /* Already done */
699                                 ;
700                 }
701                 else
702                         cinfo->ret.storage = ArgOnStack;
703                 break;
704         case MONO_TYPE_VOID:
705                 break;
706         default:
707                 g_error ("Can't handle as return value 0x%x", sig->ret->type);
708         }
709
710         cinfo->stack_usage = stack_size;
711         cinfo->reg_usage = gr;
712         return cinfo;
713 }
714
715 GList *
716 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
717 {
718         GList *vars = NULL;
719         int i;
720
721         /* 
722          * FIXME: If an argument is allocated to a register, then load it from the
723          * stack in the prolog.
724          */
725
726         for (i = 0; i < cfg->num_varinfo; i++) {
727                 MonoInst *ins = cfg->varinfo [i];
728                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
729
730                 /* unused vars */
731                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
732                         continue;
733
734                 /* FIXME: Make arguments on stack allocateable to registers */
735                 if (ins->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT) || (ins->opcode == OP_REGVAR) || (ins->opcode == OP_ARG))
736                         continue;
737
738                 if (mono_is_regsize_var (ins->inst_vtype)) {
739                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
740                         g_assert (i == vmv->idx);
741
742                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
743                 }
744         }
745
746         return vars;
747 }
748
749 GList *
750 mono_arch_get_global_int_regs (MonoCompile *cfg)
751 {
752         GList *regs = NULL;
753         int i;
754         MonoMethodSignature *sig;
755         CallInfo *cinfo;
756
757         sig = mono_method_signature (cfg->method);
758
759         cinfo = get_call_info (cfg, sig, FALSE);
760
761         /* Use unused input registers */
762         for (i = cinfo->reg_usage; i < 6; ++i)
763                 regs = g_list_prepend (regs, GUINT_TO_POINTER (sparc_i0 + i));
764
765         /* Use %l0..%l6 as global registers */
766         for (i = sparc_l0; i < sparc_l7; ++i)
767                 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
768
769         g_free (cinfo);
770
771         return regs;
772 }
773
774 /*
775  * mono_arch_regalloc_cost:
776  *
777  *  Return the cost, in number of memory references, of the action of 
778  * allocating the variable VMV into a register during global register
779  * allocation.
780  */
781 guint32
782 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
783 {
784         return 0;
785 }
786
787 /*
788  * Set var information according to the calling convention. sparc version.
789  * The locals var stuff should most likely be split in another method.
790  */
791
792 void
793 mono_arch_allocate_vars (MonoCompile *cfg)
794 {
795         MonoMethodSignature *sig;
796         MonoMethodHeader *header;
797         MonoInst *inst;
798         int i, offset, size, align, curinst;
799         CallInfo *cinfo;
800
801         header = mono_method_get_header (cfg->method);
802
803         sig = mono_method_signature (cfg->method);
804
805         cinfo = get_call_info (cfg, sig, FALSE);
806
807         if (sig->ret->type != MONO_TYPE_VOID) {
808                 switch (cinfo->ret.storage) {
809                 case ArgInIReg:
810                 case ArgInFReg:
811                         cfg->ret->opcode = OP_REGVAR;
812                         cfg->ret->inst_c0 = cinfo->ret.reg;
813                         break;
814                 case ArgInIRegPair:
815                         if (cfg->new_ir && ((sig->ret->type == MONO_TYPE_I8) || (sig->ret->type == MONO_TYPE_U8))) {
816                                 MonoInst *low = get_vreg_to_inst (cfg, cfg->ret->dreg + 1);
817                                 MonoInst *high = get_vreg_to_inst (cfg, cfg->ret->dreg + 2);
818
819                                 low->opcode = OP_REGVAR;
820                                 low->dreg = cinfo->ret.reg + 1;
821                                 high->opcode = OP_REGVAR;
822                                 high->dreg = cinfo->ret.reg;
823                         }
824                         cfg->ret->opcode = OP_REGVAR;
825                         cfg->ret->inst_c0 = cinfo->ret.reg;
826                         break;
827                 case ArgOnStack:
828 #ifdef SPARCV9
829                         g_assert_not_reached ();
830 #else
831                         /* valuetypes */
832                         cfg->vret_addr->opcode = OP_REGOFFSET;
833                         cfg->vret_addr->inst_basereg = sparc_fp;
834                         cfg->vret_addr->inst_offset = 64;
835 #endif
836                         break;
837                 default:
838                         NOT_IMPLEMENTED;
839                 }
840                 cfg->ret->dreg = cfg->ret->inst_c0;
841         }
842
843         /*
844          * We use the ABI calling conventions for managed code as well.
845          * Exception: valuetypes are never returned in registers on V9.
846          * FIXME: Use something more optimized.
847          */
848
849         /* Locals are allocated backwards from %fp */
850         cfg->frame_reg = sparc_fp;
851         offset = 0;
852
853         /* 
854          * Reserve a stack slot for holding information used during exception 
855          * handling.
856          */
857         if (header->num_clauses)
858                 offset += sizeof (gpointer) * 2;
859
860         if (cfg->method->save_lmf) {
861                 offset += sizeof (MonoLMF);
862                 cfg->arch.lmf_offset = offset;
863         }
864
865         curinst = cfg->locals_start;
866         for (i = curinst; i < cfg->num_varinfo; ++i) {
867                 inst = cfg->varinfo [i];
868
869                 if ((inst->opcode == OP_REGVAR) || (inst->opcode == OP_REGOFFSET)) {
870                         //g_print ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg));
871                         continue;
872                 }
873
874                 if (inst->flags & MONO_INST_IS_DEAD)
875                         continue;
876
877                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
878                 * pinvoke wrappers when they call functions returning structure */
879                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
880                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
881                 else
882                         size = mini_type_stack_size (cfg->generic_sharing_context, inst->inst_vtype, &align);
883
884                 /* 
885                  * This is needed since structures containing doubles must be doubleword 
886          * aligned.
887                  * FIXME: Do this only if needed.
888                  */
889                 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
890                         align = 8;
891
892                 /*
893                  * variables are accessed as negative offsets from %fp, so increase
894                  * the offset before assigning it to a variable
895                  */
896                 offset += size;
897
898                 offset += align - 1;
899                 offset &= ~(align - 1);
900                 inst->opcode = OP_REGOFFSET;
901                 inst->inst_basereg = sparc_fp;
902                 inst->inst_offset = STACK_BIAS + -offset;
903
904                 //g_print ("allocating local %d to [%s - %d]\n", i, mono_arch_regname (inst->inst_basereg), - inst->inst_offset);
905         }
906
907         if (sig->call_convention == MONO_CALL_VARARG) {
908                 cfg->sig_cookie = cinfo->sig_cookie.offset + ARGS_OFFSET;
909         }
910
911         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
912                 inst = cfg->args [i];
913                 if (inst->opcode != OP_REGVAR) {
914                         ArgInfo *ainfo = &cinfo->args [i];
915                         gboolean inreg = TRUE;
916                         MonoType *arg_type;
917                         ArgStorage storage;
918
919                         if (sig->hasthis && (i == 0))
920                                 arg_type = &mono_defaults.object_class->byval_arg;
921                         else
922                                 arg_type = sig->params [i - sig->hasthis];
923
924 #ifndef SPARCV9
925                         if (!arg_type->byref && ((arg_type->type == MONO_TYPE_R4) 
926                                                                          || (arg_type->type == MONO_TYPE_R8)))
927                                 /*
928                                  * Since float arguments are passed in integer registers, we need to
929                                  * save them to the stack in the prolog.
930                                  */
931                                 inreg = FALSE;
932 #endif
933
934                         /* FIXME: Allocate volatile arguments to registers */
935                         /* FIXME: This makes the argument holding a vtype address into volatile */
936                         if (inst->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
937                                 inreg = FALSE;
938
939                         if (MONO_TYPE_ISSTRUCT (arg_type))
940                                 /* FIXME: this isn't needed */
941                                 inreg = FALSE;
942
943                         inst->opcode = OP_REGOFFSET;
944
945                         if (!inreg)
946                                 storage = ArgOnStack;
947                         else
948                                 storage = ainfo->storage;
949
950                         switch (storage) {
951                         case ArgInIReg:
952                                 inst->opcode = OP_REGVAR;
953                                 inst->dreg = sparc_i0 + ainfo->reg;
954                                 break;
955                         case ArgInIRegPair:
956                                 if (cfg->new_ir && (inst->type == STACK_I8)) {
957                                         MonoInst *low = get_vreg_to_inst (cfg, inst->dreg + 1);
958                                         MonoInst *high = get_vreg_to_inst (cfg, inst->dreg + 2);
959
960                                         low->opcode = OP_REGVAR;
961                                         low->dreg = sparc_i0 + ainfo->reg + 1;
962                                         high->opcode = OP_REGVAR;
963                                         high->dreg = sparc_i0 + ainfo->reg;
964                                 }
965                                 inst->opcode = OP_REGVAR;
966                                 inst->dreg = sparc_i0 + ainfo->reg;                                     
967                                 break;
968                         case ArgInFloatReg:
969                         case ArgInDoubleReg:
970                                 /* 
971                                  * Since float regs are volatile, we save the arguments to
972                                  * the stack in the prolog.
973                                  * FIXME: Avoid this if the method contains no calls.
974                                  */
975                         case ArgOnStack:
976                         case ArgOnStackPair:
977                         case ArgInSplitRegStack:
978                                 /* Split arguments are saved to the stack in the prolog */
979                                 inst->opcode = OP_REGOFFSET;
980                                 /* in parent frame */
981                                 inst->inst_basereg = sparc_fp;
982                                 inst->inst_offset = ainfo->offset + ARGS_OFFSET;
983
984                                 if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
985                                         /* 
986                                          * It is very hard to load doubles from non-doubleword aligned
987                                          * memory locations. So if the offset is misaligned, we copy the
988                                          * argument to a stack location in the prolog.
989                                          */
990                                         if ((inst->inst_offset - STACK_BIAS) % 8) {
991                                                 inst->inst_basereg = sparc_fp;
992                                                 offset += 8;
993                                                 align = 8;
994                                                 offset += align - 1;
995                                                 offset &= ~(align - 1);
996                                                 inst->inst_offset = STACK_BIAS + -offset;
997
998                                         }
999                                 }
1000                                 break;
1001                         default:
1002                                 NOT_IMPLEMENTED;
1003                         }
1004
1005                         if (MONO_TYPE_ISSTRUCT (arg_type)) {
1006                                 /* Add a level of indirection */
1007                                 /*
1008                                  * It would be easier to add OP_LDIND_I here, but ldind_i instructions
1009                                  * are destructively modified in a lot of places in inssel.brg.
1010                                  */
1011                                 MonoInst *indir;
1012                                 MONO_INST_NEW (cfg, indir, 0);
1013                                 *indir = *inst;
1014                                 inst->opcode = OP_VTARG_ADDR;
1015                                 inst->inst_left = indir;
1016                         }
1017                 }
1018         }
1019
1020         /* Add a properly aligned dword for use by int<->float conversion opcodes */
1021         offset += 8;
1022         offset = ALIGN_TO (offset, 8);
1023         cfg->arch.float_spill_slot_offset = offset;
1024
1025         /* 
1026          * spillvars are stored between the normal locals and the storage reserved
1027          * by the ABI.
1028          */
1029
1030         cfg->stack_offset = offset;
1031
1032         g_free (cinfo);
1033 }
1034
1035 void
1036 mono_arch_create_vars (MonoCompile *cfg)
1037 {
1038         MonoMethodSignature *sig;
1039
1040         sig = mono_method_signature (cfg->method);
1041
1042         if (MONO_TYPE_ISSTRUCT ((sig->ret))) {
1043                 cfg->vret_addr = mono_compile_create_var (cfg, &mono_defaults.int_class->byval_arg, OP_ARG);
1044                 if (G_UNLIKELY (cfg->verbose_level > 1)) {
1045                         printf ("vret_addr = ");
1046                         mono_print_ins (cfg->vret_addr);
1047                 }
1048         }
1049 }
1050
1051 static MonoInst *
1052 make_group (MonoCompile *cfg, MonoInst *left, int basereg, int offset)
1053 {
1054         MonoInst *group;
1055
1056         MONO_INST_NEW (cfg, group, OP_GROUP);
1057         group->inst_left = left;
1058         group->inst_basereg = basereg;
1059         group->inst_imm = offset;
1060
1061         return group;
1062 }
1063
1064 static void
1065 emit_sig_cookie (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1066 {
1067         MonoInst *arg;
1068         MonoMethodSignature *tmp_sig;
1069         MonoInst *sig_arg;
1070
1071         /*
1072          * mono_ArgIterator_Setup assumes the signature cookie is 
1073          * passed first and all the arguments which were before it are
1074          * passed on the stack after the signature. So compensate by 
1075          * passing a different signature.
1076          */
1077         tmp_sig = mono_metadata_signature_dup (call->signature);
1078         tmp_sig->param_count -= call->signature->sentinelpos;
1079         tmp_sig->sentinelpos = 0;
1080         memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1081
1082         /* FIXME: Add support for signature tokens to AOT */
1083         cfg->disable_aot = TRUE;
1084         /* We allways pass the signature on the stack for simplicity */
1085         MONO_INST_NEW (cfg, arg, OP_SPARC_OUTARG_MEM);
1086         arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset);
1087         MONO_INST_NEW (cfg, sig_arg, OP_ICONST);
1088         sig_arg->inst_p0 = tmp_sig;
1089         arg->inst_left = sig_arg;
1090         arg->type = STACK_PTR;
1091         /* prepend, so they get reversed */
1092         arg->next = call->out_args;
1093         call->out_args = arg;
1094 }
1095
1096 /* 
1097  * take the arguments and generate the arch-specific
1098  * instructions to properly call the function in call.
1099  * This includes pushing, moving arguments to the right register
1100  * etc.
1101  */
1102 MonoCallInst*
1103 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) {
1104         MonoInst *arg, *in;
1105         MonoMethodSignature *sig;
1106         int i, n;
1107         CallInfo *cinfo;
1108         ArgInfo *ainfo;
1109         guint32 extra_space = 0;
1110
1111         sig = call->signature;
1112         n = sig->param_count + sig->hasthis;
1113         
1114         cinfo = get_call_info (cfg, sig, sig->pinvoke);
1115
1116         for (i = 0; i < n; ++i) {
1117                 ainfo = cinfo->args + i;
1118
1119                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1120                         /* Emit the signature cookie just before the first implicit argument */
1121                         emit_sig_cookie (cfg, call, cinfo);
1122                 }
1123
1124                 if (is_virtual && i == 0) {
1125                         /* the argument will be attached to the call instruction */
1126                         in = call->args [i];
1127                 } else {
1128                         MONO_INST_NEW (cfg, arg, OP_OUTARG);
1129                         in = call->args [i];
1130                         arg->cil_code = in->cil_code;
1131                         arg->inst_left = in;
1132                         arg->type = in->type;
1133                         /* prepend, we'll need to reverse them later */
1134                         arg->next = call->out_args;
1135                         call->out_args = arg;
1136
1137                         if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis]))) {
1138                                 MonoInst *inst;
1139                                 gint align;
1140                                 guint32 offset, pad;
1141                                 guint32 size;
1142
1143 #ifdef SPARCV9
1144                                 if (sig->pinvoke)
1145                                         NOT_IMPLEMENTED;
1146 #endif
1147
1148                                 if (sig->params [i - sig->hasthis]->type == MONO_TYPE_TYPEDBYREF) {
1149                                         size = sizeof (MonoTypedRef);
1150                                         align = sizeof (gpointer);
1151                                 }
1152                                 else
1153                                 if (sig->pinvoke)
1154                                         size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1155                                 else {
1156                                         /* 
1157                                          * Can't use mini_type_stack_size (), but that
1158                                          * aligns the size to sizeof (gpointer), which is larger 
1159                                          * than the size of the source, leading to reads of invalid
1160                                          * memory if the source is at the end of address space or
1161                                          * misaligned reads.
1162                                          */
1163                                         size = mono_class_value_size (in->klass, &align);
1164                                 }
1165
1166                                 /* 
1167                                  * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1168                                  * use the normal OUTARG opcodes to pass the address of the location to
1169                                  * the callee.
1170                                  */
1171                                 MONO_INST_NEW (cfg, inst, OP_OUTARG_VT);
1172                                 inst->inst_left = in;
1173
1174                                 /* The first 6 argument locations are reserved */
1175                                 if (cinfo->stack_usage < 6 * sizeof (gpointer))
1176                                         cinfo->stack_usage = 6 * sizeof (gpointer);
1177
1178                                 offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1179                                 pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1180
1181                                 inst->inst_c1 = STACK_BIAS + offset;
1182                                 inst->backend.size = size;
1183                                 arg->inst_left = inst;
1184
1185                                 cinfo->stack_usage += size;
1186                                 cinfo->stack_usage += pad;
1187                         }
1188
1189                         arg->inst_right = make_group (cfg, (MonoInst*)call, sparc_sp, ARGS_OFFSET + ainfo->offset);
1190
1191                         switch (ainfo->storage) {
1192                         case ArgInIReg:
1193                         case ArgInFReg:
1194                         case ArgInIRegPair:
1195                                 if (ainfo->storage == ArgInIRegPair)
1196                                         arg->opcode = OP_SPARC_OUTARG_REGPAIR;
1197                                 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1198                                 call->used_iregs |= 1 << ainfo->reg;
1199
1200                                 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))) {
1201                                         /* An fp value is passed in an ireg */
1202
1203                                         if (arg->opcode == OP_SPARC_OUTARG_REGPAIR)
1204                                                 arg->opcode = OP_SPARC_OUTARG_REGPAIR_FLOAT;
1205                                         else
1206                                                 arg->opcode = OP_SPARC_OUTARG_FLOAT;
1207
1208                                         /*
1209                                          * The OUTARG (freg) implementation needs an extra dword to store
1210                                          * the temporary value.
1211                                          */                                     
1212                                         extra_space += 8;
1213                                 }
1214                                 break;
1215                         case ArgOnStack:
1216                                 arg->opcode = OP_SPARC_OUTARG_MEM;
1217                                 break;
1218                         case ArgOnStackPair:
1219                                 arg->opcode = OP_SPARC_OUTARG_MEMPAIR;
1220                                 break;
1221                         case ArgInSplitRegStack:
1222                                 arg->opcode = OP_SPARC_OUTARG_SPLIT_REG_STACK;
1223                                 arg->backend.reg3 = sparc_o0 + ainfo->reg;
1224                                 call->used_iregs |= 1 << ainfo->reg;
1225                                 break;
1226                         case ArgInFloatReg:
1227                                 arg->opcode = OP_SPARC_OUTARG_FLOAT_REG;
1228                                 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1229                                 break;
1230                         case ArgInDoubleReg:
1231                                 arg->opcode = OP_SPARC_OUTARG_DOUBLE_REG;
1232                                 arg->backend.reg3 = sparc_f0 + ainfo->reg;
1233                                 break;
1234                         default:
1235                                 NOT_IMPLEMENTED;
1236                         }
1237                 }
1238         }
1239
1240         /* Handle the case where there are no implicit arguments */
1241         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1242                 emit_sig_cookie (cfg, call, cinfo);
1243         }
1244
1245         /*
1246          * Reverse the call->out_args list.
1247          */
1248         {
1249                 MonoInst *prev = NULL, *list = call->out_args, *next;
1250                 while (list) {
1251                         next = list->next;
1252                         list->next = prev;
1253                         prev = list;
1254                         list = next;
1255                 }
1256                 call->out_args = prev;
1257         }
1258         call->stack_usage = cinfo->stack_usage + extra_space;
1259         call->out_ireg_args = NULL;
1260         call->out_freg_args = NULL;
1261         cfg->param_area = MAX (cfg->param_area, call->stack_usage);
1262         cfg->flags |= MONO_CFG_HAS_CALLS;
1263
1264         g_free (cinfo);
1265         return call;
1266 }
1267
1268 /* FIXME: Remove these later */
1269 #define NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { \
1270         MONO_INST_NEW ((cfg), (dest), (op)); \
1271         (dest)->dreg = (dr); \
1272         (dest)->inst_basereg = (base); \
1273         (dest)->inst_offset = (offset); \
1274         (dest)->type = STACK_I4; \
1275         } while (0)
1276
1277 #define EMIT_NEW_LOAD_MEMBASE(cfg,dest,op,dr,base,offset) do { NEW_LOAD_MEMBASE ((cfg), (dest), (op), (dr), (base), (offset)); MONO_ADD_INS ((cfg)->cbb, (dest)); } while (0)
1278
1279 #undef MONO_EMIT_NEW_STORE_MEMBASE_IMM
1280 #define MONO_EMIT_NEW_STORE_MEMBASE_IMM(cfg,op,base,offset,imm) do { \
1281         MonoInst *inst; \
1282         MONO_INST_NEW ((cfg), (inst), (op)); \
1283         inst->inst_destbasereg = base; \
1284         inst->inst_offset = offset; \
1285         inst->inst_p1 = (gpointer)(gssize)imm; \
1286         MONO_ADD_INS ((cfg)->cbb, inst); \
1287         } while (0)
1288
1289 static void
1290 add_outarg_reg2 (MonoCompile *cfg, MonoCallInst *call, ArgStorage storage, int reg, guint32 sreg)
1291 {
1292         MonoInst *arg;
1293
1294         MONO_INST_NEW (cfg, arg, 0);
1295
1296         arg->sreg1 = sreg;
1297
1298         switch (storage) {
1299         case ArgInIReg:
1300                 arg->opcode = OP_MOVE;
1301                 arg->dreg = mono_alloc_ireg (cfg);
1302
1303                 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, FALSE);
1304                 break;
1305         case ArgInFloatReg:
1306                 arg->opcode = OP_FMOVE;
1307                 arg->dreg = mono_alloc_freg (cfg);
1308
1309                 mono_call_inst_add_outarg_reg (cfg, call, arg->dreg, reg, TRUE);
1310                 break;
1311         default:
1312                 g_assert_not_reached ();
1313         }
1314
1315         MONO_ADD_INS (cfg->cbb, arg);
1316 }
1317
1318 static void
1319 add_outarg_load (MonoCompile *cfg, MonoCallInst *call, int opcode, int basereg, int offset, int reg)
1320 {
1321         MonoInst *arg;
1322         int dreg = mono_alloc_ireg (cfg);
1323
1324         EMIT_NEW_LOAD_MEMBASE (cfg, arg, OP_LOAD_MEMBASE, dreg, sparc_sp, offset);
1325         MONO_ADD_INS (cfg->cbb, arg);
1326
1327         mono_call_inst_add_outarg_reg (cfg, call, dreg, reg, FALSE);
1328 }
1329
1330 static void
1331 emit_pass_long (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1332 {
1333         int offset = ARGS_OFFSET + ainfo->offset;
1334
1335         switch (ainfo->storage) {
1336         case ArgInIRegPair:
1337                 add_outarg_reg2 (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg + 1, in->dreg + 1);
1338                 add_outarg_reg2 (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
1339                 break;
1340         case ArgOnStackPair:
1341                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset, in->dreg + 2);
1342                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
1343                 break;
1344         case ArgInSplitRegStack:
1345                 add_outarg_reg2 (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg + 2);
1346                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, offset + 4, in->dreg + 1);
1347                 break;
1348         default:
1349                 g_assert_not_reached ();
1350         }
1351 }
1352
1353 static void
1354 emit_pass_double (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1355 {
1356         int offset = ARGS_OFFSET + ainfo->offset;
1357
1358         switch (ainfo->storage) {
1359         case ArgInIRegPair:
1360                 /* floating-point <-> integer transfer must go through memory */
1361                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1362
1363                 /* Load into a register pair */
1364                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1365                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset + 4, sparc_o0 + ainfo->reg + 1);
1366                 break;
1367         case ArgOnStackPair:
1368                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1369                 break;
1370         case ArgInSplitRegStack:
1371                 /* floating-point <-> integer transfer must go through memory */
1372                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER8_MEMBASE_REG, sparc_sp, offset, in->dreg);
1373                 /* Load most significant word into register */
1374                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1375                 break;
1376         default:
1377                 g_assert_not_reached ();
1378         }
1379 }
1380
1381 static void
1382 emit_pass_float (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoInst *in)
1383 {
1384         int offset = ARGS_OFFSET + ainfo->offset;
1385
1386         switch (ainfo->storage) {
1387         case ArgInIReg:
1388                 /* floating-point <-> integer transfer must go through memory */
1389                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
1390                 add_outarg_load (cfg, call, OP_LOADI4_MEMBASE, sparc_sp, offset, sparc_o0 + ainfo->reg);
1391                 break;
1392         case ArgOnStack:
1393                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STORER4_MEMBASE_REG, sparc_sp, offset, in->dreg);
1394                 break;
1395         default:
1396                 g_assert_not_reached ();
1397         }
1398 }
1399
1400 static void
1401 emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in);
1402
1403 static void
1404 emit_pass_vtype (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in, gboolean pinvoke)
1405 {
1406         MonoInst *arg;
1407         guint32 align, offset, pad, size;
1408
1409         if (arg_type->type == MONO_TYPE_TYPEDBYREF) {
1410                 size = sizeof (MonoTypedRef);
1411                 align = sizeof (gpointer);
1412         }
1413         else if (pinvoke)
1414                 size = mono_type_native_stack_size (&in->klass->byval_arg, &align);
1415         else {
1416                 /* 
1417                  * Other backends use mono_type_stack_size (), but that
1418                  * aligns the size to 8, which is larger than the size of
1419                  * the source, leading to reads of invalid memory if the
1420                  * source is at the end of address space.
1421                  */
1422                 size = mono_class_value_size (in->klass, &align);
1423         }
1424
1425         /* The first 6 argument locations are reserved */
1426         if (cinfo->stack_usage < 6 * sizeof (gpointer))
1427                 cinfo->stack_usage = 6 * sizeof (gpointer);
1428
1429         offset = ALIGN_TO ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage, align);
1430         pad = offset - ((ARGS_OFFSET - STACK_BIAS) + cinfo->stack_usage);
1431
1432         cinfo->stack_usage += size;
1433         cinfo->stack_usage += pad;
1434
1435         /* 
1436          * We use OP_OUTARG_VT to copy the valuetype to a stack location, then
1437          * use the normal OUTARG opcodes to pass the address of the location to
1438          * the callee.
1439          */
1440         if (size > 0) {
1441                 MONO_INST_NEW (cfg, arg, OP_OUTARG_VT);
1442                 arg->sreg1 = in->dreg;
1443                 arg->klass = in->klass;
1444                 arg->backend.size = size;
1445                 arg->inst_p0 = call;
1446                 arg->inst_p1 = mono_mempool_alloc (cfg->mempool, sizeof (ArgInfo));
1447                 memcpy (arg->inst_p1, ainfo, sizeof (ArgInfo));
1448                 ((ArgInfo*)(arg->inst_p1))->offset = STACK_BIAS + offset;
1449                 MONO_ADD_INS (cfg->cbb, arg);
1450
1451                 MONO_INST_NEW (cfg, arg, OP_ADD_IMM);
1452                 arg->dreg = mono_alloc_preg (cfg);
1453                 arg->sreg1 = sparc_sp;
1454                 arg->inst_imm = STACK_BIAS + offset;
1455                 MONO_ADD_INS (cfg->cbb, arg);
1456
1457                 emit_pass_other (cfg, call, ainfo, NULL, arg);
1458         }
1459 }
1460
1461 static void
1462 emit_pass_other (MonoCompile *cfg, MonoCallInst *call, ArgInfo *ainfo, MonoType *arg_type, MonoInst *in)
1463 {
1464         int offset = ARGS_OFFSET + ainfo->offset;
1465         int opcode;
1466
1467         switch (ainfo->storage) {
1468         case ArgInIReg:
1469                 add_outarg_reg2 (cfg, call, ArgInIReg, sparc_o0 + ainfo->reg, in->dreg);
1470                 break;
1471         case ArgOnStack:
1472 #ifdef SPARCV9
1473                 NOT_IMPLEMENTED;
1474 #else
1475                 if (offset & 0x1)
1476                         opcode = OP_STOREI1_MEMBASE_REG;
1477                 else if (offset & 0x2)
1478                         opcode = OP_STOREI2_MEMBASE_REG;
1479                 else
1480                         opcode = OP_STOREI4_MEMBASE_REG;
1481                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, opcode, sparc_sp, offset, in->dreg);
1482 #endif
1483                 break;
1484         default:
1485                 g_assert_not_reached ();
1486         }
1487 }
1488
1489 static void
1490 emit_sig_cookie2 (MonoCompile *cfg, MonoCallInst *call, CallInfo *cinfo)
1491 {
1492         MonoMethodSignature *tmp_sig;
1493
1494         /*
1495          * mono_ArgIterator_Setup assumes the signature cookie is 
1496          * passed first and all the arguments which were before it are
1497          * passed on the stack after the signature. So compensate by 
1498          * passing a different signature.
1499          */
1500         tmp_sig = mono_metadata_signature_dup (call->signature);
1501         tmp_sig->param_count -= call->signature->sentinelpos;
1502         tmp_sig->sentinelpos = 0;
1503         memcpy (tmp_sig->params, call->signature->params + call->signature->sentinelpos, tmp_sig->param_count * sizeof (MonoType*));
1504
1505         /* FIXME: Add support for signature tokens to AOT */
1506         cfg->disable_aot = TRUE;
1507         /* We allways pass the signature on the stack for simplicity */
1508         MONO_EMIT_NEW_STORE_MEMBASE_IMM (cfg, OP_STORE_MEMBASE_IMM, sparc_sp, ARGS_OFFSET + cinfo->sig_cookie.offset, tmp_sig);
1509 }
1510
1511 void
1512 mono_arch_emit_call (MonoCompile *cfg, MonoCallInst *call)
1513 {
1514         MonoInst *in;
1515         MonoMethodSignature *sig;
1516         int i, n;
1517         CallInfo *cinfo;
1518         ArgInfo *ainfo;
1519         guint32 extra_space = 0;
1520
1521         sig = call->signature;
1522         n = sig->param_count + sig->hasthis;
1523         
1524         cinfo = get_call_info (cfg, sig, sig->pinvoke);
1525
1526         if (sig->ret && MONO_TYPE_ISSTRUCT (sig->ret)) {
1527                 /* Set the 'struct/union return pointer' location on the stack */
1528                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, call->vret_var->dreg);
1529         }
1530
1531         for (i = 0; i < n; ++i) {
1532                 MonoType *arg_type;
1533
1534                 ainfo = cinfo->args + i;
1535
1536                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
1537                         /* Emit the signature cookie just before the first implicit argument */
1538                         emit_sig_cookie2 (cfg, call, cinfo);
1539                 }
1540
1541                 in = call->args [i];
1542
1543                 if (sig->hasthis && (i == 0))
1544                         arg_type = &mono_defaults.object_class->byval_arg;
1545                 else
1546                         arg_type = sig->params [i - sig->hasthis];
1547
1548                 if ((i >= sig->hasthis) && (MONO_TYPE_ISSTRUCT(sig->params [i - sig->hasthis])))
1549                         emit_pass_vtype (cfg, call, cinfo, ainfo, arg_type, in, sig->pinvoke);
1550                 else if (!arg_type->byref && ((arg_type->type == MONO_TYPE_I8) || (arg_type->type == MONO_TYPE_U8)))
1551                         emit_pass_long (cfg, call, ainfo, in);
1552                 else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R8))
1553                         emit_pass_double (cfg, call, ainfo, in);
1554                 else if (!arg_type->byref && (arg_type->type == MONO_TYPE_R4))
1555                         emit_pass_float (cfg, call, ainfo, in);
1556                 else
1557                         emit_pass_other (cfg, call, ainfo, arg_type, in);
1558         }
1559
1560         /* Handle the case where there are no implicit arguments */
1561         if (!sig->pinvoke && (sig->call_convention == MONO_CALL_VARARG) && (n == sig->sentinelpos)) {
1562                 emit_sig_cookie2 (cfg, call, cinfo);
1563         }
1564
1565         call->stack_usage = cinfo->stack_usage + extra_space;
1566
1567         g_free (cinfo);
1568 }
1569
1570 void
1571 mono_arch_emit_outarg_vt (MonoCompile *cfg, MonoInst *ins, MonoInst *src)
1572 {
1573         ArgInfo *ainfo = (ArgInfo*)ins->inst_p1;
1574         int size = ins->backend.size;
1575
1576         mini_emit_memcpy2 (cfg, sparc_sp, ainfo->offset, src->dreg, 0, size, 0);
1577 }
1578
1579 void
1580 mono_arch_emit_setret (MonoCompile *cfg, MonoMethod *method, MonoInst *val)
1581 {
1582         CallInfo *cinfo = get_call_info (cfg, mono_method_signature (method), FALSE);
1583
1584         switch (cinfo->ret.storage) {
1585         case ArgInIReg:
1586                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg);
1587                 break;
1588         case ArgInIRegPair:
1589                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg, val->dreg + 2);
1590                 MONO_EMIT_NEW_UNALU (cfg, OP_MOVE, cfg->ret->dreg + 1, val->dreg + 1);
1591                 break;
1592         case ArgInFReg:
1593                 if (mono_method_signature (method)->ret->type == MONO_TYPE_R4)
1594                         MONO_EMIT_NEW_UNALU (cfg, OP_SETFRET, cfg->ret->dreg, val->dreg);
1595                 else
1596                         MONO_EMIT_NEW_UNALU (cfg, OP_FMOVE, cfg->ret->dreg, val->dreg);
1597                 break;
1598         default:
1599                 g_assert_not_reached ();
1600         }
1601
1602         g_assert (cinfo);
1603 }
1604
1605 int cond_to_sparc_cond [][3] = {
1606         {sparc_be,   sparc_be,   sparc_fbe},
1607         {sparc_bne,  sparc_bne,  0},
1608         {sparc_ble,  sparc_ble,  sparc_fble},
1609         {sparc_bge,  sparc_bge,  sparc_fbge},
1610         {sparc_bl,   sparc_bl,   sparc_fbl},
1611         {sparc_bg,   sparc_bg,   sparc_fbg},
1612         {sparc_bleu, sparc_bleu, 0},
1613         {sparc_beu,  sparc_beu,  0},
1614         {sparc_blu,  sparc_blu,  sparc_fbl},
1615         {sparc_bgu,  sparc_bgu,  sparc_fbg}
1616 };
1617
1618 /* Map opcode to the sparc condition codes */
1619 static inline SparcCond
1620 opcode_to_sparc_cond (int opcode)
1621 {
1622         CompRelation rel;
1623         CompType t;
1624
1625         switch (opcode) {
1626         case OP_COND_EXC_OV:
1627         case OP_COND_EXC_IOV:
1628                 return sparc_bvs;
1629         case OP_COND_EXC_C:
1630         case OP_COND_EXC_IC:
1631                 return sparc_bcs;
1632         case OP_COND_EXC_NO:
1633         case OP_COND_EXC_NC:
1634                 NOT_IMPLEMENTED;
1635         default:
1636                 rel = mono_opcode_to_cond (opcode);
1637                 t = mono_opcode_to_type (opcode, -1);
1638
1639                 return cond_to_sparc_cond [rel][t];
1640                 break;
1641         }
1642
1643         return -1;
1644 }
1645
1646 #define COMPUTE_DISP(ins) \
1647 if (ins->flags & MONO_INST_BRLABEL) { \
1648         if (ins->inst_i0->inst_c0) \
1649            disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2; \
1650         else { \
1651             disp = 0; \
1652                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1653         } \
1654 } else { \
1655         if (ins->inst_true_bb->native_offset) \
1656            disp = (ins->inst_true_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2; \
1657         else { \
1658             disp = 0; \
1659                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1660         } \
1661 }
1662
1663 #ifdef SPARCV9
1664 #define DEFAULT_ICC sparc_xcc_short
1665 #else
1666 #define DEFAULT_ICC sparc_icc_short
1667 #endif
1668
1669 #ifdef SPARCV9
1670 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) \
1671     do { \
1672         gint32 disp; \
1673         guint32 predict; \
1674         COMPUTE_DISP(ins); \
1675         predict = (disp != 0) ? 1 : 0; \
1676         g_assert (sparc_is_imm19 (disp)); \
1677         sparc_branchp (code, (annul), cond, icc, (predict), disp); \
1678         if (filldelay) sparc_nop (code); \
1679     } while (0)
1680 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_ICC ((ins), (cond), (annul), (filldelay), (sparc_xcc_short))
1681 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) \
1682     do { \
1683         gint32 disp; \
1684         guint32 predict; \
1685         COMPUTE_DISP(ins); \
1686         predict = (disp != 0) ? 1 : 0; \
1687         g_assert (sparc_is_imm19 (disp)); \
1688         sparc_fbranch (code, (annul), cond, disp); \
1689         if (filldelay) sparc_nop (code); \
1690     } while (0)
1691 #else
1692 #define EMIT_COND_BRANCH_ICC(ins,cond,annul,filldelay,icc) g_assert_not_reached ()
1693 #define EMIT_COND_BRANCH_GENERAL(ins,bop,cond,annul,filldelay) \
1694     do { \
1695         gint32 disp; \
1696         COMPUTE_DISP(ins); \
1697         g_assert (sparc_is_imm22 (disp)); \
1698         sparc_ ## bop (code, (annul), cond, disp); \
1699         if (filldelay) sparc_nop (code); \
1700     } while (0)
1701 #define EMIT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),branch,(cond),annul,filldelay)
1702 #define EMIT_FLOAT_COND_BRANCH(ins,cond,annul,filldelay) EMIT_COND_BRANCH_GENERAL((ins),fbranch,(cond),annul,filldelay)
1703 #endif
1704
1705 #define EMIT_COND_BRANCH_PREDICTED(ins,cond,annul,filldelay) \
1706     do { \
1707             gint32 disp; \
1708         guint32 predict; \
1709         COMPUTE_DISP(ins); \
1710         predict = (disp != 0) ? 1 : 0; \
1711         g_assert (sparc_is_imm19 (disp)); \
1712                 sparc_branchp (code, (annul), (cond), DEFAULT_ICC, (predict), disp); \
1713         if (filldelay) sparc_nop (code); \
1714     } while (0)
1715
1716 #define EMIT_COND_BRANCH_BPR(ins,bop,predict,annul,filldelay) \
1717     do { \
1718             gint32 disp; \
1719         COMPUTE_DISP(ins); \
1720                 g_assert (sparc_is_imm22 (disp)); \
1721                 sparc_ ## bop (code, (annul), (predict), ins->sreg1, disp); \
1722         if (filldelay) sparc_nop (code); \
1723     } while (0)
1724
1725 /* emit an exception if condition is fail */
1726 /*
1727  * We put the exception throwing code out-of-line, at the end of the method
1728  */
1729 #define EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,filldelay,icc) do {     \
1730                 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,   \
1731                                     MONO_PATCH_INFO_EXC, sexc_name);  \
1732         if (sparcv9 && ((icc) != sparc_icc_short)) {          \
1733            sparc_branchp (code, 0, (cond), (icc), 0, 0); \
1734         } \
1735         else { \
1736                         sparc_branch (code, 0, cond, 0);     \
1737         } \
1738         if (filldelay) sparc_nop (code);     \
1739         } while (0); 
1740
1741 #define EMIT_COND_SYSTEM_EXCEPTION(ins,cond,sexc_name) EMIT_COND_SYSTEM_EXCEPTION_GENERAL(ins,cond,sexc_name,TRUE,DEFAULT_ICC)
1742
1743 #define EMIT_COND_SYSTEM_EXCEPTION_BPR(ins,bop,sexc_name) do { \
1744                 mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,   \
1745                                     MONO_PATCH_INFO_EXC, sexc_name);  \
1746                 sparc_ ## bop (code, FALSE, FALSE, ins->sreg1, 0); \
1747         sparc_nop (code);    \
1748 } while (0);
1749
1750 #define EMIT_ALU_IMM(ins,op,setcc) do { \
1751                         if (sparc_is_imm13 ((ins)->inst_imm)) \
1752                                 sparc_ ## op ## _imm (code, (setcc), (ins)->sreg1, ins->inst_imm, (ins)->dreg); \
1753                         else { \
1754                                 sparc_set (code, ins->inst_imm, sparc_o7); \
1755                                 sparc_ ## op (code, (setcc), (ins)->sreg1, sparc_o7, (ins)->dreg); \
1756                         } \
1757 } while (0);
1758
1759 #define EMIT_LOAD_MEMBASE(ins,op) do { \
1760                         if (sparc_is_imm13 (ins->inst_offset)) \
1761                                 sparc_ ## op ## _imm (code, ins->inst_basereg, ins->inst_offset, ins->dreg); \
1762                         else { \
1763                                 sparc_set (code, ins->inst_offset, sparc_o7); \
1764                                 sparc_ ## op (code, ins->inst_basereg, sparc_o7, ins->dreg); \
1765                         } \
1766 } while (0);
1767
1768 /* max len = 5 */
1769 #define EMIT_STORE_MEMBASE_IMM(ins,op) do { \
1770                         guint32 sreg; \
1771                         if (ins->inst_imm == 0) \
1772                                 sreg = sparc_g0; \
1773                         else { \
1774                                 sparc_set (code, ins->inst_imm, sparc_o7); \
1775                                 sreg = sparc_o7; \
1776                         } \
1777                         if (!sparc_is_imm13 (ins->inst_offset)) { \
1778                                 sparc_set (code, ins->inst_offset, GP_SCRATCH_REG); \
1779                                 sparc_ ## op (code, sreg, ins->inst_destbasereg, GP_SCRATCH_REG); \
1780                         } \
1781                         else \
1782                                 sparc_ ## op ## _imm (code, sreg, ins->inst_destbasereg, ins->inst_offset); \
1783                                                                                                                                                                                  } while (0);
1784
1785 #define EMIT_STORE_MEMBASE_REG(ins,op) do { \
1786                         if (!sparc_is_imm13 (ins->inst_offset)) { \
1787                                 sparc_set (code, ins->inst_offset, sparc_o7); \
1788                                 sparc_ ## op (code, ins->sreg1, ins->inst_destbasereg, sparc_o7); \
1789                         } \
1790                                   else \
1791                                 sparc_ ## op ## _imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset); \
1792                                                                                                                                                                                  } while (0);
1793
1794 #define EMIT_CALL() do { \
1795     if (v64) { \
1796         sparc_set_template (code, sparc_o7); \
1797         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7); \
1798     } \
1799     else { \
1800         sparc_call_simple (code, 0); \
1801     } \
1802     sparc_nop (code); \
1803 } while (0);
1804
1805 /*
1806  * A call template is 7 instructions long, so we want to avoid it if possible.
1807  */
1808 static guint32*
1809 emit_call (MonoCompile *cfg, guint32 *code, guint32 patch_type, gconstpointer data)
1810 {
1811         gpointer target;
1812
1813         /* FIXME: This only works if the target method is already compiled */
1814         if (0 && v64 && !cfg->compile_aot) {
1815                 MonoJumpInfo patch_info;
1816
1817                 patch_info.type = patch_type;
1818                 patch_info.data.target = data;
1819
1820                 target = mono_resolve_patch_target (cfg->method, cfg->domain, NULL, &patch_info, FALSE);
1821
1822                 /* FIXME: Add optimizations if the target is close enough */
1823                 sparc_set (code, target, sparc_o7);
1824                 sparc_jmpl (code, sparc_o7, sparc_g0, sparc_o7);
1825                 sparc_nop (code);
1826         }
1827         else {
1828                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, patch_type, data);
1829                 EMIT_CALL ();
1830         }
1831         
1832         return code;
1833 }
1834
1835 void
1836 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
1837 {
1838 }
1839
1840 void
1841 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
1842 {
1843         MonoInst *ins, *n, *last_ins = NULL;
1844         ins = bb->code;
1845
1846         MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
1847                 switch (ins->opcode) {
1848                 case OP_MUL_IMM: 
1849                         /* remove unnecessary multiplication with 1 */
1850                         if (ins->inst_imm == 1) {
1851                                 if (ins->dreg != ins->sreg1) {
1852                                         ins->opcode = OP_MOVE;
1853                                 } else {
1854                                         MONO_DELETE_INS (bb, ins);
1855                                         continue;
1856                                 }
1857                         }
1858                         break;
1859 #ifndef SPARCV9
1860                 case OP_LOAD_MEMBASE:
1861                 case OP_LOADI4_MEMBASE:
1862                         /* 
1863                          * OP_STORE_MEMBASE_REG reg, offset(basereg) 
1864                          * OP_LOAD_MEMBASE offset(basereg), reg
1865                          */
1866                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_REG 
1867                                          || last_ins->opcode == OP_STORE_MEMBASE_REG) &&
1868                             ins->inst_basereg == last_ins->inst_destbasereg &&
1869                             ins->inst_offset == last_ins->inst_offset) {
1870                                 if (ins->dreg == last_ins->sreg1) {
1871                                         MONO_DELETE_INS (bb, ins);
1872                                         continue;
1873                                 } else {
1874                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1875                                         ins->opcode = OP_MOVE;
1876                                         ins->sreg1 = last_ins->sreg1;
1877                                 }
1878
1879                         /* 
1880                          * Note: reg1 must be different from the basereg in the second load
1881                          * OP_LOAD_MEMBASE offset(basereg), reg1
1882                          * OP_LOAD_MEMBASE offset(basereg), reg2
1883                          * -->
1884                          * OP_LOAD_MEMBASE offset(basereg), reg1
1885                          * OP_MOVE reg1, reg2
1886                          */
1887                         } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
1888                                            || last_ins->opcode == OP_LOAD_MEMBASE) &&
1889                               ins->inst_basereg != last_ins->dreg &&
1890                               ins->inst_basereg == last_ins->inst_basereg &&
1891                               ins->inst_offset == last_ins->inst_offset) {
1892
1893                                 if (ins->dreg == last_ins->dreg) {
1894                                         MONO_DELETE_INS (bb, ins);
1895                                         continue;
1896                                 } else {
1897                                         ins->opcode = OP_MOVE;
1898                                         ins->sreg1 = last_ins->dreg;
1899                                 }
1900
1901                                 //g_assert_not_reached ();
1902
1903 #if 0
1904                         /* 
1905                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1906                          * OP_LOAD_MEMBASE offset(basereg), reg
1907                          * -->
1908                          * OP_STORE_MEMBASE_IMM imm, offset(basereg) 
1909                          * OP_ICONST reg, imm
1910                          */
1911                         } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
1912                                                 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
1913                                    ins->inst_basereg == last_ins->inst_destbasereg &&
1914                                    ins->inst_offset == last_ins->inst_offset) {
1915                                 //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1916                                 ins->opcode = OP_ICONST;
1917                                 ins->inst_c0 = last_ins->inst_imm;
1918                                 g_assert_not_reached (); // check this rule
1919 #endif
1920                         }
1921                         break;
1922 #endif
1923                 case OP_LOADI1_MEMBASE:
1924                         if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
1925                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1926                                         ins->inst_offset == last_ins->inst_offset) {
1927                                 if (ins->dreg == last_ins->sreg1) {
1928                                         MONO_DELETE_INS (bb, ins);
1929                                         continue;
1930                                 } else {
1931                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1932                                         ins->opcode = OP_MOVE;
1933                                         ins->sreg1 = last_ins->sreg1;
1934                                 }
1935                         }
1936                         break;
1937                 case OP_LOADI2_MEMBASE:
1938                         if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
1939                                         ins->inst_basereg == last_ins->inst_destbasereg &&
1940                                         ins->inst_offset == last_ins->inst_offset) {
1941                                 if (ins->dreg == last_ins->sreg1) {
1942                                         MONO_DELETE_INS (bb, ins);
1943                                         continue;
1944                                 } else {
1945                                         //static int c = 0; printf ("MATCHX %s %d\n", cfg->method->name,c++);
1946                                         ins->opcode = OP_MOVE;
1947                                         ins->sreg1 = last_ins->sreg1;
1948                                 }
1949                         }
1950                         break;
1951                 case OP_STOREI4_MEMBASE_IMM:
1952                         /* Convert pairs of 0 stores to a dword 0 store */
1953                         /* Used when initializing temporaries */
1954                         /* We know sparc_fp is dword aligned */
1955                         if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM) &&
1956                                 (ins->inst_destbasereg == last_ins->inst_destbasereg) && 
1957                                 (ins->inst_destbasereg == sparc_fp) &&
1958                                 (ins->inst_offset < 0) &&
1959                                 ((ins->inst_offset % 8) == 0) &&
1960                                 ((ins->inst_offset == last_ins->inst_offset - 4)) &&
1961                                 (ins->inst_imm == 0) &&
1962                                 (last_ins->inst_imm == 0)) {
1963                                 if (sparcv9) {
1964                                         last_ins->opcode = OP_STOREI8_MEMBASE_IMM;
1965                                         last_ins->inst_offset = ins->inst_offset;
1966                                         MONO_DELETE_INS (bb, ins);
1967                                         continue;
1968                                 }
1969                         }
1970                         break;
1971                 case OP_IBEQ:
1972                 case OP_IBNE_UN:
1973                 case OP_IBLT:
1974                 case OP_IBGT:
1975                 case OP_IBGE:
1976                 case OP_IBLE:
1977                 case OP_COND_EXC_EQ:
1978                 case OP_COND_EXC_GE:
1979                 case OP_COND_EXC_GT:
1980                 case OP_COND_EXC_LE:
1981                 case OP_COND_EXC_LT:
1982                 case OP_COND_EXC_NE_UN:
1983                         /*
1984                          * Convert compare with zero+branch to BRcc
1985                          */
1986                         /* 
1987                          * This only works in 64 bit mode, since it examines all 64
1988                          * bits of the register.
1989                          * Only do this if the method is small since BPr only has a 16bit
1990                          * displacement.
1991                          */
1992                         if (v64 && (mono_method_get_header (cfg->method)->code_size < 10000) && last_ins && 
1993                                 (last_ins->opcode == OP_COMPARE_IMM) &&
1994                                 (last_ins->inst_imm == 0)) {
1995                                 switch (ins->opcode) {
1996                                 case OP_IBEQ:
1997                                         ins->opcode = OP_SPARC_BRZ;
1998                                         break;
1999                                 case OP_IBNE_UN:
2000                                         ins->opcode = OP_SPARC_BRNZ;
2001                                         break;
2002                                 case OP_IBLT:
2003                                         ins->opcode = OP_SPARC_BRLZ;
2004                                         break;
2005                                 case OP_IBGT:
2006                                         ins->opcode = OP_SPARC_BRGZ;
2007                                         break;
2008                                 case OP_IBGE:
2009                                         ins->opcode = OP_SPARC_BRGEZ;
2010                                         break;
2011                                 case OP_IBLE:
2012                                         ins->opcode = OP_SPARC_BRLEZ;
2013                                         break;
2014                                 case OP_COND_EXC_EQ:
2015                                         ins->opcode = OP_SPARC_COND_EXC_EQZ;
2016                                         break;
2017                                 case OP_COND_EXC_GE:
2018                                         ins->opcode = OP_SPARC_COND_EXC_GEZ;
2019                                         break;
2020                                 case OP_COND_EXC_GT:
2021                                         ins->opcode = OP_SPARC_COND_EXC_GTZ;
2022                                         break;
2023                                 case OP_COND_EXC_LE:
2024                                         ins->opcode = OP_SPARC_COND_EXC_LEZ;
2025                                         break;
2026                                 case OP_COND_EXC_LT:
2027                                         ins->opcode = OP_SPARC_COND_EXC_LTZ;
2028                                         break;
2029                                 case OP_COND_EXC_NE_UN:
2030                                         ins->opcode = OP_SPARC_COND_EXC_NEZ;
2031                                         break;
2032                                 default:
2033                                         g_assert_not_reached ();
2034                                 }
2035                                 ins->sreg1 = last_ins->sreg1;
2036                                 *last_ins = *ins;
2037                                 MONO_DELETE_INS (bb, ins);
2038                                 continue;
2039                         }
2040                         break;
2041                 case OP_MOVE:
2042                         /* 
2043                          * OP_MOVE reg, reg 
2044                          */
2045                         if (ins->dreg == ins->sreg1) {
2046                                 MONO_DELETE_INS (bb, ins);
2047                                 continue;
2048                         }
2049                         /* 
2050                          * OP_MOVE sreg, dreg 
2051                          * OP_MOVE dreg, sreg
2052                          */
2053                         if (last_ins && last_ins->opcode == OP_MOVE &&
2054                             ins->sreg1 == last_ins->dreg &&
2055                             ins->dreg == last_ins->sreg1) {
2056                                 MONO_DELETE_INS (bb, ins);
2057                                 continue;
2058                         }
2059                         break;
2060                 }
2061                 last_ins = ins;
2062                 ins = ins->next;
2063         }
2064         bb->last_ins = last_ins;
2065 }
2066
2067 void
2068 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
2069 {
2070 }
2071
2072 /* FIXME: Strange loads from the stack in basic-float.cs:test_2_rem */
2073
2074 static void
2075 sparc_patch (guint32 *code, const gpointer target)
2076 {
2077         guint32 *c = code;
2078         guint32 ins = *code;
2079         guint32 op = ins >> 30;
2080         guint32 op2 = (ins >> 22) & 0x7;
2081         guint32 rd = (ins >> 25) & 0x1f;
2082         guint8* target8 = (guint8*)target;
2083         gint64 disp = (target8 - (guint8*)code) >> 2;
2084         int reg;
2085
2086 //      g_print ("patching 0x%08x (0x%08x) to point to 0x%08x\n", code, ins, target);
2087
2088         if ((op == 0) && (op2 == 2)) {
2089                 if (!sparc_is_imm22 (disp))
2090                         NOT_IMPLEMENTED;
2091                 /* Bicc */
2092                 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
2093         }
2094         else if ((op == 0) && (op2 == 1)) {
2095                 if (!sparc_is_imm19 (disp))
2096                         NOT_IMPLEMENTED;
2097                 /* BPcc */
2098                 *code = ((ins >> 19) << 19) | (disp & 0x7ffff);
2099         }
2100         else if ((op == 0) && (op2 == 3)) {
2101                 if (!sparc_is_imm16 (disp))
2102                         NOT_IMPLEMENTED;
2103                 /* BPr */
2104                 *code &= ~(0x180000 | 0x3fff);
2105                 *code |= ((disp << 21) & (0x180000)) | (disp & 0x3fff);
2106         }
2107         else if ((op == 0) && (op2 == 6)) {
2108                 if (!sparc_is_imm22 (disp))
2109                         NOT_IMPLEMENTED;
2110                 /* FBicc */
2111                 *code = ((ins >> 22) << 22) | (disp & 0x3fffff);
2112         }
2113         else if ((op == 0) && (op2 == 4)) {
2114                 guint32 ins2 = code [1];
2115
2116                 if (((ins2 >> 30) == 2) && (((ins2 >> 19) & 0x3f) == 2)) {
2117                         /* sethi followed by or */                      
2118                         guint32 *p = code;
2119                         sparc_set (p, target8, rd);
2120                         while (p <= (code + 1))
2121                                 sparc_nop (p);
2122                 }
2123                 else if (ins2 == 0x01000000) {
2124                         /* sethi followed by nop */
2125                         guint32 *p = code;
2126                         sparc_set (p, target8, rd);
2127                         while (p <= (code + 1))
2128                                 sparc_nop (p);
2129                 }
2130                 else if ((sparc_inst_op (ins2) == 3) && (sparc_inst_imm (ins2))) {
2131                         /* sethi followed by load/store */
2132 #ifndef SPARCV9
2133                         guint32 t = (guint32)target8;
2134                         *code &= ~(0x3fffff);
2135                         *code |= (t >> 10);
2136                         *(code + 1) &= ~(0x3ff);
2137                         *(code + 1) |= (t & 0x3ff);
2138 #endif
2139                 }
2140                 else if (v64 && 
2141                                  (sparc_inst_rd (ins) == sparc_g1) &&
2142                                  (sparc_inst_op (c [1]) == 0) && (sparc_inst_op2 (c [1]) == 4) &&
2143                                  (sparc_inst_op (c [2]) == 2) && (sparc_inst_op3 (c [2]) == 2) &&
2144                                  (sparc_inst_op (c [3]) == 2) && (sparc_inst_op3 (c [3]) == 2))
2145                 {
2146                         /* sparc_set */
2147                         guint32 *p = c;
2148                         reg = sparc_inst_rd (c [1]);
2149                         sparc_set (p, target8, reg);
2150                         while (p < (c + 6))
2151                                 sparc_nop (p);
2152                 }
2153                 else if ((sparc_inst_op (ins2) == 2) && (sparc_inst_op3 (ins2) == 0x38) && 
2154                                  (sparc_inst_imm (ins2))) {
2155                         /* sethi followed by jmpl */
2156 #ifndef SPARCV9
2157                         guint32 t = (guint32)target8;
2158                         *code &= ~(0x3fffff);
2159                         *code |= (t >> 10);
2160                         *(code + 1) &= ~(0x3ff);
2161                         *(code + 1) |= (t & 0x3ff);
2162 #endif
2163                 }
2164                 else
2165                         NOT_IMPLEMENTED;
2166         }
2167         else if (op == 01) {
2168                 gint64 disp = (target8 - (guint8*)code) >> 2;
2169
2170                 if (!sparc_is_imm30 (disp))
2171                         NOT_IMPLEMENTED;
2172                 sparc_call_simple (code, target8 - (guint8*)code);
2173         }
2174         else if ((op == 2) && (sparc_inst_op3 (ins) == 0x2) && sparc_inst_imm (ins)) {
2175                 /* mov imm, reg */
2176                 g_assert (sparc_is_imm13 (target8));
2177                 *code &= ~(0x1fff);
2178                 *code |= (guint32)target8;
2179         }
2180         else if ((sparc_inst_op (ins) == 2) && (sparc_inst_op3 (ins) == 0x7)) {
2181                 /* sparc_set case 5. */
2182                 guint32 *p = c;
2183
2184                 g_assert (v64);
2185                 reg = sparc_inst_rd (c [3]);
2186                 sparc_set (p, target, reg);
2187                 while (p < (c + 6))
2188                         sparc_nop (p);
2189         }
2190         else
2191                 NOT_IMPLEMENTED;
2192
2193 //      g_print ("patched with 0x%08x\n", ins);
2194 }
2195
2196 /*
2197  * mono_sparc_emit_save_lmf:
2198  *
2199  *  Emit the code neccesary to push a new entry onto the lmf stack. Used by
2200  * trampolines as well.
2201  */
2202 guint32*
2203 mono_sparc_emit_save_lmf (guint32 *code, guint32 lmf_offset)
2204 {
2205         /* Save lmf_addr */
2206         sparc_sti_imm (code, sparc_o0, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr));
2207         /* Save previous_lmf */
2208         sparc_ldi (code, sparc_o0, sparc_g0, sparc_o7);
2209         sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf));
2210         /* Set new lmf */
2211         sparc_add_imm (code, FALSE, sparc_fp, lmf_offset, sparc_o7);
2212         sparc_sti (code, sparc_o7, sparc_o0, sparc_g0);
2213
2214         return code;
2215 }
2216
2217 guint32*
2218 mono_sparc_emit_restore_lmf (guint32 *code, guint32 lmf_offset)
2219 {
2220         /* Load previous_lmf */
2221         sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, previous_lmf), sparc_l0);
2222         /* Load lmf_addr */
2223         sparc_ldi_imm (code, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, lmf_addr), sparc_l1);
2224         /* *(lmf) = previous_lmf */
2225         sparc_sti (code, sparc_l0, sparc_l1, sparc_g0);
2226         return code;
2227 }
2228
2229 static guint32*
2230 emit_save_sp_to_lmf (MonoCompile *cfg, guint32 *code)
2231 {
2232         /*
2233          * Since register windows are saved to the current value of %sp, we need to
2234          * set the sp field in the lmf before the call, not in the prolog.
2235          */
2236         if (cfg->method->save_lmf) {
2237                 gint32 lmf_offset = MONO_SPARC_STACK_BIAS - cfg->arch.lmf_offset;
2238
2239                 /* Save sp */
2240                 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
2241         }
2242
2243         return code;
2244 }
2245
2246 static guint32*
2247 emit_vret_token (MonoGenericSharingContext *gsctx, MonoInst *ins, guint32 *code)
2248 {
2249         MonoCallInst *call = (MonoCallInst*)ins;
2250         guint32 size;
2251
2252         /* 
2253          * The sparc ABI requires that calls to functions which return a structure
2254          * contain an additional unimpl instruction which is checked by the callee.
2255          */
2256         if (call->signature->pinvoke && MONO_TYPE_ISSTRUCT(call->signature->ret)) {
2257                 if (call->signature->ret->type == MONO_TYPE_TYPEDBYREF)
2258                         size = mini_type_stack_size (gsctx, call->signature->ret, NULL);
2259                 else
2260                         size = mono_class_native_size (call->signature->ret->data.klass, NULL);
2261                 sparc_unimp (code, size & 0xfff);
2262         }
2263
2264         return code;
2265 }
2266
2267 static guint32*
2268 emit_move_return_value (MonoInst *ins, guint32 *code)
2269 {
2270         /* Move return value to the target register */
2271         /* FIXME: do more things in the local reg allocator */
2272         switch (ins->opcode) {
2273         case OP_VOIDCALL:
2274         case OP_VOIDCALL_REG:
2275         case OP_VOIDCALL_MEMBASE:
2276                 break;
2277         case OP_CALL:
2278         case OP_CALL_REG:
2279         case OP_CALL_MEMBASE:
2280                 g_assert (ins->dreg == sparc_o0);
2281                 break;
2282         case OP_LCALL:
2283         case OP_LCALL_REG:
2284         case OP_LCALL_MEMBASE:
2285                 /* 
2286                  * ins->dreg is the least significant reg due to the lreg: LCALL rule
2287                  * in inssel-long32.brg.
2288                  */
2289 #ifdef SPARCV9
2290                 sparc_mov_reg_reg (code, sparc_o0, ins->dreg);
2291 #else
2292                 g_assert (ins->dreg == sparc_o1);
2293 #endif
2294                 break;
2295         case OP_FCALL:
2296         case OP_FCALL_REG:
2297         case OP_FCALL_MEMBASE:
2298 #ifdef SPARCV9
2299                 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4) {
2300                         sparc_fmovs (code, sparc_f0, ins->dreg);
2301                         sparc_fstod (code, ins->dreg, ins->dreg);
2302                 }
2303                 else
2304                         sparc_fmovd (code, sparc_f0, ins->dreg);
2305 #else           
2306                 sparc_fmovs (code, sparc_f0, ins->dreg);
2307                 if (((MonoCallInst*)ins)->signature->ret->type == MONO_TYPE_R4)
2308                         sparc_fstod (code, ins->dreg, ins->dreg);
2309                 else
2310                         sparc_fmovs (code, sparc_f1, ins->dreg + 1);
2311 #endif
2312                 break;
2313         case OP_VCALL:
2314         case OP_VCALL_REG:
2315         case OP_VCALL_MEMBASE:
2316         case OP_VCALL2:
2317         case OP_VCALL2_REG:
2318         case OP_VCALL2_MEMBASE:
2319                 break;
2320         default:
2321                 NOT_IMPLEMENTED;
2322         }
2323
2324         return code;
2325 }
2326
2327 /*
2328  * emit_load_volatile_arguments:
2329  *
2330  *  Load volatile arguments from the stack to the original input registers.
2331  * Required before a tail call.
2332  */
2333 static guint32*
2334 emit_load_volatile_arguments (MonoCompile *cfg, guint32 *code)
2335 {
2336         MonoMethod *method = cfg->method;
2337         MonoMethodSignature *sig;
2338         MonoInst *inst;
2339         CallInfo *cinfo;
2340         guint32 i, ireg;
2341
2342         /* FIXME: Generate intermediate code instead */
2343
2344         sig = mono_method_signature (method);
2345
2346         cinfo = get_call_info (cfg, sig, FALSE);
2347         
2348         /* This is the opposite of the code in emit_prolog */
2349
2350         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2351                 ArgInfo *ainfo = cinfo->args + i;
2352                 gint32 stack_offset;
2353                 MonoType *arg_type;
2354
2355                 inst = cfg->args [i];
2356
2357                 if (sig->hasthis && (i == 0))
2358                         arg_type = &mono_defaults.object_class->byval_arg;
2359                 else
2360                         arg_type = sig->params [i - sig->hasthis];
2361
2362                 stack_offset = ainfo->offset + ARGS_OFFSET;
2363                 ireg = sparc_i0 + ainfo->reg;
2364
2365                 if (ainfo->storage == ArgInSplitRegStack) {
2366                         g_assert (inst->opcode == OP_REGOFFSET);
2367
2368                         if (!sparc_is_imm13 (stack_offset))
2369                                 NOT_IMPLEMENTED;
2370                         sparc_st_imm (code, inst->inst_basereg, stack_offset, sparc_i5);
2371                 }
2372
2373                 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
2374                         if (ainfo->storage == ArgInIRegPair) {
2375                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
2376                                         NOT_IMPLEMENTED;
2377                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2378                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2379                         }
2380                         else
2381                                 if (ainfo->storage == ArgInSplitRegStack) {
2382                                         if (stack_offset != inst->inst_offset) {
2383                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_i5);
2384                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2385                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2386
2387                                         }
2388                                 }
2389                         else
2390                                 if (ainfo->storage == ArgOnStackPair) {
2391                                         if (stack_offset != inst->inst_offset) {
2392                                                 /* stack_offset is not dword aligned, so we need to make a copy */
2393                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, sparc_o7);
2394                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset);
2395
2396                                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset + 4, sparc_o7);
2397                                                 sparc_st_imm (code, sparc_o7, sparc_fp, stack_offset + 4);
2398
2399                                         }
2400                                 }
2401                          else
2402                                 g_assert_not_reached ();
2403                 }
2404                 else
2405                         if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
2406                                 /* Argument in register, but need to be saved to stack */
2407                                 if (!sparc_is_imm13 (stack_offset))
2408                                         NOT_IMPLEMENTED;
2409                                 if ((stack_offset - ARGS_OFFSET) & 0x1)
2410                                         /* FIXME: Is this ldsb or ldub ? */
2411                                         sparc_ldsb_imm (code, inst->inst_basereg, stack_offset, ireg);
2412                                 else
2413                                         if ((stack_offset - ARGS_OFFSET) & 0x2)
2414                                                 sparc_ldsh_imm (code, inst->inst_basereg, stack_offset, ireg);
2415                                 else
2416                                         if ((stack_offset - ARGS_OFFSET) & 0x4)
2417                                                 sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2418                                         else {
2419                                                 if (v64)
2420                                                         sparc_ldx_imm (code, inst->inst_basereg, stack_offset, ireg);
2421                                                 else
2422                                                         sparc_ld_imm (code, inst->inst_basereg, stack_offset, ireg);
2423                                         }
2424                         }
2425                         else if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
2426                                 /* Argument in regpair, but need to be saved to stack */
2427                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
2428                                         NOT_IMPLEMENTED;
2429                                 sparc_ld_imm (code, inst->inst_basereg, inst->inst_offset, ireg);
2430                                 sparc_st_imm (code, inst->inst_basereg, inst->inst_offset + 4, ireg + 1);
2431                         }
2432                         else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
2433                                 NOT_IMPLEMENTED;
2434                         }
2435                         else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
2436                                 NOT_IMPLEMENTED;
2437                         }
2438
2439                 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
2440                         if (inst->opcode == OP_REGVAR)
2441                                 /* FIXME: Load the argument into memory */
2442                                 NOT_IMPLEMENTED;
2443         }
2444
2445         g_free (cinfo);
2446
2447         return code;
2448 }
2449
2450 /*
2451  * mono_sparc_is_virtual_call:
2452  *
2453  *  Determine whenever the instruction at CODE is a virtual call.
2454  */
2455 gboolean 
2456 mono_sparc_is_virtual_call (guint32 *code)
2457 {
2458         guint32 buf[1];
2459         guint32 *p;
2460
2461         p = buf;
2462
2463         if ((sparc_inst_op (*code) == 0x2) && (sparc_inst_op3 (*code) == 0x38)) {
2464                 /*
2465                  * Register indirect call. If it is a virtual call, then the 
2466                  * instruction in the delay slot is a special kind of nop.
2467                  */
2468
2469                 /* Construct special nop */
2470                 sparc_or_imm (p, FALSE, sparc_g0, 0xca, sparc_g0);
2471                 p --;
2472
2473                 if (code [1] == p [0])
2474                         return TRUE;
2475         }
2476
2477         return FALSE;
2478 }
2479
2480 /*
2481  * mono_arch_get_vcall_slot:
2482  *
2483  *  Determine the vtable slot used by a virtual call.
2484  */
2485 gpointer
2486 mono_arch_get_vcall_slot (guint8 *code8, gpointer *regs, int *displacement)
2487 {
2488         guint32 *code = (guint32*)(gpointer)code8;
2489         guint32 ins = code [0];
2490         guint32 prev_ins = code [-1];
2491
2492         mono_sparc_flushw ();
2493
2494         *displacement = 0;
2495
2496         if (!mono_sparc_is_virtual_call (code))
2497                 return NULL;
2498
2499         if ((sparc_inst_op (ins) == 0x2) && (sparc_inst_op3 (ins) == 0x38)) {
2500                 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)) {
2501                         /* ld [r1 + CONST ], r2; call r2 */
2502                         guint32 base = sparc_inst_rs1 (prev_ins);
2503                         gint32 disp = (((gint32)(sparc_inst_imm13 (prev_ins))) << 19) >> 19;
2504                         gpointer base_val;
2505
2506                         g_assert (sparc_inst_rd (prev_ins) == sparc_inst_rs1 (ins));
2507
2508                         g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2509
2510                         base_val = regs [base];
2511
2512                         *displacement = disp;
2513
2514                         return (gpointer)base_val;
2515                 }
2516                 else if ((sparc_inst_op (prev_ins) == 0x3) && (sparc_inst_i (prev_ins) == 0) && (sparc_inst_op3 (prev_ins) == 0)) {
2517                         /* set r1, ICONST; ld [r1 + r2], r2; call r2 */
2518                         /* Decode a sparc_set32 */
2519                         guint32 base = sparc_inst_rs1 (prev_ins);
2520                         guint32 disp;
2521                         gpointer base_val;
2522                         guint32 s1 = code [-3];
2523                         guint32 s2 = code [-2];
2524
2525 #ifdef SPARCV9
2526                         NOT_IMPLEMENTED;
2527 #endif
2528
2529                         /* sparc_sethi */
2530                         g_assert (sparc_inst_op (s1) == 0);
2531                         g_assert (sparc_inst_op2 (s1) == 4);
2532
2533                         /* sparc_or_imm */
2534                         g_assert (sparc_inst_op (s2) == 2);
2535                         g_assert (sparc_inst_op3 (s2) == 2);
2536                         g_assert (sparc_inst_i (s2) == 1);
2537                         g_assert (sparc_inst_rs1 (s2) == sparc_inst_rd (s2));
2538                         g_assert (sparc_inst_rd (s1) == sparc_inst_rs1 (s2));
2539
2540                         disp = ((s1 & 0x3fffff) << 10) | sparc_inst_imm13 (s2);
2541
2542                         g_assert ((base >= sparc_o0) && (base <= sparc_i7));
2543
2544                         base_val = regs [base];
2545
2546                         *displacement = disp;
2547
2548                         return (gpointer)base_val;
2549                 } else
2550                         g_assert_not_reached ();
2551         }
2552         else
2553                 g_assert_not_reached ();
2554
2555         return NULL;
2556 }
2557
2558 gpointer*
2559 mono_arch_get_vcall_slot_addr (guint8 *code, gpointer *regs)
2560 {
2561         gpointer vt;
2562         int displacement;
2563         vt = mono_arch_get_vcall_slot (code, regs, &displacement);
2564         if (!vt)
2565                 return NULL;
2566         return (gpointer*)((char*)vt + displacement);
2567 }
2568
2569 #define CMP_SIZE 3
2570 #define BR_SMALL_SIZE 2
2571 #define BR_LARGE_SIZE 2
2572 #define JUMP_IMM_SIZE 5
2573 #define ENABLE_WRONG_METHOD_CHECK 0
2574
2575 /*
2576  * LOCKING: called with the domain lock held
2577  */
2578 gpointer
2579 mono_arch_build_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count,
2580         gpointer fail_tramp)
2581 {
2582         int i;
2583         int size = 0;
2584         guint32 *code, *start;
2585
2586         g_assert (!fail_tramp);
2587
2588         for (i = 0; i < count; ++i) {
2589                 MonoIMTCheckItem *item = imt_entries [i];
2590                 if (item->is_equals) {
2591                         if (item->check_target_idx) {
2592                                 if (!item->compare_done)
2593                                         item->chunk_size += CMP_SIZE;
2594                                 item->chunk_size += BR_SMALL_SIZE + JUMP_IMM_SIZE;
2595                         } else {
2596                                 item->chunk_size += JUMP_IMM_SIZE;
2597 #if ENABLE_WRONG_METHOD_CHECK
2598                                 item->chunk_size += CMP_SIZE + BR_SMALL_SIZE + 1;
2599 #endif
2600                         }
2601                 } else {
2602                         item->chunk_size += CMP_SIZE + BR_LARGE_SIZE;
2603                         imt_entries [item->check_target_idx]->compare_done = TRUE;
2604                 }
2605                 size += item->chunk_size;
2606         }
2607         code = mono_code_manager_reserve (domain->code_mp, size * 4);
2608         start = code;
2609
2610         for (i = 0; i < count; ++i) {
2611                 MonoIMTCheckItem *item = imt_entries [i];
2612                 item->code_target = (guint8*)code;
2613                 if (item->is_equals) {
2614                         if (item->check_target_idx) {
2615                                 if (!item->compare_done) {
2616                                         sparc_set (code, (guint32)item->key, sparc_g5);
2617                                         sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2618                                 }
2619                                 item->jmp_code = (guint8*)code;
2620                                 sparc_branch (code, 0, sparc_bne, 0);
2621                                 sparc_nop (code);
2622                                 sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
2623                                 sparc_ld (code, sparc_g5, 0, sparc_g5);
2624                                 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2625                                 sparc_nop (code);
2626                         } else {
2627                                 /* enable the commented code to assert on wrong method */
2628 #if ENABLE_WRONG_METHOD_CHECK
2629                                 g_assert_not_reached ();
2630 #endif
2631                                 sparc_set (code, ((guint32)(&(vtable->vtable [item->value.vtable_slot]))), sparc_g5);
2632                                 sparc_ld (code, sparc_g5, 0, sparc_g5);
2633                                 sparc_jmpl (code, sparc_g5, sparc_g0, sparc_g0);
2634                                 sparc_nop (code);
2635 #if ENABLE_WRONG_METHOD_CHECK
2636                                 g_assert_not_reached ();
2637 #endif
2638                         }
2639                 } else {
2640                         sparc_set (code, (guint32)item->key, sparc_g5);
2641                         sparc_cmp (code, MONO_ARCH_IMT_REG, sparc_g5);
2642                         item->jmp_code = (guint8*)code;
2643                         sparc_branch (code, 0, sparc_beu, 0);
2644                         sparc_nop (code);
2645                 }
2646         }
2647         /* patch the branches to get to the target items */
2648         for (i = 0; i < count; ++i) {
2649                 MonoIMTCheckItem *item = imt_entries [i];
2650                 if (item->jmp_code) {
2651                         if (item->check_target_idx) {
2652                                 sparc_patch ((guint32*)item->jmp_code, imt_entries [item->check_target_idx]->code_target);
2653                         }
2654                 }
2655         }
2656
2657         mono_arch_flush_icache ((guint8*)start, (code - start) * 4);
2658
2659         mono_stats.imt_thunks_size += (code - start) * 4;
2660         g_assert (code - start <= size);
2661         return start;
2662 }
2663
2664 MonoMethod*
2665 mono_arch_find_imt_method (gpointer *regs, guint8 *code)
2666 {
2667 #ifdef SPARCV9
2668         g_assert_not_reached ();
2669 #endif
2670
2671         return (MonoMethod*)regs [sparc_g1];
2672 }
2673
2674 MonoObject*
2675 mono_arch_find_this_argument (gpointer *regs, MonoMethod *method, MonoGenericSharingContext *gsctx)
2676 {
2677         mono_sparc_flushw ();
2678
2679         return (gpointer)regs [sparc_o0];
2680 }
2681
2682 /*
2683  * Some conventions used in the following code.
2684  * 2) The only scratch registers we have are o7 and g1.  We try to
2685  * stick to o7 when we can, and use g1 when necessary.
2686  */
2687
2688 void
2689 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
2690 {
2691         MonoInst *ins;
2692         MonoCallInst *call;
2693         guint offset;
2694         guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
2695         MonoInst *last_ins = NULL;
2696         int max_len, cpos;
2697         const char *spec;
2698
2699         if (cfg->verbose_level > 2)
2700                 g_print ("Basic block %d starting at offset 0x%x\n", bb->block_num, bb->native_offset);
2701
2702         cpos = bb->max_offset;
2703
2704         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
2705                 NOT_IMPLEMENTED;
2706         }
2707
2708         MONO_BB_FOR_EACH_INS (bb, ins) {
2709                 guint8* code_start;
2710
2711                 offset = (guint8*)code - cfg->native_code;
2712
2713                 spec = ins_get_spec (ins->opcode);
2714
2715                 max_len = ((guint8 *)spec)[MONO_INST_LEN];
2716
2717                 if (offset > (cfg->code_size - max_len - 16)) {
2718                         cfg->code_size *= 2;
2719                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2720                         code = (guint32*)(cfg->native_code + offset);
2721                 }
2722                 code_start = (guint8*)code;
2723                 //      if (ins->cil_code)
2724                 //              g_print ("cil code\n");
2725                 mono_debug_record_line_number (cfg, ins, offset);
2726
2727                 switch (ins->opcode) {
2728                 case OP_STOREI1_MEMBASE_IMM:
2729                         EMIT_STORE_MEMBASE_IMM (ins, stb);
2730                         break;
2731                 case OP_STOREI2_MEMBASE_IMM:
2732                         EMIT_STORE_MEMBASE_IMM (ins, sth);
2733                         break;
2734                 case OP_STORE_MEMBASE_IMM:
2735                         EMIT_STORE_MEMBASE_IMM (ins, sti);
2736                         break;
2737                 case OP_STOREI4_MEMBASE_IMM:
2738                         EMIT_STORE_MEMBASE_IMM (ins, st);
2739                         break;
2740                 case OP_STOREI8_MEMBASE_IMM:
2741 #ifdef SPARCV9
2742                         EMIT_STORE_MEMBASE_IMM (ins, stx);
2743 #else
2744                         /* Only generated by peephole opts */
2745                         g_assert ((ins->inst_offset % 8) == 0);
2746                         g_assert (ins->inst_imm == 0);
2747                         EMIT_STORE_MEMBASE_IMM (ins, stx);
2748 #endif
2749                         break;
2750                 case OP_STOREI1_MEMBASE_REG:
2751                         EMIT_STORE_MEMBASE_REG (ins, stb);
2752                         break;
2753                 case OP_STOREI2_MEMBASE_REG:
2754                         EMIT_STORE_MEMBASE_REG (ins, sth);
2755                         break;
2756                 case OP_STOREI4_MEMBASE_REG:
2757                         EMIT_STORE_MEMBASE_REG (ins, st);
2758                         break;
2759                 case OP_STOREI8_MEMBASE_REG:
2760 #ifdef SPARCV9
2761                         EMIT_STORE_MEMBASE_REG (ins, stx);
2762 #else
2763                         /* Only used by OP_MEMSET */
2764                         EMIT_STORE_MEMBASE_REG (ins, std);
2765 #endif
2766                         break;
2767                 case OP_STORE_MEMBASE_REG:
2768                         EMIT_STORE_MEMBASE_REG (ins, sti);
2769                         break;
2770                 case OP_LOADU4_MEM:
2771                         sparc_set (code, ins->inst_c0, ins->dreg);
2772                         sparc_ld (code, ins->dreg, sparc_g0, ins->dreg);
2773                         break;
2774                 case OP_LOADI4_MEMBASE:
2775 #ifdef SPARCV9
2776                         EMIT_LOAD_MEMBASE (ins, ldsw);
2777 #else
2778                         EMIT_LOAD_MEMBASE (ins, ld);
2779 #endif
2780                         break;
2781                 case OP_LOADU4_MEMBASE:
2782                         EMIT_LOAD_MEMBASE (ins, ld);
2783                         break;
2784                 case OP_LOADU1_MEMBASE:
2785                         EMIT_LOAD_MEMBASE (ins, ldub);
2786                         break;
2787                 case OP_LOADI1_MEMBASE:
2788                         EMIT_LOAD_MEMBASE (ins, ldsb);
2789                         break;
2790                 case OP_LOADU2_MEMBASE:
2791                         EMIT_LOAD_MEMBASE (ins, lduh);
2792                         break;
2793                 case OP_LOADI2_MEMBASE:
2794                         EMIT_LOAD_MEMBASE (ins, ldsh);
2795                         break;
2796                 case OP_LOAD_MEMBASE:
2797 #ifdef SPARCV9
2798                                 EMIT_LOAD_MEMBASE (ins, ldx);
2799 #else
2800                                 EMIT_LOAD_MEMBASE (ins, ld);
2801 #endif
2802                         break;
2803 #ifdef SPARCV9
2804                 case OP_LOADI8_MEMBASE:
2805                         EMIT_LOAD_MEMBASE (ins, ldx);
2806                         break;
2807 #endif
2808                 case OP_ICONV_TO_I1:
2809                         sparc_sll_imm (code, ins->sreg1, 24, sparc_o7);
2810                         sparc_sra_imm (code, sparc_o7, 24, ins->dreg);
2811                         break;
2812                 case OP_ICONV_TO_I2:
2813                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2814                         sparc_sra_imm (code, sparc_o7, 16, ins->dreg);
2815                         break;
2816                 case OP_ICONV_TO_U1:
2817                         sparc_and_imm (code, FALSE, ins->sreg1, 0xff, ins->dreg);
2818                         break;
2819                 case OP_ICONV_TO_U2:
2820                         sparc_sll_imm (code, ins->sreg1, 16, sparc_o7);
2821                         sparc_srl_imm (code, sparc_o7, 16, ins->dreg);
2822                         break;
2823                 case OP_LCONV_TO_OVF_U4:
2824                 case OP_ICONV_TO_OVF_U4:
2825                         /* Only used on V9 */
2826                         sparc_cmp_imm (code, ins->sreg1, 0);
2827                         mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2828                                                                  MONO_PATCH_INFO_EXC, "OverflowException");
2829                         sparc_branchp (code, 0, sparc_bl, sparc_xcc_short, 0, 0);
2830                         /* Delay slot */
2831                         sparc_set (code, 1, sparc_o7);
2832                         sparc_sllx_imm (code, sparc_o7, 32, sparc_o7);
2833                         sparc_cmp (code, ins->sreg1, sparc_o7);
2834                         mono_add_patch_info (cfg, (guint8*)(code) - (cfg)->native_code,
2835                                                                  MONO_PATCH_INFO_EXC, "OverflowException");
2836                         sparc_branchp (code, 0, sparc_bge, sparc_xcc_short, 0, 0);
2837                         sparc_nop (code);
2838                         sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
2839                         break;
2840                 case OP_LCONV_TO_OVF_I4_UN:
2841                 case OP_ICONV_TO_OVF_I4_UN:
2842                         /* Only used on V9 */
2843                         NOT_IMPLEMENTED;
2844                         break;
2845                 case OP_COMPARE:
2846                 case OP_LCOMPARE:
2847                 case OP_ICOMPARE:
2848                         sparc_cmp (code, ins->sreg1, ins->sreg2);
2849                         break;
2850                 case OP_COMPARE_IMM:
2851                 case OP_ICOMPARE_IMM:
2852                         if (sparc_is_imm13 (ins->inst_imm))
2853                                 sparc_cmp_imm (code, ins->sreg1, ins->inst_imm);
2854                         else {
2855                                 sparc_set (code, ins->inst_imm, sparc_o7);
2856                                 sparc_cmp (code, ins->sreg1, sparc_o7);
2857                         }
2858                         break;
2859                 case OP_BREAK:
2860                         /*
2861                          * gdb does not like encountering 'ta 1' in the debugged code. So 
2862                          * instead of emitting a trap, we emit a call a C function and place a 
2863                          * breakpoint there.
2864                          */
2865                         //sparc_ta (code, 1);
2866                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, mono_break);
2867                         EMIT_CALL();
2868                         break;
2869                 case OP_ADDCC:
2870                 case OP_IADDCC:
2871                         sparc_add (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2872                         break;
2873                 case OP_IADD:
2874                         sparc_add (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2875                         break;
2876                 case OP_ADDCC_IMM:
2877                 case OP_ADD_IMM:
2878                 case OP_IADD_IMM:
2879                         /* according to inssel-long32.brg, this should set cc */
2880                         EMIT_ALU_IMM (ins, add, TRUE);
2881                         break;
2882                 case OP_ADC:
2883                 case OP_IADC:
2884                         /* according to inssel-long32.brg, this should set cc */
2885                         sparc_addx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2886                         break;
2887                 case OP_ADC_IMM:
2888                 case OP_IADC_IMM:
2889                         EMIT_ALU_IMM (ins, addx, TRUE);
2890                         break;
2891                 case OP_SUBCC:
2892                 case OP_ISUBCC:
2893                         sparc_sub (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2894                         break;
2895                 case OP_ISUB:
2896                         sparc_sub (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2897                         break;
2898                 case OP_SUBCC_IMM:
2899                 case OP_SUB_IMM:
2900                 case OP_ISUB_IMM:
2901                         /* according to inssel-long32.brg, this should set cc */
2902                         EMIT_ALU_IMM (ins, sub, TRUE);
2903                         break;
2904                 case OP_SBB:
2905                 case OP_ISBB:
2906                         /* according to inssel-long32.brg, this should set cc */
2907                         sparc_subx (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2908                         break;
2909                 case OP_SBB_IMM:
2910                 case OP_ISBB_IMM:
2911                         EMIT_ALU_IMM (ins, subx, TRUE);
2912                         break;
2913                 case OP_IAND:
2914                         sparc_and (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2915                         break;
2916                 case OP_AND_IMM:
2917                 case OP_IAND_IMM:
2918                         EMIT_ALU_IMM (ins, and, FALSE);
2919                         break;
2920                 case OP_IDIV:
2921                         /* Sign extend sreg1 into %y */
2922                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2923                         sparc_wry (code, sparc_o7, sparc_g0);
2924                         sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
2925                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2926                         break;
2927                 case OP_IDIV_UN:
2928                         sparc_wry (code, sparc_g0, sparc_g0);
2929                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
2930                         break;
2931                 case OP_DIV_IMM:
2932                 case OP_IDIV_IMM: {
2933                         int i, imm;
2934
2935                         /* Transform division into a shift */
2936                         for (i = 1; i < 30; ++i) {
2937                                 imm = (1 << i);
2938                                 if (ins->inst_imm == imm)
2939                                         break;
2940                         }
2941                         if (i < 30) {
2942                                 if (i == 1) {
2943                                         /* gcc 2.95.3 */
2944                                         sparc_srl_imm (code, ins->sreg1, 31, sparc_o7);
2945                                         sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2946                                         sparc_sra_imm (code, ins->dreg, 1, ins->dreg);
2947                                 }
2948                                 else {
2949                                         /* http://compilers.iecc.com/comparch/article/93-04-079 */
2950                                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2951                                         sparc_srl_imm (code, sparc_o7, 32 - i, sparc_o7);
2952                                         sparc_add (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2953                                         sparc_sra_imm (code, ins->dreg, i, ins->dreg);
2954                                 }
2955                         }
2956                         else {
2957                                 /* Sign extend sreg1 into %y */
2958                                 sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2959                                 sparc_wry (code, sparc_o7, sparc_g0);
2960                                 EMIT_ALU_IMM (ins, sdiv, TRUE);
2961                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2962                         }
2963                         break;
2964                 }
2965                 case OP_IDIV_UN_IMM:
2966                         sparc_wry (code, sparc_g0, sparc_g0);
2967                         EMIT_ALU_IMM (ins, udiv, FALSE);
2968                         break;
2969                 case OP_IREM:
2970                         /* Sign extend sreg1 into %y */
2971                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2972                         sparc_wry (code, sparc_o7, sparc_g0);
2973                         sparc_sdiv (code, TRUE, ins->sreg1, ins->sreg2, sparc_o7);
2974                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2975                         sparc_smul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2976                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2977                         break;
2978                 case OP_IREM_UN:
2979                         sparc_wry (code, sparc_g0, sparc_g0);
2980                         sparc_udiv (code, FALSE, ins->sreg1, ins->sreg2, sparc_o7);
2981                         sparc_umul (code, FALSE, ins->sreg2, sparc_o7, sparc_o7);
2982                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
2983                         break;
2984                 case OP_REM_IMM:
2985                 case OP_IREM_IMM:
2986                         /* Sign extend sreg1 into %y */
2987                         sparc_sra_imm (code, ins->sreg1, 31, sparc_o7);
2988                         sparc_wry (code, sparc_o7, sparc_g0);
2989                         if (!sparc_is_imm13 (ins->inst_imm)) {
2990                                 sparc_set (code, ins->inst_imm, GP_SCRATCH_REG);
2991                                 sparc_sdiv (code, TRUE, ins->sreg1, GP_SCRATCH_REG, sparc_o7);
2992                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2993                                 sparc_smul (code, FALSE, sparc_o7, GP_SCRATCH_REG, sparc_o7);
2994                         }
2995                         else {
2996                                 sparc_sdiv_imm (code, TRUE, ins->sreg1, ins->inst_imm, sparc_o7);
2997                                 EMIT_COND_SYSTEM_EXCEPTION_GENERAL (code, sparc_boverflow, "ArithmeticException", TRUE, sparc_icc_short);
2998                                 sparc_smul_imm (code, FALSE, sparc_o7, ins->inst_imm, sparc_o7);
2999                         }
3000                         sparc_sub (code, FALSE, ins->sreg1, sparc_o7, ins->dreg);
3001                         break;
3002                 case OP_IOR:
3003                         sparc_or (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
3004                         break;
3005                 case OP_OR_IMM:
3006                 case OP_IOR_IMM:
3007                         EMIT_ALU_IMM (ins, or, FALSE);
3008                         break;
3009                 case OP_IXOR:
3010                         sparc_xor (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
3011                         break;
3012                 case OP_XOR_IMM:
3013                 case OP_IXOR_IMM:
3014                         EMIT_ALU_IMM (ins, xor, FALSE);
3015                         break;
3016                 case OP_ISHL:
3017                         sparc_sll (code, ins->sreg1, ins->sreg2, ins->dreg);
3018                         break;
3019                 case OP_SHL_IMM:
3020                 case OP_ISHL_IMM:
3021                         if (ins->inst_imm < (1 << 5))
3022                                 sparc_sll_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3023                         else {
3024                                 sparc_set (code, ins->inst_imm, sparc_o7);
3025                                 sparc_sll (code, ins->sreg1, sparc_o7, ins->dreg);
3026                         }
3027                         break;
3028                 case OP_ISHR:
3029                         sparc_sra (code, ins->sreg1, ins->sreg2, ins->dreg);
3030                         break;
3031                 case OP_ISHR_IMM:
3032                 case OP_SHR_IMM:
3033                         if (ins->inst_imm < (1 << 5))
3034                                 sparc_sra_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3035                         else {
3036                                 sparc_set (code, ins->inst_imm, sparc_o7);
3037                                 sparc_sra (code, ins->sreg1, sparc_o7, ins->dreg);
3038                         }
3039                         break;
3040                 case OP_SHR_UN_IMM:
3041                 case OP_ISHR_UN_IMM:
3042                         if (ins->inst_imm < (1 << 5))
3043                                 sparc_srl_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3044                         else {
3045                                 sparc_set (code, ins->inst_imm, sparc_o7);
3046                                 sparc_srl (code, ins->sreg1, sparc_o7, ins->dreg);
3047                         }
3048                         break;
3049                 case OP_ISHR_UN:
3050                         sparc_srl (code, ins->sreg1, ins->sreg2, ins->dreg);
3051                         break;
3052                 case OP_LSHL:
3053                         sparc_sllx (code, ins->sreg1, ins->sreg2, ins->dreg);
3054                         break;
3055                 case OP_LSHL_IMM:
3056                         if (ins->inst_imm < (1 << 6))
3057                                 sparc_sllx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3058                         else {
3059                                 sparc_set (code, ins->inst_imm, sparc_o7);
3060                                 sparc_sllx (code, ins->sreg1, sparc_o7, ins->dreg);
3061                         }
3062                         break;
3063                 case OP_LSHR:
3064                         sparc_srax (code, ins->sreg1, ins->sreg2, ins->dreg);
3065                         break;
3066                 case OP_LSHR_IMM:
3067                         if (ins->inst_imm < (1 << 6))
3068                                 sparc_srax_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3069                         else {
3070                                 sparc_set (code, ins->inst_imm, sparc_o7);
3071                                 sparc_srax (code, ins->sreg1, sparc_o7, ins->dreg);
3072                         }
3073                         break;
3074                 case OP_LSHR_UN:
3075                         sparc_srlx (code, ins->sreg1, ins->sreg2, ins->dreg);
3076                         break;
3077                 case OP_LSHR_UN_IMM:
3078                         if (ins->inst_imm < (1 << 6))
3079                                 sparc_srlx_imm (code, ins->sreg1, ins->inst_imm, ins->dreg);
3080                         else {
3081                                 sparc_set (code, ins->inst_imm, sparc_o7);
3082                                 sparc_srlx (code, ins->sreg1, sparc_o7, ins->dreg);
3083                         }
3084                         break;
3085                 case OP_INOT:
3086                         /* can't use sparc_not */
3087                         sparc_xnor (code, FALSE, ins->sreg1, sparc_g0, ins->dreg);
3088                         break;
3089                 case OP_INEG:
3090                         /* can't use sparc_neg */
3091                         sparc_sub (code, FALSE, sparc_g0, ins->sreg1, ins->dreg);
3092                         break;
3093                 case OP_IMUL:
3094                         sparc_smul (code, FALSE, ins->sreg1, ins->sreg2, ins->dreg);
3095                         break;
3096                 case OP_IMUL_IMM:
3097                 case OP_MUL_IMM: {
3098                         int i, imm;
3099
3100                         if ((ins->inst_imm == 1) && (ins->sreg1 == ins->dreg))
3101                                 break;
3102
3103                         /* Transform multiplication into a shift */
3104                         for (i = 0; i < 30; ++i) {
3105                                 imm = (1 << i);
3106                                 if (ins->inst_imm == imm)
3107                                         break;
3108                         }
3109                         if (i < 30)
3110                                 sparc_sll_imm (code, ins->sreg1, i, ins->dreg);
3111                         else
3112                                 EMIT_ALU_IMM (ins, smul, FALSE);
3113                         break;
3114                 }
3115                 case OP_IMUL_OVF:
3116                         sparc_smul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
3117                         sparc_rdy (code, sparc_g1);
3118                         sparc_sra_imm (code, ins->dreg, 31, sparc_o7);
3119                         sparc_cmp (code, sparc_g1, sparc_o7);
3120                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
3121                         break;
3122                 case OP_IMUL_OVF_UN:
3123                         sparc_umul (code, TRUE, ins->sreg1, ins->sreg2, ins->dreg);
3124                         sparc_rdy (code, sparc_o7);
3125                         sparc_cmp (code, sparc_o7, sparc_g0);
3126                         EMIT_COND_SYSTEM_EXCEPTION_GENERAL (ins, sparc_bne, "OverflowException", TRUE, sparc_icc_short);
3127                         break;
3128                 case OP_ICONST:
3129                         sparc_set (code, ins->inst_c0, ins->dreg);
3130                         break;
3131                 case OP_I8CONST:
3132                         sparc_set (code, ins->inst_l, ins->dreg);
3133                         break;
3134                 case OP_AOTCONST:
3135                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3136                         sparc_set_template (code, ins->dreg);
3137                         break;
3138                 case OP_JUMP_TABLE:
3139                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
3140                         sparc_set_template (code, ins->dreg);
3141                         break;
3142                 case OP_ICONV_TO_I4:
3143                 case OP_ICONV_TO_U4:
3144                 case OP_MOVE:
3145                         if (ins->sreg1 != ins->dreg)
3146                                 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3147                         break;
3148                 case OP_FMOVE:
3149 #ifdef SPARCV9
3150                         if (ins->sreg1 != ins->dreg)
3151                                 sparc_fmovd (code, ins->sreg1, ins->dreg);
3152 #else
3153                         sparc_fmovs (code, ins->sreg1, ins->dreg);
3154                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3155 #endif
3156                         break;
3157                 case OP_SPARC_SETFREG_FLOAT:
3158                         /* Only used on V9 */
3159                         sparc_fdtos (code, ins->sreg1, ins->dreg);
3160                         break;
3161                 case OP_JMP:
3162                         if (cfg->method->save_lmf)
3163                                 NOT_IMPLEMENTED;
3164
3165                         code = emit_load_volatile_arguments (cfg, code);
3166                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
3167                         sparc_set_template (code, sparc_o7);
3168                         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_g0);
3169                         /* Restore parent frame in delay slot */
3170                         sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
3171                         break;
3172                 case OP_CHECK_THIS:
3173                         /* ensure ins->sreg1 is not NULL */
3174                         /* Might be misaligned in case of vtypes so use a byte load */
3175                         sparc_ldsb_imm (code, ins->sreg1, 0, sparc_g0);
3176                         break;
3177                 case OP_ARGLIST:
3178                         sparc_add_imm (code, FALSE, sparc_fp, cfg->sig_cookie, sparc_o7);
3179                         sparc_sti_imm (code, sparc_o7, ins->sreg1, 0);
3180                         break;
3181                 case OP_FCALL:
3182                 case OP_LCALL:
3183                 case OP_VCALL:
3184                 case OP_VCALL2:
3185                 case OP_VOIDCALL:
3186                 case OP_CALL:
3187                         call = (MonoCallInst*)ins;
3188                         g_assert (!call->virtual);
3189                         code = emit_save_sp_to_lmf (cfg, code);
3190                         if (ins->flags & MONO_INST_HAS_METHOD)
3191                             code = emit_call (cfg, code, MONO_PATCH_INFO_METHOD, call->method);
3192                         else
3193                             code = emit_call (cfg, code, MONO_PATCH_INFO_ABS, call->fptr);
3194
3195                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
3196                         code = emit_move_return_value (ins, code);
3197                         break;
3198                 case OP_FCALL_REG:
3199                 case OP_LCALL_REG:
3200                 case OP_VCALL_REG:
3201                 case OP_VCALL2_REG:
3202                 case OP_VOIDCALL_REG:
3203                 case OP_CALL_REG:
3204                         call = (MonoCallInst*)ins;
3205                         code = emit_save_sp_to_lmf (cfg, code);
3206                         sparc_jmpl (code, ins->sreg1, sparc_g0, sparc_callsite);
3207                         /*
3208                          * We emit a special kind of nop in the delay slot to tell the 
3209                          * trampoline code that this is a virtual call, thus an unbox
3210                          * trampoline might need to be called.
3211                          */
3212                         if (call->virtual)
3213                                 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
3214                         else
3215                                 sparc_nop (code);
3216
3217                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
3218                         code = emit_move_return_value (ins, code);
3219                         break;
3220                 case OP_FCALL_MEMBASE:
3221                 case OP_LCALL_MEMBASE:
3222                 case OP_VCALL_MEMBASE:
3223                 case OP_VCALL2_MEMBASE:
3224                 case OP_VOIDCALL_MEMBASE:
3225                 case OP_CALL_MEMBASE:
3226                         call = (MonoCallInst*)ins;
3227                         code = emit_save_sp_to_lmf (cfg, code);
3228                         if (sparc_is_imm13 (ins->inst_offset)) {
3229                                 sparc_ldi_imm (code, ins->inst_basereg, ins->inst_offset, sparc_o7);
3230                         } else {
3231                                 sparc_set (code, ins->inst_offset, sparc_o7);
3232                                 sparc_ldi (code, ins->inst_basereg, sparc_o7, sparc_o7);
3233                         }
3234                         sparc_jmpl (code, sparc_o7, sparc_g0, sparc_callsite);
3235                         if (call->virtual)
3236                                 sparc_or_imm (code, FALSE, sparc_g0, 0xca, sparc_g0);
3237                         else
3238                                 sparc_nop (code);
3239
3240                         code = emit_vret_token (cfg->generic_sharing_context, ins, code);
3241                         code = emit_move_return_value (ins, code);
3242                         break;
3243                 case OP_SETFRET:
3244                         if (mono_method_signature (cfg->method)->ret->type == MONO_TYPE_R4)
3245                                 sparc_fdtos (code, ins->sreg1, sparc_f0);
3246                         else {
3247 #ifdef SPARCV9
3248                                 sparc_fmovd (code, ins->sreg1, ins->dreg);
3249 #else
3250                                 /* FIXME: Why not use fmovd ? */
3251                                 sparc_fmovs (code, ins->sreg1, ins->dreg);
3252                                 sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3253 #endif
3254                         }
3255                         break;
3256                 case OP_OUTARG:
3257                         g_assert_not_reached ();
3258                         break;
3259                 case OP_LOCALLOC: {
3260                         guint32 size_reg;
3261                         gint32 offset2;
3262
3263 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3264                         /* Perform stack touching */
3265                         NOT_IMPLEMENTED;
3266 #endif
3267
3268                         /* Keep alignment */
3269                         /* Add 4 to compensate for the rounding of localloc_offset */
3270                         sparc_add_imm (code, FALSE, ins->sreg1, 4 + MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->dreg);
3271                         sparc_set (code, ~(MONO_ARCH_LOCALLOC_ALIGNMENT - 1), sparc_o7);
3272                         sparc_and (code, FALSE, ins->dreg, sparc_o7, ins->dreg);
3273
3274                         if ((ins->flags & MONO_INST_INIT) && (ins->sreg1 == ins->dreg)) {
3275 #ifdef SPARCV9
3276                                 size_reg = sparc_g4;
3277 #else
3278                                 size_reg = sparc_g1;
3279 #endif
3280                                 sparc_mov_reg_reg (code, ins->dreg, size_reg);
3281                         }
3282                         else
3283                                 size_reg = ins->sreg1;
3284
3285                         sparc_sub (code, FALSE, sparc_sp, ins->dreg, ins->dreg);
3286                         /* Keep %sp valid at all times */
3287                         sparc_mov_reg_reg (code, ins->dreg, sparc_sp);
3288                         /* Round localloc_offset too so the result is at least 8 aligned */
3289                         offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
3290                         g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
3291                         sparc_add_imm (code, FALSE, ins->dreg, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
3292
3293                         if (ins->flags & MONO_INST_INIT) {
3294                                 guint32 *br [3];
3295                                 /* Initialize memory region */
3296                                 sparc_cmp_imm (code, size_reg, 0);
3297                                 br [0] = code;
3298                                 sparc_branch (code, 0, sparc_be, 0);
3299                                 /* delay slot */
3300                                 sparc_set (code, 0, sparc_o7);
3301                                 sparc_sub_imm (code, 0, size_reg, sparcv9 ? 8 : 4, size_reg);
3302                                 /* start of loop */
3303                                 br [1] = code;
3304                                 if (sparcv9)
3305                                         sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3306                                 else
3307                                         sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3308                                 sparc_cmp (code, sparc_o7, size_reg);
3309                                 br [2] = code;
3310                                 sparc_branch (code, 0, sparc_bl, 0);
3311                                 sparc_patch (br [2], br [1]);
3312                                 /* delay slot */
3313                                 sparc_add_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3314                                 sparc_patch (br [0], code);
3315                         }
3316                         break;
3317                 }
3318                 case OP_LOCALLOC_IMM: {
3319                         gint32 offset = ins->inst_imm;
3320                         gint32 offset2;
3321                         
3322 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
3323                         /* Perform stack touching */
3324                         NOT_IMPLEMENTED;
3325 #endif
3326
3327                         /* To compensate for the rounding of localloc_offset */
3328                         offset += sizeof (gpointer);
3329                         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
3330                         if (sparc_is_imm13 (offset))
3331                                 sparc_sub_imm (code, FALSE, sparc_sp, offset, sparc_sp);
3332                         else {
3333                                 sparc_set (code, offset, sparc_o7);
3334                                 sparc_sub (code, FALSE, sparc_sp, sparc_o7, sparc_sp);
3335                         }
3336                         /* Round localloc_offset too so the result is at least 8 aligned */
3337                         offset2 = ALIGN_TO (cfg->arch.localloc_offset, 8);
3338                         g_assert (sparc_is_imm13 (MONO_SPARC_STACK_BIAS + offset2));
3339                         sparc_add_imm (code, FALSE, sparc_sp, MONO_SPARC_STACK_BIAS + offset2, ins->dreg);
3340                         if ((ins->flags & MONO_INST_INIT) && (offset > 0)) {
3341                                 guint32 *br [2];
3342                                 int i;
3343
3344                                 if (offset <= 16) {
3345                                         i = 0;
3346                                         while (i < offset) {
3347                                                 if (sparcv9) {
3348                                                         sparc_stx_imm (code, sparc_g0, ins->dreg, i);
3349                                                         i += 8;
3350                                                 }
3351                                                 else {
3352                                                         sparc_st_imm (code, sparc_g0, ins->dreg, i);
3353                                                         i += 4;
3354                                                 }
3355                                         }
3356                                 }
3357                                 else {
3358                                         sparc_set (code, offset, sparc_o7);
3359                                         sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3360                                         /* beginning of loop */
3361                                         br [0] = code;
3362                                         if (sparcv9)
3363                                                 sparc_stx (code, sparc_g0, ins->dreg, sparc_o7);
3364                                         else
3365                                                 sparc_st (code, sparc_g0, ins->dreg, sparc_o7);
3366                                         sparc_cmp_imm (code, sparc_o7, 0);
3367                                         br [1] = code;
3368                                         sparc_branch (code, 0, sparc_bne, 0);
3369                                         /* delay slot */
3370                                         sparc_sub_imm (code, 0, sparc_o7, sparcv9 ? 8 : 4, sparc_o7);
3371                                         sparc_patch (br [1], br [0]);
3372                                 }
3373                         }
3374                         break;
3375                 }
3376                 case OP_THROW:
3377                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3378                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3379                                              (gpointer)"mono_arch_throw_exception");
3380                         EMIT_CALL ();
3381                         break;
3382                 case OP_RETHROW:
3383                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3384                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
3385                                              (gpointer)"mono_arch_rethrow_exception");
3386                         EMIT_CALL ();
3387                         break;
3388                 case OP_START_HANDLER: {
3389                         /*
3390                          * The START_HANDLER instruction marks the beginning of a handler 
3391                          * block. It is called using a call instruction, so %o7 contains 
3392                          * the return address. Since the handler executes in the same stack
3393              * frame as the method itself, we can't use save/restore to save 
3394                          * the return address. Instead, we save it into a dedicated 
3395                          * variable.
3396                          */
3397                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3398                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3399                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3400                                 sparc_sti (code, sparc_o7, spvar->inst_basereg, GP_SCRATCH_REG);
3401                         }
3402                         else
3403                                 sparc_sti_imm (code, sparc_o7, spvar->inst_basereg, spvar->inst_offset);
3404                         break;
3405                 }
3406                 case OP_ENDFILTER: {
3407                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3408                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3409                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3410                                 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3411                         }
3412                         else
3413                                 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3414                         sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3415                         /* Delay slot */
3416                         sparc_mov_reg_reg (code, ins->sreg1, sparc_o0);
3417                         break;
3418                 }
3419                 case OP_ENDFINALLY: {
3420                         MonoInst *spvar = mono_find_spvar_for_region (cfg, bb->region);
3421                         if (!sparc_is_imm13 (spvar->inst_offset)) {
3422                                 sparc_set (code, spvar->inst_offset, GP_SCRATCH_REG);
3423                                 sparc_ldi (code, spvar->inst_basereg, GP_SCRATCH_REG, sparc_o7);
3424                         }
3425                         else
3426                                 sparc_ldi_imm (code, spvar->inst_basereg, spvar->inst_offset, sparc_o7);
3427                         sparc_jmpl_imm (code, sparc_o7, 8, sparc_g0);
3428                         sparc_nop (code);
3429                         break;
3430                 }
3431                 case OP_CALL_HANDLER: 
3432                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3433                         /* This is a jump inside the method, so call_simple works even on V9 */
3434                         sparc_call_simple (code, 0);
3435                         sparc_nop (code);
3436                         break;
3437                 case OP_LABEL:
3438                         ins->inst_c0 = (guint8*)code - cfg->native_code;
3439                         break;
3440                 case OP_RELAXED_NOP:
3441                 case OP_NOP:
3442                 case OP_DUMMY_USE:
3443                 case OP_DUMMY_STORE:
3444                 case OP_NOT_REACHED:
3445                 case OP_NOT_NULL:
3446                         break;
3447                 case OP_BR:
3448                         //g_print ("target: %p, next: %p, curr: %p, last: %p\n", ins->inst_target_bb, bb->next_bb, ins, bb->last_ins);
3449                         if ((ins->inst_target_bb == bb->next_bb) && ins == bb->last_ins)
3450                                 break;
3451                         if (ins->flags & MONO_INST_BRLABEL) {
3452                                 if (ins->inst_i0->inst_c0) {
3453                                         gint32 disp = (ins->inst_i0->inst_c0 - ((guint8*)code - cfg->native_code)) >> 2;
3454                                         g_assert (sparc_is_imm22 (disp));
3455                                         sparc_branch (code, 1, sparc_ba, disp);
3456                                 } else {
3457                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
3458                                         sparc_branch (code, 1, sparc_ba, 0);
3459                                 }
3460                         } else {
3461                                 if (ins->inst_target_bb->native_offset) {
3462                                         gint32 disp = (ins->inst_target_bb->native_offset - ((guint8*)code - cfg->native_code)) >> 2;
3463                                         g_assert (sparc_is_imm22 (disp));
3464                                         sparc_branch (code, 1, sparc_ba, disp);
3465                                 } else {
3466                                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
3467                                         sparc_branch (code, 1, sparc_ba, 0);
3468                                 } 
3469                         }
3470                         sparc_nop (code);
3471                         break;
3472                 case OP_BR_REG:
3473                         sparc_jmp (code, ins->sreg1, sparc_g0);
3474                         sparc_nop (code);
3475                         break;
3476                 case OP_CEQ:
3477                 case OP_CLT:
3478                 case OP_CLT_UN:
3479                 case OP_CGT:
3480                 case OP_CGT_UN:
3481                         if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3482                                 sparc_clr_reg (code, ins->dreg);
3483                                 sparc_movcc_imm (code, sparc_xcc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3484                         }
3485                         else {
3486                                 sparc_clr_reg (code, ins->dreg);
3487 #ifdef SPARCV9
3488                                 sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), DEFAULT_ICC, 0, 2);
3489 #else
3490                                 sparc_branch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3491 #endif
3492                                 /* delay slot */
3493                                 sparc_set (code, 1, ins->dreg);
3494                         }
3495                         break;
3496                 case OP_ICEQ:
3497                 case OP_ICLT:
3498                 case OP_ICLT_UN:
3499                 case OP_ICGT:
3500                 case OP_ICGT_UN:
3501                     if (v64 && (cfg->opt & MONO_OPT_CMOV)) {
3502                                 sparc_clr_reg (code, ins->dreg);
3503                                 sparc_movcc_imm (code, sparc_icc, opcode_to_sparc_cond (ins->opcode), 1, ins->dreg);
3504                     }
3505                     else {
3506                         sparc_clr_reg (code, ins->dreg);
3507                         sparc_branchp (code, 1, opcode_to_sparc_cond (ins->opcode), sparc_icc_short, 0, 2);
3508                         /* delay slot */
3509                         sparc_set (code, 1, ins->dreg);
3510                     }
3511                     break;
3512                 case OP_COND_EXC_EQ:
3513                 case OP_COND_EXC_NE_UN:
3514                 case OP_COND_EXC_LT:
3515                 case OP_COND_EXC_LT_UN:
3516                 case OP_COND_EXC_GT:
3517                 case OP_COND_EXC_GT_UN:
3518                 case OP_COND_EXC_GE:
3519                 case OP_COND_EXC_GE_UN:
3520                 case OP_COND_EXC_LE:
3521                 case OP_COND_EXC_LE_UN:
3522                 case OP_COND_EXC_OV:
3523                 case OP_COND_EXC_NO:
3524                 case OP_COND_EXC_C:
3525                 case OP_COND_EXC_NC:
3526                 case OP_COND_EXC_IEQ:
3527                 case OP_COND_EXC_INE_UN:
3528                 case OP_COND_EXC_ILT:
3529                 case OP_COND_EXC_ILT_UN:
3530                 case OP_COND_EXC_IGT:
3531                 case OP_COND_EXC_IGT_UN:
3532                 case OP_COND_EXC_IGE:
3533                 case OP_COND_EXC_IGE_UN:
3534                 case OP_COND_EXC_ILE:
3535                 case OP_COND_EXC_ILE_UN:
3536                 case OP_COND_EXC_IOV:
3537                 case OP_COND_EXC_INO:
3538                 case OP_COND_EXC_IC:
3539                 case OP_COND_EXC_INC:
3540 #ifdef SPARCV9
3541                         NOT_IMPLEMENTED;
3542 #else
3543                         EMIT_COND_SYSTEM_EXCEPTION (ins, opcode_to_sparc_cond (ins->opcode), ins->inst_p1);
3544 #endif
3545                         break;
3546                 case OP_SPARC_COND_EXC_EQZ:
3547                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brz, ins->inst_p1);
3548                         break;
3549                 case OP_SPARC_COND_EXC_GEZ:
3550                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgez, ins->inst_p1);
3551                         break;
3552                 case OP_SPARC_COND_EXC_GTZ:
3553                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brgz, ins->inst_p1);
3554                         break;
3555                 case OP_SPARC_COND_EXC_LEZ:
3556                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlez, ins->inst_p1);
3557                         break;
3558                 case OP_SPARC_COND_EXC_LTZ:
3559                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brlz, ins->inst_p1);
3560                         break;
3561                 case OP_SPARC_COND_EXC_NEZ:
3562                         EMIT_COND_SYSTEM_EXCEPTION_BPR (ins, brnz, ins->inst_p1);
3563                         break;
3564
3565                 case OP_IBEQ:
3566                 case OP_IBNE_UN:
3567                 case OP_IBLT:
3568                 case OP_IBLT_UN:
3569                 case OP_IBGT:
3570                 case OP_IBGT_UN:
3571                 case OP_IBGE:
3572                 case OP_IBGE_UN:
3573                 case OP_IBLE:
3574                 case OP_IBLE_UN: {
3575                         if (sparcv9)
3576                                 EMIT_COND_BRANCH_PREDICTED (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3577                         else
3578                                 EMIT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3579                         break;
3580                 }
3581
3582                 case OP_SPARC_BRZ:
3583                         EMIT_COND_BRANCH_BPR (ins, brz, 1, 1, 1);
3584                         break;
3585                 case OP_SPARC_BRLEZ:
3586                         EMIT_COND_BRANCH_BPR (ins, brlez, 1, 1, 1);
3587                         break;
3588                 case OP_SPARC_BRLZ:
3589                         EMIT_COND_BRANCH_BPR (ins, brlz, 1, 1, 1);
3590                         break;
3591                 case OP_SPARC_BRNZ:
3592                         EMIT_COND_BRANCH_BPR (ins, brnz, 1, 1, 1);
3593                         break;
3594                 case OP_SPARC_BRGZ:
3595                         EMIT_COND_BRANCH_BPR (ins, brgz, 1, 1, 1);
3596                         break;
3597                 case OP_SPARC_BRGEZ:
3598                         EMIT_COND_BRANCH_BPR (ins, brgez, 1, 1, 1);
3599                         break;
3600
3601                 /* floating point opcodes */
3602                 case OP_R8CONST:
3603                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R8, ins->inst_p0);
3604 #ifdef SPARCV9
3605                         sparc_set_template (code, sparc_o7);
3606 #else
3607                         sparc_sethi (code, 0, sparc_o7);
3608 #endif
3609                         sparc_lddf_imm (code, sparc_o7, 0, ins->dreg);
3610                         break;
3611                 case OP_R4CONST:
3612                         mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_R4, ins->inst_p0);
3613 #ifdef SPARCV9
3614                         sparc_set_template (code, sparc_o7);
3615 #else
3616                         sparc_sethi (code, 0, sparc_o7);
3617 #endif
3618                         sparc_ldf_imm (code, sparc_o7, 0, FP_SCRATCH_REG);
3619
3620                         /* Extend to double */
3621                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3622                         break;
3623                 case OP_STORER8_MEMBASE_REG:
3624                         if (!sparc_is_imm13 (ins->inst_offset + 4)) {
3625                                 sparc_set (code, ins->inst_offset, sparc_o7);
3626                                 /* SPARCV9 handles misaligned fp loads/stores */
3627                                 if (!v64 && (ins->inst_offset % 8)) {
3628                                         /* Misaligned */
3629                                         sparc_add (code, FALSE, ins->inst_destbasereg, sparc_o7, sparc_o7);
3630                                         sparc_stf (code, ins->sreg1, sparc_o7, sparc_g0);
3631                                         sparc_stf_imm (code, ins->sreg1 + 1, sparc_o7, 4);
3632                                 } else
3633                                         sparc_stdf (code, ins->sreg1, ins->inst_destbasereg, sparc_o7);
3634                         }
3635                         else {
3636                                 if (!v64 && (ins->inst_offset % 8)) {
3637                                         /* Misaligned */
3638                                         sparc_stf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3639                                         sparc_stf_imm (code, ins->sreg1 + 1, ins->inst_destbasereg, ins->inst_offset + 4);
3640                                 } else
3641                                         sparc_stdf_imm (code, ins->sreg1, ins->inst_destbasereg, ins->inst_offset);
3642                         }
3643                         break;
3644                 case OP_LOADR8_MEMBASE:
3645                         EMIT_LOAD_MEMBASE (ins, lddf);
3646                         break;
3647                 case OP_STORER4_MEMBASE_REG:
3648                         /* This requires a double->single conversion */
3649                         sparc_fdtos (code, ins->sreg1, FP_SCRATCH_REG);
3650                         if (!sparc_is_imm13 (ins->inst_offset)) {
3651                                 sparc_set (code, ins->inst_offset, sparc_o7);
3652                                 sparc_stf (code, FP_SCRATCH_REG, ins->inst_destbasereg, sparc_o7);
3653                         }
3654                         else
3655                                 sparc_stf_imm (code, FP_SCRATCH_REG, ins->inst_destbasereg, ins->inst_offset);
3656                         break;
3657                 case OP_LOADR4_MEMBASE: {
3658                         /* ldf needs a single precision register */
3659                         int dreg = ins->dreg;
3660                         ins->dreg = FP_SCRATCH_REG;
3661                         EMIT_LOAD_MEMBASE (ins, ldf);
3662                         ins->dreg = dreg;
3663                         /* Extend to double */
3664                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3665                         break;
3666                 }
3667                 case OP_ICONV_TO_R4: {
3668                         gint32 offset = cfg->arch.float_spill_slot_offset;
3669 #ifdef SPARCV9
3670                         if (!sparc_is_imm13 (offset)) {
3671                                 sparc_set (code, offset, sparc_o7);
3672                                 sparc_stx (code, ins->sreg1, sparc_sp, offset);
3673                                 sparc_lddf (code, sparc_sp, offset, FP_SCRATCH_REG);
3674                         } else {
3675                                 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3676                                 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3677                         }
3678                         sparc_fxtos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3679 #else
3680                         if (!sparc_is_imm13 (offset)) {
3681                                 sparc_set (code, offset, sparc_o7);
3682                                 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3683                                 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3684                         } else {
3685                                 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3686                                 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3687                         }
3688                         sparc_fitos (code, FP_SCRATCH_REG, FP_SCRATCH_REG);
3689 #endif
3690                         sparc_fstod (code, FP_SCRATCH_REG, ins->dreg);
3691                         break;
3692                 }
3693                 case OP_ICONV_TO_R8: {
3694                         gint32 offset = cfg->arch.float_spill_slot_offset;
3695 #ifdef SPARCV9
3696                         if (!sparc_is_imm13 (offset)) {
3697                                 sparc_set (code, offset, sparc_o7);
3698                                 sparc_stx (code, ins->sreg1, sparc_sp, sparc_o7);
3699                                 sparc_lddf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3700                         } else {
3701                                 sparc_stx_imm (code, ins->sreg1, sparc_sp, offset);
3702                                 sparc_lddf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3703                         }
3704                         sparc_fxtod (code, FP_SCRATCH_REG, ins->dreg);
3705 #else
3706                         if (!sparc_is_imm13 (offset)) {
3707                                 sparc_set (code, offset, sparc_o7);
3708                                 sparc_st (code, ins->sreg1, sparc_sp, sparc_o7);
3709                                 sparc_ldf (code, sparc_sp, sparc_o7, FP_SCRATCH_REG);
3710                         } else {
3711                                 sparc_st_imm (code, ins->sreg1, sparc_sp, offset);
3712                                 sparc_ldf_imm (code, sparc_sp, offset, FP_SCRATCH_REG);
3713                         }
3714                         sparc_fitod (code, FP_SCRATCH_REG, ins->dreg);
3715 #endif
3716                         break;
3717                 }
3718                 case OP_FCONV_TO_I1:
3719                 case OP_FCONV_TO_U1:
3720                 case OP_FCONV_TO_I2:
3721                 case OP_FCONV_TO_U2:
3722 #ifndef SPARCV9
3723                 case OP_FCONV_TO_I:
3724                 case OP_FCONV_TO_U:
3725 #endif
3726                 case OP_FCONV_TO_I4:
3727                 case OP_FCONV_TO_U4: {
3728                         gint32 offset = cfg->arch.float_spill_slot_offset;
3729                         sparc_fdtoi (code, ins->sreg1, FP_SCRATCH_REG);
3730                         if (!sparc_is_imm13 (offset)) {
3731                                 sparc_set (code, offset, sparc_o7);
3732                                 sparc_stdf (code, FP_SCRATCH_REG, sparc_sp, sparc_o7);
3733                                 sparc_ld (code, sparc_sp, sparc_o7, ins->dreg);
3734                         } else {
3735                                 sparc_stdf_imm (code, FP_SCRATCH_REG, sparc_sp, offset);
3736                                 sparc_ld_imm (code, sparc_sp, offset, ins->dreg);
3737                         }
3738
3739                         switch (ins->opcode) {
3740                         case OP_FCONV_TO_I1:
3741                         case OP_FCONV_TO_U1:
3742                                 sparc_and_imm (code, 0, ins->dreg, 0xff, ins->dreg);
3743                                 break;
3744                         case OP_FCONV_TO_I2:
3745                         case OP_FCONV_TO_U2:
3746                                 sparc_set (code, 0xffff, sparc_o7);
3747                                 sparc_and (code, 0, ins->dreg, sparc_o7, ins->dreg);
3748                                 break;
3749                         default:
3750                                 break;
3751                         }
3752                         break;
3753                 }
3754                 case OP_FCONV_TO_I8:
3755                 case OP_FCONV_TO_U8:
3756                         /* Emulated */
3757                         g_assert_not_reached ();
3758                         break;
3759                 case OP_FCONV_TO_R4:
3760                         /* FIXME: Change precision ? */
3761 #ifdef SPARCV9
3762                         sparc_fmovd (code, ins->sreg1, ins->dreg);
3763 #else
3764                         sparc_fmovs (code, ins->sreg1, ins->dreg);
3765                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3766 #endif
3767                         break;
3768                 case OP_LCONV_TO_R_UN: { 
3769                         /* Emulated */
3770                         g_assert_not_reached ();
3771                         break;
3772                 }
3773                 case OP_LCONV_TO_OVF_I:
3774                 case OP_LCONV_TO_OVF_I4_2: {
3775                         guint32 *br [3], *label [1];
3776
3777                         /* 
3778                          * Valid ints: 0xffffffff:8000000 to 00000000:0x7f000000
3779                          */
3780                         sparc_cmp_imm (code, ins->sreg1, 0);
3781                         br [0] = code; 
3782                         sparc_branch (code, 1, sparc_bneg, 0);
3783                         sparc_nop (code);
3784
3785                         /* positive */
3786                         /* ms word must be 0 */
3787                         sparc_cmp_imm (code, ins->sreg2, 0);
3788                         br [1] = code;
3789                         sparc_branch (code, 1, sparc_be, 0);
3790                         sparc_nop (code);
3791
3792                         label [0] = code;
3793
3794                         EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_ba, "OverflowException");
3795
3796                         /* negative */
3797                         sparc_patch (br [0], code);
3798
3799                         /* ms word must 0xfffffff */
3800                         sparc_cmp_imm (code, ins->sreg2, -1);
3801                         br [2] = code;
3802                         sparc_branch (code, 1, sparc_bne, 0);
3803                         sparc_nop (code);
3804                         sparc_patch (br [2], label [0]);
3805
3806                         /* Ok */
3807                         sparc_patch (br [1], code);
3808                         if (ins->sreg1 != ins->dreg)
3809                                 sparc_mov_reg_reg (code, ins->sreg1, ins->dreg);
3810                         break;
3811                 }
3812                 case OP_FADD:
3813                         sparc_faddd (code, ins->sreg1, ins->sreg2, ins->dreg);
3814                         break;
3815                 case OP_FSUB:
3816                         sparc_fsubd (code, ins->sreg1, ins->sreg2, ins->dreg);
3817                         break;          
3818                 case OP_FMUL:
3819                         sparc_fmuld (code, ins->sreg1, ins->sreg2, ins->dreg);
3820                         break;          
3821                 case OP_FDIV:
3822                         sparc_fdivd (code, ins->sreg1, ins->sreg2, ins->dreg);
3823                         break;          
3824                 case OP_FNEG:
3825 #ifdef SPARCV9
3826                         sparc_fnegd (code, ins->sreg1, ins->dreg);
3827 #else
3828                         /* FIXME: why don't use fnegd ? */
3829                         sparc_fnegs (code, ins->sreg1, ins->dreg);
3830 #endif
3831                         break;          
3832                 case OP_FREM:
3833                         sparc_fdivd (code, ins->sreg1, ins->sreg2, FP_SCRATCH_REG);
3834                         sparc_fmuld (code, ins->sreg2, FP_SCRATCH_REG, FP_SCRATCH_REG);
3835                         sparc_fsubd (code, ins->sreg1, FP_SCRATCH_REG, ins->dreg);
3836                         break;
3837                 case OP_FCOMPARE:
3838                         sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3839                         break;
3840                 case OP_FCEQ:
3841                 case OP_FCLT:
3842                 case OP_FCLT_UN:
3843                 case OP_FCGT:
3844                 case OP_FCGT_UN:
3845                         sparc_fcmpd (code, ins->sreg1, ins->sreg2);
3846                         sparc_clr_reg (code, ins->dreg);
3847                         switch (ins->opcode) {
3848                         case OP_FCLT_UN:
3849                         case OP_FCGT_UN:
3850                                 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 4);
3851                                 /* delay slot */
3852                                 sparc_set (code, 1, ins->dreg);
3853                                 sparc_fbranch (code, 1, sparc_fbu, 2);
3854                                 /* delay slot */
3855                                 sparc_set (code, 1, ins->dreg);
3856                                 break;
3857                         default:
3858                                 sparc_fbranch (code, 1, opcode_to_sparc_cond (ins->opcode), 2);
3859                                 /* delay slot */
3860                                 sparc_set (code, 1, ins->dreg);                         
3861                         }
3862                         break;
3863                 case OP_FBEQ:
3864                 case OP_FBLT:
3865                 case OP_FBGT:
3866                         EMIT_FLOAT_COND_BRANCH (ins, opcode_to_sparc_cond (ins->opcode), 1, 1);
3867                         break;
3868                 case OP_FBGE: {
3869                         /* clt.un + brfalse */
3870                         guint32 *p = code;
3871                         sparc_fbranch (code, 1, sparc_fbul, 0);
3872                         /* delay slot */
3873                         sparc_nop (code);
3874                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3875                         sparc_patch (p, (guint8*)code);
3876                         break;
3877                 }
3878                 case OP_FBLE: {
3879                         /* cgt.un + brfalse */
3880                         guint32 *p = code;
3881                         sparc_fbranch (code, 1, sparc_fbug, 0);
3882                         /* delay slot */
3883                         sparc_nop (code);
3884                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fba, 1, 1);
3885                         sparc_patch (p, (guint8*)code);
3886                         break;
3887                 }
3888                 case OP_FBNE_UN:
3889                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbne, 1, 1);
3890                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3891                         break;
3892                 case OP_FBLT_UN:
3893                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbl, 1, 1);
3894                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3895                         break;
3896                 case OP_FBGT_UN:
3897                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbg, 1, 1);
3898                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3899                         break;
3900                 case OP_FBGE_UN:
3901                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbge, 1, 1);
3902                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3903                         break;
3904                 case OP_FBLE_UN:
3905                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fble, 1, 1);
3906                         EMIT_FLOAT_COND_BRANCH (ins, sparc_fbu, 1, 1);
3907                         break;
3908                 case OP_CKFINITE: {
3909                         gint32 offset = cfg->arch.float_spill_slot_offset;
3910                         if (!sparc_is_imm13 (offset)) {
3911                                 sparc_set (code, offset, sparc_o7);
3912                                 sparc_stdf (code, ins->sreg1, sparc_sp, sparc_o7);
3913                                 sparc_lduh (code, sparc_sp, sparc_o7, sparc_o7);
3914                         } else {
3915                                 sparc_stdf_imm (code, ins->sreg1, sparc_sp, offset);
3916                                 sparc_lduh_imm (code, sparc_sp, offset, sparc_o7);
3917                         }
3918                         sparc_srl_imm (code, sparc_o7, 4, sparc_o7);
3919                         sparc_and_imm (code, FALSE, sparc_o7, 2047, sparc_o7);
3920                         sparc_cmp_imm (code, sparc_o7, 2047);
3921                         EMIT_COND_SYSTEM_EXCEPTION (ins, sparc_be, "ArithmeticException");
3922 #ifdef SPARCV9
3923                         sparc_fmovd (code, ins->sreg1, ins->dreg);
3924 #else
3925                         sparc_fmovs (code, ins->sreg1, ins->dreg);
3926                         sparc_fmovs (code, ins->sreg1 + 1, ins->dreg + 1);
3927 #endif
3928                         break;
3929                 }
3930
3931                 case OP_MEMORY_BARRIER:
3932                         sparc_membar (code, sparc_membar_all);
3933                         break;
3934
3935                 default:
3936 #ifdef __GNUC__
3937                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
3938 #else
3939                         g_warning ("%s:%d: unknown opcode %s\n", __FILE__, __LINE__, mono_inst_name (ins->opcode));
3940 #endif
3941                         g_assert_not_reached ();
3942                 }
3943
3944                 if ((((guint8*)code) - code_start) > max_len) {
3945                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
3946                                    mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
3947                         g_assert_not_reached ();
3948                 }
3949                
3950                 cpos += max_len;
3951
3952                 last_ins = ins;
3953         }
3954
3955         cfg->code_len = (guint8*)code - cfg->native_code;
3956 }
3957
3958 void
3959 mono_arch_register_lowlevel_calls (void)
3960 {
3961         mono_register_jit_icall (mono_arch_get_lmf_addr, "mono_arch_get_lmf_addr", NULL, TRUE);
3962 }
3963
3964 void
3965 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
3966 {
3967         MonoJumpInfo *patch_info;
3968
3969         /* FIXME: Move part of this to arch independent code */
3970         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
3971                 unsigned char *ip = patch_info->ip.i + code;
3972                 gpointer target;
3973
3974                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
3975
3976                 switch (patch_info->type) {
3977                 case MONO_PATCH_INFO_NONE:
3978                         continue;
3979                 case MONO_PATCH_INFO_CLASS_INIT: {
3980                         guint32 *ip2 = (guint32*)ip;
3981                         /* Might already been changed to a nop */
3982 #ifdef SPARCV9
3983                         sparc_set_template (ip2, sparc_o7);
3984                         sparc_jmpl (ip2, sparc_o7, sparc_g0, sparc_o7);
3985 #else
3986                         sparc_call_simple (ip2, 0);
3987 #endif
3988                         break;
3989                 }
3990                 case MONO_PATCH_INFO_METHOD_JUMP: {
3991                         guint32 *ip2 = (guint32*)ip;
3992                         /* Might already been patched */
3993                         sparc_set_template (ip2, sparc_o7);
3994                         break;
3995                 }
3996                 default:
3997                         break;
3998                 }
3999                 sparc_patch ((guint32*)ip, target);
4000         }
4001 }
4002
4003 void*
4004 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
4005 {
4006         int i;
4007         guint32 *code = (guint32*)p;
4008         MonoMethodSignature *sig = mono_method_signature (cfg->method);
4009         CallInfo *cinfo;
4010
4011         /* Save registers to stack */
4012         for (i = 0; i < 6; ++i)
4013                 sparc_sti_imm (code, sparc_i0 + i, sparc_fp, ARGS_OFFSET + (i * sizeof (gpointer)));
4014
4015         cinfo = get_call_info (cfg, sig, FALSE);
4016
4017         /* Save float regs on V9, since they are caller saved */
4018         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4019                 ArgInfo *ainfo = cinfo->args + i;
4020                 gint32 stack_offset;
4021
4022                 stack_offset = ainfo->offset + ARGS_OFFSET;
4023
4024                 if (ainfo->storage == ArgInFloatReg) {
4025                         if (!sparc_is_imm13 (stack_offset))
4026                                 NOT_IMPLEMENTED;
4027                         sparc_stf_imm (code, ainfo->reg, sparc_fp, stack_offset);
4028                 }
4029                 else if (ainfo->storage == ArgInDoubleReg) {
4030                         /* The offset is guaranteed to be aligned by the ABI rules */
4031                         sparc_stdf_imm (code, ainfo->reg, sparc_fp, stack_offset);
4032                 }
4033         }
4034
4035         sparc_set (code, cfg->method, sparc_o0);
4036         sparc_add_imm (code, FALSE, sparc_fp, MONO_SPARC_STACK_BIAS, sparc_o1);
4037
4038         mono_add_patch_info (cfg, (guint8*)code-cfg->native_code, MONO_PATCH_INFO_ABS, func);
4039         EMIT_CALL ();
4040
4041         /* Restore float regs on V9 */
4042         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4043                 ArgInfo *ainfo = cinfo->args + i;
4044                 gint32 stack_offset;
4045
4046                 stack_offset = ainfo->offset + ARGS_OFFSET;
4047
4048                 if (ainfo->storage == ArgInFloatReg) {
4049                         if (!sparc_is_imm13 (stack_offset))
4050                                 NOT_IMPLEMENTED;
4051                         sparc_ldf_imm (code, sparc_fp, stack_offset, ainfo->reg);
4052                 }
4053                 else if (ainfo->storage == ArgInDoubleReg) {
4054                         /* The offset is guaranteed to be aligned by the ABI rules */
4055                         sparc_lddf_imm (code, sparc_fp, stack_offset, ainfo->reg);
4056                 }
4057         }
4058
4059         g_free (cinfo);
4060
4061         return code;
4062 }
4063
4064 enum {
4065         SAVE_NONE,
4066         SAVE_STRUCT,
4067         SAVE_ONE,
4068         SAVE_TWO,
4069         SAVE_FP
4070 };
4071
4072 void*
4073 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
4074 {
4075         guint32 *code = (guint32*)p;
4076         int save_mode = SAVE_NONE;
4077         MonoMethod *method = cfg->method;
4078
4079         switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
4080         case MONO_TYPE_VOID:
4081                 /* special case string .ctor icall */
4082                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
4083                         save_mode = SAVE_ONE;
4084                 else
4085                         save_mode = SAVE_NONE;
4086                 break;
4087         case MONO_TYPE_I8:
4088         case MONO_TYPE_U8:
4089 #ifdef SPARCV9
4090                 save_mode = SAVE_ONE;
4091 #else
4092                 save_mode = SAVE_TWO;
4093 #endif
4094                 break;
4095         case MONO_TYPE_R4:
4096         case MONO_TYPE_R8:
4097                 save_mode = SAVE_FP;
4098                 break;
4099         case MONO_TYPE_VALUETYPE:
4100                 save_mode = SAVE_STRUCT;
4101                 break;
4102         default:
4103                 save_mode = SAVE_ONE;
4104                 break;
4105         }
4106
4107         /* Save the result to the stack and also put it into the output registers */
4108
4109         switch (save_mode) {
4110         case SAVE_TWO:
4111                 /* V8 only */
4112                 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
4113                 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
4114                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
4115                 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
4116                 break;
4117         case SAVE_ONE:
4118                 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
4119                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
4120                 break;
4121         case SAVE_FP:
4122 #ifdef SPARCV9
4123                 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
4124 #else
4125                 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
4126                 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
4127                 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
4128 #endif
4129                 break;
4130         case SAVE_STRUCT:
4131 #ifdef SPARCV9
4132                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
4133 #else
4134                 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
4135 #endif
4136                 break;
4137         case SAVE_NONE:
4138         default:
4139                 break;
4140         }
4141
4142         sparc_set (code, cfg->method, sparc_o0);
4143
4144         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
4145         EMIT_CALL ();
4146
4147         /* Restore result */
4148
4149         switch (save_mode) {
4150         case SAVE_TWO:
4151                 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
4152                 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
4153                 break;
4154         case SAVE_ONE:
4155                 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
4156                 break;
4157         case SAVE_FP:
4158                 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
4159                 break;
4160         case SAVE_NONE:
4161         default:
4162                 break;
4163         }
4164
4165         return code;
4166 }
4167
4168 guint8 *
4169 mono_arch_emit_prolog (MonoCompile *cfg)
4170 {
4171         MonoMethod *method = cfg->method;
4172         MonoMethodSignature *sig;
4173         MonoInst *inst;
4174         guint32 *code;
4175         CallInfo *cinfo;
4176         guint32 i, offset;
4177
4178         cfg->code_size = 256;
4179         cfg->native_code = g_malloc (cfg->code_size);
4180         code = (guint32*)cfg->native_code;
4181
4182         /* FIXME: Generate intermediate code instead */
4183
4184         offset = cfg->stack_offset;
4185         offset += (16 * sizeof (gpointer)); /* register save area */
4186 #ifndef SPARCV9
4187         offset += 4; /* struct/union return pointer */
4188 #endif
4189
4190         /* add parameter area size for called functions */
4191         if (cfg->param_area < (6 * sizeof (gpointer)))
4192                 /* Reserve space for the first 6 arguments even if it is unused */
4193                 offset += 6 * sizeof (gpointer);
4194         else
4195                 offset += cfg->param_area;
4196         
4197         /* align the stack size */
4198         offset = ALIGN_TO (offset, MONO_ARCH_FRAME_ALIGNMENT);
4199
4200         /*
4201          * localloc'd memory is stored between the local variables (whose
4202          * size is given by cfg->stack_offset), and between the space reserved
4203          * by the ABI.
4204          */
4205         cfg->arch.localloc_offset = offset - cfg->stack_offset;
4206
4207         cfg->stack_offset = offset;
4208
4209 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4210                         /* Perform stack touching */
4211                         NOT_IMPLEMENTED;
4212 #endif
4213
4214         if (!sparc_is_imm13 (- cfg->stack_offset)) {
4215                 /* Can't use sparc_o7 here, since we're still in the caller's frame */
4216                 sparc_set (code, (- cfg->stack_offset), GP_SCRATCH_REG);
4217                 sparc_save (code, sparc_sp, GP_SCRATCH_REG, sparc_sp);
4218         }
4219         else
4220                 sparc_save_imm (code, sparc_sp, - cfg->stack_offset, sparc_sp);
4221
4222 /*
4223         if (strstr (cfg->method->name, "foo")) {
4224                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4225                 sparc_call_simple (code, 0);
4226                 sparc_nop (code);
4227         }
4228 */
4229
4230         sig = mono_method_signature (method);
4231
4232         cinfo = get_call_info (cfg, sig, FALSE);
4233
4234         /* Keep in sync with emit_load_volatile_arguments */
4235         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
4236                 ArgInfo *ainfo = cinfo->args + i;
4237                 gint32 stack_offset;
4238                 MonoType *arg_type;
4239                 inst = cfg->args [i];
4240
4241                 if (sig->hasthis && (i == 0))
4242                         arg_type = &mono_defaults.object_class->byval_arg;
4243                 else
4244                         arg_type = sig->params [i - sig->hasthis];
4245
4246                 stack_offset = ainfo->offset + ARGS_OFFSET;
4247
4248                 /* Save the split arguments so they will reside entirely on the stack */
4249                 if (ainfo->storage == ArgInSplitRegStack) {
4250                         /* Save the register to the stack */
4251                         g_assert (inst->opcode == OP_REGOFFSET);
4252                         if (!sparc_is_imm13 (stack_offset))
4253                                 NOT_IMPLEMENTED;
4254                         sparc_st_imm (code, sparc_i5, inst->inst_basereg, stack_offset);
4255                 }
4256
4257                 if (!v64 && !arg_type->byref && (arg_type->type == MONO_TYPE_R8)) {
4258                         /* Save the argument to a dword aligned stack location */
4259                         /*
4260                          * stack_offset contains the offset of the argument on the stack.
4261                          * inst->inst_offset contains the dword aligned offset where the value 
4262                          * should be stored.
4263                          */
4264                         if (ainfo->storage == ArgInIRegPair) {
4265                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
4266                                         NOT_IMPLEMENTED;
4267                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
4268                                 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);
4269                         }
4270                         else
4271                                 if (ainfo->storage == ArgInSplitRegStack) {
4272 #ifdef SPARCV9
4273                                         g_assert_not_reached ();
4274 #endif
4275                                         if (stack_offset != inst->inst_offset) {
4276                                                 /* stack_offset is not dword aligned, so we need to make a copy */
4277                                                 sparc_st_imm (code, sparc_i5, inst->inst_basereg, inst->inst_offset);
4278                                                 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
4279                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
4280                                         }
4281                                 }
4282                         else
4283                                 if (ainfo->storage == ArgOnStackPair) {
4284 #ifdef SPARCV9
4285                                         g_assert_not_reached ();
4286 #endif
4287                                         if (stack_offset != inst->inst_offset) {
4288                                                 /* stack_offset is not dword aligned, so we need to make a copy */
4289                                                 sparc_ld_imm (code, sparc_fp, stack_offset, sparc_o7);
4290                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset);
4291                                                 sparc_ld_imm (code, sparc_fp, stack_offset + 4, sparc_o7);
4292                                                 sparc_st_imm (code, sparc_o7, inst->inst_basereg, inst->inst_offset + 4);
4293                                         }
4294                                 }
4295                         else
4296                                 g_assert_not_reached ();
4297                 }
4298                 else
4299                         if ((ainfo->storage == ArgInIReg) && (inst->opcode != OP_REGVAR)) {
4300                                 /* Argument in register, but need to be saved to stack */
4301                                 if (!sparc_is_imm13 (stack_offset))
4302                                         NOT_IMPLEMENTED;
4303                                 if ((stack_offset - ARGS_OFFSET) & 0x1)
4304                                         sparc_stb_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4305                                 else
4306                                         if ((stack_offset - ARGS_OFFSET) & 0x2)
4307                                                 sparc_sth_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4308                                 else
4309                                         if ((stack_offset - ARGS_OFFSET) & 0x4)
4310                                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);                           
4311                                         else {
4312                                                 if (v64)
4313                                                         sparc_stx_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4314                                                 else
4315                                                         sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, stack_offset);
4316                                         }
4317                         }
4318                 else
4319                         if ((ainfo->storage == ArgInIRegPair) && (inst->opcode != OP_REGVAR)) {
4320 #ifdef SPARCV9
4321                                 NOT_IMPLEMENTED;
4322 #endif
4323                                 /* Argument in regpair, but need to be saved to stack */
4324                                 if (!sparc_is_imm13 (inst->inst_offset + 4))
4325                                         NOT_IMPLEMENTED;
4326                                 sparc_st_imm (code, sparc_i0 + ainfo->reg, inst->inst_basereg, inst->inst_offset);
4327                                 sparc_st_imm (code, sparc_i0 + ainfo->reg + 1, inst->inst_basereg, inst->inst_offset + 4);                              
4328                         }
4329                 else if ((ainfo->storage == ArgInFloatReg) && (inst->opcode != OP_REGVAR)) {
4330                                 if (!sparc_is_imm13 (stack_offset))
4331                                         NOT_IMPLEMENTED;
4332                                 sparc_stf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4333                                 }
4334                         else if ((ainfo->storage == ArgInDoubleReg) && (inst->opcode != OP_REGVAR)) {
4335                                 /* The offset is guaranteed to be aligned by the ABI rules */
4336                                 sparc_stdf_imm (code, ainfo->reg, inst->inst_basereg, inst->inst_offset);
4337                         }
4338                                         
4339                 if ((ainfo->storage == ArgInFloatReg) && (inst->opcode == OP_REGVAR)) {
4340                         /* Need to move into the a double precision register */
4341                         sparc_fstod (code, ainfo->reg, ainfo->reg - 1);
4342                 }
4343
4344                 if ((ainfo->storage == ArgInSplitRegStack) || (ainfo->storage == ArgOnStack))
4345                         if (inst->opcode == OP_REGVAR)
4346                                 /* FIXME: Load the argument into memory */
4347                                 NOT_IMPLEMENTED;
4348         }
4349
4350         g_free (cinfo);
4351
4352         if (cfg->method->save_lmf) {
4353                 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4354
4355                 /* Save ip */
4356                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
4357                 sparc_set_template (code, sparc_o7);
4358                 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ip));
4359                 /* Save sp */
4360                 sparc_sti_imm (code, sparc_sp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, sp));
4361                 /* Save fp */
4362                 sparc_sti_imm (code, sparc_fp, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, ebp));
4363                 /* Save method */
4364                 /* FIXME: add a relocation for this */
4365                 sparc_set (code, cfg->method, sparc_o7);
4366                 sparc_sti_imm (code, sparc_o7, sparc_fp, lmf_offset + G_STRUCT_OFFSET (MonoLMF, method));
4367
4368                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
4369                                                          (gpointer)"mono_arch_get_lmf_addr");           
4370                 EMIT_CALL ();
4371
4372                 code = (guint32*)mono_sparc_emit_save_lmf (code, lmf_offset);
4373         }
4374
4375         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4376                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
4377
4378         cfg->code_len = (guint8*)code - cfg->native_code;
4379
4380         g_assert (cfg->code_len <= cfg->code_size);
4381
4382         return (guint8*)code;
4383 }
4384
4385 void
4386 mono_arch_emit_epilog (MonoCompile *cfg)
4387 {
4388         MonoMethod *method = cfg->method;
4389         guint32 *code;
4390         int can_fold = 0;
4391         int max_epilog_size = 16 + 20 * 4;
4392         
4393         if (cfg->method->save_lmf)
4394                 max_epilog_size += 128;
4395         
4396         if (mono_jit_trace_calls != NULL)
4397                 max_epilog_size += 50;
4398
4399         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
4400                 max_epilog_size += 50;
4401
4402         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
4403                 cfg->code_size *= 2;
4404                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4405                 mono_jit_stats.code_reallocs++;
4406         }
4407
4408         code = (guint32*)(cfg->native_code + cfg->code_len);
4409
4410         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
4411                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
4412
4413         if (cfg->method->save_lmf) {
4414                 gint32 lmf_offset = STACK_BIAS - cfg->arch.lmf_offset;
4415
4416                 code = mono_sparc_emit_restore_lmf (code, lmf_offset);
4417         }
4418
4419         /* 
4420          * The V8 ABI requires that calls to functions which return a structure
4421          * return to %i7+12
4422          */
4423         if (!v64 && mono_method_signature (cfg->method)->pinvoke && MONO_TYPE_ISSTRUCT(mono_method_signature (cfg->method)->ret))
4424                 sparc_jmpl_imm (code, sparc_i7, 12, sparc_g0);
4425         else
4426                 sparc_ret (code);
4427
4428         /* Only fold last instruction into the restore if the exit block has an in count of 1
4429            and the previous block hasn't been optimized away since it may have an in count > 1 */
4430         if (cfg->bb_exit->in_count == 1 && cfg->bb_exit->in_bb[0]->native_offset != cfg->bb_exit->native_offset)
4431                 can_fold = 1;
4432
4433         if (cfg->new_ir) {
4434                 /* 
4435                  * FIXME: The last instruction might have a branch pointing into it like in 
4436                  * int_ceq sparc_i0 <-
4437                  */
4438                 can_fold = 0;
4439         }
4440
4441         /* Try folding last instruction into the restore */
4442         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)) {
4443                 /* or reg, imm, %i0 */
4444                 int reg = sparc_inst_rs1 (code [-2]);
4445                 int imm = (((gint32)(sparc_inst_imm13 (code [-2]))) << 19) >> 19;
4446                 code [-2] = code [-1];
4447                 code --;
4448                 sparc_restore_imm (code, reg, imm, sparc_o0);
4449         }
4450         else
4451         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)) {
4452                 /* or reg, reg, %i0 */
4453                 int reg1 = sparc_inst_rs1 (code [-2]);
4454                 int reg2 = sparc_inst_rs2 (code [-2]);
4455                 code [-2] = code [-1];
4456                 code --;
4457                 sparc_restore (code, reg1, reg2, sparc_o0);
4458         }
4459         else
4460                 sparc_restore_imm (code, sparc_g0, 0, sparc_g0);
4461
4462         cfg->code_len = (guint8*)code - cfg->native_code;
4463
4464         g_assert (cfg->code_len < cfg->code_size);
4465
4466 }
4467
4468 void
4469 mono_arch_emit_exceptions (MonoCompile *cfg)
4470 {
4471         MonoJumpInfo *patch_info;
4472         guint32 *code;
4473         int nthrows = 0, i;
4474         int exc_count = 0;
4475         guint32 code_size;
4476         MonoClass *exc_classes [16];
4477         guint8 *exc_throw_start [16], *exc_throw_end [16];
4478
4479         /* Compute needed space */
4480         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4481                 if (patch_info->type == MONO_PATCH_INFO_EXC)
4482                         exc_count++;
4483         }
4484      
4485         /* 
4486          * make sure we have enough space for exceptions
4487          */
4488 #ifdef SPARCV9
4489         code_size = exc_count * (20 * 4);
4490 #else
4491         code_size = exc_count * 24;
4492 #endif
4493
4494         while (cfg->code_len + code_size > (cfg->code_size - 16)) {
4495                 cfg->code_size *= 2;
4496                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
4497                 mono_jit_stats.code_reallocs++;
4498         }
4499
4500         code = (guint32*)(cfg->native_code + cfg->code_len);
4501
4502         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
4503                 switch (patch_info->type) {
4504                 case MONO_PATCH_INFO_EXC: {
4505                         MonoClass *exc_class;
4506                         guint32 *buf, *buf2;
4507                         guint32 throw_ip, type_idx;
4508                         gint32 disp;
4509
4510                         sparc_patch ((guint32*)(cfg->native_code + patch_info->ip.i), code);
4511
4512                         exc_class = mono_class_from_name (mono_defaults.corlib, "System", patch_info->data.name);
4513                         g_assert (exc_class);
4514                         type_idx = exc_class->type_token - MONO_TOKEN_TYPE_DEF;
4515                         throw_ip = patch_info->ip.i;
4516
4517                         /* Find a throw sequence for the same exception class */
4518                         for (i = 0; i < nthrows; ++i)
4519                                 if (exc_classes [i] == exc_class)
4520                                         break;
4521
4522                         if (i < nthrows) {
4523                                 guint32 throw_offset = (((guint8*)exc_throw_end [i] - cfg->native_code) - throw_ip) >> 2;
4524                                 if (!sparc_is_imm13 (throw_offset))
4525                                         sparc_set32 (code, throw_offset, sparc_o1);
4526
4527                                 disp = (exc_throw_start [i] - (guint8*)code) >> 2;
4528                                 g_assert (sparc_is_imm22 (disp));
4529                                 sparc_branch (code, 0, sparc_ba, disp);
4530                                 if (sparc_is_imm13 (throw_offset))
4531                                         sparc_set32 (code, throw_offset, sparc_o1);
4532                                 else
4533                                         sparc_nop (code);
4534                                 patch_info->type = MONO_PATCH_INFO_NONE;
4535                         }
4536                         else {
4537                                 /* Emit the template for setting o1 */
4538                                 buf = code;
4539                                 if (sparc_is_imm13 (((((guint8*)code - cfg->native_code) - throw_ip) >> 2) - 8))
4540                                         /* Can use a short form */
4541                                         sparc_nop (code);
4542                                 else
4543                                         sparc_set_template (code, sparc_o1);
4544                                 buf2 = code;
4545
4546                                 if (nthrows < 16) {
4547                                         exc_classes [nthrows] = exc_class;
4548                                         exc_throw_start [nthrows] = (guint8*)code;
4549                                 }
4550
4551                                 /*
4552                                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, mono_sparc_break);
4553                                 EMIT_CALL();
4554                                 */
4555
4556                                 /* first arg = type token */
4557                                 /* Pass the type index to reduce the size of the sparc_set */
4558                                 if (!sparc_is_imm13 (type_idx))
4559                                         sparc_set32 (code, type_idx, sparc_o0);
4560
4561                                 /* second arg = offset between the throw ip and the current ip */
4562                                 /* On sparc, the saved ip points to the call instruction */
4563                                 disp = (((guint8*)code - cfg->native_code) - throw_ip) >> 2;
4564                                 sparc_set32 (buf, disp, sparc_o1);
4565                                 while (buf < buf2)
4566                                         sparc_nop (buf);
4567
4568                                 if (nthrows < 16) {
4569                                         exc_throw_end [nthrows] = (guint8*)code;
4570                                         nthrows ++;
4571                                 }
4572
4573                                 patch_info->data.name = "mono_arch_throw_corlib_exception";
4574                                 patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
4575                                 patch_info->ip.i = (guint8*)code - cfg->native_code;
4576
4577                                 EMIT_CALL ();
4578
4579                                 if (sparc_is_imm13 (type_idx)) {
4580                                         /* Put it into the delay slot */
4581                                         code --;
4582                                         buf = code;
4583                                         sparc_set32 (code, type_idx, sparc_o0);
4584                                         g_assert (code - buf == 1);
4585                                 }
4586                         }
4587                         break;
4588                 }
4589                 default:
4590                         /* do nothing */
4591                         break;
4592                 }
4593         }
4594
4595         cfg->code_len = (guint8*)code - cfg->native_code;
4596
4597         g_assert (cfg->code_len < cfg->code_size);
4598
4599 }
4600
4601 gboolean lmf_addr_key_inited = FALSE;
4602
4603 #ifdef MONO_SPARC_THR_TLS
4604 thread_key_t lmf_addr_key;
4605 #else
4606 pthread_key_t lmf_addr_key;
4607 #endif
4608
4609 gpointer
4610 mono_arch_get_lmf_addr (void)
4611 {
4612         /* This is perf critical so we bypass the IO layer */
4613         /* The thr_... functions seem to be somewhat faster */
4614 #ifdef MONO_SPARC_THR_TLS
4615         gpointer res;
4616         thr_getspecific (lmf_addr_key, &res);
4617         return res;
4618 #else
4619         return pthread_getspecific (lmf_addr_key);
4620 #endif
4621 }
4622
4623 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
4624
4625 /*
4626  * There seems to be no way to determine stack boundaries under solaris,
4627  * so it's not possible to determine whenever a SIGSEGV is caused by stack
4628  * overflow or not.
4629  */
4630 #error "--with-sigaltstack=yes not supported on solaris"
4631
4632 #endif
4633
4634 void
4635 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
4636 {
4637         if (!lmf_addr_key_inited) {
4638                 int res;
4639
4640                 lmf_addr_key_inited = TRUE;
4641
4642 #ifdef MONO_SPARC_THR_TLS
4643                 res = thr_keycreate (&lmf_addr_key, NULL);
4644 #else
4645                 res = pthread_key_create (&lmf_addr_key, NULL);
4646 #endif
4647                 g_assert (res == 0);
4648
4649         }
4650
4651 #ifdef MONO_SPARC_THR_TLS
4652         thr_setspecific (lmf_addr_key, &tls->lmf);
4653 #else
4654         pthread_setspecific (lmf_addr_key, &tls->lmf);
4655 #endif
4656 }
4657
4658 void
4659 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
4660 {
4661 }
4662
4663 void
4664 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *call, int this_reg, int this_type, int vt_reg)
4665 {
4666         int this_out_reg = sparc_o0;
4667
4668         if (vt_reg != -1) {
4669 #ifdef SPARCV9
4670                 MonoInst *ins;
4671                 MONO_INST_NEW (cfg, ins, OP_MOVE);
4672                 ins->sreg1 = vt_reg;
4673                 ins->dreg = mono_regstate_next_int (cfg->rs);
4674                 mono_bblock_add_inst (cfg->cbb, ins);
4675
4676                 mono_call_inst_add_outarg_reg (cfg, call, ins->dreg, sparc_o0, FALSE);
4677
4678                 this_out_reg = sparc_o1;
4679 #else
4680                 /* Set the 'struct/union return pointer' location on the stack */
4681                 MONO_EMIT_NEW_STORE_MEMBASE (cfg, OP_STOREI4_MEMBASE_REG, sparc_sp, 64, vt_reg);
4682 #endif
4683         }
4684
4685         /* add the this argument */
4686         if (this_reg != -1) {
4687                 MonoInst *this;
4688                 MONO_INST_NEW (cfg, this, OP_MOVE);
4689                 this->type = this_type;
4690                 this->sreg1 = this_reg;
4691                 this->dreg = mono_regstate_next_int (cfg->rs);
4692                 mono_bblock_add_inst (cfg->cbb, this);
4693
4694                 mono_call_inst_add_outarg_reg (cfg, call, this->dreg, this_out_reg, FALSE);
4695         }
4696 }
4697
4698
4699 MonoInst*
4700 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4701 {
4702         MonoInst *ins = NULL;
4703
4704         return ins;
4705 }
4706
4707 MonoInst*
4708 mono_arch_emit_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
4709 {
4710         MonoInst *ins = NULL;
4711
4712         return ins;
4713 }
4714
4715 /*
4716  * mono_arch_get_argument_info:
4717  * @csig:  a method signature
4718  * @param_count: the number of parameters to consider
4719  * @arg_info: an array to store the result infos
4720  *
4721  * Gathers information on parameters such as size, alignment and
4722  * padding. arg_info should be large enought to hold param_count + 1 entries. 
4723  *
4724  * Returns the size of the activation frame.
4725  */
4726 int
4727 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
4728 {
4729         int k, align;
4730         CallInfo *cinfo;
4731         ArgInfo *ainfo;
4732
4733         cinfo = get_call_info (NULL, csig, FALSE);
4734
4735         if (csig->hasthis) {
4736                 ainfo = &cinfo->args [0];
4737                 arg_info [0].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4738         }
4739
4740         for (k = 0; k < param_count; k++) {
4741                 ainfo = &cinfo->args [k + csig->hasthis];
4742
4743                 arg_info [k + 1].offset = ARGS_OFFSET - MONO_SPARC_STACK_BIAS + ainfo->offset;
4744                 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
4745         }
4746
4747         g_free (cinfo);
4748
4749         return 0;
4750 }
4751
4752 gboolean
4753 mono_arch_print_tree (MonoInst *tree, int arity)
4754 {
4755         return 0;
4756 }
4757
4758 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
4759 {
4760         return NULL;
4761 }
4762
4763 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
4764 {
4765         return NULL;
4766 }
4767
4768 gpointer
4769 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
4770 {
4771         /* FIXME: implement */
4772         g_assert_not_reached ();
4773 }