Added standalone grgen dumper for firm graphs
[libfirm] / ir / ir / irdump_grgen.c
1 /*
2 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3 *
4 * This file is part of libFirm.
5 *
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
10 *
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
14 *
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE.
18 */
19
20 /**
21 * @file
22 * @brief   Write ir graph as a grgen construction rule
23 * @author  Andreas Schoesser
24 * @version $Id:$
25 */
26
27 /*
28  * THIS IS A COMPLETE QUICK HACK! USE WITH CARE
29  * NOT FOR PRODUCTION BUILD ;-)
30  */
31
32 #define MAX_NODENAME_LEN 100
33
34 #include <assert.h>
35 #include <stdio.h>
36 #include <obst.h>
37
38
39 #include "irgraph.h"
40 #include "firm_types.h"
41 #include "pmap.h"
42 #include "tv.h"
43 #include "irgwalk.h"
44 #include "firm_types.h"
45
46
47 typedef struct
48 {
49         ir_graph *irg;
50         struct pmap *mode_edge_map;
51         struct pmap *edge_name_map;
52         struct pmap *node_name_map;  // Contains the mapping firm node -> node name
53         struct obstack node_names;   // Contains the node name data
54         struct pmap *mode_name_map;  // Contains the mapping firm mode -> mode node name
55         struct obstack mode_names;   // Contains the "mode node name" data
56         struct pmap *nodes_to_dump;  // Contains firm nodes, that have to be dumped
57 } grgen_dumpinfo_t;
58
59 typedef struct                  // Holds information needed througout the usage of a grgen dumper instance
60 {
61         FILE *output_file;      // The file the grgen rules will be dumped to
62 } irg_grgen_dumper_env_t;
63
64 static void dump_grg_node(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp);
65 static void dump_grg_egde(ir_node *n, int n_edge, grgen_dumpinfo_t *dump_info, FILE *fp);
66 static void dump_grgen_mode(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp, ir_mode *alt_mode);
67 static char *dump_grgen_mode_node(ir_mode *irn_mode, grgen_dumpinfo_t *dump_info, FILE *fp);
68 static void dump_grgen_eval(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp);
69 static int dump_pattern(grgen_dumpinfo_t *dump_info, FILE *fp);
70 static int  get_indent(void);
71 static void set_indent(int i);
72
73
74
75 /*****************************************************************************
76 * Program:              grgen_dumper.c
77 * Function:             Dumps parts of a firm graph (those which have to be extracted
78 *                               as search and replace patterns) as a grGen rule.
79 * Depends on:   Needs the analysis info generated by the pattern creator
80 * Author:               Andreas Schoesser
81 * Date:         2006-12-07
82 *****************************************************************************/
83
84
85 // ---------------------------- INCLUDES --------------------------------
86
87
88
89
90 /* #include "grgen_dumper__t.h"
91 #include "create_pattern_t.h"
92 #include "firm_node_ext.h" */
93
94 // ----------------------------- GLOABALS --------------------------------
95
96 // Saves the current indent value and keeps spaces in a string
97 #define MAX_INDENT 100
98 static char indent[MAX_INDENT] = "";
99 static int  current_indent = 0;
100
101 // Saves the current node number to generate node names
102 static int node_counter;
103 static int edge_counter;
104
105
106
107
108 /************************************************************************
109 * Initializes the grgen_dumper module and dumps the GrGen File header
110 * Returns:              An environment to be passed to all functions of the GrGen
111 *                               dumper.
112 * Parameters:   file:   filename of the file to dump to
113 *                               append: 1 if the previous file content should be
114 *                                               maintained.
115 ************************************************************************/
116
117 irg_grgen_dumper_env_t *init_irg_grgen_dumper(char *file, int append)
118 {
119         irg_grgen_dumper_env_t *grgen_dumper_env = malloc(sizeof(irg_grgen_dumper_env_t));
120         FILE *fp;
121
122         if(append)
123                 fp = fopen(file, "at");
124         else
125         {
126                 fp = fopen(file, "wt");
127
128                 // *** Dump header
129                 fprintf(fp, "%susing Firm;\n\n", indent);
130         }
131
132         grgen_dumper_env -> output_file = fp;
133         return(grgen_dumper_env);
134 }
135
136
137
138 /************************************************************************
139 * Frees information used by the grgen_dumper and closes the output file
140 ************************************************************************/
141
142 void deinit_irg_grgen_dumper(irg_grgen_dumper_env_t *grgen_dumper_env)
143 {
144         fclose(grgen_dumper_env->output_file);
145         free(grgen_dumper_env);
146 }
147
148 static void collect_nodes(ir_node *n, void * env)
149 {
150         pmap *nodes_to_dump = (pmap *) env;
151
152         pmap_insert(nodes_to_dump, n, NULL);
153 }
154
155
156
157
158 /************************************************************************
159  * Starts dumping
160  ************************************************************************/
161
162 void dump_irg_grgen(ir_graph *irg, char *filename, int append)
163 {
164         FILE *fp;
165         grgen_dumpinfo_t dump_info;
166         int uses_memory = 0;
167
168
169         irg_grgen_dumper_env_t *grgen_dumper_env = init_irg_grgen_dumper(filename, append);
170         fp = grgen_dumper_env -> output_file;
171
172         // Do initialization
173         dump_info.irg = irg;                    // Basically copy the graph_ana_struct, ugly
174         dump_info.mode_edge_map = pmap_create();                // Create some additional pmaps to hold
175         dump_info.edge_name_map = pmap_create();                // node and edge name information etc.
176         dump_info.node_name_map = pmap_create();
177         dump_info.mode_name_map = pmap_create();
178         dump_info.nodes_to_dump = pmap_create();
179
180         // Node and egde count start at 0 for each pattern to be dumped.
181         node_counter = 0;
182         edge_counter = 0;
183         obstack_init(&(dump_info.node_names));
184         obstack_init(&(dump_info.mode_names));
185
186         // Dump rule header
187         set_indent(0);
188         fprintf(fp, "\n\n%srule %s\n%s{\n", indent, get_entity_name(get_irg_entity(irg)), indent);
189         set_indent(2);
190
191         fprintf(fp, "%spattern { }\n", indent);                  // Empty pattern
192         fprintf(fp, "%sreplace\n%s{\n", indent, indent); // Graph is contrcuted in the replacement part
193
194         set_indent(4);
195
196         irg_walk_graph(irg, collect_nodes, NULL, dump_info.nodes_to_dump);
197         uses_memory = dump_pattern(&dump_info, fp);
198
199         // *** Dump footer
200         set_indent(0);
201         fprintf(fp, "%s}\n", indent);
202
203         // Clean up
204         pmap_destroy(dump_info.mode_edge_map);
205         pmap_destroy(dump_info.edge_name_map);
206         pmap_destroy(dump_info.node_name_map);
207         pmap_destroy(dump_info.mode_name_map);
208         obstack_free(&(dump_info.node_names), NULL);
209         obstack_finish(&(dump_info.node_names));
210         obstack_free(&(dump_info.mode_names), NULL);
211         obstack_finish(&(dump_info.mode_names));
212
213         deinit_irg_grgen_dumper(grgen_dumper_env);
214 }
215
216
217 /************************************************************************
218  * Dumps the left hand side of the rule
219  ************************************************************************/
220
221 static int dump_pattern(grgen_dumpinfo_t *dump_info, FILE *fp)
222 {
223         struct pmap *nodes_to_dump = dump_info->nodes_to_dump;
224         pmap_entry *entry;
225         int uses_memory = 0;
226
227         // Dump all nodes
228         pmap_foreach(nodes_to_dump, entry)
229         {
230                 ir_node *n = (ir_node *) entry->key;
231
232                 // Dump node
233                 if(get_irn_opcode(n) == iro_Proj && get_irn_modecode(n) == irm_M)
234                         uses_memory = 1;
235                 dump_grg_node(n, dump_info, fp);
236                 dump_grgen_mode(n, dump_info, fp, NULL);
237         }
238
239         // Dump all edges
240         pmap_foreach(nodes_to_dump, entry)
241         {
242                 ir_node *n = (ir_node *) entry->key;
243                 int i;
244
245                 // Dump edges
246                 for(i = -1; i < get_irn_arity(n); i++)
247                         dump_grg_egde(n, i, dump_info, fp);
248         }
249
250         fprintf(fp, "%seval {\n", indent);
251         set_indent(6);
252         pmap_foreach(nodes_to_dump, entry)
253         {
254                 ir_node *n = (ir_node *) entry->key;
255                 dump_grgen_eval(n, dump_info, fp);
256         }
257         set_indent(4);
258         fprintf(fp, "%s}\n", indent);
259
260
261         set_indent(2);
262         fprintf(fp, "%s} /* Replacement */\n", indent);
263         return(uses_memory);
264 }
265
266
267
268 /************************************************************************
269 * Dumps a node in GrGen Format
270 ************************************************************************/
271
272 static void dump_grg_node(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp)
273 {
274         char *node_name;
275
276         // Already dumped the node? Then do nothing
277         if(pmap_contains(dump_info -> node_name_map, n))
278                 return;
279
280         // Else generate new node name and dump the node
281
282         node_name = obstack_alloc(&(dump_info -> node_names), MAX_NODENAME_LEN);
283
284         sprintf(node_name, "%s%d", get_op_name(get_irn_op(n)), get_irn_node_nr(n));
285         fprintf(fp, "%s%s : %s;\n", indent, node_name, get_op_name(get_irn_op(n)));
286
287         pmap_insert(dump_info -> node_name_map, n, node_name);
288         node_counter++;
289 }
290
291
292
293 /************************************************************************
294 * Dumps an edge in GrGen format
295 ************************************************************************/
296
297 static void dump_grg_egde(ir_node *n, int n_edge, grgen_dumpinfo_t *dump_info, FILE *fp)
298 {
299         ir_node *to_node;
300         char *from_node_name, *to_node_name;
301         char **nodes_edge_names;
302
303
304         // Check if to_node has also to be dumped. If not, skip this edge
305         // We have to dump to_node here, because to_node has to be known by grgen before
306         // connecting an edge to it.
307         to_node =  get_irn_n(n, n_edge);
308         if(!pmap_contains(dump_info -> nodes_to_dump, to_node))
309                 return;
310
311         if((nodes_edge_names = pmap_get(dump_info -> edge_name_map, n)) == NULL)
312         {
313                 nodes_edge_names = (char **) obstack_alloc(&(dump_info->node_names), (get_irn_arity(n) + 1) * sizeof(char *));
314                 memset(nodes_edge_names, 0, (get_irn_arity(n) + 1) * sizeof(char *));
315                 pmap_insert(dump_info->edge_name_map, n, nodes_edge_names);
316         }
317
318         assert(pmap_contains(dump_info -> node_name_map, n));
319         assert(pmap_contains(dump_info -> node_name_map, to_node));
320         from_node_name = (char *) pmap_get(dump_info -> node_name_map, n);
321         to_node_name = (char *) pmap_get(dump_info -> node_name_map, to_node);
322
323         {
324
325         char edge_name[50], *edge_name_obst;
326
327         sprintf(edge_name, "pos%d_%d", n_edge + 1, edge_counter++);
328         edge_name_obst = obstack_alloc(&(dump_info->node_names), strlen(edge_name) + 1);
329         strcpy(edge_name_obst, edge_name);
330         nodes_edge_names[n_edge + 1] = edge_name_obst;
331
332         fprintf(fp, "%s%s -%s:df-> %s;\n", indent, from_node_name, edge_name_obst, to_node_name);
333         }
334
335
336 }
337
338
339
340 /************************************************************************
341 * Dumps an FIRM Mode as GrGen Code
342 * If source_node_name == NULL, that name of n that was already
343 * generated is used.
344 * If source_node_name != NULL, this given source will be used
345 * (useful for retyped nodes)
346 ************************************************************************/
347
348 static void dump_grgen_mode(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp, ir_mode *alt_mode)
349 {
350         char *node_name = (char *) pmap_get(dump_info -> node_name_map, n);
351         ir_mode *irn_mode = (alt_mode != NULL) ? alt_mode : get_irn_mode(n);
352         char edge_name[50];
353         char *mode_node_name;
354
355         mode_node_name = dump_grgen_mode_node(irn_mode, dump_info, fp);
356
357         //mode_code =  get_mode_modecode(irn_mode);
358         //mode_name =  get_mode_name(irn_mode);
359
360         // Yes, use the given mode-node
361         //mode_node_name = pmap_get(dump_info -> mode_name_map, (void *) mode_code);
362         sprintf(edge_name, "m%d", edge_counter++);
363
364         if(pmap_get(dump_info->mode_edge_map, n) == NULL)
365         {
366                 char *edge_name_obst = obstack_alloc(&(dump_info->node_names), strlen(edge_name) + 1);
367                 strcpy(edge_name_obst, edge_name);
368                 pmap_insert(dump_info->mode_edge_map, n, edge_name_obst);
369         }
370
371         // Dump the edge from the current node to it's mode node
372         fprintf(fp, "%s%s -%s:has_mode-> %s;\n", indent, node_name, edge_name, mode_node_name);
373 }
374
375
376
377 /************************************************************************
378 * Dumps a node representing a node
379 ************************************************************************/
380
381 static char *dump_grgen_mode_node(ir_mode *irn_mode, grgen_dumpinfo_t *dump_info, FILE *fp)
382 {
383         modecode mode_code = get_mode_modecode(irn_mode);
384         const char *mode_name =  get_mode_name(irn_mode);
385         char *mode_node_name;
386
387         if(!pmap_contains(dump_info -> mode_name_map, (void *) mode_code))
388         {
389                 // No, create a new mode-node
390                 mode_node_name = obstack_alloc(&(dump_info -> mode_names), MAX_NODENAME_LEN);
391                 sprintf(mode_node_name, "mode_%s_node", mode_name);
392                 pmap_insert(dump_info -> mode_name_map, (void *) mode_code, mode_node_name);
393                 fprintf(fp, "%s%s : Mode_%s;\n", indent, mode_node_name, mode_name);
394                 return(mode_node_name);
395         }
396         else
397         {
398                 return((char *) pmap_get(dump_info -> mode_name_map, (void *) mode_code));
399         }
400
401 }
402
403
404
405 /************************************************************************
406 * Dumps the condition for the given node, depending on the node's
407 * attributes and the node's opcode
408 ************************************************************************/
409
410 static void dump_grgen_eval(ir_node *n, grgen_dumpinfo_t *dump_info, FILE *fp)
411 {
412         char *node_name;
413         ir_opcode code = get_irn_opcode(n);
414
415         if(code == iro_Const)
416         {
417                 node_name = pmap_get(dump_info->node_name_map, n);
418                 fprintf(fp, "%s%s.value = \"%d\";\n", indent, node_name, get_tarval_long(get_Const_tarval(n)));
419         }
420
421
422         if(code == iro_Proj)
423         {
424                 node_name = pmap_get(dump_info->node_name_map, n);
425                 fprintf(fp, "%s%s.proj = %d;\n", indent, node_name, get_Proj_proj(n));
426         }
427
428         /*if(code == iro_Block)
429         {
430                 node_name = pmap_get(dump_info->node_name_map, n);
431                 fprintf(fp, "%s%s.pos = %d;\n", indent, node_name, ??);
432         }
433
434         if(code == iro_Phi)
435         {
436                 node_name = pmap_get(dump_info->node_name_map, n);
437                 fprintf(fp, "%s%s.pos = %d;\n", indent, node_name, ??);
438         }*/
439
440         // TODO: Dump Block evals: edge numbers
441
442
443         if(code == iro_Phi || code == iro_Block)
444         {
445                 char **edge_names;
446                 int i;
447
448                 //assert((get_irn_arity(n) == get_irn_arity(phi_block)) && "Phi has other arity than it's block! Pattern seems to be broken.");
449
450                 // Load the edge names that have been saved
451                 edge_names = pmap_get(dump_info->edge_name_map, n);
452                 assert(edge_names && "Some edge names have not been dumped!");
453
454                 // Correlate the matched phi edges with the matched block edges
455                 // Caution: Position 0 in the edge_names array is the block edge, so start at 1
456                 for(i = 0; i < get_irn_arity(n) + 1; i++)
457                 {
458                         assert(edge_names[i] != NULL && "Some edges have not been dumped!");
459
460                         fprintf(fp, "%s%s.pos = %d;\n", indent, edge_names[i], i);
461                 }
462                 return;
463         }
464 }
465
466
467
468
469
470 /************************************************************************
471 * Sets current indent
472 ************************************************************************/
473
474 static void set_indent(int i)
475 {
476         int j;
477
478         // Generate a string containing i blank characters
479         if(i < MAX_INDENT - 1)
480         {
481                 for(j = 0; j < i; j++)
482                         indent[j] = ' ';
483                 indent[j] = 0x0;
484                 current_indent = i;
485         }
486 }
487
488
489
490 /************************************************************************
491 * Gets current indent value
492 ************************************************************************/
493
494 static int  get_indent(void)
495 {
496         return(current_indent);
497 }
498
499
500 /************************************************************************
501 * Collects all nodes of a ir graph, so that the ir graph can be
502 * dumped completely
503 ************************************************************************/