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;
178 if (priv->pattern_id > priv->max_pattern)
181 fprintf(priv->f, " edge: { sourcename: \"n%u_%u\" targetname: \"n%u_%u\" label: \"%u\" }\n",
182 priv->pattern_id, src,
183 priv->pattern_id, tgt,
186 } /* vcg_dump_edge */
191 static pattern_dumper_t vcg_dump = {
192 vcg_dump_new_pattern,
193 vcg_dump_finish_pattern,
205 * Starts a new pattern.
207 static void stdout_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
209 FILE *f = self->data;
211 fprintf(f, "%8u ", cnt_to_uint(cnt));
212 } /* stdout_dump_new_pattern */
216 * Finish the current pattern.
218 static void stdout_dump_finish_pattern(pattern_dumper_t *self)
220 FILE *f = self->data;
223 } /* stdout_dump_finish_pattern */
228 static void stdout_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
230 FILE *f = self->data;
231 ir_op *op = stat_get_op_from_opcode(op_code);
232 ir_mode *mode = (ir_mode *)mode_code;
235 /* if (env->options & OPT_ENC_GRAPH) */
236 fprintf(f, "%u:", id);
238 fprintf(f, "%s", get_id_str(op->name));
241 fprintf(f, "%s", get_mode_name(mode));
242 } /* stdout_dump_node */
247 static void stdout_dump_ref(pattern_dumper_t *self, unsigned id)
249 FILE *f = self->data;
251 fprintf(f, "REF:%u", id);
252 } /* stdout_dump_ref */
257 static void stdout_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
259 FILE *f = self->data;
267 } /* stdout_dump_edge */
270 * Start the children dumper.
272 static void stdout_start_children(pattern_dumper_t *self, unsigned id)
274 FILE *f = self->data;
278 } /* stdout_start_children */
281 * Finish the children dumper.
283 static void stdout_finish_children(pattern_dumper_t *self, unsigned id)
285 FILE *f = self->data;
289 } /* stdout_finish_children */
294 static const pattern_dumper_t stdout_dump = {
295 stdout_dump_new_pattern,
296 stdout_dump_finish_pattern,
300 stdout_start_children,
301 stdout_finish_children,
307 /* ------------------------------------ API ------------------------------------- */
310 * Starts a new pattern.
312 void pattern_dump_new_pattern(pattern_dumper_t *self, counter_t *cnt)
314 if (self->dump_new_pattern)
315 self->dump_new_pattern(self, cnt);
316 } /* pattern_dump_new_pattern */
320 * Finish the current pattern.
322 void pattern_dump_finish_pattern(pattern_dumper_t *self)
324 if (self->dump_finish_pattern)
325 self->dump_finish_pattern(self);
326 } /* pattern_dump_finish_pattern */
332 void pattern_dump_node(pattern_dumper_t *self, unsigned id, unsigned op_code, unsigned mode_code, void *attr)
335 self->dump_node(self, id, op_code, mode_code, attr);
336 } /* pattern_dump_node */
341 void pattern_dump_ref(pattern_dumper_t *self, unsigned id)
344 self->dump_ref(self, id);
345 } /* pattern_dump_ref */
350 void pattern_dump_edge(pattern_dumper_t *self, unsigned tgt, unsigned src, unsigned pos, unsigned mode_code)
353 self->dump_edge(self, tgt, src, pos, mode_code);
354 } /* pattern_dump_edge */
357 * Start the children dumper.
359 void pattern_start_children(pattern_dumper_t *self, unsigned id)
361 if (self->dump_start_children)
362 self->dump_start_children(self, id);
363 } /* pattern_start_children */
366 * Finish the the children dumper.
368 void pattern_finish_children(pattern_dumper_t *self, unsigned id)
370 if (self->dump_finish_children)
371 self->dump_finish_children(self, id);
372 } /* pattern_finish_children */
375 * Finish the the dumper.
377 void pattern_end(pattern_dumper_t *self)
380 self->dump_end(self);
386 * pattern dumper factory for text dumper
388 pattern_dumper_t *new_text_dumper(void)
390 pattern_dumper_t *res = malloc(sizeof(*res));
393 memcpy(res, &stdout_dump, sizeof(*res));
397 res->dump_start(res);
400 } /* new_text_dumper */
403 * pattern dumper factory for vcg dumper
405 pattern_dumper_t *new_vcg_dumper(const char *vcg_name, unsigned max_pattern)
407 pattern_dumper_t *res = malloc(sizeof(*res) + sizeof(vcg_private_t));
413 memcpy(res, &vcg_dump, sizeof(*res));
415 priv = (vcg_private_t *)(res + 1);
416 memset(priv, 0, sizeof(*priv));
418 f = fopen(vcg_name, "w");
420 priv->pattern_id = 0;
421 priv->max_pattern = max_pattern ? max_pattern : (unsigned)-1;
425 res->dump_start(res);
429 } /* new_vcg_dumper */