3 * @file bechordal_draw.c
5 * @author Sebastian Hack
7 * Paint chordal graphs.
9 * Copyright (C) 2005 Universitaet Karlsruhe
10 * Released under the GPL
23 #include "iredges_t.h"
26 #include "bechordal_t.h"
27 #include "besched_t.h"
28 #include "bechordal_draw.h"
31 be_chordal_env_t *env;
37 #define decl_self(type, from) \
38 type *self = (type *) from
40 static void set_color(plotter_t *_self, const color_t *color)
42 decl_self(base_plotter_t, _self);
46 static const color_t *get_color(const plotter_t *_self)
48 decl_self(const base_plotter_t, _self);
52 static void set_width(plotter_t *_self, int width)
54 decl_self(base_plotter_t, _self);
58 static int get_width(const plotter_t *_self)
60 decl_self(const base_plotter_t, _self);
64 static void plotter_default_free(plotter_t *self)
77 | _ \/ ___| | _ \| | ___ | |_| |_ ___ _ __
78 | |_) \___ \ | |_) | |/ _ \| __| __/ _ \ '__|
79 | __/ ___) | | __/| | (_) | |_| || __/ |
80 |_| |____/ |_| |_|\___/ \__|\__\___|_|
84 static void ps_begin(plotter_t *_self, const rect_t *vis)
87 decl_self(ps_plotter_t, _self);
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);
93 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
94 fprintf(f, "mainfont setfont\n");
98 static void ps_setcolor(plotter_t *_self, const color_t *color)
100 decl_self(ps_plotter_t, _self);
101 set_color(_self, color);
103 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
104 color->r, color->g, color->b);
107 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
109 decl_self(ps_plotter_t, _self);
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");
116 static void ps_box(plotter_t *_self, const rect_t *rect)
118 decl_self(ps_plotter_t, _self);
120 fprintf(self->f, "%d %d %d %d rectstroke\n",
121 rect->x, rect->y, rect->w, rect->h);
124 void ps_text(plotter_t *_self, int x, int y, const char *str)
126 decl_self(ps_plotter_t, _self);
128 fprintf(self->f, "%d %d moveto\n", x, y);
129 fprintf(self->f, "(%s) show\n", str);
132 static void ps_finish(plotter_t *_self)
134 decl_self(ps_plotter_t, _self);
138 const plotter_if_t ps_plotter_vtab = {
151 plotter_t *new_plotter_ps(const char *filename)
153 ps_plotter_t *ps_plotter = xmalloc(sizeof(*ps_plotter));
154 plotter_t *p = (plotter_t *) ps_plotter;
156 ps_plotter->filename = filename;
157 p->vtab = &ps_plotter_vtab;
162 _____ _ _ _____ ____ _ _ _
163 |_ _(_) | __|__ / | _ \| | ___ | |_| |_ ___ _ __
164 | | | | |/ / / / | |_) | |/ _ \| __| __/ _ \ '__|
165 | | | | < / /_ | __/| | (_) | |_| || __/ |
166 |_| |_|_|\_\/____| |_| |_|\___/ \__|\__\___|_|
172 const char *filename;
176 static void tikz_begin(plotter_t *_self, const rect_t *vis)
179 decl_self(tikz_plotter_t, _self);
181 f = self->f = fopen(self->filename, "wt");
182 fprintf(f, "\\begin{tikzpicture}\n");
185 static void tikz_setcolor(plotter_t *_self, const color_t *color)
187 set_color(_self, color);
190 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
192 decl_self(tikz_plotter_t, _self);
193 fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
196 static void tikz_box(plotter_t *_self, const rect_t *rect)
198 decl_self(tikz_plotter_t, _self);
200 fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
201 rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
204 void tikz_text(plotter_t *_self, int x, int y, const char *str)
206 decl_self(tikz_plotter_t, _self);
207 fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
210 static void tikz_finish(plotter_t *_self)
212 decl_self(tikz_plotter_t, _self);
217 extern void plotter_free(plotter_t *self)
219 self->vtab->free(self);
223 const draw_chordal_opts_t draw_chordal_def_opts = {
224 10, 10, 30, 8, 10, 10
227 typedef struct _draw_chordal_env_t {
228 const be_chordal_env_t *chordal_env;
229 const arch_env_t *arch_env;
230 const arch_register_class_t *cls;
233 const draw_chordal_opts_t *opts;
237 } draw_chordal_env_t;
248 static INLINE int min(int a, int b)
250 return a < b ? a : b;
254 static INLINE int max(int a, int b)
256 return a > b ? a : b;
259 #define doz(a, b) max((a) - (b), 0)
261 static void block_dims_walker(ir_node *block, void *data)
263 draw_chordal_env_t *env = data;
265 struct list_head *head = get_block_border_head(env->chordal_env, block);
266 const draw_chordal_opts_t *opts = env->opts;
267 struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
269 memset(dims, 0, sizeof(*dims));
270 dims->min_step = INT_MAX;
272 list_for_each_entry_reverse(border_t, b, head, list) {
273 ir_node *irn = b->irn;
274 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
275 int col = arch_register_get_index(reg);
277 dims->max_step = max(dims->max_step, b->step);
278 dims->max_color = max(dims->max_color, col);
279 env->max_color = max(env->max_color, col);
285 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
286 dims->box.h = dims->max_step * opts->v_inter_gap;
288 dims->box.w = dims->box.h = 10;
291 pmap_insert(env->block_dims, block, dims);
294 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
296 const draw_chordal_opts_t *opts = env->opts;
297 struct block_dims *dims = pmap_get(env->block_dims, bl);
299 rect_t *rect = &dims->subtree_box;
300 int h_space = 0, v_space = 0;
302 memset(rect, 0, sizeof(*rect));
305 dominates_for_each(bl, sub) {
306 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
308 layout(env, sub, rect->x + rect->w);
310 rect->w += h_space + bl_dim->subtree_box.w;
311 rect->h = max(rect->h, bl_dim->subtree_box.h);
313 h_space = opts->h_gap;
314 v_space = opts->v_gap;
317 rect->w = max(rect->w, dims->box.w + opts->h_gap);
319 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
320 dims->box.y = rect->h + v_space;
322 rect->h += dims->box.h + v_space;
325 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
327 const draw_chordal_opts_t *opts = env->opts;
328 struct block_dims *dims = pmap_get(env->block_dims, bl);
329 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
332 dominates_for_each(bl, sub) {
333 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
334 int height_diff = max_height - bl_dim->subtree_box.h;
336 set_y(env, sub, up + height_diff);
339 dims->subtree_box.y += up;
343 static color_t *reg_to_color(const draw_chordal_env_t *env,
344 ir_node *rel_bl, ir_node *irn, color_t *color)
347 const ir_edge_t *edge;
349 foreach_out_edge(irn, edge)
350 phi_arg |= is_Phi(edge->src);
353 color->r = is_Phi(irn) ? 0.5 : 0.0;
354 color->g = phi_arg ? 0.5 : 0.0;
358 int live_in = is_live_in(rel_bl, irn);
359 int live_out = is_live_out(rel_bl, irn);
371 static void draw_block(ir_node *bl, void *data)
373 static const color_t black = { 0, 0, 0 };
375 const draw_chordal_env_t *env = data;
376 pset *live_in = be_lv_pset_put_in(env->chordal_env->lv, bl, pset_new_ptr_default());
379 struct list_head *head = get_block_border_head(env->chordal_env, bl);
380 ir_node *dom = get_Block_idom(bl);
381 const draw_chordal_opts_t *opts = env->opts;
382 struct block_dims *dims = pmap_get(env->block_dims, bl);
385 ir_snprintf(buf, sizeof(buf), "%F", bl);
387 env->plotter->vtab->set_color(env->plotter, &black);
388 env->plotter->vtab->box(env->plotter, &dims->box);
391 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
394 list_for_each_entry(border_t, b, head, list) {
396 const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn);
397 int col = arch_register_get_index(reg);
398 int live_out = be_is_live_out(env->chordal_env->lv, bl, b->irn);
399 int x = (col + 1) * opts->h_inter_gap;
400 int ystart = (b->step) * opts->v_inter_gap;
401 int ystop = (b->other_end->step)
402 * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
405 reg_to_color(env, bl, b->irn, &color);
408 ystart += dims->box.y;
409 ystop += dims->box.y;
411 env->plotter->vtab->set_color(env->plotter, &color);
412 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
414 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
415 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
420 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
422 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
423 if(arch_irn_has_reg_class(env->arch_env, irn, -1, env->cls)) {
424 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
425 int col = arch_register_get_index(reg);
426 int x = (col + 1) * opts->h_inter_gap;
429 reg_to_color(env, bl, irn, &color);
431 env->plotter->vtab->set_color(env->plotter, &color);
432 env->plotter->vtab->line(env->plotter,
434 dims->box.y + dims->box.h,
444 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
446 plotter_t *p = env->plotter;
450 bbox.w = start_box->w + 2 * env->opts->x_margin;
451 bbox.h = start_box->h + 2 * env->opts->y_margin;
453 p->vtab->begin(p, &bbox);
454 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
458 void draw_interval_tree(const draw_chordal_opts_t *opts,
459 const be_chordal_env_t *chordal_env,
462 draw_chordal_env_t env;
463 struct block_dims *start_dims;
464 ir_node *start_block = get_irg_start_block(chordal_env->irg);
466 env.arch_env = chordal_env->birg->main_env->arch_env;
468 env.block_dims = pmap_create();
469 env.plotter = plotter;
470 env.cls = chordal_env->cls;
472 env.chordal_env = chordal_env;
473 obstack_init(&env.obst);
475 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
476 layout(&env, start_block, opts->x_margin);
477 set_y(&env, start_block, opts->y_margin);
478 start_dims = pmap_get(env.block_dims, start_block);
479 draw(&env, &start_dims->subtree_box);
481 pmap_destroy(env.block_dims);
482 obstack_free(&env.obst, NULL);