2011-01-22 02:39:05 +00:00
|
|
|
/*
|
2012-11-01 03:19:38 +00:00
|
|
|
dot_sblock.c
|
2011-01-22 02:39:05 +00:00
|
|
|
|
2012-11-01 03:19:38 +00:00
|
|
|
"emit" sblock graphs to dot (graphvis).
|
2011-01-22 02:39:05 +00:00
|
|
|
|
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2011/01/21
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
#include <QF/dstring.h>
|
2012-05-04 00:45:51 +00:00
|
|
|
#include <QF/quakeio.h>
|
2020-03-17 06:23:24 +00:00
|
|
|
#include <QF/set.h>
|
2011-01-22 02:39:05 +00:00
|
|
|
#include <QF/va.h>
|
|
|
|
|
2012-07-18 06:29:30 +00:00
|
|
|
#include "dags.h"
|
2012-10-30 10:25:35 +00:00
|
|
|
#include "flow.h"
|
2011-01-22 02:39:05 +00:00
|
|
|
#include "expr.h"
|
2012-11-05 05:51:39 +00:00
|
|
|
#include "qfcc.h"
|
|
|
|
#include "function.h"
|
2011-01-22 02:39:05 +00:00
|
|
|
#include "statements.h"
|
2012-05-08 02:43:29 +00:00
|
|
|
#include "strpool.h"
|
2011-01-22 02:39:05 +00:00
|
|
|
#include "symtab.h"
|
2011-02-08 05:45:48 +00:00
|
|
|
#include "type.h"
|
2011-01-22 02:39:05 +00:00
|
|
|
|
|
|
|
static void
|
2012-05-04 00:45:51 +00:00
|
|
|
flow_statement (dstring_t *dstr, statement_t *s)
|
2011-01-22 02:39:05 +00:00
|
|
|
{
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, " <tr>");
|
2012-11-05 10:01:04 +00:00
|
|
|
dasprintf (dstr, "<td>%d</td>", s->number);
|
2012-10-30 03:24:03 +00:00
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(quote_string (s->opcode)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(operand_string (s->opa)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(operand_string (s->opb)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(operand_string (s->opc)));
|
2020-03-17 06:23:24 +00:00
|
|
|
#if 0
|
|
|
|
if (s->number >= 0) {
|
|
|
|
set_t *use = set_new ();
|
|
|
|
set_t *def = set_new ();
|
|
|
|
set_t *kill = set_new ();
|
|
|
|
set_t *ops = set_new ();
|
2020-03-17 07:32:26 +00:00
|
|
|
operand_t *operands[FLOW_OPERANDS];
|
2020-03-17 06:23:24 +00:00
|
|
|
|
|
|
|
flow_analyze_statement (s, use, def, kill, operands);
|
2020-03-17 07:32:26 +00:00
|
|
|
for (int i = 0; i < FLOW_OPERANDS; i++) {
|
2020-03-17 06:23:24 +00:00
|
|
|
if (operands[i]) {
|
|
|
|
set_add (ops, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(set_as_string (use)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(set_as_string (def)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(set_as_string (kill)));
|
|
|
|
dasprintf (dstr, "<td>%s</td>", html_string(set_as_string (ops)));
|
|
|
|
|
|
|
|
set_delete (use);
|
|
|
|
set_delete (def);
|
|
|
|
set_delete (kill);
|
|
|
|
}
|
|
|
|
#endif
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, "</tr>\n");
|
2011-01-22 02:39:05 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 02:31:55 +00:00
|
|
|
void
|
|
|
|
dot_sblock (dstring_t *dstr, sblock_t *sblock, int blockno)
|
2011-01-22 02:39:05 +00:00
|
|
|
{
|
|
|
|
statement_t *s;
|
2012-05-03 10:10:44 +00:00
|
|
|
ex_label_t *l;
|
2011-01-22 02:39:05 +00:00
|
|
|
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, " sb_%p [shape=none,label=<\n", sblock);
|
|
|
|
dasprintf (dstr, " <table border=\"0\" cellborder=\"1\" "
|
|
|
|
"cellspacing=\"0\">\n");
|
|
|
|
dasprintf (dstr, " <tr>\n");
|
2012-11-05 10:01:04 +00:00
|
|
|
dasprintf (dstr, " <td colspan=\"2\" >%p(%d)</td>\n", sblock,
|
|
|
|
blockno);
|
2020-03-17 15:02:36 +00:00
|
|
|
dasprintf (dstr, " <td height=\"0\" colspan=\"3\" port=\"s\">\n");
|
2012-05-03 10:10:44 +00:00
|
|
|
for (l = sblock->labels; l; l = l->next)
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, " %s(%d)\n", l->name, l->used);
|
|
|
|
dasprintf (dstr, " </td>\n");
|
|
|
|
dasprintf (dstr, " </tr>\n");
|
2011-01-22 02:39:05 +00:00
|
|
|
for (s = sblock->statements; s; s = s->next)
|
2012-05-04 00:45:51 +00:00
|
|
|
flow_statement (dstr, s);
|
|
|
|
dasprintf (dstr, " <tr>\n");
|
2012-11-06 13:10:04 +00:00
|
|
|
dasprintf (dstr, " <td colspan=\"2\"></td>\n");
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, " <td height=\"0\" colspan=\"2\" "
|
|
|
|
"port=\"e\"></td>\n");
|
|
|
|
dasprintf (dstr, " <td></td>\n");
|
|
|
|
dasprintf (dstr, " </tr>\n");
|
|
|
|
dasprintf (dstr, " </table>>];\n");
|
2012-12-11 02:31:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
flow_sblock (dstring_t *dstr, sblock_t *sblock, int blockno)
|
|
|
|
{
|
|
|
|
sblock_t **target;
|
|
|
|
sblock_t **target_list;
|
|
|
|
|
|
|
|
dot_sblock (dstr, sblock, blockno);
|
2012-11-21 03:23:15 +00:00
|
|
|
if (sblock->statements) {
|
|
|
|
statement_t *st = (statement_t *) sblock->tail;
|
|
|
|
if (sblock->next
|
2012-11-30 05:06:52 +00:00
|
|
|
&& !statement_is_goto (st)
|
|
|
|
&& !statement_is_jumpb (st)
|
|
|
|
&& !statement_is_return (st))
|
2012-11-21 03:23:15 +00:00
|
|
|
dasprintf (dstr, " sb_%p:e -> sb_%p:s;\n", sblock, sblock->next);
|
2012-11-30 05:06:52 +00:00
|
|
|
if ((target_list = statement_get_targetlist (st))) {
|
2012-11-21 03:23:15 +00:00
|
|
|
for (target = target_list; *target; target++)
|
|
|
|
dasprintf (dstr, " sb_%p:e -> sb_%p:s [label=\"%s\"];\n",
|
|
|
|
sblock, *target, st->opcode);
|
|
|
|
free (target_list);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sblock->next)
|
|
|
|
dasprintf (dstr, " sb_%p:e -> sb_%p:s;\n", sblock, sblock->next);
|
2012-11-03 09:59:15 +00:00
|
|
|
}
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, "\n");
|
2011-01-22 02:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-01 03:19:38 +00:00
|
|
|
print_sblock (sblock_t *sblock, const char *filename)
|
2011-01-22 02:39:05 +00:00
|
|
|
{
|
2012-05-03 13:09:49 +00:00
|
|
|
int i;
|
2012-05-04 00:45:51 +00:00
|
|
|
dstring_t *dstr = dstring_newstr();
|
2012-05-03 13:09:49 +00:00
|
|
|
|
2012-11-01 03:19:38 +00:00
|
|
|
dasprintf (dstr, "digraph sblock_%p {\n", sblock);
|
2020-03-06 12:05:53 +00:00
|
|
|
dasprintf (dstr, " graph [label=\"%s\"];\n", quote_string (filename));
|
2012-05-04 00:45:51 +00:00
|
|
|
dasprintf (dstr, " layout=dot; rankdir=TB;\n");
|
2012-05-03 13:09:49 +00:00
|
|
|
for (i = 0; sblock; sblock = sblock->next, i++)
|
2012-05-04 00:45:51 +00:00
|
|
|
flow_sblock (dstr, sblock, i);
|
|
|
|
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);
|
2011-01-22 02:39:05 +00:00
|
|
|
}
|