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 = malloc(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 decl_self(tikz_plotter_t, _self);
188 set_color(_self, color);
191 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
193 decl_self(tikz_plotter_t, _self);
194 fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
197 static void tikz_box(plotter_t *_self, const rect_t *rect)
199 decl_self(tikz_plotter_t, _self);
201 fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
202 rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
205 void tikz_text(plotter_t *_self, int x, int y, const char *str)
207 decl_self(tikz_plotter_t, _self);
208 fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
211 static void tikz_finish(plotter_t *_self)
213 decl_self(tikz_plotter_t, _self);
218 extern void plotter_free(plotter_t *self)
220 self->vtab->free(self);
224 const draw_chordal_opts_t draw_chordal_def_opts = {
225 10, 10, 30, 8, 10, 10
228 typedef struct _draw_chordal_env_t {
229 const be_chordal_env_t *chordal_env;
230 const arch_env_t *arch_env;
231 const arch_register_class_t *cls;
234 const draw_chordal_opts_t *opts;
238 } draw_chordal_env_t;
249 static INLINE int min(int a, int b)
251 return a < b ? a : b;
255 static INLINE int max(int a, int b)
257 return a > b ? a : b;
260 #define doz(a, b) max((a) - (b), 0)
262 static void block_dims_walker(ir_node *block, void *data)
264 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));
270 memset(dims, 0, sizeof(*dims));
271 dims->min_step = INT_MAX;
273 list_for_each_entry_reverse(border_t, b, head, list) {
274 ir_node *irn = b->irn;
275 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
276 int col = arch_register_get_index(reg);
278 dims->max_step = max(dims->max_step, b->step);
279 dims->max_color = max(dims->max_color, col);
280 env->max_color = max(env->max_color, col);
286 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
287 dims->box.h = dims->max_step * opts->v_inter_gap;
289 dims->box.w = dims->box.h = 10;
292 pmap_insert(env->block_dims, block, dims);
295 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
297 const draw_chordal_opts_t *opts = env->opts;
298 struct block_dims *dims = pmap_get(env->block_dims, bl);
300 rect_t *rect = &dims->subtree_box;
301 int h_space = 0, v_space = 0;
303 memset(rect, 0, sizeof(*rect));
306 dominates_for_each(bl, sub) {
307 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
309 layout(env, sub, rect->x + rect->w);
311 rect->w += h_space + bl_dim->subtree_box.w;
312 rect->h = max(rect->h, bl_dim->subtree_box.h);
314 h_space = opts->h_gap;
315 v_space = opts->v_gap;
318 rect->w = max(rect->w, dims->box.w + opts->h_gap);
320 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
321 dims->box.y = rect->h + v_space;
323 rect->h += dims->box.h + v_space;
326 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
328 const draw_chordal_opts_t *opts = env->opts;
329 struct block_dims *dims = pmap_get(env->block_dims, bl);
330 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
333 dominates_for_each(bl, sub) {
334 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
335 int height_diff = max_height - bl_dim->subtree_box.h;
337 set_y(env, sub, up + height_diff);
340 dims->subtree_box.y += up;
344 static color_t *reg_to_color(const draw_chordal_env_t *env,
345 ir_node *rel_bl, ir_node *irn, color_t *color)
348 const ir_edge_t *edge;
350 foreach_out_edge(irn, edge)
351 phi_arg |= is_Phi(edge->src);
354 color->r = is_Phi(irn) ? 0.5 : 0.0;
355 color->g = phi_arg ? 0.5 : 0.0;
359 int live_in = is_live_in(rel_bl, irn);
360 int live_out = is_live_out(rel_bl, irn);
372 static void draw_block(ir_node *bl, void *data)
374 static const color_t black = { 0, 0, 0 };
376 const draw_chordal_env_t *env = data;
377 pset *live_in = put_live_in(bl, pset_new_ptr_default());
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);
386 ir_snprintf(buf, sizeof(buf), "%F", bl);
388 env->plotter->vtab->set_color(env->plotter, &black);
389 env->plotter->vtab->box(env->plotter, &dims->box);
392 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
395 list_for_each_entry(border_t, b, head, list) {
397 const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn);
398 int col = arch_register_get_index(reg);
399 int live_out = is_live_out(bl, b->irn);
400 int x = (col + 1) * opts->h_inter_gap;
401 int ystart = (b->step) * opts->v_inter_gap;
402 int ystop = (b->other_end->step)
403 * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
406 reg_to_color(env, bl, b->irn, &color);
409 ystart += dims->box.y;
410 ystop += dims->box.y;
412 env->plotter->vtab->set_color(env->plotter, &color);
413 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
415 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
416 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
421 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
423 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
424 if(arch_irn_has_reg_class(env->arch_env, irn, -1, env->cls)) {
425 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
426 int col = arch_register_get_index(reg);
427 int x = (col + 1) * opts->h_inter_gap;
430 reg_to_color(env, bl, irn, &color);
432 env->plotter->vtab->set_color(env->plotter, &color);
433 env->plotter->vtab->line(env->plotter,
435 dims->box.y + dims->box.h,
445 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
447 plotter_t *p = env->plotter;
451 bbox.w = start_box->w + 2 * env->opts->x_margin;
452 bbox.h = start_box->h + 2 * env->opts->y_margin;
454 p->vtab->begin(p, &bbox);
455 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
459 void draw_interval_tree(const draw_chordal_opts_t *opts,
460 const be_chordal_env_t *chordal_env,
463 draw_chordal_env_t env;
464 struct block_dims *start_dims;
465 ir_node *start_block = get_irg_start_block(chordal_env->irg);
467 env.arch_env = chordal_env->birg->main_env->arch_env;
469 env.block_dims = pmap_create();
470 env.plotter = plotter;
471 env.cls = chordal_env->cls;
473 env.chordal_env = chordal_env;
474 obstack_init(&env.obst);
476 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
477 layout(&env, start_block, opts->x_margin);
478 set_y(&env, start_block, opts->y_margin);
479 start_dims = pmap_get(env.block_dims, start_block);
480 draw(&env, &start_dims->subtree_box);
482 pmap_destroy(env.block_dims);
483 obstack_free(&env.obst, NULL);