3 * @file bechordal_draw.c
5 * @author Sebastian Hack
7 * Paint chordal graphs.
9 * Copyright (C) 2005 Universitaet Karlsruhe
10 * Released under the GPL
22 #include "bechordal_t.h"
23 #include "besched_t.h"
24 #include "bechordal_draw.h"
27 be_chordal_env_t *env;
33 #define decl_self(type, from) \
34 type *self = (type *) from
36 static void set_color(plotter_t *_self, const color_t *color)
38 decl_self(base_plotter_t, _self);
42 static const color_t *get_color(const plotter_t *_self)
44 decl_self(const base_plotter_t, _self);
48 static void set_width(plotter_t *_self, int width)
50 decl_self(base_plotter_t, _self);
54 static int get_width(const plotter_t *_self)
56 decl_self(const base_plotter_t, _self);
60 static void plotter_default_free(plotter_t *self)
70 static void ps_begin(plotter_t *_self, const rect_t *vis)
73 decl_self(ps_plotter_t, _self);
75 f = self->f = fopen(self->filename, "wt");
76 fprintf(f, "%%!PS-Adobe-2.0\n");
77 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
79 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
80 fprintf(f, "mainfont setfont\n");
84 static void ps_setcolor(plotter_t *_self, const color_t *color)
86 decl_self(ps_plotter_t, _self);
87 set_color(_self, color);
89 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
90 color->r, color->g, color->b);
93 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
95 decl_self(ps_plotter_t, _self);
97 fprintf(self->f, "%d %d moveto\n", x1, y1);
98 fprintf(self->f, "%d %d lineto\n", x2, y2);
99 fprintf(self->f, "stroke\n");
102 static void ps_box(plotter_t *_self, const rect_t *rect)
104 decl_self(ps_plotter_t, _self);
106 fprintf(self->f, "%d %d %d %d rectstroke\n",
107 rect->x, rect->y, rect->w, rect->h);
110 void ps_text(plotter_t *_self, int x, int y, const char *str)
112 decl_self(ps_plotter_t, _self);
114 fprintf(self->f, "%d %d moveto\n", x, y);
115 fprintf(self->f, "(%s) show\n", str);
118 static void ps_finish(plotter_t *_self)
120 decl_self(ps_plotter_t, _self);
124 const plotter_if_t ps_plotter_vtab = {
137 plotter_t *new_plotter_ps(const char *filename)
139 ps_plotter_t *ps_plotter = malloc(sizeof(*ps_plotter));
140 plotter_t *p = (plotter_t *) ps_plotter;
142 ps_plotter->filename = filename;
143 p->vtab = &ps_plotter_vtab;
147 extern void plotter_free(plotter_t *self)
149 self->vtab->free(self);
153 const draw_chordal_opts_t draw_chordal_def_opts = {
154 10, 10, 30, 8, 10, 10
157 typedef struct _draw_chordal_env_t {
158 const be_chordal_env_t *chordal_env;
159 const arch_env_t *arch_env;
160 const arch_register_class_t *cls;
163 const draw_chordal_opts_t *opts;
167 } draw_chordal_env_t;
177 static INLINE int min(int a, int b)
179 return a < b ? a : b;
182 static INLINE int max(int a, int b)
184 return a > b ? a : b;
187 #define doz(a, b) max((a) - (b), 0)
189 static void block_dims_walker(ir_node *block, void *data)
191 draw_chordal_env_t *env = data;
193 struct list_head *head = get_block_border_head(env->chordal_env, block);
194 const draw_chordal_opts_t *opts = env->opts;
195 struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
197 memset(dims, 0, sizeof(*dims));
198 dims->min_step = INT_MAX;
200 list_for_each_entry_reverse(border_t, b, head, list) {
201 ir_node *irn = b->irn;
202 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
203 int col = arch_register_get_index(reg);
205 dims->max_step = max(dims->max_step, b->step);
206 dims->max_color = max(dims->max_color, col);
207 env->max_color = max(env->max_color, col);
213 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
214 dims->box.h = dims->max_step * opts->v_inter_gap;
216 dims->box.w = dims->box.h = 10;
219 pmap_insert(env->block_dims, block, dims);
222 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
224 const draw_chordal_opts_t *opts = env->opts;
225 struct block_dims *dims = pmap_get(env->block_dims, bl);
227 rect_t *rect = &dims->subtree_box;
228 int h_space = 0, v_space = 0;
230 memset(rect, 0, sizeof(*rect));
233 dominates_for_each(bl, sub) {
234 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
236 layout(env, sub, rect->x + rect->w);
238 rect->w += h_space + bl_dim->subtree_box.w;
239 rect->h = max(rect->h, bl_dim->subtree_box.h);
241 h_space = opts->h_gap;
242 v_space = opts->v_gap;
245 rect->w = max(rect->w, dims->box.w + opts->h_gap);
247 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
248 dims->box.y = rect->h + v_space;
250 rect->h += dims->box.h + v_space;
253 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
255 const draw_chordal_opts_t *opts = env->opts;
256 struct block_dims *dims = pmap_get(env->block_dims, bl);
257 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
260 dominates_for_each(bl, sub) {
261 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
262 int height_diff = max_height - bl_dim->subtree_box.h;
264 set_y(env, sub, up + height_diff);
267 dims->subtree_box.y += up;
271 static color_t *reg_to_color(const draw_chordal_env_t *env,
272 ir_node *rel_bl, ir_node *irn, color_t *color)
274 int i, n, phi_arg = 0;
276 for(i = 0, n = get_irn_n_outs(irn); i < n && !phi_arg; ++i)
277 phi_arg |= is_Phi(get_irn_out(irn, i));
280 color->r = is_Phi(irn) ? 0.5 : 0.0;
281 color->g = phi_arg ? 0.5 : 0.0;
285 int live_in = is_live_in(rel_bl, irn);
286 int live_out = is_live_out(rel_bl, irn);
298 static void draw_block(ir_node *bl, void *data)
300 static const color_t black = { 0, 0, 0 };
302 const draw_chordal_env_t *env = data;
303 pset *live_in = get_live_in(bl);
306 struct list_head *head = get_block_border_head(env->chordal_env, bl);
307 ir_node *dom = get_Block_idom(bl);
308 const draw_chordal_opts_t *opts = env->opts;
309 struct block_dims *dims = pmap_get(env->block_dims, bl);
312 ir_snprintf(buf, sizeof(buf), "%F", bl);
314 env->plotter->vtab->set_color(env->plotter, &black);
315 env->plotter->vtab->box(env->plotter, &dims->box);
318 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
321 list_for_each_entry(border_t, b, head, list) {
323 const arch_register_t *reg = arch_get_irn_register(env->arch_env, b->irn, 0);
324 int col = arch_register_get_index(reg);
325 int live_in = is_live_in(bl, b->irn);
326 int live_out = is_live_out(bl, b->irn);
327 int x = (col + 1) * opts->h_inter_gap;
328 int ystart = (b->step) * opts->v_inter_gap;
329 int ystop = (b->other_end->step)
330 * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
333 reg_to_color(env, bl, b->irn, &color);
336 ystart += dims->box.y;
337 ystop += dims->box.y;
339 env->plotter->vtab->set_color(env->plotter, &color);
340 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
342 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
343 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
348 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
350 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
351 if(arch_irn_has_reg_class(env->arch_env, irn, 0, env->cls)) {
352 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
353 int col = arch_register_get_index(reg);
354 int x = (col + 1) * opts->h_inter_gap;
357 reg_to_color(env, bl, irn, &color);
359 env->plotter->vtab->set_color(env->plotter, &color);
360 env->plotter->vtab->line(env->plotter,
362 dims->box.y + dims->box.h,
371 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
374 line.x = dims->box.x;
375 line.y = dims->box.y;
376 line.w = dom_dims->box.x;
377 line.h = dom_dims->box.y;
379 env->plotter->vtab->line(env->plotter, &line);
384 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
386 plotter_t *p = env->plotter;
390 bbox.w = start_box->w + 2 * env->opts->x_margin;
391 bbox.h = start_box->h + 2 * env->opts->y_margin;
393 p->vtab->begin(p, &bbox);
394 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
398 void draw_interval_tree(const draw_chordal_opts_t *opts,
399 const be_chordal_env_t *chordal_env,
400 plotter_t *plotter, const arch_env_t *arch_env,
401 const arch_register_class_t *cls)
403 draw_chordal_env_t env;
404 struct block_dims *start_dims;
405 ir_node *start_block = get_irg_start_block(chordal_env->irg);
407 env.arch_env = arch_env;
409 env.block_dims = pmap_create();
410 env.plotter = plotter;
411 env.chordal_env = chordal_env;
414 env.chordal_env = chordal_env;
415 obstack_init(&env.obst);
417 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
418 layout(&env, start_block, opts->x_margin);
419 set_y(&env, start_block, opts->y_margin);
420 start_dims = pmap_get(env.block_dims, start_block);
421 draw(&env, &start_dims->subtree_box);
423 pmap_destroy(env.block_dims);
424 obstack_free(&env.obst, NULL);