3 * @file bechordal_draw.c
5 * @author Sebastian Hack
7 * Paint chordal graphs.
9 * Copyright (C) 2005 Universitaet Karlsruhe
10 * Released under the GPL
25 #include "bechordal_t.h"
26 #include "besched_t.h"
27 #include "bechordal_draw.h"
30 be_chordal_env_t *env;
36 #define decl_self(type, from) \
37 type *self = (type *) from
39 static void set_color(plotter_t *_self, const color_t *color)
41 decl_self(base_plotter_t, _self);
45 static const color_t *get_color(const plotter_t *_self)
47 decl_self(const base_plotter_t, _self);
51 static void set_width(plotter_t *_self, int width)
53 decl_self(base_plotter_t, _self);
57 static int get_width(const plotter_t *_self)
59 decl_self(const base_plotter_t, _self);
63 static void plotter_default_free(plotter_t *self)
73 static void ps_begin(plotter_t *_self, const rect_t *vis)
76 decl_self(ps_plotter_t, _self);
78 f = self->f = fopen(self->filename, "wt");
79 fprintf(f, "%%!PS-Adobe-2.0\n");
80 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
82 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
83 fprintf(f, "mainfont setfont\n");
87 static void ps_setcolor(plotter_t *_self, const color_t *color)
89 decl_self(ps_plotter_t, _self);
90 set_color(_self, color);
92 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
93 color->r, color->g, color->b);
96 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
98 decl_self(ps_plotter_t, _self);
100 fprintf(self->f, "%d %d moveto\n", x1, y1);
101 fprintf(self->f, "%d %d lineto\n", x2, y2);
102 fprintf(self->f, "stroke\n");
105 static void ps_box(plotter_t *_self, const rect_t *rect)
107 decl_self(ps_plotter_t, _self);
109 fprintf(self->f, "%d %d %d %d rectstroke\n",
110 rect->x, rect->y, rect->w, rect->h);
113 void ps_text(plotter_t *_self, int x, int y, const char *str)
115 decl_self(ps_plotter_t, _self);
117 fprintf(self->f, "%d %d moveto\n", x, y);
118 fprintf(self->f, "(%s) show\n", str);
121 static void ps_finish(plotter_t *_self)
123 decl_self(ps_plotter_t, _self);
127 const plotter_if_t ps_plotter_vtab = {
140 plotter_t *new_plotter_ps(const char *filename)
142 ps_plotter_t *ps_plotter = malloc(sizeof(*ps_plotter));
143 plotter_t *p = (plotter_t *) ps_plotter;
145 ps_plotter->filename = filename;
146 p->vtab = &ps_plotter_vtab;
150 extern void plotter_free(plotter_t *self)
152 self->vtab->free(self);
156 const draw_chordal_opts_t draw_chordal_def_opts = {
157 10, 10, 30, 8, 10, 10
160 typedef struct _draw_chordal_env_t {
161 const be_chordal_env_t *chordal_env;
162 const arch_env_t *arch_env;
163 const arch_register_class_t *cls;
166 const draw_chordal_opts_t *opts;
170 } draw_chordal_env_t;
181 static INLINE int min(int a, int b)
183 return a < b ? a : b;
187 static INLINE int max(int a, int b)
189 return a > b ? a : b;
192 #define doz(a, b) max((a) - (b), 0)
194 static void block_dims_walker(ir_node *block, void *data)
196 draw_chordal_env_t *env = data;
198 struct list_head *head = get_block_border_head(env->chordal_env, block);
199 const draw_chordal_opts_t *opts = env->opts;
200 struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
202 memset(dims, 0, sizeof(*dims));
203 dims->min_step = INT_MAX;
205 list_for_each_entry_reverse(border_t, b, head, list) {
206 ir_node *irn = b->irn;
207 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
208 int col = arch_register_get_index(reg);
210 dims->max_step = max(dims->max_step, b->step);
211 dims->max_color = max(dims->max_color, col);
212 env->max_color = max(env->max_color, col);
218 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
219 dims->box.h = dims->max_step * opts->v_inter_gap;
221 dims->box.w = dims->box.h = 10;
224 pmap_insert(env->block_dims, block, dims);
227 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
229 const draw_chordal_opts_t *opts = env->opts;
230 struct block_dims *dims = pmap_get(env->block_dims, bl);
232 rect_t *rect = &dims->subtree_box;
233 int h_space = 0, v_space = 0;
235 memset(rect, 0, sizeof(*rect));
238 dominates_for_each(bl, sub) {
239 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
241 layout(env, sub, rect->x + rect->w);
243 rect->w += h_space + bl_dim->subtree_box.w;
244 rect->h = max(rect->h, bl_dim->subtree_box.h);
246 h_space = opts->h_gap;
247 v_space = opts->v_gap;
250 rect->w = max(rect->w, dims->box.w + opts->h_gap);
252 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
253 dims->box.y = rect->h + v_space;
255 rect->h += dims->box.h + v_space;
258 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
260 const draw_chordal_opts_t *opts = env->opts;
261 struct block_dims *dims = pmap_get(env->block_dims, bl);
262 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
265 dominates_for_each(bl, sub) {
266 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
267 int height_diff = max_height - bl_dim->subtree_box.h;
269 set_y(env, sub, up + height_diff);
272 dims->subtree_box.y += up;
276 static color_t *reg_to_color(const draw_chordal_env_t *env,
277 ir_node *rel_bl, ir_node *irn, color_t *color)
279 int i, n, phi_arg = 0;
281 for(i = 0, n = get_irn_n_outs(irn); i < n && !phi_arg; ++i)
282 phi_arg |= is_Phi(get_irn_out(irn, i));
285 color->r = is_Phi(irn) ? 0.5 : 0.0;
286 color->g = phi_arg ? 0.5 : 0.0;
290 int live_in = is_live_in(rel_bl, irn);
291 int live_out = is_live_out(rel_bl, irn);
303 static void draw_block(ir_node *bl, void *data)
305 static const color_t black = { 0, 0, 0 };
307 const draw_chordal_env_t *env = data;
308 pset *live_in = get_live_in(bl);
311 struct list_head *head = get_block_border_head(env->chordal_env, bl);
312 ir_node *dom = get_Block_idom(bl);
313 const draw_chordal_opts_t *opts = env->opts;
314 struct block_dims *dims = pmap_get(env->block_dims, bl);
317 ir_snprintf(buf, sizeof(buf), "%F", bl);
319 env->plotter->vtab->set_color(env->plotter, &black);
320 env->plotter->vtab->box(env->plotter, &dims->box);
323 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
326 list_for_each_entry(border_t, b, head, list) {
328 const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn, 0);
329 int col = arch_register_get_index(reg);
330 int live_in = is_live_in(bl, b->irn);
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);
338 reg_to_color(env, bl, b->irn, &color);
341 ystart += dims->box.y;
342 ystop += dims->box.y;
344 env->plotter->vtab->set_color(env->plotter, &color);
345 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
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);
353 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
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;
362 reg_to_color(env, bl, irn, &color);
364 env->plotter->vtab->set_color(env->plotter, &color);
365 env->plotter->vtab->line(env->plotter,
367 dims->box.y + dims->box.h,
376 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
379 line.x = dims->box.x;
380 line.y = dims->box.y;
381 line.w = dom_dims->box.x;
382 line.h = dom_dims->box.y;
384 env->plotter->vtab->line(env->plotter, &line);
389 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
391 plotter_t *p = env->plotter;
395 bbox.w = start_box->w + 2 * env->opts->x_margin;
396 bbox.h = start_box->h + 2 * env->opts->y_margin;
398 p->vtab->begin(p, &bbox);
399 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
403 void draw_interval_tree(const draw_chordal_opts_t *opts,
404 const be_chordal_env_t *chordal_env,
405 plotter_t *plotter, const arch_env_t *arch_env,
406 const arch_register_class_t *cls)
408 draw_chordal_env_t env;
409 struct block_dims *start_dims;
410 ir_node *start_block = get_irg_start_block(chordal_env->irg);
412 env.arch_env = arch_env;
414 env.block_dims = pmap_create();
415 env.plotter = plotter;
416 env.chordal_env = chordal_env;
419 env.chordal_env = chordal_env;
420 obstack_init(&env.obst);
422 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
423 layout(&env, start_block, opts->x_margin);
424 set_y(&env, start_block, opts->y_margin);
425 start_dims = pmap_get(env.block_dims, start_block);
426 draw(&env, &start_dims->subtree_box);
428 pmap_destroy(env.block_dims);
429 obstack_free(&env.obst, NULL);