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