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)
74 static void ps_begin(plotter_t *_self, const rect_t *vis)
77 decl_self(ps_plotter_t, _self);
79 f = self->f = fopen(self->filename, "wt");
80 fprintf(f, "%%!PS-Adobe-2.0\n");
81 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
83 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
84 fprintf(f, "mainfont setfont\n");
88 static void ps_setcolor(plotter_t *_self, const color_t *color)
90 decl_self(ps_plotter_t, _self);
91 set_color(_self, color);
93 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
94 color->r, color->g, color->b);
97 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
99 decl_self(ps_plotter_t, _self);
101 fprintf(self->f, "%d %d moveto\n", x1, y1);
102 fprintf(self->f, "%d %d lineto\n", x2, y2);
103 fprintf(self->f, "stroke\n");
106 static void ps_box(plotter_t *_self, const rect_t *rect)
108 decl_self(ps_plotter_t, _self);
110 fprintf(self->f, "%d %d %d %d rectstroke\n",
111 rect->x, rect->y, rect->w, rect->h);
114 void ps_text(plotter_t *_self, int x, int y, const char *str)
116 decl_self(ps_plotter_t, _self);
118 fprintf(self->f, "%d %d moveto\n", x, y);
119 fprintf(self->f, "(%s) show\n", str);
122 static void ps_finish(plotter_t *_self)
124 decl_self(ps_plotter_t, _self);
128 const plotter_if_t ps_plotter_vtab = {
141 plotter_t *new_plotter_ps(const char *filename)
143 ps_plotter_t *ps_plotter = malloc(sizeof(*ps_plotter));
144 plotter_t *p = (plotter_t *) ps_plotter;
146 ps_plotter->filename = filename;
147 p->vtab = &ps_plotter_vtab;
151 extern void plotter_free(plotter_t *self)
153 self->vtab->free(self);
157 const draw_chordal_opts_t draw_chordal_def_opts = {
158 10, 10, 30, 8, 10, 10
161 typedef struct _draw_chordal_env_t {
162 const be_chordal_env_t *chordal_env;
163 const arch_env_t *arch_env;
164 const arch_register_class_t *cls;
167 const draw_chordal_opts_t *opts;
171 } draw_chordal_env_t;
182 static INLINE int min(int a, int b)
184 return a < b ? a : b;
188 static INLINE int max(int a, int b)
190 return a > b ? a : b;
193 #define doz(a, b) max((a) - (b), 0)
195 static void block_dims_walker(ir_node *block, void *data)
197 draw_chordal_env_t *env = data;
199 struct list_head *head = get_block_border_head(env->chordal_env, block);
200 const draw_chordal_opts_t *opts = env->opts;
201 struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
203 memset(dims, 0, sizeof(*dims));
204 dims->min_step = INT_MAX;
206 list_for_each_entry_reverse(border_t, b, head, list) {
207 ir_node *irn = b->irn;
208 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
209 int col = arch_register_get_index(reg);
211 dims->max_step = max(dims->max_step, b->step);
212 dims->max_color = max(dims->max_color, col);
213 env->max_color = max(env->max_color, col);
219 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
220 dims->box.h = dims->max_step * opts->v_inter_gap;
222 dims->box.w = dims->box.h = 10;
225 pmap_insert(env->block_dims, block, dims);
228 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
230 const draw_chordal_opts_t *opts = env->opts;
231 struct block_dims *dims = pmap_get(env->block_dims, bl);
233 rect_t *rect = &dims->subtree_box;
234 int h_space = 0, v_space = 0;
236 memset(rect, 0, sizeof(*rect));
239 dominates_for_each(bl, sub) {
240 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
242 layout(env, sub, rect->x + rect->w);
244 rect->w += h_space + bl_dim->subtree_box.w;
245 rect->h = max(rect->h, bl_dim->subtree_box.h);
247 h_space = opts->h_gap;
248 v_space = opts->v_gap;
251 rect->w = max(rect->w, dims->box.w + opts->h_gap);
253 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
254 dims->box.y = rect->h + v_space;
256 rect->h += dims->box.h + v_space;
259 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
261 const draw_chordal_opts_t *opts = env->opts;
262 struct block_dims *dims = pmap_get(env->block_dims, bl);
263 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
266 dominates_for_each(bl, sub) {
267 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
268 int height_diff = max_height - bl_dim->subtree_box.h;
270 set_y(env, sub, up + height_diff);
273 dims->subtree_box.y += up;
277 static color_t *reg_to_color(const draw_chordal_env_t *env,
278 ir_node *rel_bl, ir_node *irn, color_t *color)
281 const ir_edge_t *edge;
283 foreach_out_edge(irn, edge)
284 phi_arg |= is_Phi(edge->src);
287 color->r = is_Phi(irn) ? 0.5 : 0.0;
288 color->g = phi_arg ? 0.5 : 0.0;
292 int live_in = is_live_in(rel_bl, irn);
293 int live_out = is_live_out(rel_bl, irn);
305 static void draw_block(ir_node *bl, void *data)
307 static const color_t black = { 0, 0, 0 };
309 const draw_chordal_env_t *env = data;
310 pset *live_in = put_live_in(bl, pset_new_ptr_default());
313 struct list_head *head = get_block_border_head(env->chordal_env, bl);
314 ir_node *dom = get_Block_idom(bl);
315 const draw_chordal_opts_t *opts = env->opts;
316 struct block_dims *dims = pmap_get(env->block_dims, bl);
319 ir_snprintf(buf, sizeof(buf), "%F", bl);
321 env->plotter->vtab->set_color(env->plotter, &black);
322 env->plotter->vtab->box(env->plotter, &dims->box);
325 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
328 list_for_each_entry(border_t, b, head, list) {
330 const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn);
331 int col = arch_register_get_index(reg);
332 int live_out = is_live_out(bl, b->irn);
333 int x = (col + 1) * opts->h_inter_gap;
334 int ystart = (b->step) * opts->v_inter_gap;
335 int ystop = (b->other_end->step)
336 * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
339 reg_to_color(env, bl, b->irn, &color);
342 ystart += dims->box.y;
343 ystop += dims->box.y;
345 env->plotter->vtab->set_color(env->plotter, &color);
346 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
348 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
349 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
354 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
356 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
357 if(arch_irn_has_reg_class(env->arch_env, irn, 0, env->cls)) {
358 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn);
359 int col = arch_register_get_index(reg);
360 int x = (col + 1) * opts->h_inter_gap;
363 reg_to_color(env, bl, irn, &color);
365 env->plotter->vtab->set_color(env->plotter, &color);
366 env->plotter->vtab->line(env->plotter,
368 dims->box.y + dims->box.h,
378 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
380 plotter_t *p = env->plotter;
384 bbox.w = start_box->w + 2 * env->opts->x_margin;
385 bbox.h = start_box->h + 2 * env->opts->y_margin;
387 p->vtab->begin(p, &bbox);
388 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
392 void draw_interval_tree(const draw_chordal_opts_t *opts,
393 const be_chordal_env_t *chordal_env,
396 draw_chordal_env_t env;
397 struct block_dims *start_dims;
398 ir_node *start_block = get_irg_start_block(chordal_env->irg);
400 env.arch_env = chordal_env->main_env->arch_env;
402 env.block_dims = pmap_create();
403 env.plotter = plotter;
404 env.cls = chordal_env->cls;
406 env.chordal_env = chordal_env;
407 obstack_init(&env.obst);
409 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
410 layout(&env, start_block, opts->x_margin);
411 set_y(&env, start_block, opts->y_margin);
412 start_dims = pmap_get(env.block_dims, start_block);
413 draw(&env, &start_dims->subtree_box);
415 pmap_destroy(env.block_dims);
416 obstack_free(&env.obst, NULL);