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