merge r98600
[mono.git] / mono / mini / mini-hppa.c
1 /*
2  * mini-hppa.c: HPPA backend for the Mono code generator
3  *
4  * Copyright (c) 2007 Randolph Chung
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  */
25 #include "mini.h"
26 #include <string.h>
27 #include <pthread.h>
28 #include <unistd.h>
29
30 #include <unistd.h>
31 #include <sys/mman.h>
32
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/metadata/tokentype.h>
36 #include <mono/utils/mono-math.h>
37
38 #include "mini-hppa.h"
39 #include "inssel.h"
40 #include "trace.h"
41 #include "cpu-hppa.h"
42
43 #define ALIGN_TO(val,align) (((val) + ((align) - 1)) & ~((align) - 1))
44 #define SIGNAL_STACK_SIZE (64 * 1024)
45
46 #define DEBUG(a) // a
47 #define DEBUG_FUNC_ENTER() // printf("Entering %s\n", __FUNCTION__)
48 #define DEBUG_FUNC_EXIT() // printf("Exiting %s\n", __FUNCTION__)
49
50 static const guchar 
51 branch_b0_table [] = {
52         TRUE, /* OP_HPPA_BEQ */
53         FALSE, /* OP_HPPA_BGE */
54         FALSE, /* OP_HPPA_BGT */
55         TRUE, /* OP_HPPA_BLE */
56         TRUE, /* OP_HPPA_BLT */
57         FALSE, /* OP_HPPA_BNE */
58         FALSE, /* OP_HPPA_BGE_UN */
59         FALSE, /* OP_HPPA_BGT_UN */
60         TRUE, /* OP_HPPA_BLE_UN */
61         TRUE, /* OP_HPPA_BLT_UN */
62 };
63
64 static const guchar 
65 branch_b1_table [] = {
66         HPPA_CMP_COND_EQ, /* OP_HPPA_BEQ */
67         HPPA_CMP_COND_SLT, /* OP_HPPA_BGE */
68         HPPA_CMP_COND_SLE, /* OP_HPPA_BGT */
69         HPPA_CMP_COND_SLE, /* OP_HPPA_BLE */
70         HPPA_CMP_COND_SLT, /* OP_HPPA_BLT */
71         HPPA_CMP_COND_EQ, /* OP_HPPA_BNE_UN */
72         HPPA_CMP_COND_ULT, /* OP_HPPA_BGE_UN */
73         HPPA_CMP_COND_ULE, /* OP_HPPA_BGT_UN */
74         HPPA_CMP_COND_ULE, /* OP_HPPA_BLE_UN */
75         HPPA_CMP_COND_ULT, /* OP_HPPA_BLT_UN */
76 };
77
78 /* Note that these are inverted from the OP_xxx, because we nullify
79  * the branch if the condition is met
80  */
81 static const guchar
82 float_branch_table [] = {
83         26, /* OP_FBEQ */
84         11, /* OP_FBGE */
85         15, /* OP_FBGT */
86         19, /* OP_FBLE */
87         23, /* OP_FBLT */
88         4, /* OP_FBNE_UN */
89         8, /* OP_FBGE_UN */
90         13, /* OP_FBGT_UN */
91         17, /* OP_FBLE_UN */
92         20, /* OP_FBLT_UN */
93 };
94
95 static const guchar
96 float_ceq_table [] = {
97         26, /* OP_FCEQ */
98         15, /* OP_FCGT */
99         13, /* OP_FCGT_UN */
100         23, /* OP_FCLT */
101         21, /* OP_FCLT_UN */
102 };
103
104 /*
105  * Branches have short (14 or 17 bit) targets on HPPA. To make longer jumps,
106  * we will need to rely on stubs - basically we create stub structures in
107  * the epilogue that uses a long branch to the destination, and any short
108  * jumps inside a method that cannot reach the destination directly will
109  * branch first to the stub.
110  */
111 typedef struct MonoOvfJump {
112         union {
113                 MonoBasicBlock *bb;
114                 const char *exception;
115         } data;
116         guint32 ip_offset;
117 } MonoOvfJump;
118
119 /* Create a literal 0.0 double for FNEG */
120 double hppa_zero = 0;
121
122 const char*
123 mono_arch_regname (int reg) 
124 {
125         static const char * rnames[] = {
126                 "hppa_r0", "hppa_r1", "hppa_rp", "hppa_r3", "hppa_r4",
127                 "hppa_r5", "hppa_r6", "hppa_r7", "hppa_r8", "hppa_r9",
128                 "hppa_r10", "hppa_r11", "hppa_r12", "hppa_r13", "hppa_r14",
129                 "hppa_r15", "hppa_r16", "hppa_r17", "hppa_r18", "hppa_r19",
130                 "hppa_r20", "hppa_r21", "hppa_r22", "hppa_r23", "hppa_r24",
131                 "hppa_r25", "hppa_r26", "hppa_r27", "hppa_r28", "hppa_r29",
132                 "hppa_sp", "hppa_r31"
133         };
134         if (reg >= 0 && reg < MONO_MAX_IREGS)
135                 return rnames [reg];
136         return "unknown";
137 }
138
139 const char*
140 mono_arch_fregname (int reg) 
141 {
142         static const char *rnames [] = {
143                 "hppa_fr0", "hppa_fr1", "hppa_fr2", "hppa_fr3", "hppa_fr4", 
144                 "hppa_fr5", "hppa_fr6", "hppa_fr7", "hppa_fr8", "hppa_fr9",
145                 "hppa_fr10", "hppa_fr11", "hppa_fr12", "hppa_fr13", "hppa_fr14", 
146                 "hppa_fr15", "hppa_fr16", "hppa_fr17", "hppa_fr18", "hppa_fr19",
147                 "hppa_fr20", "hppa_fr21", "hppa_fr22", "hppa_fr23", "hppa_fr24", 
148                 "hppa_fr25", "hppa_fr26", "hppa_fr27", "hppa_fr28", "hppa_fr29",
149                 "hppa_fr30", "hppa_fr31",
150         };
151
152         if (reg >= 0 && reg < MONO_MAX_FREGS)
153                 return rnames [reg];
154         else
155                 return "unknown";
156 }
157
158 /*
159  * Initialize the cpu to execute managed code.
160  */
161 void
162 mono_arch_cpu_init (void)
163 {
164         guint32 dummy;
165         mono_arch_cpu_optimizazions(&dummy);
166 }
167
168 /*
169  * Initialize architecture specific code.
170  */
171 void
172 mono_arch_init (void)
173 {
174 }
175
176 /*
177  * Cleanup architecture specific code.
178  */
179 void
180 mono_arch_cleanup (void)
181 {
182 }
183
184 /*
185  * This function returns the optimizations supported on this cpu.
186  */
187 guint32
188 mono_arch_cpu_optimizazions (guint32 *exclude_mask)
189 {
190         guint32 opts = 0;
191         *exclude_mask = 0;
192         return opts;
193 }
194
195 void
196 mono_arch_flush_icache (guint8 *code, gint size)
197 {
198         guint8* p = (guint8*)((guint32)code & ~(0x3f));
199         guint8* end = (guint8*)((guint32)code + size);
200         while (p < end) {
201                 __asm__ __volatile__ ("fdc %%r0(%%sr3, %0)\n"
202                         "sync\n"
203                         "fic %%r0(%%sr3, %0)\n"
204                         "sync\n"
205                         : : "r"(p));
206                 p += 32; /* can be 64 on pa20 cpus */
207         }
208 }
209
210 void
211 mono_arch_flush_register_windows (void)
212 {
213         /* No register windows on hppa */
214 }
215
216 typedef enum {
217         ArgInIReg,
218         ArgInIRegPair,
219         ArgInFReg,
220         ArgInDReg,
221         ArgOnStack,
222 } ArgStorage;
223
224 typedef struct {
225         gint16 offset;
226         gint16 size;
227         guint8 type;
228         gint8  reg;
229         ArgStorage storage;
230 } ArgInfo;
231
232 typedef struct {
233         int nargs;
234         guint32 stack_usage;
235         int struct_return;
236         ArgInfo ret;
237         ArgInfo sig_cookie;
238         ArgInfo args [1];
239 } CallInfo;
240
241 #define PARAM_REGS 4
242 #define ARGS_OFFSET 36
243
244 static void
245 add_parameter (CallInfo *cinfo, ArgInfo *ainfo, MonoType *type)
246 {
247         int is_fp = (type->type == MONO_TYPE_R4 || type->type == MONO_TYPE_R8);
248         int ofs, align;
249
250         DEBUG_FUNC_ENTER ();
251         ainfo->reg = -1;
252         ainfo->size = mono_type_size (type, &align);
253         ainfo->type = type->type;
254
255         if (ainfo->size <= 4) {
256                 cinfo->stack_usage += 4;
257                 ainfo->offset = cinfo->stack_usage - (4 - ainfo->size);
258         }
259         else if (ainfo->size <= 8)
260         {
261                 cinfo->stack_usage += 8;
262                 cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, 8);
263                 ainfo->offset = cinfo->stack_usage - (8 - ainfo->size);
264         }
265         else
266         {
267                 cinfo->stack_usage += ainfo->size;
268                 cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, align);
269                 ainfo->offset = cinfo->stack_usage;
270         }
271
272         ofs = (ALIGN_TO (ainfo->offset, 4) - ARGS_OFFSET) / 4;
273         if (ofs < PARAM_REGS) {
274                 if (!is_fp) {
275                         if (ainfo->size <= 4)
276                                 ainfo->storage = ArgInIReg;
277                         else
278                                 ainfo->storage = ArgInIRegPair;
279                         ainfo->reg = hppa_r26 - ofs;
280                 } else if (type->type == MONO_TYPE_R4) {
281                         ainfo->storage = ArgInFReg;
282                         ainfo->reg = hppa_fr4 + ofs;
283                 } else { /* type->type == MONO_TYPE_R8 */
284                         ainfo->storage = ArgInDReg;
285                         ainfo->reg = hppa_fr4 + ofs;
286                 }
287         }
288         else {
289                 /* frame pointer based offset */
290                 ainfo->reg = hppa_r3;
291                 ainfo->storage = ArgOnStack;
292         }
293
294         /* All offsets are negative relative to the frame pointer */
295         ainfo->offset = -ainfo->offset;
296
297         DEBUG_FUNC_EXIT ();
298 }
299
300 static void 
301 analyze_return (CallInfo *cinfo, MonoMethodSignature *sig)
302 {
303         MonoType *type;
304         int align;
305         int size;
306
307         type = sig->ret;
308         size = mono_type_size (type, &align);
309
310         /* ref: mono_type_to_stind */
311         cinfo->ret.type = type->type;
312         if (type->byref) {
313                 cinfo->ret.storage = ArgInIReg;
314                 cinfo->ret.reg = hppa_r28;
315         } else {
316 handle_enum:
317                 switch (type->type) {
318                 case MONO_TYPE_VOID:
319                         break;
320                 case MONO_TYPE_BOOLEAN:
321                 case MONO_TYPE_I1:
322                 case MONO_TYPE_U1:
323                 case MONO_TYPE_I2:
324                 case MONO_TYPE_U2:
325                 case MONO_TYPE_CHAR:
326                 case MONO_TYPE_I4:
327                 case MONO_TYPE_U4:
328                 case MONO_TYPE_I:
329                 case MONO_TYPE_U:
330                 case MONO_TYPE_PTR:
331                 case MONO_TYPE_FNPTR:
332                 case MONO_TYPE_CLASS:
333                 case MONO_TYPE_STRING:
334                 case MONO_TYPE_OBJECT:
335                 case MONO_TYPE_SZARRAY:
336                 case MONO_TYPE_ARRAY:
337                         cinfo->ret.storage = ArgInIReg;
338                         cinfo->ret.reg = hppa_r28;
339                         break;
340                 case MONO_TYPE_U8:
341                 case MONO_TYPE_I8:
342                         cinfo->ret.storage = ArgInIRegPair;
343                         cinfo->ret.reg = hppa_r28;
344                         break;
345                 case MONO_TYPE_R4:
346                         cinfo->ret.storage = ArgInFReg;
347                         cinfo->ret.reg = hppa_fr4;
348                         break;
349                 case MONO_TYPE_R8:
350                         cinfo->ret.storage = ArgInDReg;
351                         cinfo->ret.reg = hppa_fr4;
352                         break;
353                 case MONO_TYPE_GENERICINST:
354                         type = &type->data.generic_class->container_class->byval_arg;
355                         goto handle_enum;
356                         
357                 case MONO_TYPE_VALUETYPE:
358                         if (type->data.klass->enumtype) {
359                                 type = type->data.klass->enum_basetype;
360                                 goto handle_enum;
361                         }
362                         /* Fall through */
363                 case MONO_TYPE_TYPEDBYREF:
364                         cinfo->struct_return = 1;
365                         /* cinfo->ret.storage tells us how the ABI expects 
366                          * the parameter to be returned
367                          */
368                         if (size <= 4) {
369                                 cinfo->ret.storage = ArgInIReg;
370                                 cinfo->ret.reg = hppa_r28;
371                         } else if (size <= 8) {
372                                 cinfo->ret.storage = ArgInIRegPair;
373                                 cinfo->ret.reg = hppa_r28;
374                         } else {
375                                 cinfo->ret.storage = ArgOnStack;
376                                 cinfo->ret.reg = hppa_sp;
377                         }
378
379                         /* We always allocate stack space for this because the
380                          * arch-indep code expects us to
381                          */
382                         cinfo->stack_usage += size;
383                         cinfo->stack_usage = ALIGN_TO (cinfo->stack_usage, align);
384                         cinfo->ret.offset = -cinfo->stack_usage;
385                         break;
386
387                 default:
388                         g_error ("Can't handle as return value 0x%x", sig->ret->type);
389                 }
390         }
391 }
392
393 /*
394  * get_call_info:
395  *
396  *  Obtain information about a call according to the calling convention.
397  */
398 static CallInfo*
399 get_call_info (MonoMethodSignature *sig, gboolean is_pinvoke)
400 {
401         guint32 i;
402         int n = sig->hasthis + sig->param_count;
403         CallInfo *cinfo;
404         MonoType *type;
405         MonoType ptrtype;
406         int dummy;
407
408         ptrtype.type = MONO_TYPE_PTR;
409
410         DEBUG_FUNC_ENTER();
411         cinfo = g_malloc0 (sizeof (CallInfo) + (sizeof (ArgInfo) * n));
412
413         /* The area below ARGS_OFFSET is the linkage area... */
414         cinfo->stack_usage = ARGS_OFFSET - 4;
415         /* -4, because the first argument will allocate the area it needs */
416
417         /* this */
418         if (sig->hasthis) {
419                 add_parameter (cinfo, cinfo->args + 0, &ptrtype);
420                 DEBUG (printf ("param <this>: assigned to reg %s offset %d\n", mono_arch_regname (cinfo->args[0].reg), cinfo->args[0].offset));
421         }
422
423         /* TODO: What to do with varargs? */
424
425         for (i = 0; i < sig->param_count; ++i) {
426                 ArgInfo *ainfo = &cinfo->args [sig->hasthis + i];
427                 if (sig->params [i]->byref)
428                         type = &ptrtype;
429                 else
430                         type = mono_type_get_underlying_type (sig->params [i]);
431                 add_parameter (cinfo, ainfo, type);
432
433                 DEBUG (printf ("param %d: type %d size %d assigned to reg %s offset %d\n", i, type->type, mono_type_size (type, &dummy), mono_arch_regname (ainfo->reg), ainfo->offset));
434         }
435
436         analyze_return (cinfo, sig);
437
438         DEBUG_FUNC_EXIT();
439         return cinfo;
440 }
441
442 GList *
443 mono_arch_get_allocatable_int_vars (MonoCompile *cfg)
444 {
445         GList *vars = NULL;
446         int i;
447
448         DEBUG_FUNC_ENTER();
449         for (i = 0; i < cfg->num_varinfo; i++) {
450                 MonoInst *ins = cfg->varinfo [i];
451                 MonoMethodVar *vmv = MONO_VARINFO (cfg, i);
452
453                 /* unused vars */
454                 if (vmv->range.first_use.abs_pos >= vmv->range.last_use.abs_pos)
455                         continue;
456
457                 if ((ins->flags & (MONO_INST_IS_DEAD|MONO_INST_VOLATILE|MONO_INST_INDIRECT)) || 
458                     (ins->opcode != OP_LOCAL && ins->opcode != OP_ARG))
459                         continue;
460
461                 if (mono_is_regsize_var (ins->inst_vtype)) {
462                         g_assert (MONO_VARINFO (cfg, i)->reg == -1);
463                         g_assert (i == vmv->idx);
464                         vars = mono_varlist_insert_sorted (cfg, vars, vmv, FALSE);
465                 }
466         }
467         DEBUG_FUNC_EXIT();
468
469         return vars;
470 }
471
472 GList *
473 mono_arch_get_global_int_regs (MonoCompile *cfg)
474 {
475         GList *regs = NULL;
476         int i;
477
478         /* r3 is sometimes used as our frame pointer, so don't allocate it
479          * r19 is the GOT pointer, don't allocate it either
480          */
481
482         DEBUG_FUNC_ENTER();
483         for (i = 4; i <= 18; i++)
484                 regs = g_list_prepend (regs, GUINT_TO_POINTER (i));
485         DEBUG_FUNC_EXIT();
486
487         return regs;
488 }
489
490 /*
491  * mono_arch_regalloc_cost:
492  *
493  *  Return the cost, in number of memory references, of the action of 
494  * allocating the variable VMV into a register during global register
495  * allocation.
496  */
497 guint32
498 mono_arch_regalloc_cost (MonoCompile *cfg, MonoMethodVar *vmv)
499 {
500         /* FIXME */
501         return 0;
502 }
503
504 /*
505  * Set var information according to the calling convention.
506  * The locals var stuff should most likely be split in another method.
507  *
508  * updates m->stack_offset based on the amount of stack space needed for
509  * local vars
510  */
511 void
512 mono_arch_allocate_vars (MonoCompile *m)
513 {
514         MonoMethodSignature *sig;
515         MonoMethodHeader *header;
516         MonoInst *inst;
517         int i, offset, size, align, curinst;
518         guint32 stack_ptr;
519         guint rettype;
520         CallInfo *cinfo;
521
522         DEBUG_FUNC_ENTER();
523         m->flags |= MONO_CFG_HAS_SPILLUP;
524
525         header = mono_method_get_header (m->method);
526
527         sig = mono_method_signature (m->method);
528         DEBUG (printf ("Allocating locals - incoming params:\n"));
529         cinfo = get_call_info (sig, FALSE);
530
531         /*
532          * We use the ABI calling conventions for managed code as well.
533          */
534         if (m->flags & MONO_CFG_HAS_ALLOCA) {
535                 stack_ptr = hppa_r4;
536                 m->used_int_regs |= 1 << hppa_r4;
537         } else {
538                 stack_ptr = hppa_sp;
539         }
540
541         /* Before this function is called, we would have looked at all 
542          * calls from this method and figured out how much space is needed
543          * for the param area.
544          *
545          * Locals are allocated backwards, right before the param area 
546          */
547         /* TODO: in some cases we don't need the frame pointer... */
548         m->frame_reg = hppa_r3;
549         offset = m->param_area;
550
551         /* Return values can be passed back either in four ways:
552          * r28 is used for data <= 4 bytes (32-bit ABI)
553          * r28/r29 are used for data >4 && <= 8 bytes
554          * fr4 is used for floating point data
555          * data larger than 8 bytes is returned on the stack pointed to 
556          *      by r28
557          *
558          * This code needs to be in sync with how CEE_RET is handled
559          * in mono_method_to_ir (). In some cases when we return small
560          * structs, the ABI specifies that they should be returned in
561          * registers, but the code in mono_method_to_ir () always emits
562          * a memcpy for valuetype returns, so we need to make sure we
563          * allocate space on the stack for this copy.
564          */
565         if (cinfo->struct_return) {
566                 /* this is used to stash the incoming r28 pointer */
567                 offset += sizeof (gpointer);
568                 m->ret->opcode = OP_REGOFFSET;
569                 m->ret->inst_basereg = stack_ptr;
570                 m->ret->inst_offset = -offset;
571         } else if (sig->ret->type != MONO_TYPE_VOID) {
572                 m->ret->opcode = OP_REGVAR;
573                 m->ret->inst_c0 = cinfo->ret.reg;
574         }
575
576         curinst = m->locals_start;
577         for (i = curinst; i < m->num_varinfo; ++i) {
578                 inst = m->varinfo [i];
579
580                 if (inst->opcode == OP_REGVAR) {
581                         DEBUG (printf ("allocating local %d to %s\n", i, mono_arch_regname (inst->dreg)));
582                         continue;
583                 }
584
585                 if (inst->flags & MONO_INST_IS_DEAD)
586                         continue;
587
588                 /* inst->backend.is_pinvoke indicates native sized value types, this is used by the
589                 * pinvoke wrappers when they call functions returning structure */
590                 if (inst->backend.is_pinvoke && MONO_TYPE_ISSTRUCT (inst->inst_vtype) && inst->inst_vtype->type != MONO_TYPE_TYPEDBYREF)
591                         size = mono_class_native_size (inst->inst_vtype->data.klass, &align);
592                 else
593                         size = mini_type_stack_size (cfg->generic_sharing_context, inst->inst_vtype, &align);
594
595                 /* 
596                  * This is needed since structures containing doubles must be doubleword 
597          * aligned.
598                  * FIXME: Do this only if needed.
599                  */
600                 if (MONO_TYPE_ISSTRUCT (inst->inst_vtype))
601                         align = 8;
602
603                 /*
604                  * variables are accessed as negative offsets from hppa_sp
605                  */
606                 inst->opcode = OP_REGOFFSET;
607                 inst->inst_basereg = stack_ptr;
608                 offset += size;
609                 offset = ALIGN_TO (offset, align);
610                 inst->inst_offset = -offset;
611
612                 DEBUG (printf ("allocating local %d (size = %d) to [%s - %d]\n", i, size, mono_arch_regname (inst->inst_basereg), -inst->inst_offset));
613         }
614
615         if (sig->call_convention == MONO_CALL_VARARG) {
616                 /* TODO */
617         }
618
619         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
620                 ArgInfo *ainfo = &cinfo->args [i];
621                 inst = m->args [i];
622                 if (inst->opcode != OP_REGVAR) {
623                         switch (ainfo->storage) {
624                         case ArgInIReg:
625                         case ArgInIRegPair:
626                         case ArgInFReg:
627                         case ArgInDReg:
628                                 /* Currently mono requests all incoming registers
629                                  * be assigned to a stack location :-(
630                                  */
631 #if 0
632                                 if (!(inst->flags & (MONO_INST_VOLATILE | MONO_INST_INDIRECT))) {
633                                         inst->opcode = OP_REGVAR;
634                                         inst->dreg = ainfo->reg;
635                                         DEBUG (printf ("param %d in register %s\n", i, mono_arch_regname (inst->dreg)));
636                                         break;
637                                 }
638 #endif
639                                 /* fallthrough */
640                         case ArgOnStack:
641                                 inst->opcode = OP_REGOFFSET;
642                                 inst->inst_basereg = hppa_r3;
643                                 inst->inst_offset = ainfo->offset;
644                                 DEBUG (printf ("param %d stored on stack [%s - %d]\n", i, mono_arch_regname (hppa_r3), -inst->inst_offset));
645                                 break;
646                         }
647                 }
648         }
649
650         m->stack_offset = offset; /* Includes cfg->param_area */
651
652         g_free (cinfo);
653         DEBUG_FUNC_EXIT();
654 }
655
656 /* 
657  * take the arguments and generate the arch-specific
658  * instructions to properly call the function in call.
659  * This includes pushing, moving arguments to the right register
660  * etc.
661  *
662  * sets call->stack_usage and cfg->param_area
663  */
664 MonoCallInst*
665 mono_arch_call_opcode (MonoCompile *cfg, MonoBasicBlock* bb, MonoCallInst *call, int is_virtual) 
666 {
667         MonoInst *arg, *in;
668         MonoMethodSignature *sig;
669         int i, n;
670         CallInfo *cinfo;
671         ArgInfo *ainfo;
672
673         DEBUG_FUNC_ENTER();
674         DEBUG (printf ("is_virtual = %d\n", is_virtual));
675
676         sig = call->signature;
677         n = sig->param_count + sig->hasthis;
678
679         DEBUG (printf ("Calling method with %d parameters\n", n));
680         
681         cinfo = get_call_info (sig, sig->pinvoke);
682
683         // DEBUG
684         g_assert (sig->call_convention != MONO_CALL_VARARG);
685
686         for (i = 0; i < n; ++i) {
687                 ainfo = &cinfo->args [i];
688
689                 if ((sig->call_convention == MONO_CALL_VARARG) && (i == sig->sentinelpos)) {
690                         /* TODO */
691                 }
692
693                 if (is_virtual && i == 0) {
694                         /* the argument will be attached to the call instruction */
695                         in = call->args [i];
696                         call->used_iregs |= 1 << ainfo->reg;
697                 } else {
698                         MONO_INST_NEW (cfg, arg, OP_OUTARG);
699                         in = call->args [i];
700                         arg->cil_code = in->cil_code;
701                         arg->inst_left = in;
702                         arg->inst_call = call;
703                         arg->type = in->type;
704                         MONO_INST_LIST_ADD_TAIL (&arg->node, &call->out_args);
705
706                         switch (ainfo->storage) {
707                         case ArgInIReg:
708                         case ArgInIRegPair: {
709                                 MonoHPPAArgInfo *ai = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoHPPAArgInfo));
710                                 ai->reg = ainfo->reg;
711                                 ai->size = ainfo->size;
712                                 ai->offset = ainfo->offset;
713                                 ai->pass_in_reg = 1;
714                                 arg->backend.data = ai;
715
716                                 call->used_iregs |= 1 << ainfo->reg;
717                                 if (ainfo->storage == ArgInIRegPair)
718                                         call->used_iregs |= 1 << (ainfo->reg + 1);
719                                 if (ainfo->type == MONO_TYPE_VALUETYPE)
720                                         arg->opcode = OP_OUTARG_VT;
721                                 break;
722                         }
723                         case ArgOnStack: {
724                                 MonoHPPAArgInfo *ai = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoHPPAArgInfo));
725                                 ai->reg = hppa_sp;
726                                 ai->size = ainfo->size;
727                                 ai->offset = ainfo->offset;
728                                 ai->pass_in_reg = 0;
729                                 arg->backend.data = ai;
730                                 if (ainfo->type == MONO_TYPE_VALUETYPE)
731                                         arg->opcode = OP_OUTARG_VT;
732                                 else
733                                         arg->opcode = OP_OUTARG_MEMBASE;
734                                 call->used_iregs |= 1 << ainfo->reg;
735                                 break;
736                         }
737                         case ArgInFReg:
738                                 arg->backend.reg3 = ainfo->reg;
739                                 arg->opcode = OP_OUTARG_R4;
740                                 call->used_fregs |= 1 << ainfo->reg;
741                                 break;
742                         case ArgInDReg:
743                                 arg->backend.reg3 = ainfo->reg;
744                                 arg->opcode = OP_OUTARG_R8;
745                                 call->used_fregs |= 1 << ainfo->reg;
746                                 break;
747                         default:
748                                 NOT_IMPLEMENTED;
749                         }
750                 }
751         }
752
753         call->stack_usage = cinfo->stack_usage;
754         cfg->param_area = MAX (cfg->param_area, call->stack_usage);
755         cfg->param_area = ALIGN_TO (cfg->param_area, MONO_ARCH_FRAME_ALIGNMENT);
756
757         cfg->flags |= MONO_CFG_HAS_CALLS;
758
759         g_free (cinfo);
760
761         DEBUG_FUNC_EXIT();
762         return call;
763 }
764
765 void
766 mono_arch_peephole_pass_1 (MonoCompile *cfg, MonoBasicBlock *bb)
767 {
768 }
769
770 void
771 mono_arch_peephole_pass_2 (MonoCompile *cfg, MonoBasicBlock *bb)
772 {
773         DEBUG_FUNC_ENTER();
774         DEBUG_FUNC_EXIT();
775 }
776
777 #define NEW_INS(cfg,ins,dest,op) do {                                   \
778                 (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
779                 (dest)->opcode = (op);  \
780                 MONO_INST_LIST_ADD_TAIL (&(dest)->node, &(ins)->node); \
781         } while (0)
782
783 static int
784 map_to_reg_reg_op (int op)
785 {
786         switch (op) {
787         case OP_ADD_IMM:
788                 return CEE_ADD;
789         case OP_SUB_IMM:
790                 return CEE_SUB;
791         case OP_AND_IMM:
792                 return CEE_AND;
793         case OP_COMPARE_IMM:
794                 return OP_COMPARE;
795         case OP_ADDCC_IMM:
796                 return OP_ADDCC;
797         case OP_ADC_IMM:
798                 return OP_ADC;
799         case OP_SUBCC_IMM:
800                 return OP_SUBCC;
801         case OP_SBB_IMM:
802                 return OP_SBB;
803         case OP_OR_IMM:
804                 return CEE_OR;
805         case OP_XOR_IMM:
806                 return CEE_XOR;
807         case OP_MUL_IMM:
808                 return CEE_MUL;
809         case OP_LOAD_MEMBASE:
810                 return OP_LOAD_MEMINDEX;
811         case OP_LOADI4_MEMBASE:
812                 return OP_LOADI4_MEMINDEX;
813         case OP_LOADU4_MEMBASE:
814                 return OP_LOADU4_MEMINDEX;
815         case OP_LOADU1_MEMBASE:
816                 return OP_LOADU1_MEMINDEX;
817         case OP_LOADI2_MEMBASE:
818                 return OP_LOADI2_MEMINDEX;
819         case OP_LOADU2_MEMBASE:
820                 return OP_LOADU2_MEMINDEX;
821         case OP_LOADI1_MEMBASE:
822                 return OP_LOADI1_MEMINDEX;
823         case OP_LOADR4_MEMBASE:
824                 return OP_LOADR4_MEMINDEX;
825         case OP_LOADR8_MEMBASE:
826                 return OP_LOADR8_MEMINDEX;
827         case OP_STOREI1_MEMBASE_REG:
828                 return OP_STOREI1_MEMINDEX;
829         case OP_STOREI2_MEMBASE_REG:
830                 return OP_STOREI2_MEMINDEX;
831         case OP_STOREI4_MEMBASE_REG:
832                 return OP_STOREI4_MEMINDEX;
833         case OP_STORE_MEMBASE_REG:
834                 return OP_STORE_MEMINDEX;
835         case OP_STORER4_MEMBASE_REG:
836                 return OP_STORER4_MEMINDEX;
837         case OP_STORER8_MEMBASE_REG:
838                 return OP_STORER8_MEMINDEX;
839         case OP_STORE_MEMBASE_IMM:
840                 return OP_STORE_MEMBASE_REG;
841         case OP_STOREI1_MEMBASE_IMM:
842                 return OP_STOREI1_MEMBASE_REG;
843         case OP_STOREI2_MEMBASE_IMM:
844                 return OP_STOREI2_MEMBASE_REG;
845         case OP_STOREI4_MEMBASE_IMM:
846                 return OP_STOREI4_MEMBASE_REG;
847         }
848         g_assert_not_reached ();
849 }
850
851 /*
852  * Remove from the instruction list the instructions that can't be
853  * represented with very simple instructions with no register
854  * requirements.
855  */
856 void
857 mono_arch_lowering_pass (MonoCompile *cfg, MonoBasicBlock *bb)
858 {
859         MonoInst *ins, *next, *temp, *temp2;
860         int imm;
861
862         /* setup the virtual reg allocator */
863         if (bb->max_vreg > cfg->rs->next_vreg)
864                 cfg->rs->next_vreg = bb->max_vreg;
865
866         MONO_BB_FOR_EACH_INS (bb, ins) {
867 loop_start:
868                 switch (ins->opcode) {
869                 case OP_ADD_IMM:
870                 case OP_ADDCC_IMM:
871                         if (!hppa_check_bits (ins->inst_imm, 11)) {
872                                 NEW_INS (cfg, ins, temp, OP_ICONST);
873                                 temp->inst_c0 = ins->inst_imm;
874                                 temp->dreg = mono_regstate_next_int (cfg->rs);
875                                 ins->sreg2 = temp->dreg;
876                                 ins->opcode = map_to_reg_reg_op (ins->opcode);
877                         }
878                         break;
879                 case OP_SUB_IMM:
880                 case OP_SUBCC_IMM:
881                         if (!hppa_check_bits (ins->inst_imm, 11)) {
882                                 NEW_INS (cfg, ins, temp, OP_ICONST);
883                                 temp->inst_c0 = ins->inst_imm;
884                                 temp->dreg = mono_regstate_next_int (cfg->rs);
885                                 ins->sreg2 = temp->dreg;
886                                 ins->opcode = map_to_reg_reg_op (ins->opcode);
887                         }
888                         break;
889
890                 case OP_MUL_IMM:
891                         if (ins->inst_imm == 1) {
892                                 ins->opcode = OP_MOVE;
893                                 break;
894                         }
895                         if (ins->inst_imm == 0) {
896                                 ins->opcode = OP_ICONST;
897                                 ins->inst_c0 = 0;
898                                 break;
899                         }
900                         imm = mono_is_power_of_two (ins->inst_imm);
901                         if (imm > 0) {
902                                 ins->opcode = OP_SHL_IMM;
903                                 ins->inst_imm = imm;
904                                 break;
905                         }
906                         else {
907                                 int tmp = mono_regstate_next_int (cfg->rs);
908                                 NEW_INS (cfg, ins, temp, OP_ICONST);
909                                 temp->inst_c0 = ins->inst_c0;
910                                 temp->dreg = tmp;
911
912                                 ins->opcode = CEE_MUL;
913                                 ins->sreg2 = tmp;
914                                 /* Need to rewrite the CEE_MUL too... */
915                                 goto loop_start;
916                         }
917                         break;
918
919                 case CEE_MUL: {
920                         int freg1 = mono_regstate_next_float (cfg->rs);
921                         int freg2 = mono_regstate_next_float (cfg->rs);
922
923                         NEW_INS(cfg, ins, temp, OP_STORE_MEMBASE_REG);
924                         temp->sreg1 = ins->sreg1;
925                         temp->inst_destbasereg = hppa_sp;
926                         temp->inst_offset = -16;
927
928                         NEW_INS(cfg, temp, temp2, OP_LOADR4_MEMBASE);
929                         temp2->dreg = freg1;
930                         temp2->inst_basereg = hppa_sp;
931                         temp2->inst_offset = -16;
932
933                         NEW_INS(cfg, temp2, temp, OP_STORE_MEMBASE_REG);
934                         temp->sreg1 = ins->sreg2;
935                         temp->inst_destbasereg = hppa_sp;
936                         temp->inst_offset = -16;
937
938                         NEW_INS(cfg, temp, temp2, OP_LOADR4_MEMBASE);
939                         temp2->dreg = freg2;
940                         temp2->inst_basereg = hppa_sp;
941                         temp2->inst_offset = -16;
942
943                         NEW_INS (cfg, temp2, temp, OP_HPPA_XMPYU);
944                         temp->dreg = freg2;
945                         temp->sreg1 = freg1;
946                         temp->sreg2 = freg2;
947
948                         NEW_INS(cfg, temp, temp2, OP_HPPA_STORER4_RIGHT);
949                         temp2->sreg1 = freg2;
950                         temp2->inst_destbasereg = hppa_sp;
951                         temp2->inst_offset = -16;
952
953                         ins->opcode = OP_LOAD_MEMBASE;
954                         ins->inst_basereg = hppa_sp;
955                         ins->inst_offset = -16;
956                 }
957                 break;
958
959                 default:
960                         break;
961                 }
962         }
963         bb->max_vreg = cfg->rs->next_vreg;
964         
965 }
966
967 void
968 hppa_patch (guint32 *code, const gpointer target)
969 {
970         guint32 ins = *code;
971         gint32 val = (gint32)target;
972         gint32 disp = (val - (gint32)code - 8) >> 2;
973         int reg1, reg2;
974
975         DEBUG (printf ("patching 0x%08x (0x%08x) to point to 0x%08x (disp = %d)\n", code, ins, val, disp));
976
977         switch (*code >> 26) {
978         case 0x08: /* ldil, next insn can be a ldo, ldw, or ble */
979                 *code = *code & ~0x1fffff;
980                 *code = *code | hppa_op_imm21 (hppa_lsel (val));
981                 code++;
982
983                 if ((*code >> 26) == 0x0D) { /* ldo */
984                         *code = *code & ~0x3fff;
985                         *code = *code | hppa_op_imm14 (hppa_rsel (val));
986                 } else if ((*code >> 26) == 0x12) { /* ldw */
987                         *code = *code & ~0x3fff;
988                         *code = *code | hppa_op_imm14 (hppa_rsel (val));
989                 } else if ((*code >> 26) == 0x39) { /* ble */
990                         *code = *code & ~0x1f1ffd;
991                         *code = *code | hppa_op_imm17 (hppa_rsel (val));
992                 }
993
994                 break;
995
996         case 0x3A: /* bl */
997                 if (disp == 0) {
998                         hppa_nop (code);
999                         break;
1000                 }
1001                 if (!hppa_check_bits (disp, 17)) 
1002                         goto jump_overflow;
1003                 reg1 = (*code >> 21) & 0x1f;
1004                 *code = (*code & ~0x1f1ffd) | hppa_op_imm17(disp);
1005                 break;
1006
1007         case 0x20: /* combt */
1008         case 0x22: /* combf */
1009                 if (!hppa_check_bits (disp >> 2, 12))
1010                         goto jump_overflow;
1011                 *code = (*code & ~0x1ffd) | hppa_op_imm12(disp);
1012                 break;
1013
1014         default:
1015                 g_warning ("Unpatched opcode %x\n", *code >> 26);
1016         }
1017
1018         return;
1019
1020 jump_overflow:
1021         g_warning ("cannot branch to target, insn is %08x, displacement is %d\n", (int)*code, (int)disp);
1022         g_assert_not_reached ();
1023 }
1024
1025 static guint32 *
1026 emit_float_to_int (MonoCompile *cfg, guint32 *code, int dreg, int sreg, int size, gboolean is_signed)
1027 {
1028         /* sreg is a float, dreg is an integer reg. */
1029         hppa_fcnvfxt (code, HPPA_FP_FMT_DBL, HPPA_FP_FMT_SGL, sreg, sreg);
1030         hppa_fstws (code, sreg, 0, -16, hppa_sp);
1031         hppa_ldw (code, -16, hppa_sp, dreg);
1032         if (!is_signed) {
1033                 if (size == 1)
1034                         hppa_extru (code, dreg, 31, 8, dreg);
1035                 else if (size == 2)
1036                         hppa_extru (code, dreg, 31, 16, dreg);
1037         } else {
1038                 if (size == 1)
1039                         hppa_extrs (code, dreg, 31, 8, dreg);
1040                 else if (size == 2)
1041                         hppa_extrs (code, dreg, 31, 16, dreg);
1042         }
1043         return code;
1044 }
1045
1046 /* Clobbers r1, r20, r21 */
1047 static guint32 *
1048 emit_memcpy (guint32 *code, int doff, int dreg, int soff, int sreg, int size)
1049 {
1050         /* r20 is the destination */
1051         hppa_set (code, doff, hppa_r20);
1052         hppa_add (code, hppa_r20, dreg, hppa_r20);
1053
1054         /* r21 is the source */
1055         hppa_set (code, soff, hppa_r21);
1056         hppa_add (code, hppa_r21, sreg, hppa_r21);
1057
1058         while (size >= 4) {
1059                 hppa_ldw (code, 0, hppa_r21, hppa_r1);
1060                 hppa_stw (code, hppa_r1, 0, hppa_r20);
1061                 hppa_ldo (code, 4, hppa_r21, hppa_r21);
1062                 hppa_ldo (code, 4, hppa_r20, hppa_r20);
1063                 size -= 4;
1064         }
1065         while (size >= 2) {
1066                 hppa_ldh (code, 0, hppa_r21, hppa_r1);
1067                 hppa_sth (code, hppa_r1, 0, hppa_r20);
1068                 hppa_ldo (code, 2, hppa_r21, hppa_r21);
1069                 hppa_ldo (code, 2, hppa_r20, hppa_r20);
1070                 size -= 2;
1071         }
1072         while (size > 0) {
1073                 hppa_ldb (code, 0, hppa_r21, hppa_r1);
1074                 hppa_stb (code, hppa_r1, 0, hppa_r20);
1075                 hppa_ldo (code, 1, hppa_r21, hppa_r21);
1076                 hppa_ldo (code, 1, hppa_r20, hppa_r20);
1077                 size -= 1;
1078         }
1079
1080         return code;
1081 }
1082
1083 /*
1084  * mono_arch_get_vcall_slot_addr:
1085  *
1086  *  Determine the vtable slot used by a virtual call.
1087  */
1088 gpointer*
1089 mono_arch_get_vcall_slot_addr (guint8 *code8, gpointer *regs)
1090 {
1091         guint32 *code = (guint32*)((unsigned long)code8 & ~3);
1092
1093         DEBUG_FUNC_ENTER();
1094
1095         code -= 2;
1096         /* This is the special virtual call token */
1097         if (code [-1] != 0x34000eee) /* ldo 0x777(r0),r0 */
1098                 return NULL;
1099
1100         if ((code [0] >> 26) == 0x39 &&         /* ble */
1101             (code [-2] >> 26) == 0x12) {        /* ldw */
1102                 guint32 ldw = code [-2];
1103                 guint32 reg = (ldw >> 21) & 0x1f;
1104                 gint32 disp = ((ldw & 1) ? (-1 << 13) : 0) | ((ldw & 0x3fff) >> 1);
1105                 /* FIXME: we are not guaranteed that reg is saved in the LMF.
1106                  * In fact, it probably isn't, since it is allocated as a
1107                  * callee register.  Right now just return an address; this 
1108                  * is sufficient for non-AOT operation
1109                  */
1110                 // return (gpointer)((guint8*)regs [reg] + disp);
1111                 return code;
1112         }
1113         else
1114                 g_assert_not_reached ();
1115
1116         DEBUG_FUNC_EXIT();
1117 }
1118
1119 /* ins->dreg = *(ins->inst_desgbasereg + ins->inst_offset) */
1120 #define EMIT_LOAD_MEMBASE(ins, op) do {                         \
1121         if (!hppa_check_bits (ins->inst_offset, 14)) {          \
1122                 hppa_set (code, ins->inst_offset, hppa_r1);     \
1123                 hppa_ ## op ## x (code, hppa_r1, ins->inst_basereg, ins->dreg); \
1124         }                                                       \
1125         else {                                                  \
1126                 hppa_ ## op (code, ins->inst_offset, ins->inst_basereg, ins->dreg); \
1127         }                                                       \
1128 } while (0)
1129
1130 #define EMIT_COND_BRANCH_FLAGS(ins,r1,r2,b0,b1) do {\
1131         if (ins->flags & MONO_INST_BRLABEL) { \
1132                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1133                 if (b0) \
1134                         hppa_combt (code, r1, r2, b1, 0); \
1135                 else \
1136                         hppa_combf (code, r1, r2, b1, 0); \
1137         } else { \
1138                 if (b0) \
1139                         hppa_combf (code, r1, r2, b1, 2); \
1140                 else \
1141                         hppa_combt (code, r1, r2, b1, 2); \
1142                 hppa_nop (code); \
1143                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1144                 hppa_bl (code, 0, hppa_r0); \
1145         } \
1146         hppa_nop (code); \
1147 } while (0)
1148
1149 #define EMIT_COND_BRANCH(ins,r1,r2,cond) EMIT_COND_BRANCH_FLAGS(ins, r1, r2, branch_b0_table [(cond)], branch_b1_table [(cond)])
1150
1151 #define EMIT_FLOAT_COND_BRANCH_FLAGS(ins,r1,r2,b0) do {\
1152         hppa_fcmp (code, HPPA_FP_FMT_DBL, b0, r1, r2); \
1153         hppa_ftest (code, 0); \
1154         if (ins->flags & MONO_INST_BRLABEL) \
1155                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_LABEL, ins->inst_i0); \
1156         else \
1157                 mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_true_bb); \
1158         hppa_bl (code, 8, hppa_r0); \
1159         hppa_nop (code); \
1160 } while (0)
1161
1162 #define EMIT_FLOAT_COND_BRANCH(ins,r1,r2,cond) EMIT_FLOAT_COND_BRANCH_FLAGS(ins, r1, r2, float_branch_table [cond])
1163
1164 #define EMIT_COND_SYSTEM_EXCEPTION_FLAGS(r1,r2,b0,b1,exc_name)          \
1165 do {                                                                    \
1166         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump)); \
1167         ovfj->data.exception = (exc_name);                      \
1168         ovfj->ip_offset = (guint8*)code - cfg->native_code;     \
1169         hppa_bl (code, 8, hppa_r2);                             \
1170         hppa_depi (code, 0, 31, 2, hppa_r2);                    \
1171         hppa_ldo (code, 8, hppa_r2, hppa_r2);                   \
1172         if (b0)                                                 \
1173                 hppa_combf (code, r1, r2, b1, 2);               \
1174         else                                                    \
1175                 hppa_combt (code, r1, r2, b1, 2);               \
1176         hppa_nop (code);                                        \
1177         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj); \
1178         hppa_bl (code, 0, hppa_r0);                             \
1179         hppa_nop (code);                                        \
1180 } while (0)
1181
1182 #define EMIT_COND_SYSTEM_EXCEPTION(r1,r2,cond,exc_name) EMIT_COND_SYSTEM_EXCEPTION_FLAGS(r1, r2, branch_b0_table [(cond)], branch_b1_table [(cond)], (exc_name))
1183
1184 /* TODO: MEM_INDEX_REG - cannot be r1 */
1185 #define MEM_INDEX_REG hppa_r31
1186 /* *(ins->inst_destbasereg + ins->inst_offset) = ins->inst_imm */
1187 #define EMIT_STORE_MEMBASE_IMM(ins, op) do {                    \
1188         guint32 sreg;                                           \
1189         if (ins->inst_imm == 0)                                 \
1190                 sreg = hppa_r0;                                 \
1191         else {                                                  \
1192                 hppa_set (code, ins->inst_imm, hppa_r1);        \
1193                 sreg = hppa_r1;                                 \
1194         }                                                       \
1195         if (!hppa_check_bits (ins->inst_offset, 14)) {          \
1196                 hppa_set (code, ins->inst_offset, MEM_INDEX_REG); \
1197                 hppa_addl (code, ins->inst_destbasereg, MEM_INDEX_REG, MEM_INDEX_REG); \
1198                 hppa_ ## op (code, sreg, 0, MEM_INDEX_REG);     \
1199         }                                                       \
1200         else {                                                  \
1201                 hppa_ ## op (code, sreg, ins->inst_offset, ins->inst_destbasereg); \
1202         }                                                       \
1203 } while (0)
1204
1205 /* *(ins->inst_destbasereg + ins->inst_offset) = ins->sreg1 */
1206 #define EMIT_STORE_MEMBASE_REG(ins, op) do {                    \
1207         if (!hppa_check_bits (ins->inst_offset, 14)) {          \
1208                 hppa_set (code, ins->inst_offset, MEM_INDEX_REG); \
1209                 hppa_addl (code, ins->inst_destbasereg, MEM_INDEX_REG, MEM_INDEX_REG); \
1210                 hppa_ ## op (code, ins->sreg1, 0, MEM_INDEX_REG);       \
1211         }                                                       \
1212         else {                                                  \
1213                 hppa_ ## op (code, ins->sreg1, ins->inst_offset, ins->inst_destbasereg); \
1214         }                                                       \
1215 } while (0)
1216
1217 void
1218 mono_arch_output_basic_block (MonoCompile *cfg, MonoBasicBlock *bb)
1219 {
1220         MonoInst *ins;
1221         MonoCallInst *call;
1222         guint offset;
1223         guint32 *code = (guint32*)(cfg->native_code + cfg->code_len);
1224         int max_len, cpos;
1225         const char *spec;
1226
1227         DEBUG_FUNC_ENTER();
1228
1229         if (cfg->verbose_level > 2)
1230                 g_print ("[%s::%s] Basic block %d starting at offset 0x%x\n", cfg->method->klass->name, cfg->method->name, bb->block_num, bb->native_offset);
1231
1232         cpos = bb->max_offset;
1233
1234         if (cfg->prof_options & MONO_PROFILE_COVERAGE) {
1235                 NOT_IMPLEMENTED;
1236         }
1237
1238         MONO_BB_FOR_EACH_INS (bb, ins) {
1239                 guint8* code_start;
1240
1241                 offset = (guint8*)code - cfg->native_code;
1242
1243                 spec = ins_get_spec (ins->opcode);
1244
1245                 max_len = ((guint8 *)spec) [MONO_INST_LEN];
1246
1247                 if (offset > (cfg->code_size - max_len - 16)) {
1248                         cfg->code_size *= 2;
1249                         cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1250                         code = (guint32*)(cfg->native_code + offset);
1251                         mono_jit_stats.code_reallocs++;
1252                 }
1253                 code_start = (guint8*)code;
1254                 //      if (ins->cil_code)
1255                 //              g_print ("cil code\n");
1256                 mono_debug_record_line_number (cfg, ins, offset);
1257
1258                 switch (ins->opcode) {
1259                 case OP_STOREI1_MEMBASE_IMM:
1260                         EMIT_STORE_MEMBASE_IMM (ins, stb);
1261                         break;
1262                 case OP_STOREI2_MEMBASE_IMM:
1263                         EMIT_STORE_MEMBASE_IMM (ins, sth);
1264                         break;
1265                 case OP_STORE_MEMBASE_IMM:
1266                 case OP_STOREI4_MEMBASE_IMM:
1267                         EMIT_STORE_MEMBASE_IMM (ins, stw);
1268                         break;
1269                 case OP_STOREI1_MEMBASE_REG:
1270                         EMIT_STORE_MEMBASE_REG (ins, stb);
1271                         break;
1272                 case OP_STOREI2_MEMBASE_REG:
1273                         EMIT_STORE_MEMBASE_REG (ins, sth);
1274                         break;
1275                 case OP_STORE_MEMBASE_REG:
1276                 case OP_STOREI4_MEMBASE_REG:
1277                         EMIT_STORE_MEMBASE_REG (ins, stw);
1278                         break;
1279                 case OP_LOADU1_MEMBASE:
1280                         EMIT_LOAD_MEMBASE (ins, ldb);
1281                         break;
1282                 case OP_LOADI1_MEMBASE:
1283                         EMIT_LOAD_MEMBASE (ins, ldb);
1284                         hppa_extrs (code, ins->dreg, 31, 8, ins->dreg);
1285                         break;
1286                 case OP_LOADU2_MEMBASE:
1287                         EMIT_LOAD_MEMBASE (ins, ldh);
1288                         break;
1289                 case OP_LOADI2_MEMBASE:
1290                         EMIT_LOAD_MEMBASE (ins, ldh);
1291                         hppa_extrs (code, ins->dreg, 31, 16, ins->dreg);
1292                         break;
1293                 case OP_LOAD_MEMBASE:
1294                 case OP_LOADI4_MEMBASE:
1295                 case OP_LOADU4_MEMBASE:
1296                         EMIT_LOAD_MEMBASE (ins, ldw);
1297                         break;
1298                 case CEE_CONV_I1:
1299                         hppa_extrs (code, ins->sreg1, 31, 8, ins->dreg);
1300                         break;
1301                 case CEE_CONV_I2:
1302                         hppa_extrs (code, ins->sreg1, 31, 16, ins->dreg);
1303                         break;
1304                 case CEE_CONV_U1:
1305                         hppa_extru (code, ins->sreg1, 31, 8, ins->dreg);
1306                         break;
1307                 case CEE_CONV_U2:
1308                         hppa_extru (code, ins->sreg1, 31, 16, ins->dreg);
1309                         break;
1310                 case CEE_CONV_U:
1311                 case CEE_CONV_I4:
1312                 case CEE_CONV_U4:
1313                 case OP_MOVE:
1314                         if (ins->sreg1 != ins->dreg)
1315                                 hppa_copy (code, ins->sreg1, ins->dreg);
1316                         break;
1317                 case OP_SETLRET:
1318                         hppa_copy (code, ins->sreg1 + 1, ins->dreg);
1319                         hppa_copy (code, ins->sreg1, ins->dreg + 1);
1320                         break;
1321
1322                 case OP_BREAK:
1323                         /* break 4,8 - this is what gdb normally uses... */
1324                         *code++ = 0x00010004;
1325                         break;
1326                 case OP_ADDCC:
1327                 case CEE_ADD:
1328                         hppa_add (code, ins->sreg1, ins->sreg2, ins->dreg);
1329                         break;
1330                 case OP_ADC:
1331                         hppa_addc (code, ins->sreg1, ins->sreg2, ins->dreg);
1332                         break;
1333                 case OP_ADDCC_IMM:
1334                 case OP_ADD_IMM:
1335                         hppa_addi (code, ins->inst_imm, ins->sreg1, ins->dreg);
1336                         break;
1337                 case OP_ADC_IMM:
1338                         hppa_set (code, ins->inst_imm, hppa_r1);
1339                         hppa_addc (code, ins->sreg1, hppa_r1, ins->dreg);
1340                         break;
1341                 case OP_HPPA_ADD_OVF: {
1342                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));
1343                         hppa_bl (code, 8, hppa_r2);
1344                         hppa_depi (code, 0, 31, 2, hppa_r2);
1345                         hppa_ldo (code, 12, hppa_r2, hppa_r2);
1346
1347                         if (ins->backend.reg3 == CEE_ADD_OVF)
1348                                 hppa_add_cond (code, HPPA_ADD_COND_NSV, ins->sreg1, ins->sreg2, ins->dreg);
1349                         else                    
1350                                 hppa_add_cond (code, HPPA_ADD_COND_NUV, ins->sreg1, ins->sreg2, ins->dreg);
1351
1352                         ovfj->data.exception = "OverflowException";
1353                         ovfj->ip_offset = (guint8*)code - cfg->native_code;
1354                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj);
1355                         hppa_bl_n (code, 8, hppa_r0);
1356                         break;
1357                 }
1358                 case OP_HPPA_ADDC_OVF: {
1359                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));
1360                         hppa_bl (code, 8, hppa_r2);
1361                         hppa_depi (code, 0, 31, 2, hppa_r2);
1362                         hppa_ldo (code, 12, hppa_r2, hppa_r2);
1363
1364                         if (ins->backend.reg3 == OP_LADD_OVF)
1365                                 hppa_addc_cond (code, HPPA_ADD_COND_NSV, ins->sreg1, ins->sreg2, ins->dreg);
1366                         else                    
1367                                 hppa_addc_cond (code, HPPA_ADD_COND_NUV, ins->sreg1, ins->sreg2, ins->dreg);
1368
1369                         ovfj->data.exception = "OverflowException";
1370                         ovfj->ip_offset = (guint8*)code - cfg->native_code;
1371                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj);
1372                         hppa_bl_n (code, 8, hppa_r0);
1373                         break;
1374                 }
1375                 case OP_SUBCC:
1376                 case CEE_SUB:
1377                         hppa_sub (code, ins->sreg1, ins->sreg2, ins->dreg);
1378                         break;
1379                 case OP_SUBCC_IMM:
1380                 case OP_SUB_IMM:
1381                         hppa_addi (code, -ins->inst_imm, ins->sreg1, ins->dreg);
1382                         break;
1383                 case OP_SBB:
1384                         hppa_subb (code, ins->sreg1, ins->sreg2, ins->dreg);
1385                         break;
1386                 case OP_SBB_IMM:
1387                         hppa_set (code, ins->inst_imm, hppa_r1);
1388                         hppa_subb (code, ins->sreg1, hppa_r1, ins->dreg);
1389                         break;
1390                 case OP_HPPA_SUB_OVF: {
1391                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));
1392                         hppa_bl (code, 8, hppa_r2);
1393                         hppa_depi (code, 0, 31, 2, hppa_r2);
1394                         hppa_ldo (code, 12, hppa_r2, hppa_r2);
1395                         hppa_sub_cond (code, HPPA_SUB_COND_NSV, ins->sreg1, ins->sreg2, ins->dreg);
1396                         ovfj->data.exception = "OverflowException";
1397                         ovfj->ip_offset = (guint8*)code - cfg->native_code;
1398                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj);
1399                         hppa_bl_n (code, 8, hppa_r0);
1400                         break;
1401                 }
1402                 case OP_HPPA_SUBB_OVF: {
1403                         MonoOvfJump *ovfj = mono_mempool_alloc (cfg->mempool, sizeof (MonoOvfJump));
1404                         hppa_bl (code, 8, hppa_r2);
1405                         hppa_depi (code, 0, 31, 2, hppa_r2);
1406                         hppa_ldo (code, 12, hppa_r2, hppa_r2);
1407
1408                         hppa_subb_cond (code, HPPA_SUB_COND_NSV, ins->sreg1, ins->sreg2, ins->dreg);
1409                         ovfj->data.exception = "OverflowException";
1410                         ovfj->ip_offset = (guint8*)code - cfg->native_code;
1411                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_EXC_OVF, ovfj);
1412                         hppa_bl_n (code, 8, hppa_r0);
1413                         break;
1414                 }
1415
1416                 case CEE_AND:
1417                         hppa_and (code, ins->sreg1, ins->sreg2, ins->dreg);
1418                         break;
1419                 case OP_AND_IMM:
1420                         hppa_set (code, ins->inst_imm, hppa_r1);
1421                         hppa_and (code, ins->sreg1, hppa_r1, ins->dreg);
1422                         break;
1423
1424                 case CEE_OR:
1425                         hppa_or (code, ins->sreg1, ins->sreg2, ins->dreg);
1426                         break;
1427
1428                 case OP_OR_IMM:
1429                         hppa_set (code, ins->inst_imm, hppa_r1);
1430                         hppa_or (code, ins->sreg1, hppa_r1, ins->dreg);
1431                         break;
1432
1433                 case CEE_XOR:
1434                         hppa_xor (code, ins->sreg1, ins->sreg2, ins->dreg);
1435                         break;
1436                 case OP_XOR_IMM:
1437                         hppa_set (code, ins->inst_imm, hppa_r1);
1438                         hppa_xor (code, ins->sreg1, hppa_r1, ins->dreg);
1439                         break;
1440                 case CEE_SHL:
1441                         if (ins->sreg1 != ins->dreg) {
1442                                 hppa_shl (code, ins->sreg1, ins->sreg2, ins->dreg);
1443                         } 
1444                         else {
1445                                 hppa_copy (code, ins->sreg1, hppa_r1);
1446                                 hppa_shl (code, hppa_r1, ins->sreg2, ins->dreg);
1447                         }
1448                         break;
1449                 case OP_SHL_IMM:
1450                 case OP_ISHL_IMM:
1451                         g_assert (ins->inst_imm < 32);
1452                         if (ins->sreg1 != ins->dreg) {
1453                                 hppa_zdep (code, ins->sreg1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1454                         } 
1455                         else {
1456                                 hppa_copy (code, ins->sreg1, hppa_r1);
1457                                 hppa_zdep (code, hppa_r1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1458                         }
1459                         break;
1460                 case CEE_SHR:
1461                         if (ins->sreg1 != ins->dreg) {
1462                                 hppa_shr (code, ins->sreg1, ins->sreg2, ins->dreg);
1463                         }
1464                         else {
1465                                 hppa_copy (code, ins->sreg1, hppa_r1);
1466                                 hppa_shr (code, hppa_r1, ins->sreg2, ins->dreg);
1467                         }
1468                         break;
1469                 case OP_SHR_IMM:
1470                         g_assert (ins->inst_imm < 32);
1471                         if (ins->sreg1 != ins->dreg) {
1472                                 hppa_extrs (code, ins->sreg1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1473                         } 
1474                         else {
1475                                 hppa_copy (code, ins->sreg1, hppa_r1);
1476                                 hppa_extrs (code, hppa_r1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1477                         }
1478                         break;
1479                 case OP_SHR_UN_IMM:
1480                         g_assert (ins->inst_imm < 32);
1481                         if (ins->sreg1 != ins->dreg) {
1482                                 hppa_extru (code, ins->sreg1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1483                         } 
1484                         else {
1485                                 hppa_copy (code, ins->sreg1, hppa_r1);
1486                                 hppa_extru (code, hppa_r1, 31-ins->inst_imm, 32-ins->inst_imm, ins->dreg);
1487                         }
1488                         break;
1489                 case CEE_SHR_UN:
1490                         if (ins->sreg1 != ins->dreg) {
1491                                 hppa_lshr (code, ins->sreg1, ins->sreg2, ins->dreg);
1492                         }
1493                         else {
1494                                 hppa_copy (code, ins->sreg1, hppa_r1);
1495                                 hppa_lshr (code, hppa_r1, ins->sreg2, ins->dreg);
1496                         }
1497                         break;
1498                 case CEE_NOT:
1499                         hppa_not (code, ins->sreg1, ins->dreg);
1500                         break;
1501                 case CEE_NEG:
1502                         hppa_subi (code, 0, ins->sreg1, ins->dreg);
1503                         break;
1504
1505                 case CEE_MUL:
1506                 case OP_MUL_IMM:
1507                         /* Should have been rewritten using xmpyu */
1508                         g_assert_not_reached ();
1509
1510                 case OP_ICONST:
1511                         if ((ins->inst_c0 > 0 && ins->inst_c0 >= (1 << 13)) ||
1512                             (ins->inst_c0 < 0 && ins->inst_c0 < -(1 << 13))) {
1513                                 hppa_ldil (code, hppa_lsel (ins->inst_c0), ins->dreg);
1514                                 hppa_ldo (code, hppa_rsel (ins->inst_c0), ins->dreg, ins->dreg);
1515                         } else {
1516                                 hppa_ldo (code, ins->inst_c0, hppa_r0, ins->dreg);
1517                         }
1518                         break;
1519                 case OP_AOTCONST:
1520                         g_assert_not_reached ();
1521                 /*
1522                         mono_add_patch_info (cfg, offset, (MonoJumpInfoType)ins->inst_i1, ins->inst_p0);
1523                         hppa_set_template (code, ins->dreg);
1524                 */
1525                         g_warning ("unimplemented opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
1526                         NOT_IMPLEMENTED;
1527                         break;
1528                 case OP_FMOVE:
1529                         if (ins->sreg1 != ins->dreg)
1530                                 hppa_fcpy (code, HPPA_FP_FMT_DBL, ins->sreg1, ins->dreg);
1531                         break;
1532
1533                 case OP_HPPA_OUTARG_R4CONST:
1534                         hppa_set (code, (unsigned int)ins->inst_p0, hppa_r1);
1535                         hppa_fldwx (code, hppa_r0, hppa_r1, ins->dreg, 0);
1536                         break;
1537
1538                 case OP_HPPA_OUTARG_REGOFFSET:
1539                         hppa_ldo (code, ins->inst_offset, ins->inst_basereg, ins->dreg);
1540                         break;
1541
1542                 case OP_JMP:
1543                         /*
1544                          * Keep in sync with mono_arch_emit_epilog
1545                          */
1546                         g_assert (!cfg->method->save_lmf);
1547                         mono_add_patch_info (cfg, (guint8*) code - cfg->native_code, MONO_PATCH_INFO_METHOD_JUMP, ins->inst_p0);
1548                         hppa_bl (code, 8, hppa_r0);
1549                         break;
1550                 case OP_CHECK_THIS:
1551                         /* ensure ins->sreg1 is not NULL */
1552                         hppa_ldw (code, 0, ins->sreg1, hppa_r1);
1553                         break;
1554                 case OP_ARGLIST:
1555                         break;
1556                 case OP_FCALL:
1557                 case OP_LCALL:
1558                 case OP_VCALL:
1559                 case OP_VOIDCALL:
1560                 case OP_CALL:
1561                         call = (MonoCallInst*)ins;
1562                         if (ins->flags & MONO_INST_HAS_METHOD)
1563                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_METHOD, call->method);
1564                         else
1565                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_ABS, call->fptr);
1566                         hppa_ldil (code, 0, hppa_r1); 
1567                         hppa_ldo (code, 0, hppa_r1, hppa_r1); 
1568                         /* 
1569                          * We may have loaded an actual function address, or
1570                          * it might be a plabel. Check to see if the plabel
1571                          * bit is set, and load the actual fptr from it if
1572                          * needed
1573                          */
1574                         hppa_bb_n (code, HPPA_BIT_COND_MSB_CLR, hppa_r1, 30, 2);
1575                         hppa_depi (code, 0, 31, 2, hppa_r1);
1576                         hppa_ldw (code, 4, hppa_r1, hppa_r19);
1577                         hppa_ldw (code, 0, hppa_r1, hppa_r1);
1578                         hppa_ble (code, 0, hppa_r1);
1579                         hppa_copy (code, hppa_r31, hppa_r2);
1580                         if (call->signature->ret->type == MONO_TYPE_R4)
1581                                 hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, hppa_fr4, hppa_fr4);
1582                         break;
1583                 case OP_FCALL_REG:
1584                 case OP_LCALL_REG:
1585                 case OP_VCALL_REG:
1586                 case OP_VOIDCALL_REG:
1587                 case OP_CALL_REG:
1588                         call = (MonoCallInst*)ins;
1589                         g_assert (!call->virtual);
1590                         hppa_copy (code, ins->sreg1, hppa_r1);
1591                         hppa_bb_n (code, HPPA_BIT_COND_MSB_CLR, hppa_r1, 30, 2);
1592                         hppa_depi (code, 0, 31, 2, hppa_r1);
1593                         hppa_ldw (code, 4, hppa_r1, hppa_r19);
1594                         hppa_ldw (code, 0, hppa_r1, hppa_r1);
1595                         hppa_ble (code, 0, hppa_r1);
1596                         hppa_copy (code, hppa_r31, hppa_r2);
1597                         if (call->signature->ret->type == MONO_TYPE_R4)
1598                                 hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, hppa_fr4, hppa_fr4);
1599                         break;
1600                 case OP_FCALL_MEMBASE:
1601                 case OP_LCALL_MEMBASE:
1602                 case OP_VCALL_MEMBASE:
1603                 case OP_VOIDCALL_MEMBASE:
1604                 case OP_CALL_MEMBASE:
1605                         call = (MonoCallInst*)ins;
1606                         /* jump to ins->inst_sreg1 + ins->inst_offset */
1607                         hppa_ldw (code, ins->inst_offset, ins->sreg1, hppa_r1);
1608
1609                         /* For virtual calls, emit a special token that can
1610                          * be used by get_vcall_slot_addr
1611                          */
1612                         if (call->virtual)
1613                                 hppa_ldo (code, 0x777, hppa_r0, hppa_r0);
1614                         hppa_ble (code, 0, hppa_r1);
1615                         hppa_copy (code, hppa_r31, hppa_r2);
1616                         break;
1617                 case OP_LOCALLOC: {
1618                         guint32 size_reg;
1619
1620                         /* Keep alignment */
1621                         hppa_ldo (code, MONO_ARCH_LOCALLOC_ALIGNMENT - 1, ins->sreg1, ins->dreg);
1622                         hppa_depi (code, 0, 31, 6, ins->dreg);
1623                         hppa_copy (code, hppa_sp, hppa_r1);
1624                         hppa_addl (code, ins->dreg, hppa_sp, hppa_sp);
1625                         hppa_copy (code, hppa_r1, ins->dreg);
1626
1627                         if (ins->flags & MONO_INST_INIT) {
1628                                 hppa_stw (code, hppa_r0, 0, hppa_r1);
1629                                 hppa_combt (code, hppa_r1, hppa_sp, HPPA_CMP_COND_ULT, -3);
1630                                 hppa_ldo (code, 4, hppa_r1, hppa_r1);
1631                         }
1632                         break;
1633                 }
1634                 
1635                 case OP_THROW:
1636                         hppa_copy (code, ins->sreg1, hppa_r26);
1637                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
1638                                              (gpointer)"mono_arch_throw_exception");
1639                         hppa_ldil (code, 0, hppa_r1); 
1640                         hppa_ldo (code, 0, hppa_r1, hppa_r1);
1641                         hppa_ble (code, 0, hppa_r1);
1642                         hppa_copy (code, hppa_r31, hppa_r2);
1643                         /* should never return */
1644                         *code++ = 0xffeeddcc;
1645                         break;
1646                 case OP_RETHROW:
1647                         hppa_copy (code, ins->sreg1, hppa_r26);
1648                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
1649                                              (gpointer)"mono_arch_rethrow_exception");
1650                         hppa_ldil (code, 0, hppa_r1); 
1651                         hppa_ldo (code, 0, hppa_r1, hppa_r1);
1652                         hppa_ble (code, 0, hppa_r1);
1653                         hppa_copy (code, hppa_r31, hppa_r2);
1654                         /* should never return */
1655                         *code++ = 0xffeeddcc;
1656                         break;
1657                 case OP_START_HANDLER:
1658                         if (hppa_check_bits (ins->inst_left->inst_offset, 14))
1659                                 hppa_stw (code, hppa_r2, ins->inst_left->inst_offset, ins->inst_left->inst_basereg);
1660                         else {
1661                                 hppa_set (code, ins->inst_left->inst_offset, hppa_r1);
1662                                 hppa_addl (code, ins->inst_left->inst_basereg, hppa_r1, hppa_r1);
1663                                 hppa_stw (code, hppa_r2, 0, hppa_r1);
1664                         }
1665                         break;
1666                 case OP_ENDFILTER:
1667                         if (ins->sreg1 != hppa_r26)
1668                                 hppa_copy (code, ins->sreg1, hppa_r26);
1669                         if (hppa_check_bits (ins->inst_left->inst_offset, 14))
1670                                 hppa_ldw (code, ins->inst_left->inst_offset, ins->inst_left->inst_basereg, hppa_r2);
1671                         else {
1672                                 hppa_set (code, ins->inst_left->inst_offset, hppa_r1);
1673                                 hppa_ldwx (code, hppa_r1, ins->inst_left->inst_basereg, hppa_r2);
1674                         }
1675                         hppa_bv (code, hppa_r0, hppa_r2);
1676                         hppa_nop (code);
1677                         break;
1678                 case OP_ENDFINALLY:
1679                         if (hppa_check_bits (ins->inst_left->inst_offset, 14))
1680                                 hppa_ldw (code, ins->inst_left->inst_offset, ins->inst_left->inst_basereg, hppa_r1);
1681                         else {
1682                                 hppa_set (code, ins->inst_left->inst_offset, hppa_r1);
1683                                 hppa_ldwx (code, hppa_r1, ins->inst_left->inst_basereg, hppa_r1);
1684                         }
1685                         hppa_bv (code, hppa_r0, hppa_r1);
1686                         hppa_nop (code);
1687                         break;
1688                 case OP_CALL_HANDLER: 
1689                         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_BB, ins->inst_target_bb);
1690                         hppa_bl (code, 0, hppa_r2);
1691                         hppa_nop (code);
1692                         break;
1693                 case OP_LABEL:
1694                         ins->inst_c0 = (guint8*)code - cfg->native_code;
1695                         break;
1696                 case OP_BR: {
1697                         guint32 target;
1698                         if (ins->flags & MONO_INST_BRLABEL) {
1699                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_LABEL, ins->inst_i0);
1700                         } else {
1701                                 mono_add_patch_info (cfg, offset, MONO_PATCH_INFO_BB, ins->inst_target_bb);
1702                         }
1703                         hppa_bl (code, 8, hppa_r0); 
1704                         /* TODO: if the branch is too long, we may need to
1705                          * use a long-branch sequence:
1706                          *      hppa_ldil (code, 0, hppa_r1); 
1707                          *      hppa_ldo (code, 0, hppa_r1, hppa_r1); 
1708                          *      hppa_bv (code, hppa_r0, hppa_r1);
1709                          */
1710                         hppa_nop (code);
1711                         break;
1712                 }
1713                 case OP_BR_REG:
1714                         hppa_bv (code, hppa_r0, ins->sreg1);
1715                         hppa_nop(code);
1716                         break;
1717
1718                 case OP_SWITCH: {
1719                         int i;
1720
1721                         max_len += 8 * GPOINTER_TO_INT (ins->klass);
1722                         if (offset > (cfg->code_size - max_len - 16)) {
1723                                 cfg->code_size += max_len;
1724                                 cfg->code_size *= 2;
1725                                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
1726                                 code = cfg->native_code + offset;
1727                                 code_start = (guint8*)code;
1728                         }
1729                         hppa_blr (code, ins->sreg1, hppa_r0);
1730                         hppa_nop (code);
1731                         for (i = 0; i < GPOINTER_TO_INT (ins->klass); ++i) {
1732                                 *code++ = 0xdeadbeef;
1733                                 *code++ = 0xdeadbeef;
1734                         }
1735                         break;
1736                 }
1737
1738                 /* comclr is cool :-) */
1739                 case OP_HPPA_CEQ:
1740                         hppa_comclr_cond (code, HPPA_SUB_COND_NE, ins->sreg1, ins->sreg2, ins->dreg);
1741                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1742                         break;
1743
1744                 case OP_HPPA_CLT:
1745                         hppa_comclr_cond (code, HPPA_SUB_COND_SGE, ins->sreg1, ins->sreg2, ins->dreg);
1746                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1747                         break;
1748
1749                 case OP_HPPA_CLT_UN:
1750                         hppa_comclr_cond (code, HPPA_SUB_COND_UGE, ins->sreg1, ins->sreg2, ins->dreg);
1751                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1752                         break;
1753
1754                 case OP_HPPA_CGT:
1755                         hppa_comclr_cond (code, HPPA_SUB_COND_SLE, ins->sreg1, ins->sreg2, ins->dreg);
1756                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1757                         break;
1758
1759                 case OP_HPPA_CGT_UN:
1760                         hppa_comclr_cond (code, HPPA_SUB_COND_ULE, ins->sreg1, ins->sreg2, ins->dreg);
1761                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1762                         break;
1763
1764                 case OP_CEQ:
1765                 case OP_CLT:
1766                 case OP_CLT_UN:
1767                 case OP_CGT:
1768                 case OP_CGT_UN:
1769                 case OP_COND_EXC_EQ:
1770                 case OP_COND_EXC_NE_UN:
1771                 case OP_COND_EXC_LT:
1772                 case OP_COND_EXC_LT_UN:
1773                 case OP_COND_EXC_GT:
1774                 case OP_COND_EXC_GT_UN:
1775                 case OP_COND_EXC_GE:
1776                 case OP_COND_EXC_GE_UN:
1777                 case OP_COND_EXC_LE:
1778                 case OP_COND_EXC_LE_UN:
1779                 case OP_COND_EXC_OV:
1780                 case OP_COND_EXC_NO:
1781                 case OP_COND_EXC_C:
1782                 case OP_COND_EXC_NC:
1783                 case OP_COND_EXC_IOV:
1784                 case OP_COND_EXC_IC:
1785                 case CEE_BEQ:
1786                 case CEE_BNE_UN:
1787                 case CEE_BLT:
1788                 case CEE_BLT_UN:
1789                 case CEE_BGT:
1790                 case CEE_BGT_UN:
1791                 case CEE_BGE:
1792                 case CEE_BGE_UN:
1793                 case CEE_BLE:
1794                 case CEE_BLE_UN:
1795                 case OP_COMPARE:
1796                 case OP_LCOMPARE:
1797                 case OP_ICOMPARE:
1798                 case OP_COMPARE_IMM:
1799                 case OP_ICOMPARE_IMM:
1800                         g_warning ("got opcode %s in %s(), should be reduced\n", mono_inst_name (ins->opcode), __FUNCTION__);
1801                         g_assert_not_reached ();
1802                         break;
1803
1804                 case OP_HPPA_BEQ:
1805                 case OP_HPPA_BNE:
1806                 case OP_HPPA_BLT:
1807                 case OP_HPPA_BLT_UN:
1808                 case OP_HPPA_BGT:
1809                 case OP_HPPA_BGT_UN:
1810                 case OP_HPPA_BGE:
1811                 case OP_HPPA_BGE_UN:
1812                 case OP_HPPA_BLE:
1813                 case OP_HPPA_BLE_UN:
1814                         EMIT_COND_BRANCH (ins, ins->sreg1, ins->sreg2, ins->opcode - OP_HPPA_BEQ);
1815                         break;
1816
1817                 case OP_HPPA_COND_EXC_EQ:
1818                 case OP_HPPA_COND_EXC_GE:
1819                 case OP_HPPA_COND_EXC_GT:
1820                 case OP_HPPA_COND_EXC_LE:
1821                 case OP_HPPA_COND_EXC_LT:
1822                 case OP_HPPA_COND_EXC_NE_UN:
1823                 case OP_HPPA_COND_EXC_GE_UN:
1824                 case OP_HPPA_COND_EXC_GT_UN:
1825                 case OP_HPPA_COND_EXC_LE_UN:
1826                 case OP_HPPA_COND_EXC_LT_UN: 
1827                         EMIT_COND_SYSTEM_EXCEPTION (ins->sreg1, ins->sreg2, ins->opcode - OP_HPPA_COND_EXC_EQ, ins->inst_p1);
1828                         break;
1829
1830                 case OP_HPPA_COND_EXC_OV:
1831                 case OP_HPPA_COND_EXC_NO:
1832                 case OP_HPPA_COND_EXC_C:
1833                 case OP_HPPA_COND_EXC_NC: 
1834                         NOT_IMPLEMENTED;
1835
1836                 /* floating point opcodes */
1837                 case OP_R8CONST:
1838                         hppa_set (code, (unsigned int)ins->inst_p0, hppa_r1);
1839                         hppa_flddx (code, hppa_r0, hppa_r1, ins->dreg);
1840                         break;
1841                 case OP_R4CONST:
1842                         hppa_set (code, (unsigned int)ins->inst_p0, hppa_r1);
1843                         hppa_fldwx (code, hppa_r0, hppa_r1, hppa_fr31, 0);
1844                         hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, hppa_fr31, ins->dreg);
1845                         break;
1846                 case OP_STORER8_MEMBASE_REG:
1847                         hppa_set (code, ins->inst_offset, hppa_r1);
1848                         hppa_fstdx (code, ins->sreg1, hppa_r1, ins->inst_destbasereg);
1849                         break;
1850                 case OP_LOADR8_MEMBASE:
1851                         hppa_set (code, ins->inst_offset, hppa_r1);
1852                         hppa_flddx (code, hppa_r1, ins->inst_basereg, ins->dreg);
1853                         break;
1854                 case OP_STORER4_MEMBASE_REG:
1855                         hppa_fcnvff (code, HPPA_FP_FMT_DBL, HPPA_FP_FMT_SGL, ins->sreg1, hppa_fr31);
1856                         if (hppa_check_bits (ins->inst_offset, 5)) {
1857                                 hppa_fstws (code, hppa_fr31, 0, ins->inst_offset, ins->inst_destbasereg);
1858                         } else {
1859                                 hppa_set (code, ins->inst_offset, hppa_r1);
1860                                 hppa_fstwx (code, hppa_fr31, 0, hppa_r1, ins->inst_destbasereg);
1861                         }
1862                         break;
1863                 case OP_HPPA_STORER4_LEFT:
1864                 case OP_HPPA_STORER4_RIGHT:
1865                         if (hppa_check_bits (ins->inst_offset, 5)) {
1866                                 hppa_fstws (code, ins->sreg1, (ins->opcode == OP_HPPA_STORER4_RIGHT), ins->inst_offset, ins->inst_destbasereg);
1867                         } else {
1868                                 hppa_set (code, ins->inst_offset, hppa_r1);
1869                                 hppa_fstwx (code, ins->sreg1, (ins->opcode == OP_HPPA_STORER4_RIGHT), hppa_r1, ins->inst_destbasereg);
1870                         }
1871                         break;
1872                 case OP_LOADR4_MEMBASE:
1873                         if (hppa_check_bits (ins->inst_offset, 5)) {
1874                                 hppa_fldws (code, ins->inst_offset, ins->inst_basereg, hppa_fr31, 0);
1875                         } else {
1876                                 hppa_set (code, ins->inst_offset, hppa_r1);
1877                                 hppa_fldwx (code, hppa_r1, ins->inst_basereg, hppa_fr31, 0);
1878                         }
1879                         hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, hppa_fr31, ins->dreg);
1880                         break;
1881                 case OP_HPPA_LOADR4_LEFT:
1882                 case OP_HPPA_LOADR4_RIGHT:
1883                         if (hppa_check_bits (ins->inst_offset, 5)) {
1884                                 hppa_fldws (code, ins->inst_offset, ins->inst_basereg, ins->dreg, (ins->opcode == OP_HPPA_LOADR4_RIGHT));
1885                         } else {
1886                                 hppa_set (code, ins->inst_offset, hppa_r1);
1887                                 hppa_fldwx (code, hppa_r1, ins->inst_basereg, ins->dreg, (ins->opcode == OP_HPPA_LOADR4_RIGHT));
1888                         }
1889                         break;
1890                 
1891                 case CEE_CONV_R4:
1892                         hppa_stw (code, ins->sreg1, -16, hppa_sp);
1893                         hppa_fldws (code, -16, hppa_sp, hppa_fr31, 0);
1894                         hppa_fcnvxf (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_SGL, hppa_fr31, ins->dreg);
1895                         hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, ins->dreg, ins->dreg);
1896                         break;
1897
1898                 case OP_FCONV_TO_R4:
1899                         /* reduce precision */
1900                         hppa_fcnvff (code, HPPA_FP_FMT_DBL, HPPA_FP_FMT_SGL, ins->sreg1, ins->dreg);
1901                         hppa_fcnvff (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, ins->dreg, ins->dreg);
1902                         break;
1903
1904                 case OP_HPPA_SETF4REG:
1905                         hppa_fcnvff (code, HPPA_FP_FMT_DBL, HPPA_FP_FMT_SGL, ins->sreg1, ins->dreg);
1906                         break;
1907                 case CEE_CONV_R8: 
1908                         hppa_stw (code, ins->sreg1, -16, hppa_sp);
1909                         hppa_fldws (code, -16, hppa_sp, hppa_fr31, 0);
1910                         hppa_fcnvxf (code, HPPA_FP_FMT_SGL, HPPA_FP_FMT_DBL, hppa_fr31, ins->dreg);
1911                         break;
1912
1913                 case OP_FCONV_TO_I1:
1914                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, TRUE);
1915                         break;
1916                 case OP_FCONV_TO_U1:
1917                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 1, FALSE);
1918                         break;
1919                 case OP_FCONV_TO_I2:
1920                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, TRUE);
1921                         break;
1922                 case OP_FCONV_TO_U2:
1923                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 2, FALSE);
1924                         break;
1925                 case OP_FCONV_TO_I4:
1926                 case OP_FCONV_TO_I:
1927                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, TRUE);
1928                         break;
1929                 case OP_FCONV_TO_U4:
1930                 case OP_FCONV_TO_U:
1931                         code = emit_float_to_int (cfg, code, ins->dreg, ins->sreg1, 4, FALSE);
1932                         break;
1933
1934                 case OP_FCONV_TO_I8:
1935                 case OP_FCONV_TO_U8:
1936                         g_assert_not_reached ();
1937                         /* Implemented as helper calls */
1938                         break;
1939                 case OP_LCONV_TO_R_UN:
1940                         g_assert_not_reached ();
1941                         /* Implemented as helper calls */
1942                         break;
1943
1944                 case OP_LCONV_TO_OVF_I: 
1945                         NOT_IMPLEMENTED;
1946                         break;
1947
1948                 case OP_FADD:
1949                         hppa_fadd (code, HPPA_FP_FMT_DBL, ins->sreg1, ins->sreg2, ins->dreg);
1950                         break;
1951                 case OP_FSUB:
1952                         hppa_fsub (code, HPPA_FP_FMT_DBL, ins->sreg1, ins->sreg2, ins->dreg);
1953                         break;          
1954                 case OP_FMUL:
1955                         hppa_fmul (code, HPPA_FP_FMT_DBL, ins->sreg1, ins->sreg2, ins->dreg);
1956                         break;
1957                 case OP_FDIV:
1958                         hppa_fdiv (code, HPPA_FP_FMT_DBL, ins->sreg1, ins->sreg2, ins->dreg);
1959                         break;
1960                 case OP_FREM:
1961                         NOT_IMPLEMENTED;
1962                         break;          
1963
1964                 case OP_FCOMPARE:
1965                         g_assert_not_reached();
1966                         break;
1967
1968                 case OP_FCEQ:
1969                 case OP_FCLT:
1970                 case OP_FCLT_UN:
1971                 case OP_FCGT:
1972                 case OP_FCGT_UN:
1973                         hppa_fcmp (code, HPPA_FP_FMT_DBL, float_ceq_table [ins->opcode - OP_FCEQ], ins->sreg1, ins->sreg2);
1974                         hppa_ftest (code, 0);
1975                         hppa_bl (code, 12, hppa_r0);
1976                         hppa_ldo (code, 1, hppa_r0, ins->dreg);
1977                         hppa_ldo (code, 0, hppa_r0, ins->dreg);
1978                         break;
1979
1980                 case OP_FBEQ:
1981                 case OP_FBLT:
1982                 case OP_FBGT:
1983                 case OP_FBGE:
1984                 case OP_FBLE:
1985                 case OP_FBNE_UN:
1986                 case OP_FBLT_UN:
1987                 case OP_FBGT_UN:
1988                 case OP_FBGE_UN:
1989                 case OP_FBLE_UN:
1990                         EMIT_FLOAT_COND_BRANCH (ins, ins->sreg1, ins->sreg2, ins->opcode - OP_FBEQ);
1991                         break;
1992
1993                 case OP_CKFINITE: 
1994                 case OP_MEMORY_BARRIER:
1995                         break;
1996
1997                 case OP_HPPA_XMPYU:
1998                         hppa_xmpyu (code, ins->sreg1, ins->sreg2, ins->dreg);
1999                         break;
2000
2001                 default:
2002                         g_warning ("unknown opcode %s in %s()\n", mono_inst_name (ins->opcode), __FUNCTION__);
2003                         g_assert_not_reached ();
2004                 }
2005
2006                 if ((((guint8*)code) - code_start) > max_len) {
2007                         g_warning ("wrong maximal instruction length of instruction %s (expected %d, got %d)",
2008                                    mono_inst_name (ins->opcode), max_len, ((guint8*)code) - code_start);
2009                         g_assert_not_reached ();
2010                 }
2011                
2012                 cpos += max_len;
2013         }
2014
2015         cfg->code_len = (guint8*)code - cfg->native_code;
2016         DEBUG_FUNC_EXIT();
2017 }
2018
2019 void
2020 mono_arch_register_lowlevel_calls (void)
2021 {
2022 }
2023
2024 void
2025 mono_arch_patch_code (MonoMethod *method, MonoDomain *domain, guint8 *code, MonoJumpInfo *ji, gboolean run_cctors)
2026 {
2027         MonoJumpInfo *patch_info;
2028
2029         DEBUG_FUNC_ENTER();
2030         /* FIXME: Move part of this to arch independent code */
2031         for (patch_info = ji; patch_info; patch_info = patch_info->next) {
2032                 unsigned char *ip = patch_info->ip.i + code;
2033                 gpointer target;
2034
2035                 target = mono_resolve_patch_target (method, domain, code, patch_info, run_cctors);
2036                 DEBUG (printf ("patch_info->type = %d, target = %p\n", patch_info->type, target));
2037
2038                 switch (patch_info->type) {
2039                 case MONO_PATCH_INFO_NONE:
2040                 case MONO_PATCH_INFO_BB_OVF:
2041                 case MONO_PATCH_INFO_EXC_OVF:
2042                         continue;
2043
2044                 case MONO_PATCH_INFO_IP:
2045                         hppa_patch ((guint32 *)ip, ip);
2046                         continue;
2047
2048                 case MONO_PATCH_INFO_CLASS_INIT: {
2049                         break;
2050                 }
2051                 case MONO_PATCH_INFO_METHOD_JUMP: {
2052                         break;
2053                 }
2054                 case MONO_PATCH_INFO_SWITCH: {
2055                         int i;
2056                         gpointer *table = (gpointer *)target;
2057                         ip += 8;
2058                         for (i = 0; i < patch_info->data.table->table_size; i++) { 
2059                                 DEBUG (printf ("Patching switch table, table[%d] = %p\n", i, table[i]));
2060                                 hppa_ldil (ip, hppa_lsel (table [i]), hppa_r1);
2061                                 hppa_be_n (ip, hppa_rsel (table [i]), hppa_r1);
2062                         }
2063                         continue;
2064                 }
2065                 default:
2066                         break;
2067                 }
2068                 hppa_patch ((guint32 *)ip, target);
2069         }
2070
2071         DEBUG_FUNC_EXIT();
2072 }
2073
2074 void*
2075 mono_arch_instrument_prolog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2076 {
2077         guint32 *code = (guint32*)p;
2078
2079         DEBUG_FUNC_ENTER();
2080
2081         hppa_set (code, cfg->method, hppa_r26);
2082         hppa_copy (code, hppa_r0, hppa_r25); /* NULL sp for now */
2083         hppa_set (code, func, hppa_r1);
2084         hppa_depi (code, 0, 31, 2, hppa_r1);
2085         hppa_ldw (code, 0, hppa_r1, hppa_r1);
2086         hppa_ble (code, 0, hppa_r1);
2087         hppa_copy (code, hppa_r31, hppa_r2);
2088
2089         DEBUG_FUNC_EXIT();
2090         return code;
2091 }
2092
2093 enum {
2094         SAVE_NONE,
2095         SAVE_STRUCT,
2096         SAVE_ONE,
2097         SAVE_TWO,
2098         SAVE_FP
2099 };
2100
2101 void*
2102 mono_arch_instrument_epilog (MonoCompile *cfg, void *func, void *p, gboolean enable_arguments)
2103 {
2104         guint32 *code = (guint32*)p;
2105         DEBUG_FUNC_ENTER();
2106 #if 0
2107         int save_mode = SAVE_NONE;
2108         MonoMethod *method = cfg->method;
2109
2110         switch (mono_type_get_underlying_type (mono_method_signature (method)->ret)->type) {
2111         case MONO_TYPE_VOID:
2112                 /* special case string .ctor icall */
2113                 if (strcmp (".ctor", method->name) && method->klass == mono_defaults.string_class)
2114                         save_mode = SAVE_ONE;
2115                 else
2116                         save_mode = SAVE_NONE;
2117                 break;
2118         case MONO_TYPE_I8:
2119         case MONO_TYPE_U8:
2120 #ifdef SPARCV9
2121                 save_mode = SAVE_ONE;
2122 #else
2123                 save_mode = SAVE_TWO;
2124 #endif
2125                 break;
2126         case MONO_TYPE_R4:
2127         case MONO_TYPE_R8:
2128                 save_mode = SAVE_FP;
2129                 break;
2130         case MONO_TYPE_VALUETYPE:
2131                 save_mode = SAVE_STRUCT;
2132                 break;
2133         default:
2134                 save_mode = SAVE_ONE;
2135                 break;
2136         }
2137
2138         /* Save the result to the stack and also put it into the output registers */
2139
2140         switch (save_mode) {
2141         case SAVE_TWO:
2142                 /* V8 only */
2143                 sparc_st_imm (code, sparc_i0, sparc_fp, 68);
2144                 sparc_st_imm (code, sparc_i0, sparc_fp, 72);
2145                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
2146                 sparc_mov_reg_reg (code, sparc_i1, sparc_o2);
2147                 break;
2148         case SAVE_ONE:
2149                 sparc_sti_imm (code, sparc_i0, sparc_fp, ARGS_OFFSET);
2150                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
2151                 break;
2152         case SAVE_FP:
2153 #ifdef SPARCV9
2154                 sparc_stdf_imm (code, sparc_f0, sparc_fp, ARGS_OFFSET);
2155 #else
2156                 sparc_stdf_imm (code, sparc_f0, sparc_fp, 72);
2157                 sparc_ld_imm (code, sparc_fp, 72, sparc_o1);
2158                 sparc_ld_imm (code, sparc_fp, 72 + 4, sparc_o2);
2159 #endif
2160                 break;
2161         case SAVE_STRUCT:
2162 #ifdef SPARCV9
2163                 sparc_mov_reg_reg (code, sparc_i0, sparc_o1);
2164 #else
2165                 sparc_ld_imm (code, sparc_fp, 64, sparc_o1);
2166 #endif
2167                 break;
2168         case SAVE_NONE:
2169         default:
2170                 break;
2171         }
2172
2173         sparc_set (code, cfg->method, sparc_o0);
2174
2175         mono_add_patch_info (cfg, (guint8*)code - cfg->native_code, MONO_PATCH_INFO_ABS, func);
2176         EMIT_CALL ();
2177
2178         /* Restore result */
2179
2180         switch (save_mode) {
2181         case SAVE_TWO:
2182                 sparc_ld_imm (code, sparc_fp, 68, sparc_i0);
2183                 sparc_ld_imm (code, sparc_fp, 72, sparc_i0);
2184                 break;
2185         case SAVE_ONE:
2186                 sparc_ldi_imm (code, sparc_fp, ARGS_OFFSET, sparc_i0);
2187                 break;
2188         case SAVE_FP:
2189                 sparc_lddf_imm (code, sparc_fp, ARGS_OFFSET, sparc_f0);
2190                 break;
2191         case SAVE_NONE:
2192         default:
2193                 break;
2194         }
2195 #endif
2196         DEBUG_FUNC_EXIT();
2197         return code;
2198 }
2199
2200 /* 
2201  * The HPPA stack frame should look like this:
2202  *
2203  * ---------------------
2204  *  incoming params area
2205  * ---------------------
2206  *     linkage area             size = ARGS_OFFSET
2207  * ---------------------        fp = psp
2208  * HPPA_STACK_LMF_OFFSET
2209  * ---------------------
2210  * MonoLMF structure or saved registers
2211  * -------------------
2212  *        locals                size = cfg->stack_offset - cfg->param_area
2213  * ---------------------
2214  *      params area             size = cfg->param_area - ARGS_OFFSET  (aligned)
2215  * ---------------------
2216  *  callee linkage area         size = ARGS_OFFSET
2217  * --------------------- sp
2218  */
2219 guint8 *
2220 mono_arch_emit_prolog (MonoCompile *cfg)
2221 {
2222         MonoMethod *method = cfg->method;
2223         MonoBasicBlock *bb;
2224         MonoMethodSignature *sig;
2225         MonoInst *inst;
2226         int alloc_size, pos, max_offset, i;
2227         guint8 *code;
2228         CallInfo *cinfo;
2229         int tracing = 0;
2230         int lmf_offset = 0;
2231
2232         DEBUG_FUNC_ENTER();
2233         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2234                 tracing = 1;
2235
2236         sig = mono_method_signature (method);
2237         cfg->code_size = 512 + sig->param_count * 20;
2238         code = cfg->native_code = g_malloc (cfg->code_size);
2239
2240         /* TODO: enable tail call optimization */
2241         if (1 || cfg->flags & MONO_CFG_HAS_CALLS) {
2242                 hppa_stw (code, hppa_r2, -20, hppa_sp);
2243         }
2244
2245         /* locals area */
2246         pos = HPPA_STACK_LMF_OFFSET;
2247
2248         /* figure out how much space we need for spilling */
2249         if (!method->save_lmf) {
2250                 /* spill callee-save registers */
2251                 guint32 mask = cfg->used_int_regs & MONO_ARCH_CALLEE_SAVED_REGS;
2252                 for (i = 0; i < 32; i++) {
2253                         if ((1 << i) & mask)
2254                                 pos += sizeof (gulong);
2255                 }
2256         } else {
2257                 lmf_offset = pos;
2258                 pos += sizeof (MonoLMF);
2259         }
2260
2261         alloc_size = ALIGN_TO (pos + cfg->stack_offset, MONO_ARCH_FRAME_ALIGNMENT);
2262         g_assert ((alloc_size & (MONO_ARCH_FRAME_ALIGNMENT - 1)) == 0);
2263
2264         cfg->stack_usage = alloc_size;
2265
2266         if (alloc_size) {
2267                 hppa_copy (code, hppa_r3, hppa_r1);
2268                 hppa_copy (code, hppa_sp, hppa_r3);
2269                 if (hppa_check_bits (alloc_size, 14))
2270                         hppa_stwm (code, hppa_r1, alloc_size, hppa_sp);
2271                 else {
2272                         hppa_stwm (code, hppa_r1, 8100, hppa_sp);
2273                         hppa_addil (code, hppa_lsel (alloc_size - 8100), hppa_sp);
2274                         hppa_ldo (code, hppa_rsel (alloc_size - 8100), hppa_r1, hppa_sp);
2275                 }
2276         }
2277
2278         /* compute max_offset in order to use short forward jumps
2279          * we always do it on hppa because the immediate displacement
2280          * for jumps is small 
2281          */
2282         max_offset = 0;
2283         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
2284                 MonoInst *ins;
2285                 bb->max_offset = max_offset;
2286
2287                 if (cfg->prof_options & MONO_PROFILE_COVERAGE)
2288                         max_offset += 6; 
2289
2290                 MONO_BB_FOR_EACH_INS (bb, ins) {
2291                         max_offset += ((guint8 *)ins_get_spec (ins->opcode))[MONO_INST_LEN];
2292         }
2293
2294         DEBUG (printf ("Incoming arguments: \n"));
2295         cinfo = get_call_info (sig, sig->pinvoke);
2296
2297         /* We do this first so that we don't have to worry about the LMF-
2298          * saving code clobbering r28
2299          */
2300         if (cinfo->struct_return)
2301                 hppa_stw (code, hppa_r28, cfg->ret->inst_offset, hppa_sp);
2302
2303         /* Save the LMF or the spilled registers */
2304         pos = HPPA_STACK_LMF_OFFSET;
2305         if (!method->save_lmf) {
2306                 /* spill callee-save registers */
2307                 guint32 mask = cfg->used_int_regs & MONO_ARCH_CALLEE_SAVED_REGS;
2308                 for (i = 0; i < 32; i++) {
2309                         if ((1 << i) & mask) {
2310                                 if (i == hppa_r3) {
2311                                         hppa_ldw (code, 0, hppa_r3, hppa_r1);
2312                                         hppa_stw (code, hppa_r1, pos, hppa_r3);
2313                                 } else
2314                                         hppa_stw (code, i, pos, hppa_r3);
2315                                 pos += sizeof (gulong);
2316                         }
2317                 }
2318         } else {
2319                 int ofs = lmf_offset + G_STRUCT_OFFSET (MonoLMF, regs);
2320                 int reg;
2321
2322                 hppa_ldw (code, 0, hppa_r3, hppa_r1);
2323                 hppa_stw (code, hppa_r1, ofs, hppa_r3);
2324                 ofs += sizeof (gulong);
2325                 for (reg = 4; reg < 32; reg++) {
2326                         if (HPPA_IS_SAVED_GREG (reg)) {
2327                                 hppa_stw (code, reg, ofs, hppa_r3);
2328                                 ofs += sizeof (gulong);
2329                         }
2330                 }
2331                 /* We shouldn't need to save the FP regs.... */
2332                 ofs = ALIGN_TO (ofs, sizeof(double));
2333                 hppa_set (code, ofs, hppa_r1);
2334                 for (reg = 0; reg < 32; reg++) {
2335                         if (HPPA_IS_SAVED_FREG (reg)) {
2336                                 hppa_fstdx (code, reg, hppa_r1, hppa_r3);
2337                                 hppa_ldo (code, sizeof(double), hppa_r1, hppa_r1);
2338                         }
2339                 }
2340
2341                 /* We also spill the arguments onto the stack, because
2342                  * the call to hppa_get_lmf_addr below can clobber them
2343                  *
2344                  * This goes in the param area that is always allocated
2345                  */
2346                 ofs = -36;
2347                 for (reg = hppa_r26; reg >= hppa_r23; reg--) {
2348                         hppa_stw (code, reg, ofs, hppa_sp);
2349                         ofs -= 4;
2350                 }
2351         }
2352
2353         if (cfg->flags & MONO_CFG_HAS_ALLOCA)
2354                 hppa_copy (code, hppa_r30, hppa_r4);
2355
2356         if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
2357                 hppa_set (code, cfg->domain, hppa_r26);
2358                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, (gpointer)"mono_jit_thread_attach");
2359                 hppa_ldil (code, 0, hppa_r1);
2360                 hppa_ldo (code, 0, hppa_r1, hppa_r1);
2361                 hppa_depi (code, 0, 31, 2, hppa_r1);
2362                 hppa_ldw (code, 0, hppa_r1, hppa_r1);
2363                 hppa_ble (code, 0, hppa_r1);
2364                 hppa_copy (code, hppa_r31, hppa_r2);
2365         }
2366
2367         if (method->save_lmf) {
2368                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_INTERNAL_METHOD, 
2369                                      (gpointer)"mono_get_lmf_addr");
2370                 hppa_ldil (code, 0, hppa_r1);
2371                 hppa_ldo (code, 0, hppa_r1, hppa_r1);
2372                 hppa_depi (code, 0, 31, 2, hppa_r1);
2373                 hppa_ldw (code, 0, hppa_r1, hppa_r1);
2374                 hppa_ble (code, 0, hppa_r1);
2375                 hppa_copy (code, hppa_r31, hppa_r2);
2376
2377                 /* lmf_offset is the offset from the previous stack pointer,
2378                  * The pointer to the struct is put in hppa_r22 (new_lmf).
2379                  * The callee-saved registers are already in the MonoLMF 
2380                  * structure
2381                  */
2382
2383                 /* hppa_r22 = new_lmf (on the stack) */
2384                 hppa_ldo (code, lmf_offset, hppa_r3, hppa_r22);
2385                 /* lmf_offset is the offset from the previous stack pointer,
2386                  */
2387                 hppa_stw (code, hppa_r28, G_STRUCT_OFFSET(MonoLMF, lmf_addr), hppa_r22);
2388                 /* new_lmf->previous_lmf = *lmf_addr */
2389                 hppa_ldw (code, 0, hppa_r28, hppa_r1);
2390                 hppa_stw (code, hppa_r1, G_STRUCT_OFFSET(MonoLMF, previous_lmf), hppa_r22);
2391                 /* *(lmf_addr) = r22 */
2392                 hppa_stw (code, hppa_r22, 0, hppa_r28);
2393                 hppa_set (code, method, hppa_r1);
2394                 hppa_stw (code, hppa_r1, G_STRUCT_OFFSET(MonoLMF, method), hppa_r22);
2395                 hppa_stw (code, hppa_sp, G_STRUCT_OFFSET(MonoLMF, ebp), hppa_r22);
2396                 mono_add_patch_info (cfg, code - cfg->native_code, MONO_PATCH_INFO_IP, NULL);
2397                 hppa_ldil (code, 0, hppa_r1);
2398                 hppa_ldo (code, 0, hppa_r1, hppa_r1);
2399                 hppa_stw (code, hppa_r1, G_STRUCT_OFFSET(MonoLMF, eip), hppa_r22);
2400
2401                 /* Now reload the arguments from the stack */
2402                 hppa_ldw (code, -36, hppa_sp, hppa_r26);
2403                 hppa_ldw (code, -40, hppa_sp, hppa_r25);
2404                 hppa_ldw (code, -44, hppa_sp, hppa_r24);
2405                 hppa_ldw (code, -48, hppa_sp, hppa_r23);
2406         }
2407
2408         /* load arguments allocated to register from the stack */
2409         pos = 0;
2410
2411         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
2412                 ArgInfo *ainfo = cinfo->args + i;
2413                 inst = cfg->args [pos];
2414
2415                 if (inst->opcode == OP_REGVAR) {
2416                         /* Want the argument in a register */
2417                         switch (ainfo->storage) {
2418                         case ArgInIReg:
2419                                 if (ainfo->reg != inst->dreg)
2420                                         hppa_copy (code, ainfo->reg, inst->dreg);
2421                                 DEBUG (printf ("Argument %d assigned to register %s\n", pos, mono_arch_regname (inst->dreg)));
2422                                 break;
2423
2424                         case ArgInIRegPair:
2425                                 if (ainfo->reg != inst->dreg) {
2426                                         hppa_copy (code, ainfo->reg, inst->dreg);
2427                                         hppa_copy (code, ainfo->reg + 1, inst->dreg + 1);
2428                                 }
2429                                 DEBUG (printf ("Argument %d assigned to register %s, %s\n", pos, mono_arch_regname (inst->dreg), mono_arch_regname (inst->dreg + 1)));
2430                                 break;
2431
2432                         case ArgInFReg:
2433                                 if (ainfo->reg != inst->dreg)
2434                                         hppa_fcpy (code, HPPA_FP_FMT_SGL, ainfo->reg, inst->dreg);
2435                                 DEBUG (printf ("Argument %d assigned to single register %s\n", pos, mono_arch_fregname (inst->dreg)));
2436                                 break;
2437
2438                         case ArgInDReg:
2439                                 if (ainfo->reg != inst->dreg)
2440                                         hppa_fcpy (code, HPPA_FP_FMT_DBL, ainfo->reg, inst->dreg);
2441                                 DEBUG (printf ("Argument %d assigned to double register %s\n", pos, mono_arch_fregname (inst->dreg)));
2442                                 break;
2443
2444                         case ArgOnStack:
2445                                 switch (ainfo->size) {
2446                                 case 1:
2447                                         hppa_ldb (code, ainfo->offset, hppa_r3, inst->dreg);
2448                                         break;
2449                                 case 2:
2450                                         hppa_ldh (code, ainfo->offset, hppa_r3, inst->dreg);
2451                                         break;
2452                                 case 4:
2453                                         hppa_ldw (code, ainfo->offset, hppa_r3, inst->dreg);
2454                                         break;
2455                                 default:
2456                                         g_assert_not_reached ();
2457                                 }
2458
2459                                 
2460                                 DEBUG (printf ("Argument %d loaded from the stack [%s - %d]\n", pos, mono_arch_regname (hppa_r3), -ainfo->offset));
2461                                 break;
2462
2463                         default:
2464                                 g_assert_not_reached ();
2465                         }
2466                 } 
2467                 else {
2468                         /* Want the argument on the stack */
2469                         switch (ainfo->storage)
2470                         {
2471                         case ArgInIReg: {
2472                                 int off, reg;
2473                                 DEBUG (printf ("Argument %d stored from register %s to stack [%s + %d]\n", pos, mono_arch_regname (ainfo->reg), mono_arch_regname (inst->inst_basereg), inst->inst_offset));
2474                                 if (hppa_check_bits (inst->inst_offset, 14)) {
2475                                         off = inst->inst_offset;
2476                                         reg = inst->inst_basereg;
2477                                 }
2478                                 else {
2479                                         hppa_set (code, inst->inst_offset, hppa_r1);
2480                                         hppa_add (code, hppa_r1, inst->inst_basereg, hppa_r1);
2481                                         off = 0;
2482                                         reg = hppa_r1;
2483                                 }
2484                                 switch (ainfo->size)
2485                                 {
2486                                 case 1:
2487                                         hppa_stb (code, ainfo->reg, off, reg);
2488                                         break;
2489                                 case 2:
2490                                         hppa_sth (code, ainfo->reg, off, reg);
2491                                         break;
2492                                 case 4:
2493                                         hppa_stw (code, ainfo->reg, off, reg);
2494                                         break;
2495                                 default:
2496                                         g_assert_not_reached ();
2497                                 }
2498                                 break;
2499                         }
2500                         case ArgInIRegPair:
2501                                 DEBUG (printf ("Argument %d stored from register (%s,%s) to stack [%s + %d]\n", pos, mono_arch_regname (ainfo->reg), mono_arch_regname (ainfo->reg+1), mono_arch_regname (inst->inst_basereg), inst->inst_offset));
2502                                 if (hppa_check_bits (inst->inst_offset + 4, 14)) {
2503                                         hppa_stw (code, ainfo->reg, inst->inst_offset, inst->inst_basereg);
2504                                         hppa_stw (code, ainfo->reg + 1, inst->inst_offset + 4, inst->inst_basereg);
2505                                 }
2506                                 else {
2507                                         hppa_ldo (code, inst->inst_offset, inst->inst_basereg, hppa_r1);
2508                                         hppa_stw (code, ainfo->reg, 0, hppa_r1);
2509                                         hppa_stw (code, ainfo->reg + 1, 4, hppa_r1);
2510                                 }
2511                                 break;
2512
2513                         case ArgInFReg:
2514                                 DEBUG (printf ("Argument %d (float) stored from register %s to stack [%s + %d]\n", pos, mono_arch_fregname (ainfo->reg), mono_arch_regname (inst->inst_basereg), inst->inst_offset));
2515                                 hppa_ldo (code, inst->inst_offset, inst->inst_basereg, hppa_r1);
2516                                 hppa_fstwx (code, ainfo->reg, 0, hppa_r0, hppa_r1);
2517                                 break;
2518
2519                         case ArgInDReg:
2520                                 DEBUG (printf ("Argument %d (double) stored from register %s to stack [%s + %d]\n", pos, mono_arch_fregname (ainfo->reg), mono_arch_regname (inst->inst_basereg), inst->inst_offset));
2521                                 hppa_ldo (code, inst->inst_offset, inst->inst_basereg, hppa_r1);
2522                                 hppa_fstdx (code, ainfo->reg, hppa_r0, hppa_r1);
2523                                 break;
2524
2525                         case ArgOnStack:
2526                                 DEBUG (printf ("Argument %d copied from [%s - %d] to [%s + %d] (size=%d)\n", pos, mono_arch_regname (hppa_r3), -ainfo->offset, mono_arch_regname (inst->inst_basereg), inst->inst_offset, ainfo->size));
2527                                 if (inst->inst_offset != ainfo->offset ||
2528                                     inst->inst_basereg != hppa_r3)
2529                                         code = emit_memcpy (code, inst->inst_offset, inst->inst_basereg, ainfo->offset, hppa_r3, ainfo->size);
2530                                 break;
2531
2532                         default:
2533                                 g_assert_not_reached ();
2534                         }
2535                 }
2536
2537                 pos++;
2538         }
2539
2540
2541         if (tracing)
2542                 code = mono_arch_instrument_prolog (cfg, mono_trace_enter_method, code, TRUE);
2543
2544         if (getenv("HPPA_BREAK")) {
2545                 *(guint32*)code = 0x00010004;
2546                 code += 4;
2547         }
2548
2549         cfg->code_len = code - cfg->native_code;
2550         g_assert (cfg->code_len < cfg->code_size);
2551         g_free (cinfo);
2552
2553         DEBUG_FUNC_EXIT();
2554         return code;
2555 }
2556
2557
2558 void
2559 mono_arch_emit_epilog (MonoCompile *cfg)
2560 {
2561         MonoMethod *method = cfg->method;
2562         MonoMethodSignature *sig;
2563         guint32 *code;
2564         int max_epilog_size = 16 + 20 * 4;
2565         int pos;
2566         
2567         DEBUG_FUNC_ENTER();
2568         sig = mono_method_signature (cfg->method);
2569         if (cfg->method->save_lmf)
2570                 max_epilog_size += 128;
2571         
2572         if (mono_jit_trace_calls != NULL)
2573                 max_epilog_size += 50;
2574
2575         if (cfg->prof_options & MONO_PROFILE_ENTER_LEAVE)
2576                 max_epilog_size += 50;
2577
2578         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
2579                 cfg->code_size *= 2;
2580                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2581                 mono_jit_stats.code_reallocs++;
2582         }
2583
2584         code = (guint32*)(cfg->native_code + cfg->code_len);
2585
2586         if (mono_jit_trace_calls != NULL && mono_trace_eval (method))
2587                 code = mono_arch_instrument_epilog (cfg, mono_trace_leave_method, code, TRUE);
2588
2589         pos = HPPA_STACK_LMF_OFFSET;
2590         if (cfg->method->save_lmf) {
2591                 int reg;
2592                 hppa_ldo (code, pos, hppa_r3, hppa_r22);
2593                 hppa_ldw (code, G_STRUCT_OFFSET(MonoLMF, previous_lmf), hppa_r22, hppa_r21);
2594                 hppa_ldw (code, G_STRUCT_OFFSET(MonoLMF, lmf_addr), hppa_r22, hppa_r20);
2595                 hppa_stw (code, hppa_r21, G_STRUCT_OFFSET(MonoLMF, previous_lmf), hppa_r20);
2596
2597                 pos += G_STRUCT_OFFSET(MonoLMF, regs) + sizeof (gulong);
2598                 /* We skip the restore of r3 here, it is restored from the
2599                  * stack anyway. This makes the code a bit easier.
2600                  */
2601                 for (reg = 4; reg < 31; reg++) {
2602                         if (HPPA_IS_SAVED_GREG (reg)) {
2603                                 hppa_ldw (code, pos, hppa_r3, reg);
2604                                 pos += sizeof(gulong);
2605                         }
2606                 }
2607                 
2608                 pos = ALIGN_TO (pos, sizeof (double));
2609                 hppa_set (code, pos, hppa_r1);
2610                 for (reg = 0; reg < 31; reg++) {
2611                         if (HPPA_IS_SAVED_FREG (reg)) {
2612                                 hppa_flddx (code, hppa_r1, hppa_r3, reg);
2613                                 hppa_ldo (code, sizeof (double), hppa_r1, hppa_r1);
2614                                 pos += sizeof (double);
2615                         }
2616                 }
2617         } else {
2618                 guint32 mask = cfg->used_int_regs & MONO_ARCH_CALLEE_SAVED_REGS;
2619                 int i;
2620                 for (i = 0; i < 32; i++) {
2621                         if (i == hppa_r3)
2622                                 continue;
2623                         if ((1 << i) & mask) {
2624                                 hppa_ldw (code, pos, hppa_r3, i);
2625                                 pos += sizeof (gulong);
2626                         }
2627                 }
2628         }
2629
2630         if (sig->ret->type != MONO_TYPE_VOID && 
2631             mono_type_to_stind (sig->ret) == CEE_STOBJ) {
2632                 CallInfo *cinfo = get_call_info (sig, sig->pinvoke);
2633
2634                 switch (cinfo->ret.storage) {
2635                 case ArgInIReg:
2636                         hppa_ldw (code, cfg->ret->inst_offset, hppa_sp, hppa_r28);
2637                         hppa_ldw (code, 0, hppa_r28, hppa_r28);
2638                         break;
2639                 case ArgInIRegPair:
2640                         hppa_ldw (code, cfg->ret->inst_offset, hppa_sp, hppa_r28);
2641                         hppa_ldw (code, 4, hppa_r28, hppa_r29);
2642                         hppa_ldw (code, 0, hppa_r28, hppa_r28);
2643                         break;
2644                 case ArgOnStack:
2645                         /* Nothing to do */
2646                         break;
2647                 default:
2648                         g_assert_not_reached ();
2649                 }
2650                 g_free (cinfo);
2651         }
2652
2653         if (1 || cfg->flags & MONO_CFG_HAS_CALLS)
2654                 hppa_ldw (code, -20, hppa_r3, hppa_r2);
2655         hppa_ldo (code, 64, hppa_r3, hppa_sp);
2656         hppa_bv (code, hppa_r0, hppa_r2);
2657         hppa_ldwm (code, -64, hppa_sp, hppa_r3);
2658
2659         cfg->code_len = (guint8*)code - cfg->native_code;
2660
2661         g_assert (cfg->code_len < cfg->code_size);
2662         DEBUG_FUNC_EXIT();
2663 }
2664
2665 /* remove once throw_exception_by_name is eliminated */
2666 static int
2667 exception_id_by_name (const char *name)
2668 {
2669         if (strcmp (name, "IndexOutOfRangeException") == 0)
2670                 return MONO_EXC_INDEX_OUT_OF_RANGE;
2671         if (strcmp (name, "OverflowException") == 0)
2672                 return MONO_EXC_OVERFLOW;
2673         if (strcmp (name, "ArithmeticException") == 0)
2674                 return MONO_EXC_ARITHMETIC;
2675         if (strcmp (name, "DivideByZeroException") == 0)
2676                 return MONO_EXC_DIVIDE_BY_ZERO;
2677         if (strcmp (name, "InvalidCastException") == 0)
2678                 return MONO_EXC_INVALID_CAST;
2679         if (strcmp (name, "NullReferenceException") == 0)
2680                 return MONO_EXC_NULL_REF;
2681         if (strcmp (name, "ArrayTypeMismatchException") == 0)
2682                 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2683         g_error ("Unknown intrinsic exception %s\n", name);
2684         return 0;
2685 }
2686
2687 void
2688 mono_arch_emit_exceptions (MonoCompile *cfg)
2689 {
2690         MonoJumpInfo *patch_info;
2691         int i;
2692         guint8 *code;
2693         const guint8* exc_throw_pos [MONO_EXC_INTRINS_NUM] = {NULL};
2694         guint8 exc_throw_found [MONO_EXC_INTRINS_NUM] = {0};
2695         int max_epilog_size = 50;
2696
2697         DEBUG_FUNC_ENTER();
2698
2699         /* count the number of exception infos */
2700      
2701         /* 
2702          * make sure we have enough space for exceptions
2703          */
2704         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2705                 switch (patch_info->type) {
2706                 case MONO_PATCH_INFO_BB_OVF:
2707                         g_assert_not_reached ();
2708                         break;
2709
2710                 case MONO_PATCH_INFO_EXC_OVF: {
2711                         const MonoOvfJump *ovfj = patch_info->data.target;
2712                         max_epilog_size += 8;
2713                         i = exception_id_by_name (ovfj->data.exception);
2714                         if (!exc_throw_found [i]) {
2715                                 max_epilog_size += 24;
2716                                 exc_throw_found [i] = TRUE;
2717                         }
2718                         break;
2719                 }
2720
2721                 case MONO_PATCH_INFO_EXC:
2722                         i = exception_id_by_name (patch_info->data.target);
2723                         if (!exc_throw_found [i]) {
2724                                 max_epilog_size += 24;
2725                                 exc_throw_found [i] = TRUE;
2726                         }
2727                         break;
2728
2729                 default:
2730                         break;
2731                 }
2732         }
2733
2734         while (cfg->code_len + max_epilog_size > (cfg->code_size - 16)) {
2735                 cfg->code_size *= 2;
2736                 cfg->native_code = g_realloc (cfg->native_code, cfg->code_size);
2737                 mono_jit_stats.code_reallocs++;
2738         }
2739
2740         code = cfg->native_code + cfg->code_len;
2741
2742         /* add code to raise exceptions */
2743         for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2744                 switch (patch_info->type) {
2745                 case MONO_PATCH_INFO_BB_OVF: {
2746                         /* TODO */
2747                         break;
2748                 }
2749                 case MONO_PATCH_INFO_EXC_OVF: {
2750                         const MonoOvfJump *ovfj = patch_info->data.target;
2751                         MonoJumpInfo *newji;
2752                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
2753                         unsigned char *stub = code;
2754
2755                         /* Patch original call, point it at the stub */
2756                         hppa_patch ((guint32 *)ip, code);
2757
2758                         /* Write the stub */
2759                         /* SUBTLE: this has to be PIC, because the code block
2760                          * can be relocated
2761                          */
2762                         hppa_bl_n (code, 8, hppa_r0);
2763                         hppa_nop (code);
2764
2765                         /* Add a patch info to patch the stub to point to the exception code */
2766                         newji = mono_mempool_alloc (cfg->mempool, sizeof (MonoJumpInfo));
2767                         newji->type = MONO_PATCH_INFO_EXC;
2768                         newji->ip.i = stub - cfg->native_code;
2769                         newji->data.target = ovfj->data.exception;
2770                         newji->next = patch_info->next;
2771                         patch_info->next = newji;
2772                         break;
2773                 }
2774                 case MONO_PATCH_INFO_EXC: {
2775                         unsigned char *ip = patch_info->ip.i + cfg->native_code;
2776                         i = exception_id_by_name (patch_info->data.target);
2777                         if (exc_throw_pos [i]) {
2778                                 hppa_patch ((guint32 *)ip, exc_throw_pos [i]);
2779                                 patch_info->type = MONO_PATCH_INFO_NONE;
2780                                 break;
2781                         } else {
2782                                 exc_throw_pos [i] = code;
2783                         }
2784                         hppa_patch ((guint32 *)ip, code);
2785                         hppa_set (code, patch_info->data.target, hppa_r26);
2786                         patch_info->type = MONO_PATCH_INFO_INTERNAL_METHOD;
2787                         patch_info->data.name = "mono_arch_throw_exception_by_name";
2788                         patch_info->ip.i = code - cfg->native_code;
2789                         
2790                         /* Assume the caller has set r2, we can't set it 
2791                          * here based on ip, because the caller may 
2792                          * be relocated (also the "ip" may be from an overflow
2793                          * stub)
2794                          */
2795                         hppa_ldil (code, 0, hppa_r1);
2796                         hppa_ldo (code, 0, hppa_r1, hppa_r1);
2797                         hppa_bv (code, hppa_r0, hppa_r1);
2798                         hppa_nop (code);
2799                         break;
2800                 }
2801                 default:
2802                         /* do nothing */
2803                         break;
2804                 }
2805         }
2806
2807         cfg->code_len = code - cfg->native_code;
2808
2809         g_assert (cfg->code_len < cfg->code_size);
2810         DEBUG_FUNC_EXIT();
2811 }
2812
2813 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
2814
2815 #error "--with-sigaltstack=yes not supported on hppa"
2816
2817 #endif
2818
2819 void
2820 mono_arch_setup_jit_tls_data (MonoJitTlsData *tls)
2821 {
2822 }
2823
2824 void
2825 mono_arch_free_jit_tls_data (MonoJitTlsData *tls)
2826 {
2827 }
2828
2829 void
2830 mono_arch_emit_this_vret_args (MonoCompile *cfg, MonoCallInst *inst, int this_reg, int this_type, int vt_reg)
2831 {
2832         /* add the this argument */
2833         if (this_reg != -1) {
2834                 MonoInst *this;
2835                 MONO_INST_NEW (cfg, this, OP_MOVE);
2836                 this->type = this_type;
2837                 this->sreg1 = this_reg;
2838                 this->dreg = mono_regstate_next_int (cfg->rs);
2839                 mono_bblock_add_inst (cfg->cbb, this);
2840                 mono_call_inst_add_outarg_reg (cfg, inst, this->dreg, hppa_r26, FALSE);
2841         }
2842
2843         if (vt_reg != -1) {
2844                 MonoInst *vtarg;
2845                 MONO_INST_NEW (cfg, vtarg, OP_MOVE);
2846                 vtarg->type = STACK_MP;
2847                 vtarg->sreg1 = vt_reg;
2848                 vtarg->dreg = mono_regstate_next_int (cfg->rs);
2849                 mono_bblock_add_inst (cfg->cbb, vtarg);
2850                 mono_call_inst_add_outarg_reg (cfg, inst, vtarg->dreg, hppa_r28, FALSE);
2851         }
2852 }
2853
2854
2855 MonoInst*
2856 mono_arch_get_inst_for_method (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsig, MonoInst **args)
2857 {
2858         MonoInst *ins = NULL;
2859         DEBUG_FUNC_ENTER();
2860         DEBUG_FUNC_EXIT();
2861
2862         return ins;
2863 }
2864
2865 /*
2866  * mono_arch_get_argument_info:
2867  * @csig:  a method signature
2868  * @param_count: the number of parameters to consider
2869  * @arg_info: an array to store the result infos
2870  *
2871  * Gathers information on parameters such as size, alignment and
2872  * padding. arg_info should be large enought to hold param_count + 1 entries. 
2873  *
2874  * Returns the size of the activation frame.
2875  */
2876 int
2877 mono_arch_get_argument_info (MonoMethodSignature *csig, int param_count, MonoJitArgumentInfo *arg_info)
2878 {
2879         int k, align;
2880         CallInfo *cinfo;
2881         ArgInfo *ainfo;
2882
2883         DEBUG_FUNC_ENTER();
2884         cinfo = get_call_info (csig, FALSE);
2885
2886         if (csig->hasthis) {
2887                 ainfo = &cinfo->args [0];
2888                 arg_info [0].offset = ainfo->offset;
2889         }
2890
2891         for (k = 0; k < param_count; k++) {
2892                 ainfo = &cinfo->args [k + csig->hasthis];
2893
2894                 arg_info [k + 1].offset = ainfo->offset;
2895                 arg_info [k + 1].size = mono_type_size (csig->params [k], &align);
2896         }
2897
2898         g_free (cinfo);
2899         DEBUG_FUNC_EXIT();
2900 }
2901
2902 gboolean
2903 mono_arch_print_tree (MonoInst *tree, int arity)
2904 {
2905         return 0;
2906 }
2907
2908 MonoInst* mono_arch_get_domain_intrinsic (MonoCompile* cfg)
2909 {
2910         return NULL;
2911 }
2912
2913 MonoInst* mono_arch_get_thread_intrinsic (MonoCompile* cfg)
2914 {
2915         return NULL;
2916 }
2917
2918 gpointer
2919 mono_arch_context_get_int_reg (MonoContext *ctx, int reg)
2920 {
2921         /* FIXME: implement */
2922         g_assert_not_reached ();
2923 }