bearch: Dump the output requirement and the assigned register in the same line for...
[libfirm] / ir / kaps / 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  */
26 #include "config.h"
27
28 #include "adt/array.h"
29 #include "assert.h"
30
31 #include "pbqp_edge_t.h"
32 #include "pbqp_node_t.h"
33 #include "optimal.h"
34 #include "html_dumper.h"
35 #include "kaps.h"
36
37 /* Caution: Due to static buffer use only once per statement */
38 static const char *cost2a(num const cost)
39 {
40         static char buf[10];
41
42         if (cost == INF_COSTS) return "inf";
43 #if KAPS_USE_UNSIGNED
44         sprintf(buf, "%u", cost);
45 #else
46         sprintf(buf, "%10lld", cost);
47 #endif
48         return buf;
49 }
50
51 /* print vector */
52 static void dump_vector(FILE *f, vector_t *vec)
53 {
54         unsigned index;
55         unsigned len = vec->len;
56
57         fprintf(f, "<span class=\"vector\">( ");
58         assert(len > 0);
59         for (index = 0; index < len; ++index) {
60 #if KAPS_ENABLE_VECTOR_NAMES
61                 fprintf(f, "<span title=\"%s\">%s</span> ",
62                                 vec->entries[index].name, cost2a(vec->entries[index].data));
63 #else
64                 fprintf(f, "%s ", cost2a(vec->entries[index].data));
65 #endif
66         }
67         fprintf(f, " )</span>\n");
68 }
69
70 static void dump_matrix(FILE *f, pbqp_matrix_t *mat)
71 {
72         unsigned row, col;
73         num *p = mat->entries;
74
75         assert(mat->cols > 0);
76         assert(mat->rows > 0);
77         fprintf(f, "\t\\begin{pmatrix}\n");
78         for (row = 0; row < mat->rows; ++row) {
79                 fprintf(f, "\t %s", cost2a(*p++));
80
81                 for (col = 1; col < mat->cols; ++col) {
82                         fprintf(f, "& %s", cost2a(*p++));
83                 }
84                 fprintf(f, "\\\\\n");
85         }
86         fprintf(f, "\t\\end{pmatrix}\n");
87 }
88
89 void pbqp_dump_edge(FILE *file, pbqp_edge_t *edge)
90 {
91         fputs("<tex>\n", file);
92         fprintf(file, "\t\\overline\n{C}_{%u,%u}=\n",
93                         edge->src->index, edge->tgt->index);
94         dump_matrix(file, edge->costs);
95         fputs("</tex><br>", file);
96 }
97
98 static void dump_edge_costs(pbqp_t *pbqp)
99 {
100         unsigned src_index;
101
102         fputs("<p>", pbqp->dump_file);
103         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
104                 pbqp_node_t *src_node = get_node(pbqp, src_index);
105                 size_t edge_index;
106                 size_t len;
107
108                 if (!src_node)
109                         continue;
110
111                 len = ARR_LEN(src_node->edges);
112                 for (edge_index = 0; edge_index < len; ++edge_index) {
113                         pbqp_edge_t *edge      = src_node->edges[edge_index];
114                         unsigned     tgt_index = edge->tgt->index;
115                         if (src_index < tgt_index) {
116                                 pbqp_dump_edge(pbqp->dump_file, edge);
117                         }
118                 }
119         }
120         fputs("</p>", pbqp->dump_file);
121 }
122
123 void pbqp_dump_node(FILE *file, pbqp_node_t *node)
124 {
125         if (node) {
126                 fprintf(file, "\tc<sub>%u</sub> = ", node->index);
127                 dump_vector(file, node->costs);
128                 fputs("<br>\n", file);
129         }
130 }
131
132 static void dump_node_costs(pbqp_t *pbqp)
133 {
134         unsigned index;
135
136         /* dump node costs */
137         fputs("<p>", pbqp->dump_file);
138         for (index = 0; index < pbqp->num_nodes; ++index) {
139                 pbqp_dump_node(pbqp->dump_file, get_node(pbqp, index));
140         }
141         fputs("</p>", pbqp->dump_file);
142 }
143
144 void pbqp_dump_section(FILE *f, int level, const char *txt)
145 {
146         fprintf(f, "<h%d>%s</h%d>\n", level, txt, level);
147 }
148
149 void pbqp_dump_graph(pbqp_t *pbqp)
150 {
151         unsigned src_index;
152
153         fputs("<p>\n<graph>\n\tgraph input {\n", pbqp->dump_file);
154         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
155                 pbqp_node_t *node = get_node(pbqp, src_index);
156                 if (node && !node_is_reduced(node)) {
157                         fprintf(pbqp->dump_file, "\t n%u;\n", src_index);
158                 }
159         }
160
161         for (src_index = 0; src_index < pbqp->num_nodes; ++src_index) {
162                 pbqp_node_t *node = get_node(pbqp, src_index);
163                 unsigned len;
164                 unsigned edge_index;
165
166                 if (!node)
167                         continue;
168
169                 if (node_is_reduced(node))
170                         continue;
171
172                 len = ARR_LEN(node->edges);
173                 for (edge_index = 0; edge_index < len; ++edge_index) {
174                         pbqp_node_t *tgt_node  = node->edges[edge_index]->tgt;
175                         unsigned     tgt_index = tgt_node->index;
176
177                         if (node_is_reduced(tgt_node))
178                                 continue;
179
180                         if (src_index < tgt_index) {
181                                 fprintf(pbqp->dump_file, "\t n%u -- n%u;\n", src_index,
182                                                 tgt_index);
183                         }
184                 }
185         }
186         fputs("\t}\n</graph>\n</p>\n", pbqp->dump_file);
187 }
188
189 void pbqp_dump_input(pbqp_t *pbqp)
190 {
191         pbqp_dump_section(pbqp->dump_file, 1, "1. PBQP Problem");
192         pbqp_dump_section(pbqp->dump_file, 2, "1.1 Topology");
193         pbqp_dump_graph(pbqp);
194         pbqp_dump_section(pbqp->dump_file, 2, "1.2 Cost Vectors");
195         dump_node_costs(pbqp);
196         pbqp_dump_section(pbqp->dump_file, 2, "1.3 Cost Matrices");
197         dump_edge_costs(pbqp);
198 }
199
200 void pbqp_dump_simplifyedge(pbqp_t *pbqp, pbqp_edge_t *edge)
201 {
202         pbqp_dump_node(pbqp->dump_file, edge->src);
203         pbqp_dump_edge(pbqp->dump_file, edge);
204         pbqp_dump_node(pbqp->dump_file, edge->tgt);
205 }