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