moved firmext code into the backend dir
[libfirm] / ir / be / grgen / simd / be_spec_dumper.c
1 /***************************************************************************
2 * Program:              be_spec_dumper.c
3 * Function:             Generates and dumps the specification for the ia32 backend
4 *                               for each generated pattern.
5 * Depends on:   Needs the analysis info generated by the pattern creator
6 * Author:               Andreas Schoesser
7 * Date:                 2007-03-02
8 ***************************************************************************/
9
10
11 // ---------------------------- INCLUDES --------------------------------
12
13 #include <assert.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "simd_presets.h"
19
20 #include "irgraph.h"
21 #include "pmap.h"
22
23 #include "create_pattern_t.h"
24 #include "be_spec_dumper.h"
25
26
27
28
29 /************************************************************************
30  * Inits the backend specification dumper
31  * Generates a new file and with a header
32  * Returns:     a be_spec_env_t structure which has to be passed to all
33  *                      spec_dumper functions
34  ************************************************************************/
35
36 be_spec_env_t *init_be_spec()
37 {
38         be_spec_env_t *be_spec_env = malloc(sizeof(be_spec_env_t));
39         FILE *fp = fopen(BE_SPEC_FILE, "wt");
40
41         fprintf(fp, "# This file is generated! Do not edit, your changes will be lost!\n");
42         fprintf(fp, "#  ___ _____  _____                  _                               _\n");
43         fprintf(fp, "# / ___/ ___|| ____| __   _____  ___| |_ ___  _ __   _ __   ___   __| | ___  ___\n");
44         fprintf(fp, "# \\___ \\___ \\|  _|   \\ \\ / / _ \\/ __| __/ _ \\| '__| | '_ \\ / _ \\ / _` |/ _ \\/ __|\n");
45         fprintf(fp, "#  ___) |__) | |___   \\ V /  __/ (__| || (_) | |    | | | | (_) | (_| |  __/\\__ \\\n");
46         fprintf(fp, "# |____/____/|_____|   \\_/ \\___|\\___|\\__\\___/|_|    |_| |_|\\___/ \\__,_|\\___||___/\n\n");
47
48         be_spec_env -> output_file = fp;
49         return(be_spec_env);
50 }
51
52
53
54
55 /************************************************************************
56  * Dumps the backend specification of a rich instruction depending
57  * on the analysis information of the pattern creator
58  ************************************************************************/
59
60 void dump_be_spec(be_spec_env_t *be_spec_env, graph_ana_info_t *graph_ana_info, int uses_memory)
61 {
62         argument_descr_t *arguments[MAX_SIMD_ARGUMENTS + 2];
63         pmap_entry *entry, *entry2;
64         int max_argument = -1000, i;
65         char irg_name[255];
66         int mem_slot = (uses_memory) ? 1 : 0;
67         FILE *fp = be_spec_env->output_file;
68
69         // Order the arguments according to their arg_nr
70         memset(arguments, 0, sizeof(arguments));
71         pmap_foreach(graph_ana_info->argument_nodes, entry)
72         {
73                 argument_descr_t *argument = entry->value;
74
75                 if(argument->arg_nr > -2)
76                 {
77                         assert(argument->arg_nr + 1 < MAX_SIMD_ARGUMENTS + 1);
78                         arguments[argument->arg_nr + 1] = argument;
79                         if(argument->arg_nr + 1 > max_argument)
80                                 max_argument = argument->arg_nr + 1;
81                 }
82
83         }
84
85         get_rule_name(graph_ana_info, irg_name);
86         fprintf(fp, "$nodes{\"%s\"} = {\n", irg_name);
87
88         // Dump the in-register requirements
89         fprintf(fp, "  \"reg_req\"  => { \"in\" => [ ");
90         for(i = 1; i <= max_argument; i++)
91                 fprintf(fp, "\"%s\", ", arguments[i]->register_class);
92         if(uses_memory)
93                 fprintf(fp, "\"none\", ");
94
95         // Dump the out-register requirements
96         fprintf(fp, "], \"out\" => [ ");
97         if(arguments[0] != NULL) // The result data
98                 fprintf(fp, "\"%s\", ", arguments[0]->register_class);
99         if(uses_memory)                  // The memory out
100                 fprintf(fp, "\"none\", ");
101         pmap_foreach(graph_ana_info->destroyed_regs, entry2)
102         {
103                 char *destroyed_reg = (char *) entry2->value;
104                 fprintf(fp, "\"%s\", ", destroyed_reg);
105         }
106         fprintf(fp, "] }, \n");
107
108         // Dump the emittment
109         fprintf(fp, "  \"emit\" => \"%s\",\n", graph_ana_info->emit_statement);
110
111         fprintf(fp, "};\n\n");
112 }
113
114
115
116
117 /************************************************************************
118  * Frees information allocated by the backend specification dumper
119  ************************************************************************/
120
121 void deinit_be_spec(be_spec_env_t *be_spec_env)
122 {
123         fclose(be_spec_env->output_file);
124         free(be_spec_env);
125 }