Several bugfixes
[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 = malloc(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   decl_self(tikz_plotter_t, _self);
188   set_color(_self, color);
189 }
190
191 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
192 {
193   decl_self(tikz_plotter_t, _self);
194   fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
195 }
196
197 static void tikz_box(plotter_t *_self, const rect_t *rect)
198 {
199   decl_self(tikz_plotter_t, _self);
200
201   fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
202           rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
203 }
204
205 void tikz_text(plotter_t *_self, int x, int y, const char *str)
206 {
207   decl_self(tikz_plotter_t, _self);
208   fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
209 }
210
211 static void tikz_finish(plotter_t *_self)
212 {
213   decl_self(tikz_plotter_t, _self);
214   fclose(self->f);
215 }
216
217
218 extern void plotter_free(plotter_t *self)
219 {
220   self->vtab->free(self);
221   free(self);
222 }
223
224 const draw_chordal_opts_t draw_chordal_def_opts = {
225   10, 10, 30, 8, 10, 10
226 };
227
228 typedef struct _draw_chordal_env_t {
229   const be_chordal_env_t *chordal_env;
230   const arch_env_t *arch_env;
231   const arch_register_class_t *cls;
232   pmap *block_dims;
233   plotter_t *plotter;
234   const draw_chordal_opts_t *opts;
235
236   struct obstack obst;
237   int max_color;
238 } draw_chordal_env_t;
239
240 struct block_dims {
241   int max_step;
242   int min_step;
243   int max_color;
244   rect_t box;
245   rect_t subtree_box;
246 };
247
248 #undef min
249 static INLINE int min(int a, int b)
250 {
251   return a < b ? a : b;
252 }
253
254 #undef max
255 static INLINE int max(int a, int b)
256 {
257   return a > b ? a : b;
258 }
259
260 #define doz(a, b) max((a) - (b), 0)
261
262 static void block_dims_walker(ir_node *block, void *data)
263 {
264   draw_chordal_env_t *env = data;
265   border_t *b;
266         struct list_head *head = get_block_border_head(env->chordal_env, block);
267   const draw_chordal_opts_t *opts = env->opts;
268   struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
269
270   memset(dims, 0, sizeof(*dims));
271   dims->min_step = INT_MAX;
272
273         list_for_each_entry_reverse(border_t, b, head, list) {
274     ir_node *irn = b->irn;
275     const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
276     int col = arch_register_get_index(reg);
277
278     dims->max_step = max(dims->max_step, b->step);
279     dims->max_color = max(dims->max_color, col);
280     env->max_color = max(env->max_color, col);
281   }
282
283   dims->min_step = 1;
284
285 #if 1
286   dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
287   dims->box.h = dims->max_step * opts->v_inter_gap;
288 #else
289   dims->box.w = dims->box.h = 10;
290 #endif
291
292   pmap_insert(env->block_dims, block, dims);
293 }
294
295 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
296 {
297   const draw_chordal_opts_t *opts = env->opts;
298   struct block_dims *dims = pmap_get(env->block_dims, bl);
299   ir_node *sub;
300   rect_t *rect = &dims->subtree_box;
301   int h_space = 0, v_space = 0;
302
303   memset(rect, 0, sizeof(*rect));
304   rect->x = x;
305
306   dominates_for_each(bl, sub) {
307     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
308
309     layout(env, sub, rect->x + rect->w);
310
311     rect->w += h_space + bl_dim->subtree_box.w;
312     rect->h = max(rect->h, bl_dim->subtree_box.h);
313
314     h_space = opts->h_gap;
315     v_space = opts->v_gap;
316   }
317
318   rect->w = max(rect->w, dims->box.w + opts->h_gap);
319
320   dims->box.x = x + doz(rect->w, dims->box.w) / 2;
321   dims->box.y = rect->h + v_space;
322
323   rect->h += dims->box.h + v_space;
324 }
325
326 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
327 {
328   const draw_chordal_opts_t *opts = env->opts;
329   struct block_dims *dims = pmap_get(env->block_dims, bl);
330   int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
331   ir_node *sub;
332
333   dominates_for_each(bl, sub) {
334     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
335     int height_diff = max_height - bl_dim->subtree_box.h;
336
337     set_y(env, sub, up + height_diff);
338   }
339
340   dims->subtree_box.y += up;
341   dims->box.y += up;
342 }
343
344 static color_t *reg_to_color(const draw_chordal_env_t *env,
345     ir_node *rel_bl, ir_node *irn, color_t *color)
346 {
347   int phi_arg = 0;
348   const ir_edge_t *edge;
349
350   foreach_out_edge(irn, edge)
351     phi_arg |= is_Phi(edge->src);
352
353 #if 1
354   color->r = is_Phi(irn) ? 0.5 : 0.0;
355   color->g = phi_arg ? 0.5 : 0.0;
356   color->b = 0.0;
357 #else
358   {
359     int live_in = is_live_in(rel_bl, irn);
360     int live_out = is_live_out(rel_bl, irn);
361
362     color->r = live_in;
363     color->g = live_out;
364     color->b = 0.0;
365   }
366 #endif
367
368   return color;
369
370 }
371
372 static void draw_block(ir_node *bl, void *data)
373 {
374   static const color_t black = { 0, 0, 0 };
375
376   const draw_chordal_env_t *env = data;
377   pset *live_in = put_live_in(bl, pset_new_ptr_default());
378   ir_node *irn;
379   border_t *b;
380         struct list_head *head = get_block_border_head(env->chordal_env, bl);
381   ir_node *dom = get_Block_idom(bl);
382   const draw_chordal_opts_t *opts = env->opts;
383   struct block_dims *dims = pmap_get(env->block_dims, bl);
384   char buf[64];
385
386   ir_snprintf(buf, sizeof(buf), "%F", bl);
387
388   env->plotter->vtab->set_color(env->plotter, &black);
389   env->plotter->vtab->box(env->plotter, &dims->box);
390
391 #if 0
392   env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
393 #endif
394
395         list_for_each_entry(border_t, b, head, list) {
396     if(b->is_def) {
397       const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn);
398       int col = arch_register_get_index(reg);
399       int live_out = is_live_out(bl, b->irn);
400       int x = (col + 1) * opts->h_inter_gap;
401       int ystart = (b->step) * opts->v_inter_gap;
402       int ystop = (b->other_end->step)
403         * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
404
405       color_t color;
406       reg_to_color(env, bl, b->irn, &color);
407
408       x += dims->box.x;
409       ystart += dims->box.y;
410       ystop += dims->box.y;
411
412       env->plotter->vtab->set_color(env->plotter, &color);
413       env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
414
415       env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
416       env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
417     }
418   }
419
420   if(dom) {
421     struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
422
423     for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
424       if(arch_irn_has_reg_class(env->arch_env, irn, -1, env->cls)) {
425         const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
426         int col = arch_register_get_index(reg);
427         int x = (col + 1) * opts->h_inter_gap;
428
429         color_t color;
430         reg_to_color(env, bl, irn, &color);
431
432         env->plotter->vtab->set_color(env->plotter, &color);
433         env->plotter->vtab->line(env->plotter,
434             dims->box.x + x,
435             dims->box.y + dims->box.h,
436             dom_dims->box.x + x,
437             dom_dims->box.y);
438       }
439     }
440   }
441
442   del_pset(live_in);
443 }
444
445 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
446 {
447   plotter_t *p = env->plotter;
448   rect_t bbox;
449
450   bbox.x = bbox.y = 0;
451   bbox.w = start_box->w + 2 * env->opts->x_margin;
452   bbox.h = start_box->h + 2 * env->opts->y_margin;
453
454   p->vtab->begin(p, &bbox);
455   irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
456   p->vtab->finish(p);
457 }
458
459 void draw_interval_tree(const draw_chordal_opts_t *opts,
460                                                 const be_chordal_env_t *chordal_env,
461                                                 plotter_t *plotter)
462 {
463   draw_chordal_env_t env;
464   struct block_dims *start_dims;
465   ir_node *start_block = get_irg_start_block(chordal_env->irg);
466
467   env.arch_env = chordal_env->birg->main_env->arch_env;
468   env.opts = opts;
469   env.block_dims = pmap_create();
470   env.plotter = plotter;
471   env.cls = chordal_env->cls;
472   env.max_color = 0;
473   env.chordal_env = chordal_env;
474   obstack_init(&env.obst);
475
476   irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
477   layout(&env, start_block, opts->x_margin);
478   set_y(&env, start_block, opts->y_margin);
479   start_dims = pmap_get(env.block_dims, start_block);
480   draw(&env, &start_dims->subtree_box);
481
482   pmap_destroy(env.block_dims);
483   obstack_free(&env.obst, NULL);
484 }