Remove unused vars
[libfirm] / ir / be / bechordal_draw.c
1
2 /**
3  * @file   bechordal_draw.c
4  * @date   12.05.2005
5  * @author Sebastian Hack
6  *
7  * Paint chordal graphs.
8  *
9  * Copyright (C) 2005 Universitaet Karlsruhe
10  * Released under the GPL
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <limits.h>
17
18 #include "pmap.h"
19 #include "pset.h"
20
21 #include "irgwalk.h"
22 #include "irprintf.h"
23 #include "iredges_t.h"
24
25 #include "belive_t.h"
26 #include "bechordal_t.h"
27 #include "besched_t.h"
28 #include "bechordal_draw.h"
29
30 typedef struct {
31   be_chordal_env_t *env;
32   plotter_t inh;
33   const color_t *color;
34   int width;
35 } base_plotter_t;
36
37 #define decl_self(type, from) \
38   type *self = (type *) from
39
40 static void set_color(plotter_t *_self, const color_t *color)
41 {
42   decl_self(base_plotter_t, _self);
43   self->color = color;
44 }
45
46 static const color_t *get_color(const plotter_t *_self)
47 {
48   decl_self(const base_plotter_t, _self);
49   return self->color;
50 }
51
52 static void set_width(plotter_t *_self, int width)
53 {
54   decl_self(base_plotter_t, _self);
55   self->width = width;
56 }
57
58 static int get_width(const plotter_t *_self)
59 {
60   decl_self(const base_plotter_t, _self);
61   return self->width;
62 }
63
64 static void plotter_default_free(plotter_t *self)
65 {
66 }
67
68 typedef struct {
69   base_plotter_t inh;
70   const char *filename;
71   FILE *f;
72 } ps_plotter_t;
73
74
75 /*
76   ____  ____    ____  _       _   _
77  |  _ \/ ___|  |  _ \| | ___ | |_| |_ ___ _ __
78  | |_) \___ \  | |_) | |/ _ \| __| __/ _ \ '__|
79  |  __/ ___) | |  __/| | (_) | |_| ||  __/ |
80  |_|   |____/  |_|   |_|\___/ \__|\__\___|_|
81
82 */
83
84 static void ps_begin(plotter_t *_self, const rect_t *vis)
85 {
86   FILE *f;
87   decl_self(ps_plotter_t, _self);
88
89   f = self->f = fopen(self->filename, "wt");
90   fprintf(f, "%%!PS-Adobe-2.0\n");
91   fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
92 #if 0
93   fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
94   fprintf(f, "mainfont setfont\n");
95 #endif
96 }
97
98 static void ps_setcolor(plotter_t *_self, const color_t *color)
99 {
100   decl_self(ps_plotter_t, _self);
101   set_color(_self, color);
102
103   fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
104       color->r, color->g, color->b);
105 }
106
107 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
108 {
109   decl_self(ps_plotter_t, _self);
110
111   fprintf(self->f, "%d %d moveto\n", x1, y1);
112   fprintf(self->f, "%d %d lineto\n", x2, y2);
113   fprintf(self->f, "stroke\n");
114 }
115
116 static void ps_box(plotter_t *_self, const rect_t *rect)
117 {
118   decl_self(ps_plotter_t, _self);
119
120   fprintf(self->f, "%d %d %d %d rectstroke\n",
121       rect->x, rect->y, rect->w, rect->h);
122 }
123
124 void ps_text(plotter_t *_self, int x, int y, const char *str)
125 {
126   decl_self(ps_plotter_t, _self);
127
128   fprintf(self->f, "%d %d moveto\n", x, y);
129   fprintf(self->f, "(%s) show\n", str);
130 }
131
132 static void ps_finish(plotter_t *_self)
133 {
134   decl_self(ps_plotter_t, _self);
135   fclose(self->f);
136 }
137
138 const plotter_if_t ps_plotter_vtab = {
139   ps_begin,
140   ps_setcolor,
141   get_color,
142   set_width,
143   get_width,
144   ps_line,
145   ps_box,
146   ps_text,
147   ps_finish,
148   plotter_default_free
149 };
150
151 plotter_t *new_plotter_ps(const char *filename)
152 {
153   ps_plotter_t *ps_plotter = xmalloc(sizeof(*ps_plotter));
154   plotter_t *p = (plotter_t *) ps_plotter;
155
156   ps_plotter->filename = filename;
157   p->vtab = &ps_plotter_vtab;
158   return p;
159 }
160
161 /*
162    _____ _ _     _____  ____  _       _   _
163   |_   _(_) | __|__  / |  _ \| | ___ | |_| |_ ___ _ __
164     | | | | |/ /  / /  | |_) | |/ _ \| __| __/ _ \ '__|
165     | | | |   <  / /_  |  __/| | (_) | |_| ||  __/ |
166     |_| |_|_|\_\/____| |_|   |_|\___/ \__|\__\___|_|
167
168 */
169
170 typedef struct {
171   base_plotter_t inh;
172   const char *filename;
173   FILE *f;
174 } tikz_plotter_t;
175
176 static void tikz_begin(plotter_t *_self, const rect_t *vis)
177 {
178   FILE *f;
179   decl_self(tikz_plotter_t, _self);
180
181   f = self->f = fopen(self->filename, "wt");
182   fprintf(f, "\\begin{tikzpicture}\n");
183 }
184
185 static void tikz_setcolor(plotter_t *_self, const color_t *color)
186 {
187   set_color(_self, color);
188 }
189
190 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
191 {
192   decl_self(tikz_plotter_t, _self);
193   fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
194 }
195
196 static void tikz_box(plotter_t *_self, const rect_t *rect)
197 {
198   decl_self(tikz_plotter_t, _self);
199
200   fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
201           rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
202 }
203
204 void tikz_text(plotter_t *_self, int x, int y, const char *str)
205 {
206   decl_self(tikz_plotter_t, _self);
207   fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
208 }
209
210 static void tikz_finish(plotter_t *_self)
211 {
212   decl_self(tikz_plotter_t, _self);
213   fclose(self->f);
214 }
215
216
217 extern void plotter_free(plotter_t *self)
218 {
219   self->vtab->free(self);
220   free(self);
221 }
222
223 const draw_chordal_opts_t draw_chordal_def_opts = {
224   10, 10, 30, 8, 10, 10
225 };
226
227 typedef struct _draw_chordal_env_t {
228   const be_chordal_env_t *chordal_env;
229   const arch_env_t *arch_env;
230   const arch_register_class_t *cls;
231   pmap *block_dims;
232   plotter_t *plotter;
233   const draw_chordal_opts_t *opts;
234
235   struct obstack obst;
236   int max_color;
237 } draw_chordal_env_t;
238
239 struct block_dims {
240   int max_step;
241   int min_step;
242   int max_color;
243   rect_t box;
244   rect_t subtree_box;
245 };
246
247 #undef min
248 static INLINE int min(int a, int b)
249 {
250   return a < b ? a : b;
251 }
252
253 #undef max
254 static INLINE int max(int a, int b)
255 {
256   return a > b ? a : b;
257 }
258
259 #define doz(a, b) max((a) - (b), 0)
260
261 static void block_dims_walker(ir_node *block, void *data)
262 {
263   draw_chordal_env_t *env = data;
264   border_t *b;
265         struct list_head *head = get_block_border_head(env->chordal_env, block);
266   const draw_chordal_opts_t *opts = env->opts;
267   struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
268
269   memset(dims, 0, sizeof(*dims));
270   dims->min_step = INT_MAX;
271
272         list_for_each_entry_reverse(border_t, b, head, list) {
273     ir_node *irn = b->irn;
274     const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
275     int col = arch_register_get_index(reg);
276
277     dims->max_step = max(dims->max_step, b->step);
278     dims->max_color = max(dims->max_color, col);
279     env->max_color = max(env->max_color, col);
280   }
281
282   dims->min_step = 1;
283
284 #if 1
285   dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
286   dims->box.h = dims->max_step * opts->v_inter_gap;
287 #else
288   dims->box.w = dims->box.h = 10;
289 #endif
290
291   pmap_insert(env->block_dims, block, dims);
292 }
293
294 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
295 {
296   const draw_chordal_opts_t *opts = env->opts;
297   struct block_dims *dims = pmap_get(env->block_dims, bl);
298   ir_node *sub;
299   rect_t *rect = &dims->subtree_box;
300   int h_space = 0, v_space = 0;
301
302   memset(rect, 0, sizeof(*rect));
303   rect->x = x;
304
305   dominates_for_each(bl, sub) {
306     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
307
308     layout(env, sub, rect->x + rect->w);
309
310     rect->w += h_space + bl_dim->subtree_box.w;
311     rect->h = max(rect->h, bl_dim->subtree_box.h);
312
313     h_space = opts->h_gap;
314     v_space = opts->v_gap;
315   }
316
317   rect->w = max(rect->w, dims->box.w + opts->h_gap);
318
319   dims->box.x = x + doz(rect->w, dims->box.w) / 2;
320   dims->box.y = rect->h + v_space;
321
322   rect->h += dims->box.h + v_space;
323 }
324
325 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
326 {
327   const draw_chordal_opts_t *opts = env->opts;
328   struct block_dims *dims = pmap_get(env->block_dims, bl);
329   int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
330   ir_node *sub;
331
332   dominates_for_each(bl, sub) {
333     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
334     int height_diff = max_height - bl_dim->subtree_box.h;
335
336     set_y(env, sub, up + height_diff);
337   }
338
339   dims->subtree_box.y += up;
340   dims->box.y += up;
341 }
342
343 static color_t *reg_to_color(const draw_chordal_env_t *env,
344     ir_node *rel_bl, ir_node *irn, color_t *color)
345 {
346   int phi_arg = 0;
347   const ir_edge_t *edge;
348
349   foreach_out_edge(irn, edge)
350     phi_arg |= is_Phi(edge->src);
351
352 #if 1
353   color->r = is_Phi(irn) ? 0.5 : 0.0;
354   color->g = phi_arg ? 0.5 : 0.0;
355   color->b = 0.0;
356 #else
357   {
358     int live_in = is_live_in(rel_bl, irn);
359     int live_out = is_live_out(rel_bl, irn);
360
361     color->r = live_in;
362     color->g = live_out;
363     color->b = 0.0;
364   }
365 #endif
366
367   return color;
368
369 }
370
371 static void draw_block(ir_node *bl, void *data)
372 {
373   static const color_t black = { 0, 0, 0 };
374
375   const draw_chordal_env_t *env = data;
376   pset *live_in = be_lv_pset_put_in(env->chordal_env->lv, bl, pset_new_ptr_default());
377   ir_node *irn;
378   border_t *b;
379         struct list_head *head = get_block_border_head(env->chordal_env, bl);
380   ir_node *dom = get_Block_idom(bl);
381   const draw_chordal_opts_t *opts = env->opts;
382   struct block_dims *dims = pmap_get(env->block_dims, bl);
383   char buf[64];
384
385   ir_snprintf(buf, sizeof(buf), "%F", bl);
386
387   env->plotter->vtab->set_color(env->plotter, &black);
388   env->plotter->vtab->box(env->plotter, &dims->box);
389
390 #if 0
391   env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
392 #endif
393
394         list_for_each_entry(border_t, b, head, list) {
395     if(b->is_def) {
396       const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn);
397       int col = arch_register_get_index(reg);
398       int live_out = be_is_live_out(env->chordal_env->lv, bl, b->irn);
399       int x = (col + 1) * opts->h_inter_gap;
400       int ystart = (b->step) * opts->v_inter_gap;
401       int ystop = (b->other_end->step)
402         * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
403
404       color_t color;
405       reg_to_color(env, bl, b->irn, &color);
406
407       x += dims->box.x;
408       ystart += dims->box.y;
409       ystop += dims->box.y;
410
411       env->plotter->vtab->set_color(env->plotter, &color);
412       env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
413
414       env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
415       env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
416     }
417   }
418
419   if(dom) {
420     struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
421
422     for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
423       if(arch_irn_has_reg_class(env->arch_env, irn, -1, env->cls)) {
424         const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
425         int col = arch_register_get_index(reg);
426         int x = (col + 1) * opts->h_inter_gap;
427
428         color_t color;
429         reg_to_color(env, bl, irn, &color);
430
431         env->plotter->vtab->set_color(env->plotter, &color);
432         env->plotter->vtab->line(env->plotter,
433             dims->box.x + x,
434             dims->box.y + dims->box.h,
435             dom_dims->box.x + x,
436             dom_dims->box.y);
437       }
438     }
439   }
440
441   del_pset(live_in);
442 }
443
444 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
445 {
446   plotter_t *p = env->plotter;
447   rect_t bbox;
448
449   bbox.x = bbox.y = 0;
450   bbox.w = start_box->w + 2 * env->opts->x_margin;
451   bbox.h = start_box->h + 2 * env->opts->y_margin;
452
453   p->vtab->begin(p, &bbox);
454   irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
455   p->vtab->finish(p);
456 }
457
458 void draw_interval_tree(const draw_chordal_opts_t *opts,
459                                                 const be_chordal_env_t *chordal_env,
460                                                 plotter_t *plotter)
461 {
462   draw_chordal_env_t env;
463   struct block_dims *start_dims;
464   ir_node *start_block = get_irg_start_block(chordal_env->irg);
465
466   env.arch_env = chordal_env->birg->main_env->arch_env;
467   env.opts = opts;
468   env.block_dims = pmap_create();
469   env.plotter = plotter;
470   env.cls = chordal_env->cls;
471   env.max_color = 0;
472   env.chordal_env = chordal_env;
473   obstack_init(&env.obst);
474
475   irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
476   layout(&env, start_block, opts->x_margin);
477   set_y(&env, start_block, opts->y_margin);
478   start_dims = pmap_get(env.block_dims, start_block);
479   draw(&env, &start_dims->subtree_box);
480
481   pmap_destroy(env.block_dims);
482   obstack_free(&env.obst, NULL);
483 }