Merge pull request #2454 from tastywheattasteslikechicken/FixVtableAbort
[mono.git] / mono / mini / genmdesc.c
index 7d34d3561a89e5460ba5b29a23ace680680025f2..b996f8f83b55a07a401a31409d4f5c437228d43c 100644 (file)
 #include <string.h>
 #include <mono/metadata/opcodes.h>
 
+#if defined(__native_client__) || defined(__native_client_codegen__)
+volatile int __nacl_thread_suspension_needed = 0;
+void __nacl_suspend_thread_if_needed() {}
+#endif
+
 #define MINI_OP(a,b,dest,src1,src2) b,
 #define MINI_OP3(a,b,dest,src1,src2,src3) b,
 /* keep in sync with the enum in mini.h */
@@ -43,6 +48,7 @@ typedef struct {
        char spec [MONO_INST_MAX];
 } OpDesc;
 
+static int nacl = 0;
 static GHashTable *table;
 static GHashTable *template_table;
 
@@ -65,7 +71,7 @@ load_file (const char *name) {
         * The format of the lines are:
         * # comment
         * opcode: [dest:format] [src1:format] [src2:format] [flags:format] [clob:format] 
-        *      [cost:num] [res:format] [delay:num] [len:num]
+        *      [cost:num] [res:format] [delay:num] [len:num] [nacl:num]
         * format is a single letter that depends on the field
         * NOTE: no space between the field name and the ':'
         *
@@ -74,6 +80,8 @@ load_file (const char *name) {
        line = 0;
        while ((str = fgets (buf, sizeof (buf), f))) {
                gboolean is_template = FALSE;
+               gboolean nacl_length_set = FALSE;
+
                ++line;
                eat_whitespace (str);
                if (!str [0])
@@ -91,7 +99,7 @@ load_file (const char *name) {
                        is_template = TRUE;
                        desc = g_new0 (OpDesc, 1);
                } else {
-                       desc = g_hash_table_lookup (table, str);
+                       desc = (OpDesc *)g_hash_table_lookup (table, str);
                        if (!desc)
                                g_error ("Invalid opcode '%s' at line %d in %s\n", str, line, name);
                        if (desc->desc)
@@ -131,8 +139,24 @@ load_file (const char *name) {
                                p += 7;
                                */
                        } else if (strncmp (p, "len:", 4) == 0) {
+                               unsigned long size;
+                               char* endptr;
                                p += 4;
-                               desc->spec [MONO_INST_LEN] = strtoul (p, &p, 10);
+                               size = strtoul (p, &endptr, 10);
+                               if (size == 0 && p == endptr)
+                                       g_error ("Invalid length '%s' at line %d in %s\n", p, line, name);
+                               p = endptr;
+                               if (!nacl_length_set) {
+                                       desc->spec [MONO_INST_LEN] = size;
+                               }
+                       } else if (strncmp (p, "nacl:", 5) == 0) {
+                               unsigned long size;
+                               p += 5;
+                               size = strtoul (p, &p, 10);
+                               if (nacl) {
+                                       desc->spec [MONO_INST_LEN] = size;
+                                       nacl_length_set = TRUE;
+                               }
                        } else if (strncmp (p, "template:", 9) == 0) {
                                char *tname;
                                int i;
@@ -141,7 +165,7 @@ load_file (const char *name) {
                                tname = p;
                                while (*p && isalnum (*p)) ++p;
                                *p++ = 0;
-                               tdesc = g_hash_table_lookup (template_table, tname);
+                               tdesc = (OpDesc *)g_hash_table_lookup (template_table, tname);
                                if (!tdesc)
                                        g_error ("Invalid template name %s at '%s' at line %d in %s\n", tname, p, line, name);
                                for (i = 0; i < MONO_INST_MAX; ++i) {
@@ -199,7 +223,7 @@ output_char (FILE *f, char c) {
        if (isalnum (c))
                fprintf (f, "%c", c);
        else
-               fprintf (f, "\\x%x\" \"", c);
+               fprintf (f, "\\x%x\" \"", (guint8)c);
 }
 
 static void
@@ -214,13 +238,13 @@ build_table (const char *fname, const char *name) {
        if (!(f = fopen (fname, "w")))
                g_error ("Cannot open file '%s'", fname);
        fprintf (f, "/* File automatically generated by genmdesc, don't change */\n\n");
-       fprintf (f, "const char %s [] = {\n", name);
+       fprintf (f, "const char mono_%s [] = {\n", name);
        fprintf (f, "\t\"");
        for (j = 0; j < MONO_INST_MAX; ++j)
                fprintf (f, "\\x0");
        fprintf (f, "\"\t/* null entry */\n");
        idx = 1;
-       g_string_append_printf (idx_array, "const guint16 %s_idx [] = {\n", name);
+       g_string_append_printf (idx_array, "const guint16 mono_%s_idx [] = {\n", name);
 
        for (i = OP_LOAD; i < OP_LAST; ++i) {
                desc = opcodes + i;
@@ -284,14 +308,22 @@ main (int argc, char* argv [])
                dump ();
        } else if (argc < 4) {
                g_print ("Usage: genmdesc arguments\n");
-               g_print ("\tgenmdesc desc                        Output to stdout the description file.\n");
-               g_print ("\tgenmdesc output name desc [desc1...] Write to output the description in a table named 'name'.\n");
+               g_print ("\tgenmdesc desc     Output to stdout the description file.\n");
+               g_print ("\tgenmdesc [--nacl] output name desc [desc1...]\n"
+                       "                     Write to output the description in a table named 'name',\n"
+                       "                     use --nacl to generate Google NativeClient code\n");
                return 1;
        } else {
-               int i;
-               for (i = 3; i < argc; ++i)
+               int i = 3;
+               if (strcmp (argv [1], "--nacl") == 0) {
+                       nacl = 1;
+                       i++;
+               }
+               
+               for (; i < argc; ++i)
                        load_file (argv [i]);
-               build_table (argv [1], argv [2]);
+               
+               build_table (argv [1 + nacl], argv [2 + nacl]);
        }
        return 0;
 }