Test case for bug#325444.
[mono.git] / mono / mini / helpers.c
index 64f64280a483c1040ec284c0e011c9c31a656a02..f3c4ae770062a7f62390f4366980654d85914583 100644 (file)
@@ -7,6 +7,10 @@
 #include <ctype.h>
 #include <mono/metadata/opcodes.h>
 
+#ifndef PLATFORM_WIN32
+#include <unistd.h>
+#endif
+
 #ifdef MINI_OP
 #undef MINI_OP
 #endif
@@ -41,7 +45,7 @@ opnames[] = {
 
 #endif
 
-#ifdef __i386__
+#if defined(__i386__) || defined(__x86_64__)
 #define emit_debug_info  TRUE
 #else
 #define emit_debug_info  FALSE
@@ -94,18 +98,26 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
 {
 #ifndef DISABLE_LOGGING
        GHashTable *offset_to_bb_hash = NULL;
-       int i, bb_num;
+       int i, cindex, bb_num;
        FILE *ofd;
+#ifdef PLATFORM_WIN32
        const char *tmp = g_get_tmp_dir ();
+#endif
        const char *objdump_args = g_getenv ("MONO_OBJDUMP_ARGS");
        char *as_file;
        char *o_file;
        char *cmd;
-       
+
+#ifdef PLATFORM_WIN32
        as_file = g_strdup_printf ("%s/test.s", tmp);    
 
        if (!(ofd = fopen (as_file, "w")))
                g_assert_not_reached ();
+#else  
+       i = g_file_open_tmp (NULL, &as_file, NULL);
+       ofd = fdopen (i, "w");
+       g_assert (ofd);
+#endif
 
        for (i = 0; id [i]; ++i) {
                if (!isalnum (id [i]))
@@ -128,21 +140,32 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
                }
        }
 
+       cindex = 0;
        for (i = 0; i < size; ++i) {
                if (emit_debug_info) {
                        bb_num = GPOINTER_TO_INT (g_hash_table_lookup (offset_to_bb_hash, GINT_TO_POINTER (i)));
-                       if (bb_num)
-                               fprintf (ofd, ".stabd 68,0,%d\n", bb_num - 1);
+                       if (bb_num) {
+                               fprintf (ofd, "\n.stabd 68,0,%d\n", bb_num - 1);
+                               cindex = 0;
+                       }
+               }
+               if (cindex == 0) {
+                       fprintf (ofd, "\n.byte %d", (unsigned int) code [i]);
+               } else {
+                       fprintf (ofd, ",%d", (unsigned int) code [i]);
                }
-               fprintf (ofd, ".byte %d\n", (unsigned int) code [i]);
+               cindex++;
+               if (cindex == 64)
+                       cindex = 0;
        }
+       fprintf (ofd, "\n");
        fclose (ofd);
 #ifdef __APPLE__
 #define DIS_CMD "otool -v -t"
 #else
 #if defined(sparc) && !defined(__GNUC__)
 #define DIS_CMD "dis"
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
 #define DIS_CMD "objdump -l -d"
 #else
 #define DIS_CMD "objdump -d"
@@ -151,13 +174,21 @@ mono_disassemble_code (MonoCompile *cfg, guint8 *code, int size, char *id)
 
 #if defined(sparc)
 #define AS_CMD "as -xarch=v9"
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
 #define AS_CMD "as -gstabs"
+#elif defined(__mips__)
+#define AS_CMD "as -mips32"
 #else
 #define AS_CMD "as"
 #endif
 
-       o_file = g_strdup_printf ("%s/test.o", tmp);    
+#ifdef PLATFORM_WIN32
+       o_file = g_strdup_printf ("%s/test.o", tmp);
+#else  
+       i = g_file_open_tmp (NULL, &o_file, NULL);
+       close (i);
+#endif
+
        cmd = g_strdup_printf (AS_CMD " %s -o %s", as_file, o_file);
        system (cmd); 
        g_free (cmd);