Modified everything
[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 "irouts.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 static void ps_begin(plotter_t *_self, const rect_t *vis)
75 {
76   FILE *f;
77   decl_self(ps_plotter_t, _self);
78
79   f = self->f = fopen(self->filename, "wt");
80   fprintf(f, "%%!PS-Adobe-2.0\n");
81   fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
82 #if 0
83   fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
84   fprintf(f, "mainfont setfont\n");
85 #endif
86 }
87
88 static void ps_setcolor(plotter_t *_self, const color_t *color)
89 {
90   decl_self(ps_plotter_t, _self);
91   set_color(_self, color);
92
93   fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
94       color->r, color->g, color->b);
95 }
96
97 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
98 {
99   decl_self(ps_plotter_t, _self);
100
101   fprintf(self->f, "%d %d moveto\n", x1, y1);
102   fprintf(self->f, "%d %d lineto\n", x2, y2);
103   fprintf(self->f, "stroke\n");
104 }
105
106 static void ps_box(plotter_t *_self, const rect_t *rect)
107 {
108   decl_self(ps_plotter_t, _self);
109
110   fprintf(self->f, "%d %d %d %d rectstroke\n",
111       rect->x, rect->y, rect->w, rect->h);
112 }
113
114 void ps_text(plotter_t *_self, int x, int y, const char *str)
115 {
116   decl_self(ps_plotter_t, _self);
117
118   fprintf(self->f, "%d %d moveto\n", x, y);
119   fprintf(self->f, "(%s) show\n", str);
120 }
121
122 static void ps_finish(plotter_t *_self)
123 {
124   decl_self(ps_plotter_t, _self);
125   fclose(self->f);
126 }
127
128 const plotter_if_t ps_plotter_vtab = {
129   ps_begin,
130   ps_setcolor,
131   get_color,
132   set_width,
133   get_width,
134   ps_line,
135   ps_box,
136   ps_text,
137   ps_finish,
138   plotter_default_free
139 };
140
141 plotter_t *new_plotter_ps(const char *filename)
142 {
143   ps_plotter_t *ps_plotter = malloc(sizeof(*ps_plotter));
144   plotter_t *p = (plotter_t *) ps_plotter;
145
146   ps_plotter->filename = filename;
147   p->vtab = &ps_plotter_vtab;
148   return p;
149 }
150
151 extern void plotter_free(plotter_t *self)
152 {
153   self->vtab->free(self);
154   free(self);
155 }
156
157 const draw_chordal_opts_t draw_chordal_def_opts = {
158   10, 10, 30, 8, 10, 10
159 };
160
161 typedef struct _draw_chordal_env_t {
162   const be_chordal_env_t *chordal_env;
163   const arch_env_t *arch_env;
164   const arch_register_class_t *cls;
165   pmap *block_dims;
166   plotter_t *plotter;
167   const draw_chordal_opts_t *opts;
168
169   struct obstack obst;
170   int max_color;
171 } draw_chordal_env_t;
172
173 struct block_dims {
174   int max_step;
175   int min_step;
176   int max_color;
177   rect_t box;
178   rect_t subtree_box;
179 };
180
181 #undef min
182 static INLINE int min(int a, int b)
183 {
184   return a < b ? a : b;
185 }
186
187 #undef max
188 static INLINE int max(int a, int b)
189 {
190   return a > b ? a : b;
191 }
192
193 #define doz(a, b) max((a) - (b), 0)
194
195 static void block_dims_walker(ir_node *block, void *data)
196 {
197   draw_chordal_env_t *env = data;
198   border_t *b;
199         struct list_head *head = get_block_border_head(env->chordal_env, block);
200   const draw_chordal_opts_t *opts = env->opts;
201   struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
202
203   memset(dims, 0, sizeof(*dims));
204   dims->min_step = INT_MAX;
205
206         list_for_each_entry_reverse(border_t, b, head, list) {
207     ir_node *irn = b->irn;
208     const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
209     int col = arch_register_get_index(reg);
210
211     dims->max_step = max(dims->max_step, b->step);
212     dims->max_color = max(dims->max_color, col);
213     env->max_color = max(env->max_color, col);
214   }
215
216   dims->min_step = 1;
217
218 #if 1
219   dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
220   dims->box.h = dims->max_step * opts->v_inter_gap;
221 #else
222   dims->box.w = dims->box.h = 10;
223 #endif
224
225   pmap_insert(env->block_dims, block, dims);
226 }
227
228 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
229 {
230   const draw_chordal_opts_t *opts = env->opts;
231   struct block_dims *dims = pmap_get(env->block_dims, bl);
232   ir_node *sub;
233   rect_t *rect = &dims->subtree_box;
234   int h_space = 0, v_space = 0;
235
236   memset(rect, 0, sizeof(*rect));
237   rect->x = x;
238
239   dominates_for_each(bl, sub) {
240     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
241
242     layout(env, sub, rect->x + rect->w);
243
244     rect->w += h_space + bl_dim->subtree_box.w;
245     rect->h = max(rect->h, bl_dim->subtree_box.h);
246
247     h_space = opts->h_gap;
248     v_space = opts->v_gap;
249   }
250
251   rect->w = max(rect->w, dims->box.w + opts->h_gap);
252
253   dims->box.x = x + doz(rect->w, dims->box.w) / 2;
254   dims->box.y = rect->h + v_space;
255
256   rect->h += dims->box.h + v_space;
257 }
258
259 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
260 {
261   const draw_chordal_opts_t *opts = env->opts;
262   struct block_dims *dims = pmap_get(env->block_dims, bl);
263   int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
264   ir_node *sub;
265
266   dominates_for_each(bl, sub) {
267     struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
268     int height_diff = max_height - bl_dim->subtree_box.h;
269
270     set_y(env, sub, up + height_diff);
271   }
272
273   dims->subtree_box.y += up;
274   dims->box.y += up;
275 }
276
277 static color_t *reg_to_color(const draw_chordal_env_t *env,
278     ir_node *rel_bl, ir_node *irn, color_t *color)
279 {
280   int i, n, phi_arg = 0;
281
282   for(i = 0, n = get_irn_n_outs(irn); i < n && !phi_arg; ++i)
283     phi_arg |= is_Phi(get_irn_out(irn, i));
284
285 #if 1
286   color->r = is_Phi(irn) ? 0.5 : 0.0;
287   color->g = phi_arg ? 0.5 : 0.0;
288   color->b = 0.0;
289 #else
290   {
291     int live_in = is_live_in(rel_bl, irn);
292     int live_out = is_live_out(rel_bl, irn);
293
294     color->r = live_in;
295     color->g = live_out;
296     color->b = 0.0;
297   }
298 #endif
299
300   return color;
301
302 }
303
304 static void draw_block(ir_node *bl, void *data)
305 {
306   static const color_t black = { 0, 0, 0 };
307
308   const draw_chordal_env_t *env = data;
309   pset *live_in = put_live_in(bl, pset_new_ptr_default());
310   ir_node *irn;
311   border_t *b;
312         struct list_head *head = get_block_border_head(env->chordal_env, bl);
313   ir_node *dom = get_Block_idom(bl);
314   const draw_chordal_opts_t *opts = env->opts;
315   struct block_dims *dims = pmap_get(env->block_dims, bl);
316   char buf[64];
317
318   ir_snprintf(buf, sizeof(buf), "%F", bl);
319
320   env->plotter->vtab->set_color(env->plotter, &black);
321   env->plotter->vtab->box(env->plotter, &dims->box);
322
323 #if 0
324   env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
325 #endif
326
327         list_for_each_entry(border_t, b, head, list) {
328     if(b->is_def) {
329       const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn, 0);
330       int col = arch_register_get_index(reg);
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   del_pset(live_in);
375 }
376
377 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
378 {
379   plotter_t *p = env->plotter;
380   rect_t bbox;
381
382   bbox.x = bbox.y = 0;
383   bbox.w = start_box->w + 2 * env->opts->x_margin;
384   bbox.h = start_box->h + 2 * env->opts->y_margin;
385
386   p->vtab->begin(p, &bbox);
387   irg_block_walk_graph(env->chordal_env->session_env->irg, draw_block, NULL, env);
388   p->vtab->finish(p);
389 }
390
391 void draw_interval_tree(const draw_chordal_opts_t *opts,
392     const be_chordal_env_t *chordal_env,
393     plotter_t *plotter, const arch_env_t *arch_env,
394     const arch_register_class_t *cls)
395 {
396   draw_chordal_env_t env;
397   struct block_dims *start_dims;
398   ir_node *start_block = get_irg_start_block(chordal_env->session_env->irg);
399
400   env.arch_env = arch_env;
401   env.opts = opts;
402   env.block_dims = pmap_create();
403   env.plotter = plotter;
404   env.chordal_env = chordal_env;
405   env.cls = cls;
406   env.max_color = 0;
407   env.chordal_env = chordal_env;
408   obstack_init(&env.obst);
409
410   irg_block_walk_graph(chordal_env->session_env->irg, block_dims_walker, NULL, &env);
411   layout(&env, start_block, opts->x_margin);
412   set_y(&env, start_block, opts->y_margin);
413   start_dims = pmap_get(env.block_dims, start_block);
414   draw(&env, &start_dims->subtree_box);
415
416   pmap_destroy(env.block_dims);
417   obstack_free(&env.obst, NULL);
418 }