2012-05-08 02:44:16 +00:00
|
|
|
/*
|
|
|
|
dot_dag.c
|
|
|
|
|
|
|
|
Output dags to dot (graphvis).
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/05/08
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/quakeio.h"
|
|
|
|
#include "QF/set.h"
|
|
|
|
#include "QF/va.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
|
|
|
|
#include "dags.h"
|
|
|
|
#include "statements.h"
|
2012-11-18 10:08:16 +00:00
|
|
|
#include "strpool.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
#include "type.h"
|
|
|
|
|
2012-11-06 12:50:29 +00:00
|
|
|
static void
|
2012-12-11 10:50:31 +00:00
|
|
|
print_node_def (dstring_t *dstr, dag_t *dag, dagnode_t *node)
|
2012-11-06 12:50:29 +00:00
|
|
|
{
|
2012-12-11 10:50:31 +00:00
|
|
|
set_iter_t *id_iter;
|
|
|
|
daglabel_t *id;
|
|
|
|
|
|
|
|
dasprintf (dstr, " \"dagnode_%p\" [%slabel=\"%s%s (%d)", node,
|
|
|
|
node->type != st_none ? "" : "shape=box,",
|
|
|
|
daglabel_string (node->label),
|
|
|
|
node->killed ? " k" : "",
|
|
|
|
node->topo);
|
|
|
|
for (id_iter = set_first (node->identifiers); id_iter;
|
|
|
|
id_iter = set_next (id_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
id = dag->labels[id_iter->element];
|
2012-12-11 10:50:31 +00:00
|
|
|
dasprintf (dstr, "\\n%s", daglabel_string(id));
|
|
|
|
}
|
|
|
|
dasprintf (dstr, "\"];\n");
|
2012-11-06 12:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-17 12:14:42 +00:00
|
|
|
print_root_nodes (dstring_t *dstr, dag_t *dag)
|
2012-11-06 12:50:29 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
set_iter_t *node_iter;
|
2012-12-11 10:50:31 +00:00
|
|
|
// dasprintf (dstr, " subgraph roots_%p {", dag);
|
|
|
|
// dasprintf (dstr, " rank=same;");
|
2012-11-17 12:14:42 +00:00
|
|
|
for (node_iter = set_first (dag->roots); node_iter;
|
2012-11-18 02:34:01 +00:00
|
|
|
node_iter = set_next (node_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
dagnode_t *node = dag->nodes[node_iter->element];
|
2012-12-11 10:50:31 +00:00
|
|
|
print_node_def (dstr, dag, node);
|
2012-11-21 04:43:03 +00:00
|
|
|
dasprintf (dstr, " dag_enter_%p ->dagnode_%p [style=invis];\n",
|
|
|
|
dag, node);
|
2012-11-18 02:34:01 +00:00
|
|
|
}
|
2012-12-11 10:50:31 +00:00
|
|
|
// dasprintf (dstr, " }\n");
|
2012-11-06 12:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-17 12:14:42 +00:00
|
|
|
print_child_nodes (dstring_t *dstr, dag_t *dag)
|
2012-11-06 12:50:29 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
int i;
|
|
|
|
dagnode_t *node;
|
|
|
|
|
|
|
|
for (i = 0; i < dag->num_nodes; i++) {
|
|
|
|
node = dag->nodes[i];
|
|
|
|
if (!set_is_empty (node->parents))
|
2012-12-11 10:50:31 +00:00
|
|
|
print_node_def (dstr, dag, node);
|
2012-11-06 12:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 02:44:16 +00:00
|
|
|
static void
|
2012-11-17 12:14:42 +00:00
|
|
|
print_node (dstring_t *dstr, dag_t *dag, dagnode_t *node)
|
2012-05-08 02:44:16 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
int i;
|
2012-11-18 03:55:44 +00:00
|
|
|
set_t *edges = set_new ();
|
|
|
|
set_iter_t *edge_iter;
|
2012-11-17 12:14:42 +00:00
|
|
|
|
2012-12-25 08:07:58 +00:00
|
|
|
if (!set_is_member (dag->roots, node->number)
|
|
|
|
&& set_is_empty (node->parents))
|
|
|
|
return;
|
2012-11-18 03:55:44 +00:00
|
|
|
set_assign (edges, node->edges);
|
2012-11-17 12:14:42 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (node->children[i]) {
|
2012-11-18 03:55:44 +00:00
|
|
|
set_remove (edges, node->children[i]->number);
|
2012-11-17 12:14:42 +00:00
|
|
|
dasprintf (dstr,
|
|
|
|
" \"dagnode_%p\" -> \"dagnode_%p\" [label=%c];\n",
|
|
|
|
node, node->children[i], i + 'a');
|
|
|
|
}
|
2012-07-15 12:09:13 +00:00
|
|
|
}
|
2012-11-18 03:55:44 +00:00
|
|
|
for (edge_iter = set_first (edges); edge_iter;
|
|
|
|
edge_iter = set_next (edge_iter)) {
|
|
|
|
dasprintf (dstr,
|
|
|
|
" \"dagnode_%p\" -> \"dagnode_%p\" [style=dashed];\n",
|
2012-12-21 05:09:00 +00:00
|
|
|
node, dag->nodes[edge_iter->element]);
|
2012-11-18 03:55:44 +00:00
|
|
|
}
|
|
|
|
set_delete (edges);
|
2012-12-11 10:50:31 +00:00
|
|
|
if (0 && !set_is_empty (node->identifiers)) {
|
2012-11-17 12:14:42 +00:00
|
|
|
set_iter_t *id_iter;
|
2012-05-08 02:44:16 +00:00
|
|
|
daglabel_t *id;
|
|
|
|
|
2012-11-06 12:50:29 +00:00
|
|
|
dasprintf (dstr, " \"dagnode_%p\" -> \"dagid_%p\" "
|
2012-07-17 05:01:15 +00:00
|
|
|
"[style=dashed,dir=none];\n", node, node);
|
2012-07-15 12:09:13 +00:00
|
|
|
dasprintf (dstr, " \"dagid_%p\" [shape=none,label=<\n", node);
|
2012-05-08 02:44:16 +00:00
|
|
|
dasprintf (dstr, " <table border=\"0\" cellborder=\"1\" "
|
|
|
|
"cellspacing=\"0\">\n");
|
|
|
|
dasprintf (dstr, " <tr>\n");
|
|
|
|
dasprintf (dstr, " <td>");
|
2012-11-17 12:14:42 +00:00
|
|
|
for (id_iter = set_first (node->identifiers); id_iter;
|
|
|
|
id_iter = set_next (id_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
id = dag->labels[id_iter->element];
|
2012-11-18 10:08:16 +00:00
|
|
|
dasprintf (dstr, " %s", html_string (daglabel_string(id)));
|
2012-11-17 12:14:42 +00:00
|
|
|
}
|
2012-05-08 02:44:16 +00:00
|
|
|
dasprintf (dstr, " </td>");
|
|
|
|
dasprintf (dstr, " </tr>\n");
|
|
|
|
dasprintf (dstr, " </table>>];\n");
|
|
|
|
}
|
2012-11-21 04:43:03 +00:00
|
|
|
if (set_is_empty (node->edges))
|
|
|
|
dasprintf (dstr,
|
|
|
|
" \"dagnode_%p\" -> \"dag_leave_%p\" [style=invis];\n",
|
|
|
|
node, dag);
|
2012-05-08 02:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-21 04:43:03 +00:00
|
|
|
print_dag (dstring_t *dstr, dag_t *dag, const char *label)
|
2012-05-08 02:44:16 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
int i;
|
2012-11-21 06:03:44 +00:00
|
|
|
|
|
|
|
if (label) {
|
|
|
|
dasprintf (dstr, " subgraph cluster_dag_%p {\n", dag);
|
2012-11-21 04:43:03 +00:00
|
|
|
dasprintf (dstr, " label=\"%s\";\n", label);
|
2012-11-21 06:03:44 +00:00
|
|
|
}
|
2012-11-21 04:43:03 +00:00
|
|
|
dasprintf (dstr, " dag_enter_%p [label=\"\", style=invis];\n", dag);
|
2012-11-06 12:50:29 +00:00
|
|
|
print_root_nodes (dstr, dag);
|
|
|
|
print_child_nodes (dstr, dag);
|
2012-11-17 12:14:42 +00:00
|
|
|
for (i = 0; i < dag->num_nodes; i++)
|
|
|
|
print_node (dstr, dag, dag->nodes[i]);
|
2012-11-21 04:43:03 +00:00
|
|
|
dasprintf (dstr, " dag_leave_%p [label=\"\", style=invis];\n", dag);
|
2012-11-21 06:03:44 +00:00
|
|
|
if (label)
|
|
|
|
dasprintf (dstr, " }\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dot_dump_dag (void *_dag, const char *filename)
|
|
|
|
{
|
|
|
|
dag_t *dag = _dag;
|
|
|
|
dstring_t *dstr = dstring_newstr();
|
|
|
|
|
|
|
|
dasprintf (dstr, "digraph dag_%p {\n", dag);
|
2020-03-06 12:05:53 +00:00
|
|
|
dasprintf (dstr, " graph [label=\"%s\"];\n", quote_string (filename));
|
2012-11-21 06:03:44 +00:00
|
|
|
dasprintf (dstr, " layout=dot;\n");
|
|
|
|
dasprintf (dstr, " clusterrank=local;\n");
|
|
|
|
dasprintf (dstr, " rankdir=TB;\n");
|
|
|
|
dasprintf (dstr, " compound=true;\n");
|
|
|
|
print_dag (dstr, dag, "");
|
|
|
|
|
|
|
|
dasprintf (dstr, "}\n");
|
|
|
|
|
|
|
|
if (filename) {
|
|
|
|
QFile *file;
|
|
|
|
|
|
|
|
file = Qopen (filename, "wt");
|
|
|
|
Qwrite (file, dstr->str, dstr->size - 1);
|
|
|
|
Qclose (file);
|
|
|
|
} else {
|
|
|
|
fputs (dstr->str, stdout);
|
|
|
|
}
|
|
|
|
dstring_delete (dstr);
|
2012-05-08 02:44:16 +00:00
|
|
|
}
|