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