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