Merge pull request #4274 from kumpera/cctor-abort
[mono.git] / mono / dis / util.c
old mode 100644 (file)
new mode 100755 (executable)
index 997f67f..70f3f6e
@@ -1,5 +1,5 @@
 /*
- * util.c: Assorted utilities for the dissasembler
+ * util.c: Assorted utilities for the disassembler
  *
  * Author:
  *   Miguel de Icaza (miguel@ximian.com)
 #include <glib.h>
 #include <string.h>
 #include <stdio.h>
+#include <math.h>
 #include "util.h"
+#include "mono/utils/mono-compiler.h"
+
+#ifdef HAVE_IEEEFP_H
+#include <ieeefp.h>
+#endif
 
 /**
  * map:
@@ -20,7 +26,7 @@
  * Warning: returns static buffer.
  */
 const char *
-map (guint32 code, map_t *table)
+map (guint32 code, dis_map_t *table)
 {
        int i;
 
@@ -38,20 +44,22 @@ map (guint32 code, map_t *table)
  * Warning: returns static buffer.
  */
 const char *
-flags (guint32 code, map_t *table)
+flags (guint32 code, dis_map_t *table)
 {
        static char buffer [1024];
        int i;
        
        buffer [0] = 0;
        
-       for (i = 0; table [i].str != NULL; i++)
+       for (i = 0; code && table [i].str != NULL; i++)
                if (table [i].code & code) {
-                       if (buffer [0])
-                               strcat (buffer, " ");
+                       code &= ~table [i].code;
                        strcat (buffer, table [i].str);
                }
 
+       if (code)
+               sprintf (buffer + strlen (buffer), "unknown-flag-%2x ", code);
+
        return buffer;
 }
 
@@ -75,10 +83,81 @@ hex_dump (const char *buffer, int base, int count)
        for (i = 0; i < count; i++){
                if (show_header)
                        if ((i % 16) == 0)
-                               printf ("\n0x%08x: ", (unsigned char) base + i);
+                               printf ("\n0x%08X: ", (unsigned char) base + i);
 
-               printf ("%02x ", (unsigned char) (buffer [i]));
+               printf ("%02X ", (unsigned char) (buffer [i]));
        }
        fflush (stdout);
 }
 
+char*
+data_dump (const char *data, int len, const char* prefix) {
+       int i, j;
+       GString *str;
+       if (!len)
+               return g_strdup (" ()\n");
+       str = g_string_new (" (");
+       for (i = 0; i + 15 < len; i += 16) {
+               if (i == 0)
+                       g_string_append_printf (str, "\n");
+               g_string_append_printf (str, "%s", prefix);
+               for (j = 0; j < 16; ++j)
+                       g_string_append_printf (str, "%02X ", (unsigned char) (data [i + j]));
+               g_string_append_printf (str, i == len - 16? ") // ": "  // ");
+               for (j = 0; j < 16; ++j)
+                       g_string_append_printf (str, "%c", data [i + j] >= 32 && data [i + j] <= 126? data [i + j]: '.');
+               g_string_append_printf (str, "\n");
+       }
+       if (i == len)
+               return g_string_free (str, FALSE);
+       if (len > 16)
+               g_string_append_printf (str, "%s", prefix);
+       j = i;
+       for (; i < len; ++i)
+               g_string_append_printf (str, "%02X ", (unsigned char) (data [i]));
+       if (len > 16) {
+               /* align */
+               int count = 16 - (len % 16);
+               for (i = 0; i < count; ++i)
+                       g_string_append_printf (str, "   ");
+       }
+       g_string_append_printf (str, ") // ");
+       for (i = j; i < len; ++i)
+               g_string_append_printf (str, "%c", data [i] >= 32 && data [i] <= 126? data [i]: '.');
+       g_string_append_printf (str, "\n");
+       return g_string_free (str, FALSE);
+}
+
+int
+dis_isinf (double num)
+{
+#ifdef HAVE_ISINF
+       return isinf (num);
+#elif defined(HAVE_IEEEFP_H)
+       fpclass_t klass;
+
+       klass = fpclass (num);
+       if (klass == FP_NINF)
+               return -1;
+
+       if (klass == FP_PINF)
+               return 1;
+
+       return 0;
+#elif defined(HAVE__FINITE)
+       return _finite (num) ? 0 : 1;
+#else
+#error "Don't know how to implement isinf for this platform."
+#endif
+}
+
+int
+dis_isnan (double num)
+{
+#ifdef __MINGW32_VERSION
+return _isnan (num);
+#else
+return isnan (num);
+#endif
+}
+