2006-06-08 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / monoburg / monoburg.c
index 067e110f1b9310a13dd7715b420d3ac89b69d81d..32c75012610b52ebc39f79b5b4c9522bd0afb961 100644 (file)
@@ -161,7 +161,7 @@ create_term (char *id, int num)
        if (!term_hash) 
                term_hash = g_hash_table_new (g_str_hash , g_str_equal);
 
-       g_hash_table_foreach (term_hash, (GHFunc) check_term_num, (gpointer) num);
+       g_hash_table_foreach (term_hash, (GHFunc) check_term_num, GINT_TO_POINTER (num));
 
        term = g_new0 (Term, 1);
 
@@ -360,7 +360,7 @@ emit_decoders ()
 
        for (l = nonterm_list; l; l = l->next) {
                NonTerm *n = (NonTerm *)l->data;
-               output ("const int mono_burg_decode_%s[] = {\n", n->name);
+               output ("const short mono_burg_decode_%s[] = {\n", n->name);
                output ("\t0,\n");
                for (rl = n->rules; rl; rl = rl->next) {
                        Rule *rule = (Rule *)rl->data;
@@ -818,26 +818,49 @@ compute_nonterms (Tree *tree)
        } 
 }
 
+static int
+count_nonterms (Tree *tree)
+{
+       if (!tree)
+               return 0;
+
+       if (tree->nonterm) {
+               return 1;
+       } else {
+               return count_nonterms (tree->left) + count_nonterms (tree->right);
+       } 
+}
+
 static void
 emit_vardefs ()
 {
        GList *l;
        int i, j, c, n, *si;
        char **sa;
+       int *nts_offsets;
+       int current_offset;
 
        if (predefined_terms) {
+               output ("#if HAVE_ARRAY_ELEM_INIT\n");
+               output ("const guint8 mono_burg_arity [MBMAX_OPCODES] = {\n"); 
+               for (l = term_list, i = 0; l; l = l->next) {
+                       Term *t = (Term *)l->data;
+                       output ("\t [%s] = %d, /* %s */\n", t->name, t->arity, t->name);
+               }
+               output ("};\n\n");
+               output ("void\nmono_burg_init (void) {\n");
+               output ("}\n\n");
+               output ("#else\n");
                output ("guint8 mono_burg_arity [MBMAX_OPCODES];\n"); 
 
                output ("void\nmono_burg_init (void)\n{\n");
 
                for (l = term_list, i = 0; l; l = l->next) {
                        Term *t = (Term *)l->data;
-
                        output ("\tmono_burg_arity [%s] = %d; /* %s */\n", t->name, t->arity, t->name);
-
                }
-
                output ("}\n\n");
+               output ("#endif /* HAVE_ARRAY_ELEM_INIT */\n");
 
        } else {
                output ("const guint8 mono_burg_arity [] = {\n"); 
@@ -864,6 +887,7 @@ emit_vardefs ()
                output ("};\n\n");
        }
 
+       output ("#if MONOBURG_LOG\n");
        output ("const char * const mono_burg_rule_string [] = {\n");
        output ("\tNULL,\n");
        for (l = rule_list, i = 0; l; l = l->next) {
@@ -872,12 +896,17 @@ emit_vardefs ()
                emit_tree_string (rule->tree);
                output ("\",\n");
        }
-       output ("};\n\n");
+       output ("};\n");
+       output ("#endif /* MONOBURG_LOG */\n\n");
 
        n = g_list_length (rule_list);
        sa = g_new0 (char *, n);
        si = g_new0 (int, n);
+       nts_offsets = g_new0 (int, n);
 
+       /* at offset 0 we store 0 to mean end of list */
+       current_offset = 1;
+       output ("const guint16 mono_burg_nts_data [] = {\n\t0,\n");
        /* compress the _nts array */
        for (l = rule_list, i = 0, c = 0; l; l = l->next) {
                Rule *rule = (Rule *)l->data;
@@ -889,17 +918,20 @@ emit_vardefs ()
 
                si [i++] = j;
                if (j == c) {
-                       output ("static const guint16 mono_burg_nts_%d [] = { %s0 };\n", c, s);
+                       output ("\t%s0,\n", s);
+                       nts_offsets [c] = current_offset;
                        sa [c++] = s;
+                       current_offset += count_nonterms (rule->tree) + 1;
                }
        }       
-       output ("\n");
+       output ("\t0\n};\n\n");
 
-       output ("const guint16 *const mono_burg_nts [] = {\n");
+       output ("const guint8 mono_burg_nts [] = {\n");
        output ("\t0,\n");
        for (l = rule_list, i = 0; l; l = l->next) {
                Rule *rule = (Rule *)l->data;
-               output ("\tmono_burg_nts_%d, ", si [i++]);
+               output ("\t%d, /* %s */ ", nts_offsets [si [i]], sa [si [i]]);
+               ++i;
                emit_rule_string (rule, "");
        }
        output ("};\n\n");
@@ -914,8 +946,11 @@ emit_prototypes ()
                output ("typedef void (*MBEmitFunc) (MBTREE_TYPE *tree, MBCGEN_TYPE *s);\n\n");
        
        output ("extern const char * const mono_burg_term_string [];\n");
+       output ("#if MONOBURG_LOG\n");
        output ("extern const char * const mono_burg_rule_string [];\n");
-       output ("extern const guint16 *const mono_burg_nts [];\n");
+       output ("#endif /* MONOBURG_LOG */\n");
+       output ("extern const guint16 mono_burg_nts_data [];\n");
+       output ("extern const guint8 mono_burg_nts [];\n");
        output ("extern MBEmitFunc const mono_burg_func [];\n");
 
        output ("MBState *mono_burg_label (MBTREE_TYPE *tree, MBCOST_DATA *data);\n");