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