Adapt to coding conventions.
[libfirm] / html_dumper.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   HTML dumper for PBQP.
23  * @date    03.10.2008
24  * @author  Sebastian Buchwald
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "adt/array.h"
30 #include "assert.h"
31
32 #include "pbqp_edge_t.h"
33 #include "pbqp_node_t.h"
34 #include "optimal.h"
35 #include "html_dumper.h"
36 #include "kaps.h"
37
38 /* Caution: Due to static buffer use only once per statement */
39 static const char *cost2a(num const cost)
40 {
41         static char buf[10];
42
43         if (cost == INF_COSTS) return "inf";
44 #if KAPS_USE_UNSIGNED
45         sprintf(buf, "%u", cost);
46 #else
47         sprintf(buf, "%10lld", cost);
48 #endif
49         return buf;
50 }
51
52 /* print vector */
53 static void dump_vector(FILE *f, vector *vec)
54 {
55         unsigned index;
56         assert(vec);
57
58         fprintf(f, "<span class=\"vector\">( ");
59         unsigned len = vec->len;
60         assert(len > 0);
61         for (index = 0; index < len; ++index) {
62 #if KAPS_ENABLE_VECTOR_NAMES
63                 fprintf(f, "<span title=\"%s\">%s</span> ",
64                                 vec->entries[index].name, cost2a(vec->entries[index].data));
65 #else
66                 fprintf(f, "%s ", cost2a(vec->entries[index].data));
67 #endif
68         }
69         fprintf(f, " )</span>\n");
70 }
71
72 static void dump_matrix(FILE *f, pbqp_matrix *mat)
73 {
74         unsigned row, col;
75         assert(mat);
76         num *p = mat->entries;
77
78         assert(mat->cols> 0);
79         assert(mat->rows> 0);
80         fprintf(f, "\t\\begin{pmatrix}\n");
81         for (row = 0; row < mat->rows; ++row) {
82                 fprintf(f, "\t %s", cost2a(*p++));
83
84                 for (col = 1; col < mat->cols; ++col) {
85                         fprintf(f, "& %s", cost2a(*p++));
86                 }
87                 fprintf(f, "\\\\\n");
88         }
89         fprintf(f, "\t\\end{pmatrix}\n");
90 }
91
92 void dump_edge(FILE *file, pbqp_edge *edge)
93 {
94         fputs("<tex>\n", file);
95         fprintf(file, "\t\\overline\n{C}_{%d,%d}=\n",
96                         edge->src->index, edge->tgt->index);
97         dump_matrix(file, edge->costs);
98         fputs("</tex><br>", file);
99 }
100
101 static void dump_edge_costs(pbqp *pbqp)
102 {
103         unsigned src_index;
104
105         assert(pbqp);
106         assert(pbqp->dump_file);
107
108         fputs("<p>", pbqp->dump_file);
109         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
110                 pbqp_node *src_node = get_node(pbqp, src_index);
111
112                 if (!src_node)
113                         continue;
114
115                 unsigned edge_index;
116                 unsigned len = ARR_LEN(src_node->edges);
117                 for (edge_index = 0; edge_index < len; ++edge_index) {
118                         pbqp_edge *edge = src_node->edges[edge_index];
119                         unsigned tgt_index = edge->tgt->index;
120                         if (src_index < tgt_index) {
121                                 dump_edge(pbqp->dump_file, edge);
122                         }
123                 }
124         }
125         fputs("</p>", pbqp->dump_file);
126 }
127
128 void dump_node(FILE *file, pbqp_node *node)
129 {
130         assert(file);
131
132         if (node) {
133                 fprintf(file, "\tc<sub>%d</sub> = ", node->index);
134                 dump_vector(file, node->costs);
135                 fputs("<br>\n", file);
136         }
137 }
138
139 static void dump_node_costs(pbqp *pbqp)
140 {
141         unsigned index;
142
143         assert(pbqp);
144         assert(pbqp->dump_file);
145
146         /* dump node costs */
147         fputs("<p>", pbqp->dump_file);
148         for (index = 0; index < pbqp->num_nodes; ++index) {
149                 dump_node(pbqp->dump_file, get_node(pbqp, index));
150         }
151         fputs("</p>", pbqp->dump_file);
152 }
153
154 void dump_section(FILE *f, int level, char *txt)
155 {
156         assert(f);
157
158         fprintf(f, "<h%d>%s</h%d>\n", level, txt, level);
159 }
160
161 void pbqp_dump_graph(pbqp *pbqp)
162 {
163         unsigned src_index;
164
165         assert(pbqp);
166         assert(pbqp->dump_file);
167
168         fputs("<p>\n<graph>\n\tgraph input {\n", pbqp->dump_file);
169         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
170                 pbqp_node *node = get_node(pbqp, src_index);
171                 if (node && !node_is_reduced(node)) {
172                         fprintf(pbqp->dump_file, "\t n%d;\n", src_index);
173                 }
174         }
175
176         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
177                 pbqp_node *node = get_node(pbqp, src_index);
178
179                 if (!node)
180                         continue;
181
182                 if (node_is_reduced(node))
183                         continue;
184
185                 unsigned len = ARR_LEN(node->edges);
186                 unsigned edge_index;
187                 for (edge_index = 0; edge_index < len; ++edge_index) {
188                         pbqp_node *tgt_node = node->edges[edge_index]->tgt;
189                         unsigned tgt_index = tgt_node->index;
190
191                         if (node_is_reduced(tgt_node))
192                                 continue;
193
194                         if (src_index < tgt_index) {
195                                 fprintf(pbqp->dump_file, "\t n%d -- n%d;\n", src_index,
196                                                 tgt_index);
197                         }
198                 }
199         }
200         fputs("\t}\n</graph>\n</p>\n", pbqp->dump_file);
201 }
202
203 void pbqp_dump_input(pbqp *pbqp)
204 {
205         assert(pbqp);
206         assert(pbqp->dump_file);
207
208         dump_section(pbqp->dump_file, 1, "1. PBQP Problem");
209         dump_section(pbqp->dump_file, 2, "1.1 Topology");
210         pbqp_dump_graph(pbqp);
211         dump_section(pbqp->dump_file, 2, "1.2 Cost Vectors");
212         dump_node_costs(pbqp);
213         dump_section(pbqp->dump_file, 2, "1.3 Cost Matrices");
214         dump_edge_costs(pbqp);
215 }
216
217 void dump_simplifyedge(pbqp *pbqp, pbqp_edge *edge)
218 {
219         assert(pbqp);
220         assert(pbqp->dump_file);
221
222         dump_node(pbqp->dump_file, edge->src);
223         dump_edge(pbqp->dump_file, edge);
224         dump_node(pbqp->dump_file, edge->tgt);
225 }