s/\<\(LC_\)\?INLINE\>/inline/.
[libfirm] / ir / ir / irdump_grgen.c
index 7206b46..2da860f 100644 (file)
 * @file
 * @brief   Write ir graph as a grgen construction rule
 * @author  Andreas Schoesser
-* @version $Id:$
+* @version $Id$
 */
 
+/*
+ * THIS IS A COMPLETE QUICK HACK! USE WITH CARE
+ * NOT FOR PRODUCTION BUILD ;-)
+ */
+
 #define MAX_NODENAME_LEN 100
 
 #include <assert.h>
@@ -37,6 +42,7 @@
 #include "tv.h"
 #include "irgwalk.h"
 #include "firm_types.h"
+#include "irdump_grgen.h"
 
 
 typedef struct
@@ -62,7 +68,6 @@ static void dump_grgen_mode(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp, i
 static char *dump_grgen_mode_node(ir_mode *irn_mode, grgen_dumpinfo_t *dump_info, FILE *fp);
 static void dump_grgen_eval(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp);
 static int dump_pattern(grgen_dumpinfo_t *dump_info, FILE *fp);
-static int  get_indent(void);
 static void set_indent(int i);
 
 
@@ -91,7 +96,6 @@ static void set_indent(int i);
 // Saves the current indent value and keeps spaces in a string
 #define MAX_INDENT 100
 static char indent[MAX_INDENT] = "";
-static int  current_indent = 0;
 
 // Saves the current node number to generate node names
 static int node_counter;
@@ -111,7 +115,7 @@ static int edge_counter;
 
 irg_grgen_dumper_env_t *init_irg_grgen_dumper(char *file, int append)
 {
-       irg_grgen_dumper_env_t *grgen_dumper_env = malloc(sizeof(irg_grgen_dumper_env_t));
+       irg_grgen_dumper_env_t *const grgen_dumper_env = XMALLOC(irg_grgen_dumper_env_t);
        FILE *fp;
 
        if(append)
@@ -137,7 +141,7 @@ irg_grgen_dumper_env_t *init_irg_grgen_dumper(char *file, int append)
 void deinit_irg_grgen_dumper(irg_grgen_dumper_env_t *grgen_dumper_env)
 {
        fclose(grgen_dumper_env->output_file);
-       free(grgen_dumper_env);
+       xfree(grgen_dumper_env);
 }
 
 static void collect_nodes(ir_node *n, void * env)
@@ -154,7 +158,7 @@ static void collect_nodes(ir_node *n, void * env)
  * Starts dumping
  ************************************************************************/
 
-void dump_irg_grgen(ir_graph *irg, char *filename, int append)
+void dump_irg_grgen_file(ir_graph *irg, char *filename, int append)
 {
        FILE *fp;
        grgen_dumpinfo_t dump_info;
@@ -209,6 +213,18 @@ void dump_irg_grgen(ir_graph *irg, char *filename, int append)
 }
 
 
+void dump_irg_grgen(ir_graph *irg, char *suffix)
+{
+  char filename[100] = "";
+
+  strncat(filename, get_entity_name(get_irg_entity(irg)), 100);
+  strncat(filename, suffix, 100);
+  strncat(filename, ".grg", 100);
+
+  dump_irg_grgen_file(irg, filename, 0);
+}
+
+
 /************************************************************************
  * Dumps the left hand side of the rule
  ************************************************************************/
@@ -220,7 +236,7 @@ static int dump_pattern(grgen_dumpinfo_t *dump_info, FILE *fp)
        int uses_memory = 0;
 
        // Dump all nodes
-       pmap_foreach(nodes_to_dump, entry)
+       foreach_pmap(nodes_to_dump, entry)
        {
                ir_node *n = (ir_node *) entry->key;
 
@@ -232,19 +248,19 @@ static int dump_pattern(grgen_dumpinfo_t *dump_info, FILE *fp)
        }
 
        // Dump all edges
-       pmap_foreach(nodes_to_dump, entry)
+       foreach_pmap(nodes_to_dump, entry)
        {
                ir_node *n = (ir_node *) entry->key;
                int i;
 
                // Dump edges
-               for(i = -1; i < get_irn_arity(n); i++)
+               for(i = is_Block(n) ? 0 : -1; i < get_irn_arity(n); i++)
                        dump_grg_egde(n, i, dump_info, fp);
        }
 
        fprintf(fp, "%seval {\n", indent);
        set_indent(6);
-       pmap_foreach(nodes_to_dump, entry)
+       foreach_pmap(nodes_to_dump, entry)
        {
                ir_node *n = (ir_node *) entry->key;
                dump_grgen_eval(n, dump_info, fp);
@@ -276,7 +292,7 @@ static void dump_grg_node(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp)
 
        node_name = obstack_alloc(&(dump_info -> node_names), MAX_NODENAME_LEN);
 
-       sprintf(node_name, "%s%d", get_op_name(get_irn_op(n)), get_irn_node_nr(n));
+       sprintf(node_name, "%s%ld", get_op_name(get_irn_op(n)), get_irn_node_nr(n));
        fprintf(fp, "%s%s : %s;\n", indent, node_name, get_op_name(get_irn_op(n)));
 
        pmap_insert(dump_info -> node_name_map, n, node_name);
@@ -375,7 +391,7 @@ static void dump_grgen_mode(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp, i
 
 static char *dump_grgen_mode_node(ir_mode *irn_mode, grgen_dumpinfo_t *dump_info, FILE *fp)
 {
-       modecode mode_code = get_mode_modecode(irn_mode);
+       ir_modecode mode_code = get_mode_modecode(irn_mode);
        const char *mode_name =  get_mode_name(irn_mode);
        char *mode_node_name;
 
@@ -410,14 +426,14 @@ static void dump_grgen_eval(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp)
        if(code == iro_Const)
        {
                node_name = pmap_get(dump_info->node_name_map, n);
-               fprintf(fp, "%s%s.value = \"%d\";\n", indent, node_name, get_tarval_long(get_Const_tarval(n)));
+               fprintf(fp, "%s%s.value = \"%ld\";\n", indent, node_name, get_tarval_long(get_Const_tarval(n)));
        }
 
 
        if(code == iro_Proj)
        {
                node_name = pmap_get(dump_info->node_name_map, n);
-               fprintf(fp, "%s%s.proj = %d;\n", indent, node_name, get_Proj_proj(n));
+               fprintf(fp, "%s%s.proj = %ld;\n", indent, node_name, get_Proj_proj(n));
        }
 
        /*if(code == iro_Block)
@@ -448,7 +464,7 @@ static void dump_grgen_eval(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp)
 
                // Correlate the matched phi edges with the matched block edges
                // Caution: Position 0 in the edge_names array is the block edge, so start at 1
-               for(i = 0; i < get_irn_arity(n) + 1; i++)
+               for(i = code == iro_Block; i < get_irn_arity(n) + 1; i++)
                {
                        assert(edge_names[i] != NULL && "Some edges have not been dumped!");
 
@@ -476,23 +492,5 @@ static void set_indent(int i)
                for(j = 0; j < i; j++)
                        indent[j] = ' ';
                indent[j] = 0x0;
-               current_indent = i;
        }
 }
-
-
-
-/************************************************************************
-* Gets current indent value
-************************************************************************/
-
-static int  get_indent(void)
-{
-       return(current_indent);
-}
-
-
-/************************************************************************
-* Collects all nodes of a ir graph, so that the ir graph can be
-* dumped completely
-************************************************************************/