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