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