2 * Copyright (C) 1995-2007 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
38 #include "firmstat_t.h"
39 #include "pattern_dmp.h"
41 /* dumper operations */
42 typedef void (*DUMP_NEW_PATTERN_FUNC)(pattern_dumper_t *self, counter_t *cnt);
43 typedef void (*DUMP_FINISH_PATTERN_FUNC)(pattern_dumper_t *self);
44 typedef void (*DUMP_NODE_FUNC)(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr);
45 typedef void (*DUMP_REF_FUNC)(pattern_dumper_t *self, unsigned id);
46 typedef void (*DUMP_EDGE_FUNC)(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code);
47 typedef void (*DUMP_START_CHILDREN_FUNC)(pattern_dumper_t *self, unsigned id);
48 typedef void (*DUMP_FINISH_CHILDREN_FUNC)(pattern_dumper_t *self, unsigned id);
49 typedef void (*DUMP_START_FUNC)(pattern_dumper_t *self);
50 typedef void (*DUMP_END_FUNC)(pattern_dumper_t *self);
55 struct _pattern_dumper_t {
56 DUMP_NEW_PATTERN_FUNC dump_new_pattern;
57 DUMP_FINISH_PATTERN_FUNC dump_finish_pattern;
58 DUMP_NODE_FUNC dump_node;
59 DUMP_REF_FUNC dump_ref;
60 DUMP_EDGE_FUNC dump_edge;
61 DUMP_START_CHILDREN_FUNC dump_start_children;
62 DUMP_FINISH_CHILDREN_FUNC dump_finish_children;
63 DUMP_START_FUNC dump_start;
64 DUMP_END_FUNC dump_end;
71 typedef struct _vcg_private_t {
72 FILE *f; /**< file to dump to */
73 unsigned pattern_id; /**< ID of the pattern */
74 unsigned max_pattern; /**< maximum number of pattern to be dumped */
78 * Starts a new VCG graph.
80 static void vcg_dump_start(pattern_dumper_t *self)
82 vcg_private_t *priv = self->data;
85 "graph: { title: \"Most found pattern\"\n"
86 " display_edge_labels: no\n"
87 " layoutalgorithm: mindepth\n"
88 " manhattan_edges: yes\n"
90 " orientation: bottom_to_top\n"
92 } /* vcg_dump_start */
95 * Ends a new VCG graph.
97 static void vcg_dump_end(pattern_dumper_t *self)
99 vcg_private_t *priv = self->data;
101 fprintf(priv->f, "}\n");
106 * Starts a new pattern.
108 static void vcg_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
110 vcg_private_t *priv = self->data;
111 static unsigned nr = 0;
113 if (priv->pattern_id > priv->max_pattern)
117 " graph: { title: \"g%u\" label: \"pattern %u\" status:clustered color:yellow\n",
118 priv->pattern_id, priv->pattern_id );
120 /* add a pseudo node for the count of this pattern */
122 " node: {title: \"c%u\" label: \"cnt: %u\" color:red }\n",
123 ++nr, cnt_to_uint(cnt)
125 } /* vcg_dump_new_pattern */
128 * Finish the current pattern.
130 static void vcg_dump_finish_pattern(pattern_dumper_t *self)
132 vcg_private_t *priv = self->data;
134 if (priv->pattern_id > priv->max_pattern)
137 fprintf(priv->f, " }\n");
139 if (priv->pattern_id > 0)
140 fprintf(priv->f, " edge: { sourcename: \"g%u\" targetname: \"g%u\" linestyle:invisible}\n",
142 priv->pattern_id - 1);
145 } /* vcg_dump_finish_pattern */
150 static void vcg_dump_node(pattern_dumper_t *self, unsigned id,
151 unsigned op_code, unsigned mode_code, void *attr)
153 vcg_private_t *priv = self->data;
154 ir_op *op = stat_get_op_from_opcode(op_code);
155 ir_mode *mode = (ir_mode *)mode_code;
156 long l = attr ? *(long *)attr : 0;
158 if (priv->pattern_id > priv->max_pattern)
162 fprintf(priv->f, " node: {title: \"n%u_%u\" label: \"%s%s %ld n%u\" }\n",
163 priv->pattern_id, id, get_id_str(op->name), mode ? get_mode_name(mode) : "", l, id);
165 fprintf(priv->f, " node: {title: \"n%u_%u\" label: \"%s%s n%u\" }\n",
166 priv->pattern_id, id, get_id_str(op->name), mode ? get_mode_name(mode) : "", id);
168 } /* vcg_dump_node */
173 static void vcg_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
175 vcg_private_t *priv = self->data;
177 if (priv->pattern_id > priv->max_pattern)
180 fprintf(priv->f, " edge: { sourcename: \"n%u_%u\" targetname: \"n%u_%u\" label: \"%u\" }\n",
181 priv->pattern_id, src,
182 priv->pattern_id, tgt,
185 } /* vcg_dump_edge */
190 static pattern_dumper_t vcg_dump = {
191 vcg_dump_new_pattern,
192 vcg_dump_finish_pattern,
204 * Starts a new pattern.
206 static void stdout_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
208 FILE *f = self->data;
210 fprintf(f, "%8u ", cnt_to_uint(cnt));
211 } /* stdout_dump_new_pattern */
215 * Finish the current pattern.
217 static void stdout_dump_finish_pattern(pattern_dumper_t *self)
219 FILE *f = self->data;
222 } /* stdout_dump_finish_pattern */
227 static void stdout_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
229 FILE *f = self->data;
230 ir_op *op = stat_get_op_from_opcode(op_code);
231 ir_mode *mode = (ir_mode *)mode_code;
233 /* if (env->options & OPT_ENC_GRAPH) */
234 fprintf(f, "%u:", id);
236 fprintf(f, "%s", get_id_str(op->name));
239 fprintf(f, "%s", get_mode_name(mode));
240 } /* stdout_dump_node */
245 static void stdout_dump_ref(pattern_dumper_t *self, unsigned id)
247 FILE *f = self->data;
249 fprintf(f, "REF:%u", id);
250 } /* stdout_dump_ref */
255 static void stdout_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
257 FILE *f = self->data;
261 } /* stdout_dump_edge */
264 * Start the children dumper.
266 static void stdout_start_children(pattern_dumper_t *self, unsigned id)
268 FILE *f = self->data;
271 } /* stdout_start_children */
274 * Finish the children dumper.
276 static void stdout_finish_children(pattern_dumper_t *self, unsigned id)
278 FILE *f = self->data;
281 } /* stdout_finish_children */
286 static const pattern_dumper_t stdout_dump = {
287 stdout_dump_new_pattern,
288 stdout_dump_finish_pattern,
292 stdout_start_children,
293 stdout_finish_children,
299 /* ------------------------------------ API ------------------------------------- */
302 * Starts a new pattern.
304 void pattern_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
306 if (self->dump_new_pattern)
307 self->dump_new_pattern(self, cnt);
308 } /* pattern_dump_new_pattern */
312 * Finish the current pattern.
314 void pattern_dump_finish_pattern(pattern_dumper_t *self)
316 if (self->dump_finish_pattern)
317 self->dump_finish_pattern(self);
318 } /* pattern_dump_finish_pattern */
324 void pattern_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
327 self->dump_node(self, id, op_code, mode_code, attr);
328 } /* pattern_dump_node */
333 void pattern_dump_ref(pattern_dumper_t *self, unsigned id)
336 self->dump_ref(self, id);
337 } /* pattern_dump_ref */
342 void pattern_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
345 self->dump_edge(self, tgt, src, pos, mode_code);
346 } /* pattern_dump_edge */
349 * Start the children dumper.
351 void pattern_start_children(pattern_dumper_t *self, unsigned id)
353 if (self->dump_start_children)
354 self->dump_start_children(self, id);
355 } /* pattern_start_children */
358 * Finish the the children dumper.
360 void pattern_finish_children(pattern_dumper_t *self, unsigned id)
362 if (self->dump_finish_children)
363 self->dump_finish_children(self, id);
364 } /* pattern_finish_children */
367 * Finish the the dumper.
369 void pattern_end(pattern_dumper_t *self)
372 self->dump_end(self);
378 * pattern dumper factory for text dumper
380 pattern_dumper_t *new_text_dumper(void)
382 pattern_dumper_t *res = malloc(sizeof(*res));
385 memcpy(res, &stdout_dump, sizeof(*res));
389 res->dump_start(res);
392 } /* new_text_dumper */
395 * pattern dumper factory for vcg dumper
397 pattern_dumper_t *new_vcg_dumper(const char *vcg_name, unsigned max_pattern)
399 pattern_dumper_t *res = malloc(sizeof(*res) + sizeof(vcg_private_t));
405 memcpy(res, &vcg_dump, sizeof(*res));
407 priv = (vcg_private_t *)(res + 1);
408 memset(priv, 0, sizeof(*priv));
410 f = fopen(vcg_name, "w");
412 priv->pattern_id = 0;
413 priv->max_pattern = max_pattern ? max_pattern : (unsigned)-1;
417 res->dump_start(res);
421 } /* new_vcg_dumper */