2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Statistics for Firm. Dumping patterns.
23 * @author Michael Beck
33 #include "firmstat_t.h"
34 #include "pattern_dmp.h"
36 /* dumper operations */
37 typedef void (*DUMP_NEW_PATTERN_FUNC)(pattern_dumper_t *self, counter_t *cnt);
38 typedef void (*DUMP_FINISH_PATTERN_FUNC)(pattern_dumper_t *self);
39 typedef void (*DUMP_NODE_FUNC)(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr);
40 typedef void (*DUMP_REF_FUNC)(pattern_dumper_t *self, unsigned id);
41 typedef void (*DUMP_EDGE_FUNC)(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code);
42 typedef void (*DUMP_START_CHILDREN_FUNC)(pattern_dumper_t *self, unsigned id);
43 typedef void (*DUMP_FINISH_CHILDREN_FUNC)(pattern_dumper_t *self, unsigned id);
44 typedef void (*DUMP_START_FUNC)(pattern_dumper_t *self);
45 typedef void (*DUMP_END_FUNC)(pattern_dumper_t *self);
50 struct pattern_dumper_t {
51 DUMP_NEW_PATTERN_FUNC dump_new_pattern;
52 DUMP_FINISH_PATTERN_FUNC dump_finish_pattern;
53 DUMP_NODE_FUNC dump_node;
54 DUMP_REF_FUNC dump_ref;
55 DUMP_EDGE_FUNC dump_edge;
56 DUMP_START_CHILDREN_FUNC dump_start_children;
57 DUMP_FINISH_CHILDREN_FUNC dump_finish_children;
58 DUMP_START_FUNC dump_start;
59 DUMP_END_FUNC dump_end;
66 typedef struct vcg_private_t {
67 FILE *f; /**< file to dump to */
68 unsigned pattern_id; /**< ID of the pattern */
69 unsigned max_pattern; /**< maximum number of pattern to be dumped */
73 * Starts a new VCG graph.
75 static void vcg_dump_start(pattern_dumper_t *self)
77 vcg_private_t *priv = (vcg_private_t*)self->data;
80 "graph: { title: \"Most found pattern\"\n"
81 " display_edge_labels: no\n"
82 " layoutalgorithm: mindepth\n"
83 " manhattan_edges: yes\n"
85 " orientation: bottom_to_top\n"
87 } /* vcg_dump_start */
90 * Ends a new VCG graph.
92 static void vcg_dump_end(pattern_dumper_t *self)
94 vcg_private_t *priv = (vcg_private_t*)self->data;
96 fprintf(priv->f, "}\n");
101 * Starts a new pattern.
103 static void vcg_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
105 vcg_private_t *priv = (vcg_private_t*)self->data;
106 static unsigned nr = 0;
108 if (priv->pattern_id > priv->max_pattern)
112 " graph: { title: \"g%u\" label: \"pattern %u\" status:clustered color:yellow\n",
113 priv->pattern_id, priv->pattern_id );
115 /* add a pseudo node for the count of this pattern */
117 " node: {title: \"c%u\" label: \"cnt: %u\" color:red }\n",
118 ++nr, cnt_to_uint(cnt)
120 } /* vcg_dump_new_pattern */
123 * Finish the current pattern.
125 static void vcg_dump_finish_pattern(pattern_dumper_t *self)
127 vcg_private_t *priv = (vcg_private_t*)self->data;
129 if (priv->pattern_id > priv->max_pattern)
132 fprintf(priv->f, " }\n");
134 if (priv->pattern_id > 0)
135 fprintf(priv->f, " edge: { sourcename: \"g%u\" targetname: \"g%u\" linestyle:invisible}\n",
137 priv->pattern_id - 1);
140 } /* vcg_dump_finish_pattern */
145 static void vcg_dump_node(pattern_dumper_t *self, unsigned id,
146 unsigned op_code, unsigned mode_code, void *attr)
148 vcg_private_t *priv = (vcg_private_t*)self->data;
149 ir_op *op = stat_get_op_from_opcode(op_code);
150 ir_mode *mode = get_irp_mode(mode_code);
151 long l = attr ? *(long *)attr : 0;
153 if (priv->pattern_id > priv->max_pattern)
157 fprintf(priv->f, " node: {title: \"n%u_%u\" label: \"%s%s %ld n%u\" }\n",
158 priv->pattern_id, id, get_id_str(op->name), mode ? get_mode_name(mode) : "", l, id);
160 fprintf(priv->f, " node: {title: \"n%u_%u\" label: \"%s%s n%u\" }\n",
161 priv->pattern_id, id, get_id_str(op->name), mode ? get_mode_name(mode) : "", id);
163 } /* vcg_dump_node */
168 static void vcg_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
170 vcg_private_t *priv = (vcg_private_t*)self->data;
173 if (priv->pattern_id > priv->max_pattern)
176 fprintf(priv->f, " edge: { sourcename: \"n%u_%u\" targetname: \"n%u_%u\" label: \"%u\" }\n",
177 priv->pattern_id, src,
178 priv->pattern_id, tgt,
181 } /* vcg_dump_edge */
186 static pattern_dumper_t vcg_dump = {
187 vcg_dump_new_pattern,
188 vcg_dump_finish_pattern,
200 * Starts a new pattern.
202 static void stdout_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
204 FILE *f = (FILE*)self->data;
206 fprintf(f, "%8u ", cnt_to_uint(cnt));
207 } /* stdout_dump_new_pattern */
211 * Finish the current pattern.
213 static void stdout_dump_finish_pattern(pattern_dumper_t *self)
215 FILE *f = (FILE*)self->data;
218 } /* stdout_dump_finish_pattern */
223 static void stdout_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
225 FILE *f = (FILE*)self->data;
226 ir_op *op = stat_get_op_from_opcode(op_code);
227 ir_mode *mode = get_irp_mode(mode_code);
230 /* if (env->options & OPT_ENC_GRAPH) */
231 fprintf(f, "%u:", id);
233 fprintf(f, "%s", get_id_str(op->name));
236 fprintf(f, "%s", get_mode_name(mode));
237 } /* stdout_dump_node */
242 static void stdout_dump_ref(pattern_dumper_t *self, unsigned id)
244 FILE *f = (FILE*)self->data;
246 fprintf(f, "REF:%u", id);
247 } /* stdout_dump_ref */
252 static void stdout_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
254 FILE *f = (FILE*)self->data;
262 } /* stdout_dump_edge */
265 * Start the children dumper.
267 static void stdout_start_children(pattern_dumper_t *self, unsigned id)
269 FILE *f = (FILE*)self->data;
273 } /* stdout_start_children */
276 * Finish the children dumper.
278 static void stdout_finish_children(pattern_dumper_t *self, unsigned id)
280 FILE *f = (FILE*)self->data;
284 } /* stdout_finish_children */
289 static const pattern_dumper_t stdout_dump = {
290 stdout_dump_new_pattern,
291 stdout_dump_finish_pattern,
295 stdout_start_children,
296 stdout_finish_children,
302 /* ------------------------------------ API ------------------------------------- */
305 * Starts a new pattern.
307 void pattern_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
309 if (self->dump_new_pattern)
310 self->dump_new_pattern(self, cnt);
311 } /* pattern_dump_new_pattern */
315 * Finish the current pattern.
317 void pattern_dump_finish_pattern(pattern_dumper_t *self)
319 if (self->dump_finish_pattern)
320 self->dump_finish_pattern(self);
321 } /* pattern_dump_finish_pattern */
327 void pattern_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
330 self->dump_node(self, id, op_code, mode_code, attr);
331 } /* pattern_dump_node */
336 void pattern_dump_ref(pattern_dumper_t *self, unsigned id)
339 self->dump_ref(self, id);
340 } /* pattern_dump_ref */
345 void pattern_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
348 self->dump_edge(self, tgt, src, pos, mode_code);
349 } /* pattern_dump_edge */
352 * Start the children dumper.
354 void pattern_start_children(pattern_dumper_t *self, unsigned id)
356 if (self->dump_start_children)
357 self->dump_start_children(self, id);
358 } /* pattern_start_children */
361 * Finish the the children dumper.
363 void pattern_finish_children(pattern_dumper_t *self, unsigned id)
365 if (self->dump_finish_children)
366 self->dump_finish_children(self, id);
367 } /* pattern_finish_children */
370 * Finish the the dumper.
372 void pattern_end(pattern_dumper_t *self)
375 self->dump_end(self);
381 * pattern dumper factory for text dumper
383 pattern_dumper_t *new_text_dumper(void)
385 pattern_dumper_t *res = XMALLOC(pattern_dumper_t);
391 res->dump_start(res);
393 } /* new_text_dumper */
396 * pattern dumper factory for vcg dumper
398 pattern_dumper_t *new_vcg_dumper(const char *vcg_name, unsigned max_pattern)
400 pattern_dumper_t *res = (pattern_dumper_t*)malloc(sizeof(*res) + sizeof(vcg_private_t));
406 memcpy(res, &vcg_dump, sizeof(*res));
408 priv = (vcg_private_t *)(res + 1);
409 memset(priv, 0, sizeof(*priv));
411 f = fopen(vcg_name, "w");
413 priv->pattern_id = 0;
414 priv->max_pattern = max_pattern ? max_pattern : (unsigned)-1;
418 res->dump_start(res);
422 } /* new_vcg_dumper */