Added file headers.
[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 "heuristical.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         sprintf(buf, "%10lld", cost);
45         return buf;
46 }
47
48 /* print vector */
49 static void dump_vector(FILE *f, vector *vec)
50 {
51         unsigned index;
52         assert(vec);
53
54         fprintf(f, "<span class=\"vector\">( ");
55         unsigned len = vec->len;
56         assert(len > 0);
57         for (index = 0; index < len; ++index) {
58 #if KAPS_ENABLE_VECTOR_NAMES
59                 fprintf(f, "<span title=\"%s\">%s</span> ",
60                                 vec->entries[index].name, cost2a(vec->entries[index].data));
61 #else
62                 fprintf(f, "%s ", cost2a(vec->entries[index].data));
63 #endif
64         }
65         fprintf(f, " )</span>\n");
66 }
67
68 static void dump_matrix(FILE *f, pbqp_matrix *mat)
69 {
70         unsigned row, col;
71         assert(mat);
72         num *p = mat->entries;
73
74         assert(mat->cols> 0);
75         assert(mat->rows> 0);
76         fprintf(f, "\t\\begin{pmatrix}\n");
77         for (row = 0; row < mat->rows; ++row) {
78                 fprintf(f, "\t %s", cost2a(*p++));
79
80                 for (col = 1; col < mat->cols; ++col) {
81                         fprintf(f, "& %s", cost2a(*p++));
82                 }
83                 fprintf(f, "\\\\\n");
84         }
85         fprintf(f, "\t\\end{pmatrix}\n");
86 }
87
88 void dump_edge(FILE *file, pbqp_edge *edge)
89 {
90         fputs("<tex>\n", file);
91         fprintf(file, "\t\\overline\n{C}_{%d,%d}=\n",
92                         edge->src->index, edge->tgt->index);
93         dump_matrix(file, edge->costs);
94         fputs("</tex><br>", file);
95 }
96
97 static void dump_edge_costs(pbqp *pbqp)
98 {
99         unsigned src_index;
100
101         assert(pbqp);
102         assert(pbqp->dump_file);
103
104         fputs("<p>", pbqp->dump_file);
105         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
106                 pbqp_node *src_node = get_node(pbqp, src_index);
107
108                 if (!src_node)
109                         continue;
110
111                 unsigned edge_index;
112                 unsigned len = ARR_LEN(src_node->edges);
113                 for (edge_index = 0; edge_index < len; ++edge_index) {
114                         pbqp_edge *edge = src_node->edges[edge_index];
115                         unsigned tgt_index = edge->tgt->index;
116                         if (src_index < tgt_index) {
117                                 dump_edge(pbqp->dump_file, edge);
118                         }
119                 }
120         }
121         fputs("</p>", pbqp->dump_file);
122 }
123
124 void dump_node(FILE *file, pbqp_node *node)
125 {
126         assert(file);
127
128         if (node) {
129                 fprintf(file, "\tc<sub>%d</sub> = ", node->index);
130                 dump_vector(file, node->costs);
131                 fputs("<br>\n", file);
132         }
133 }
134
135 static void dump_node_costs(pbqp *pbqp)
136 {
137         unsigned index;
138
139         assert(pbqp);
140         assert(pbqp->dump_file);
141
142         /* dump node costs */
143         fputs("<p>", pbqp->dump_file);
144         for (index = 0; index < pbqp->num_nodes; ++index) {
145                 dump_node(pbqp->dump_file, get_node(pbqp, index));
146         }
147         fputs("</p>", pbqp->dump_file);
148 }
149
150 void dump_section(FILE *f, int level, char *txt)
151 {
152         assert(f);
153
154         fprintf(f, "<h%d>%s</h%d>\n", level, txt, level);
155 }
156
157 void pbqp_dump_graph(pbqp *pbqp)
158 {
159         unsigned src_index;
160
161         assert(pbqp);
162         assert(pbqp->dump_file);
163
164         fputs("<p>\n<graph>\n\tgraph input {\n", pbqp->dump_file);
165         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
166                 pbqp_node *node = get_node(pbqp, src_index);
167                 if (node && !node_is_reduced(node)) {
168                         fprintf(pbqp->dump_file, "\t n%d;\n", src_index);
169                 }
170         }
171
172         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
173                 pbqp_node *node = get_node(pbqp, src_index);
174
175                 if (!node)
176                         continue;
177
178                 if (node_is_reduced(node))
179                         continue;
180
181                 unsigned len = ARR_LEN(node->edges);
182                 unsigned edge_index;
183                 for (edge_index = 0; edge_index < len; ++edge_index) {
184                         pbqp_node *tgt_node = node->edges[edge_index]->tgt;
185                         unsigned tgt_index = tgt_node->index;
186
187                         if (node_is_reduced(tgt_node))
188                                 continue;
189
190                         if (src_index < tgt_index) {
191                                 fprintf(pbqp->dump_file, "\t n%d -- n%d;\n", src_index,
192                                                 tgt_index);
193                         }
194                 }
195         }
196         fputs("\t}\n</graph>\n</p>\n", pbqp->dump_file);
197 }
198
199 void pbqp_dump_input(pbqp *pbqp)
200 {
201         assert(pbqp);
202         assert(pbqp->dump_file);
203
204         dump_section(pbqp->dump_file, 1, "1. PBQP Problem");
205         dump_section(pbqp->dump_file, 2, "1.1 Topology");
206         pbqp_dump_graph(pbqp);
207         dump_section(pbqp->dump_file, 2, "1.2 Cost Vectors");
208         dump_node_costs(pbqp);
209         dump_section(pbqp->dump_file, 2, "1.3 Cost Matrices");
210         dump_edge_costs(pbqp);
211 }
212
213 void dump_simplifyedge(pbqp *pbqp, pbqp_edge *edge)
214 {
215         assert(pbqp);
216         assert(pbqp->dump_file);
217
218         dump_node(pbqp->dump_file, edge->src);
219         dump_edge(pbqp->dump_file, edge);
220         dump_node(pbqp->dump_file, edge->tgt);
221 }