moved firmext code into the backend dir
[libfirm] / ir / be / grgen / simd / rule_info_dumper.c
1 /***********************************************************************
2  * Program:             rule_info_dumper.c
3  * Function:    Dumps C source code containig information about the
4  *                              generated GrGen rules into a .h file to be included
5  *                              when the compiler is recompiled to use the new GrGen
6  *                              rules.
7  * Depends on:  Needs the analysis info generated by the pattern creator
8  * Author:              Andreas Schoesser
9  * Date:                2007-06-28
10  ************************************************************************/
11
12
13 // ----------------------------- INCLUDES --------------------------------
14
15 #include <malloc.h>
16
17 #include "rule_info_dumper.h"
18 #include "simd_presets.h"
19
20
21
22 /************************************************************************
23  * Dumps the header of the "rulenames.c" file
24  * Rulenames.c contains information about
25  * - Which rules are defined,
26  * - Which name the assembly operation the complex operation gets
27  * - Which name the backend-firmnode the complex operation gets.
28  ************************************************************************/
29
30 rule_info_env_t *init_rule_info_dumper(int max_num_rules)
31 {
32         rule_info_env_t *rule_info_env = malloc(sizeof(rule_info_env_t));
33         FILE *fp = fopen(RULENAME_FILE, "wt");
34
35         fprintf(fp, "#ifdef INCLUDE_GENERATED_PATTERNS\n");
36         fprintf(fp, "typedef ir_node* (* ia32_construct_func_t) ();\n");
37         fprintf(fp, "ext_grs_action_t *rules[%d];\n", max_num_rules);
38         fprintf(fp, "char             *operation_names[%d];\n", max_num_rules);
39         fprintf(fp, "char             *firmnode_names[%d];\n", max_num_rules);
40         fprintf(fp, "int              priorities[%d][2];\n", max_num_rules);
41         fprintf(fp, "ia32_construct_func_t ia32_construct_funcs[%d];\n", max_num_rules);
42         fprintf(fp, "int              cost_savings[%d];\n", max_num_rules);
43         fprintf(fp, "\n");
44         fprintf(fp, "void fill_rulename_array()\n{\n");
45
46         rule_info_env->output_file = fp;
47         rule_info_env->num_rules = 0;
48         return(rule_info_env);
49 }
50
51
52
53 /************************************************************************
54  * Dumps the above information for each rule.
55  ************************************************************************/
56
57 void dump_rule_info(rule_info_env_t *rule_info_env, graph_ana_info_t *graph_ana_info, int arity)
58 {
59         FILE *fp = rule_info_env->output_file;
60         char name[255];
61         const char *firmnode_name;
62         int i = rule_info_env->num_rules;
63
64         get_rule_name(graph_ana_info, name);
65         firmnode_name = name;
66
67         fprintf(fp, "  {\n");
68         fprintf(fp, "    extern ext_grs_action_t *ext_grs_action_complex_instructions_%s;\n", name);
69         fprintf(fp, "    extern ia32_construct_func_t new_rd_ia32_%s;\n", firmnode_name);
70         fprintf(fp, "    rules[%d] = ext_grs_action_complex_instructions_%s;\n", i, name);
71         fprintf(fp, "    firmnode_names[%d] = \"%s\";\n", i, firmnode_name);
72         fprintf(fp, "    ia32_construct_funcs[%d] = (ia32_construct_func_t) &new_rd_ia32_%s;\n", i, firmnode_name);
73         fprintf(fp, "    priorities[%d][0] = %d;\n", i, i);
74         fprintf(fp, "    priorities[%d][1] = %d;\n", i, graph_ana_info->priority);
75         fprintf(fp, "    cost_savings[%d] = %d;\n", i, graph_ana_info->cost_savings);
76         fprintf(fp, "  }\n");
77
78         rule_info_env->num_rules++;
79 }
80
81
82
83 /************************************************************************
84  * Frees information used by the rule_info_dumper instance
85  ************************************************************************/
86
87 void deinit_rule_info_dumper(rule_info_env_t *rule_info_env)
88 {
89         FILE *fp = rule_info_env->output_file;
90
91         // Dump footer of rule pointers file
92         fprintf(fp, "}\n\n");
93         fprintf(fp, "#define NUM_RULES %d\n\n#endif\n", rule_info_env->num_rules);
94
95         fclose(rule_info_env->output_file);
96         free(rule_info_env);
97 }