2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Paint chordal graphs.
9 * @author Sebastian Hack
21 #include "iredges_t.h"
26 #include "bechordal_t.h"
28 #include "bechordal_draw.h"
32 be_chordal_env_t *env;
38 #define decl_self(type, from) \
39 type *self = (type *) from
41 static void set_color(plotter_t *_self, const color_t *color)
43 decl_self(base_plotter_t, _self);
47 static const color_t *get_color(const plotter_t *_self)
49 decl_self(const base_plotter_t, _self);
53 static void set_width(plotter_t *_self, int width)
55 decl_self(base_plotter_t, _self);
59 static int get_width(const plotter_t *_self)
61 decl_self(const base_plotter_t, _self);
65 static void plotter_default_free(plotter_t *self)
79 | _ \/ ___| | _ \| | ___ | |_| |_ ___ _ __
80 | |_) \___ \ | |_) | |/ _ \| __| __/ _ \ '__|
81 | __/ ___) | | __/| | (_) | |_| || __/ |
82 |_| |____/ |_| |_|\___/ \__|\__\___|_|
86 static void ps_begin(plotter_t *_self, const rect_t *vis)
89 decl_self(ps_plotter_t, _self);
91 f = self->f = fopen(self->filename, "wt");
92 fprintf(f, "%%!PS-Adobe-2.0\n");
93 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
96 static void ps_setcolor(plotter_t *_self, const color_t *color)
98 decl_self(ps_plotter_t, _self);
99 set_color(_self, color);
101 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
102 color->r, color->g, color->b);
105 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
107 decl_self(ps_plotter_t, _self);
109 fprintf(self->f, "%d %d moveto\n", x1, y1);
110 fprintf(self->f, "%d %d lineto\n", x2, y2);
111 fprintf(self->f, "stroke\n");
114 static void ps_box(plotter_t *_self, const rect_t *rect)
116 decl_self(ps_plotter_t, _self);
118 fprintf(self->f, "%d %d %d %d rectstroke\n",
119 rect->x, rect->y, rect->w, rect->h);
122 static void ps_text(plotter_t *_self, int x, int y, const char *str)
124 decl_self(ps_plotter_t, _self);
126 fprintf(self->f, "%d %d moveto\n", x, y);
127 fprintf(self->f, "(%s) show\n", str);
130 static void ps_finish(plotter_t *_self)
132 decl_self(ps_plotter_t, _self);
136 static const plotter_if_t ps_plotter_vtab = {
149 plotter_t *new_plotter_ps(const char *filename)
151 ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
152 plotter_t *p = (plotter_t *) ps_plotter;
154 ps_plotter->filename = filename;
155 p->vtab = &ps_plotter_vtab;
159 extern void plotter_free(plotter_t *self)
161 self->vtab->free(self);
165 const draw_chordal_opts_t draw_chordal_def_opts = {
166 10, 10, 30, 8, 10, 10
169 typedef struct draw_chordal_env_t {
170 const be_chordal_env_t *chordal_env;
171 const arch_register_class_t *cls;
174 const draw_chordal_opts_t *opts;
177 } draw_chordal_env_t;
186 #define doz(a, b) MAX((a) - (b), 0)
188 static void block_dims_walker(ir_node *block, void *data)
190 draw_chordal_env_t *env = (draw_chordal_env_t*)data;
191 struct list_head *head = get_block_border_head(env->chordal_env, block);
192 const draw_chordal_opts_t *opts = env->opts;
193 struct block_dims *dims = OALLOCZ(&env->obst, struct block_dims);
195 foreach_border_head(head, b) {
196 ir_node *irn = b->irn;
197 const arch_register_t *reg = arch_get_irn_register(irn);
198 int col = reg->index;
200 dims->max_step = MAX(dims->max_step, b->step);
201 dims->max_color = MAX(dims->max_color, col);
202 env->max_color = MAX(env->max_color, col);
205 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
206 dims->box.h = dims->max_step * opts->v_inter_gap;
208 pmap_insert(env->block_dims, block, dims);
211 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
213 const draw_chordal_opts_t *opts = env->opts;
214 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
215 rect_t *rect = &dims->subtree_box;
220 memset(rect, 0, sizeof(*rect));
223 dominates_for_each(bl, sub) {
224 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
226 layout(env, sub, rect->x + rect->w);
228 rect->w += h_space + bl_dim->subtree_box.w;
229 rect->h = MAX(rect->h, bl_dim->subtree_box.h);
231 h_space = opts->h_gap;
232 v_space = opts->v_gap;
235 rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
237 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
238 dims->box.y = rect->h + v_space;
240 rect->h = dims->box.y + dims->box.h;
243 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
245 const draw_chordal_opts_t *opts = env->opts;
246 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
247 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
250 dominates_for_each(bl, sub) {
251 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
252 int height_diff = max_height - bl_dim->subtree_box.h;
254 set_y(env, sub, up + height_diff);
257 dims->subtree_box.y += up;
261 static color_t *reg_to_color(const draw_chordal_env_t *env,
262 ir_node *rel_bl, ir_node *irn, color_t *color)
268 foreach_out_edge(irn, edge)
269 phi_arg |= is_Phi(edge->src);
271 color->r = is_Phi(irn) ? 0.5 : 0.0;
272 color->g = phi_arg ? 0.5 : 0.0;
277 static void draw_block(ir_node *bl, void *data)
279 static const color_t black = { 0, 0, 0 };
280 const draw_chordal_env_t *env = (const draw_chordal_env_t*)data;
281 const be_lv_t *lv = be_get_irg_liveness(env->chordal_env->irg);
282 struct list_head *head = get_block_border_head(env->chordal_env, bl);
283 ir_node *dom = get_Block_idom(bl);
284 const draw_chordal_opts_t *opts = env->opts;
285 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
288 ir_snprintf(buf, sizeof(buf), "%F", bl);
290 env->plotter->vtab->set_color(env->plotter, &black);
291 env->plotter->vtab->box(env->plotter, &dims->box);
293 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
295 foreach_border_head(head, b) {
297 /* Walk from def to use, so the link is set before retrieved. */
298 set_irn_link(b->irn, b);
300 ir_node *const irn = b->irn;
301 border_t const *const def = (border_t const*)get_irn_link(irn);
302 arch_register_t const *const reg = arch_get_irn_register(irn);
304 int live_out = be_is_live_out(lv, bl, irn);
305 int x = (reg->index + 1) * opts->h_inter_gap;
306 int ystart = def->step * opts->v_inter_gap;
307 int ystop = b->step * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
310 reg_to_color(env, bl, irn, &color);
313 ystart += dims->box.y;
314 ystop += dims->box.y;
316 env->plotter->vtab->set_color(env->plotter, &color);
317 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
319 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
320 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
325 struct block_dims *dom_dims = pmap_get(struct block_dims, env->block_dims, dom);
327 be_lv_foreach_cls(lv, bl, be_lv_state_in, env->cls, irn) {
328 const arch_register_t *reg = arch_get_irn_register(irn);
329 int x = (reg->index + 1) * opts->h_inter_gap;
332 reg_to_color(env, bl, irn, &color);
334 env->plotter->vtab->set_color(env->plotter, &color);
335 env->plotter->vtab->line(env->plotter,
337 dims->box.y + dims->box.h,
344 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
346 ir_graph *irg = env->chordal_env->irg;
347 plotter_t *p = env->plotter;
351 bbox.w = start_box->w + 2 * env->opts->x_margin;
352 bbox.h = start_box->h + 2 * env->opts->y_margin;
354 be_assure_live_sets(irg);
355 be_assure_live_chk(irg);
357 p->vtab->begin(p, &bbox);
358 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
359 irg_block_walk_graph(irg, draw_block, NULL, env);
360 ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
364 void draw_interval_tree(const draw_chordal_opts_t *opts,
365 const be_chordal_env_t *chordal_env, plotter_t *plotter)
367 draw_chordal_env_t env;
368 struct block_dims *start_dims;
369 ir_node *start_block = get_irg_start_block(chordal_env->irg);
372 env.block_dims = pmap_create();
373 env.plotter = plotter;
374 env.cls = chordal_env->cls;
376 env.chordal_env = chordal_env;
377 obstack_init(&env.obst);
379 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
380 layout(&env, start_block, opts->x_margin);
381 set_y(&env, start_block, opts->y_margin);
382 start_dims = pmap_get(struct block_dims, env.block_dims, start_block);
383 draw(&env, &start_dims->subtree_box);
385 pmap_destroy(env.block_dims);
386 obstack_free(&env.obst, NULL);