2009-06-26 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / unwind.c
index 35b7ec23250ed25f51ed34c7966ccb4fe3079af5..c0d5a45fa19242f77eeb88d9f30729fd5f260080 100644 (file)
@@ -55,14 +55,14 @@ static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
 #define NUM_REGS X86_NREG + 1
 #define DWARF_DATA_ALIGN (-4)
 #define DWARF_PC_REG (mono_hw_reg_to_dwarf_reg (X86_NREG))
-#elif defined (TARGET_PPC)
+#elif defined (TARGET_POWERPC)
 // http://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi-1.9.html
 static int map_hw_reg_to_dwarf_reg [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 
                                                                                  9, 10, 11, 12, 13, 14, 15, 16,
                                                                                  17, 18, 19, 20, 21, 22, 23, 24,
                                                                                  25, 26, 27, 28, 29, 30, 31 };
 #define NUM_REGS 110
-#define DWARF_DATA_ALIGN (-sizeof (mgreg_t))
+#define DWARF_DATA_ALIGN (-(gint32)sizeof (mgreg_t))
 #define DWARF_PC_REG 108
 #else
 static int map_hw_reg_to_dwarf_reg [16];
@@ -83,7 +83,7 @@ static int map_dwarf_reg_to_hw_reg [NUM_REGS];
 int
 mono_hw_reg_to_dwarf_reg (int reg)
 {
-#ifdef TARGET_PPC
+#ifdef TARGET_POWERPC
        if (reg == ppc_lr)
                return 108;
        else
@@ -138,6 +138,37 @@ encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
        *endbuf = p;
 }
 
+static G_GNUC_UNUSED void
+encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
+{
+       gboolean more = 1;
+       gboolean negative = (value < 0);
+       guint32 size = 32;
+       guint8 byte;
+       guint8 *p = buf;
+
+       while (more) {
+               byte = value & 0x7f;
+               value >>= 7;
+               /* the following is unnecessary if the
+                * implementation of >>= uses an arithmetic rather
+                * than logical shift for a signed left operand
+                */
+               if (negative)
+                       /* sign extend */
+                       value |= - (1 <<(size - 7));
+               /* sign bit of byte is second high order bit (0x40) */
+               if ((value == 0 && !(byte & 0x40)) ||
+                       (value == -1 && (byte & 0x40)))
+                       more = 0;
+               else
+                       byte |= 0x80;
+               *p ++= byte;
+       }
+
+       *endbuf = p;
+}
+
 static inline guint32
 decode_uleb128 (guint8 *buf, guint8 **endbuf)
 {
@@ -240,7 +271,7 @@ mono_unwind_ops_encode (GSList *unwind_ops, guint32 *out_len)
                        if (reg > 63) {
                                *p ++ = DW_CFA_offset_extended_sf;
                                encode_uleb128 (reg, p, &p);
-                               encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
+                               encode_sleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
                        } else {
                                *p ++ = DW_CFA_offset | reg;
                                encode_uleb128 (op->val / DWARF_DATA_ALIGN, p, &p);
@@ -287,7 +318,7 @@ print_dwarf_state (int cfa_reg, int cfa_offset, int ip, int nregs, Loc *location
  */
 void
 mono_unwind_frame (guint8 *unwind_info, guint32 unwind_info_len, 
-                                  guint8 *start_ip, guint8 *end_ip, guint8 *ip, gssize *regs, 
+                                  guint8 *start_ip, guint8 *end_ip, guint8 *ip, mgreg_t *regs, 
                                   int nregs, guint8 **out_cfa) 
 {
        Loc locations [NUM_REGS];