2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Paint chordal graphs.
23 * @author Sebastian Hack
35 #include "iredges_t.h"
40 #include "bechordal_t.h"
42 #include "bechordal_draw.h"
46 be_chordal_env_t *env;
52 #define decl_self(type, from) \
53 type *self = (type *) from
55 static void set_color(plotter_t *_self, const color_t *color)
57 decl_self(base_plotter_t, _self);
61 static const color_t *get_color(const plotter_t *_self)
63 decl_self(const base_plotter_t, _self);
67 static void set_width(plotter_t *_self, int width)
69 decl_self(base_plotter_t, _self);
73 static int get_width(const plotter_t *_self)
75 decl_self(const base_plotter_t, _self);
79 static void plotter_default_free(plotter_t *self)
93 | _ \/ ___| | _ \| | ___ | |_| |_ ___ _ __
94 | |_) \___ \ | |_) | |/ _ \| __| __/ _ \ '__|
95 | __/ ___) | | __/| | (_) | |_| || __/ |
96 |_| |____/ |_| |_|\___/ \__|\__\___|_|
100 static void ps_begin(plotter_t *_self, const rect_t *vis)
103 decl_self(ps_plotter_t, _self);
105 f = self->f = fopen(self->filename, "wt");
106 fprintf(f, "%%!PS-Adobe-2.0\n");
107 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
109 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
110 fprintf(f, "mainfont setfont\n");
114 static void ps_setcolor(plotter_t *_self, const color_t *color)
116 decl_self(ps_plotter_t, _self);
117 set_color(_self, color);
119 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
120 color->r, color->g, color->b);
123 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
125 decl_self(ps_plotter_t, _self);
127 fprintf(self->f, "%d %d moveto\n", x1, y1);
128 fprintf(self->f, "%d %d lineto\n", x2, y2);
129 fprintf(self->f, "stroke\n");
132 static void ps_box(plotter_t *_self, const rect_t *rect)
134 decl_self(ps_plotter_t, _self);
136 fprintf(self->f, "%d %d %d %d rectstroke\n",
137 rect->x, rect->y, rect->w, rect->h);
140 static void ps_text(plotter_t *_self, int x, int y, const char *str)
142 decl_self(ps_plotter_t, _self);
144 fprintf(self->f, "%d %d moveto\n", x, y);
145 fprintf(self->f, "(%s) show\n", str);
148 static void ps_finish(plotter_t *_self)
150 decl_self(ps_plotter_t, _self);
154 static const plotter_if_t ps_plotter_vtab = {
167 plotter_t *new_plotter_ps(const char *filename)
169 ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
170 plotter_t *p = (plotter_t *) ps_plotter;
172 ps_plotter->filename = filename;
173 p->vtab = &ps_plotter_vtab;
177 extern void plotter_free(plotter_t *self)
179 self->vtab->free(self);
183 const draw_chordal_opts_t draw_chordal_def_opts = {
184 10, 10, 30, 8, 10, 10
187 typedef struct draw_chordal_env_t {
188 const be_chordal_env_t *chordal_env;
189 const arch_register_class_t *cls;
192 const draw_chordal_opts_t *opts;
195 } draw_chordal_env_t;
205 #define doz(a, b) MAX((a) - (b), 0)
207 static void block_dims_walker(ir_node *block, void *data)
209 draw_chordal_env_t *env = (draw_chordal_env_t*)data;
210 struct list_head *head = get_block_border_head(env->chordal_env, block);
211 const draw_chordal_opts_t *opts = env->opts;
212 struct block_dims *dims = OALLOCZ(&env->obst, struct block_dims);
214 dims->min_step = INT_MAX;
216 foreach_border_head(head, b) {
217 ir_node *irn = b->irn;
218 const arch_register_t *reg = arch_get_irn_register(irn);
219 int col = reg->index;
221 dims->max_step = MAX(dims->max_step, b->step);
222 dims->max_color = MAX(dims->max_color, col);
223 env->max_color = MAX(env->max_color, col);
229 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
230 dims->box.h = dims->max_step * opts->v_inter_gap;
232 dims->box.w = dims->box.h = 10;
235 pmap_insert(env->block_dims, block, dims);
238 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
240 const draw_chordal_opts_t *opts = env->opts;
241 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
242 rect_t *rect = &dims->subtree_box;
247 memset(rect, 0, sizeof(*rect));
250 dominates_for_each(bl, sub) {
251 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
253 layout(env, sub, rect->x + rect->w);
255 rect->w += h_space + bl_dim->subtree_box.w;
256 rect->h = MAX(rect->h, bl_dim->subtree_box.h);
258 h_space = opts->h_gap;
259 v_space = opts->v_gap;
262 rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
264 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
265 dims->box.y = rect->h + v_space;
267 rect->h = dims->box.y + dims->box.h;
270 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
272 const draw_chordal_opts_t *opts = env->opts;
273 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
274 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
277 dominates_for_each(bl, sub) {
278 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
279 int height_diff = max_height - bl_dim->subtree_box.h;
281 set_y(env, sub, up + height_diff);
284 dims->subtree_box.y += up;
288 static color_t *reg_to_color(const draw_chordal_env_t *env,
289 ir_node *rel_bl, ir_node *irn, color_t *color)
295 foreach_out_edge(irn, edge)
296 phi_arg |= is_Phi(edge->src);
299 color->r = is_Phi(irn) ? 0.5 : 0.0;
300 color->g = phi_arg ? 0.5 : 0.0;
304 int live_in = is_live_in(rel_bl, irn);
305 int live_out = is_live_out(rel_bl, irn);
316 static void draw_block(ir_node *bl, void *data)
318 static const color_t black = { 0, 0, 0 };
319 const draw_chordal_env_t *env = (const draw_chordal_env_t*)data;
320 const be_lv_t *lv = be_get_irg_liveness(env->chordal_env->irg);
321 struct list_head *head = get_block_border_head(env->chordal_env, bl);
322 ir_node *dom = get_Block_idom(bl);
323 const draw_chordal_opts_t *opts = env->opts;
324 struct block_dims *dims = pmap_get(struct block_dims, env->block_dims, bl);
327 ir_snprintf(buf, sizeof(buf), "%F", bl);
329 env->plotter->vtab->set_color(env->plotter, &black);
330 env->plotter->vtab->box(env->plotter, &dims->box);
333 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
336 foreach_border_head(head, b) {
338 const arch_register_t *reg = arch_get_irn_register(b->irn);
339 int live_out = be_is_live_out(lv, bl, b->irn);
340 int x = (reg->index + 1) * opts->h_inter_gap;
341 int ystart = (b->step) * opts->v_inter_gap;
342 int ystop = (b->other_end->step) * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
345 reg_to_color(env, bl, b->irn, &color);
348 ystart += dims->box.y;
349 ystop += dims->box.y;
351 env->plotter->vtab->set_color(env->plotter, &color);
352 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
354 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
355 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
360 struct block_dims *dom_dims = pmap_get(struct block_dims, env->block_dims, dom);
362 be_lv_foreach_cls(lv, bl, be_lv_state_in, env->cls, irn) {
363 const arch_register_t *reg = arch_get_irn_register(irn);
364 int x = (reg->index + 1) * opts->h_inter_gap;
367 reg_to_color(env, bl, irn, &color);
369 env->plotter->vtab->set_color(env->plotter, &color);
370 env->plotter->vtab->line(env->plotter,
372 dims->box.y + dims->box.h,
379 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
381 ir_graph *irg = env->chordal_env->irg;
382 plotter_t *p = env->plotter;
386 bbox.w = start_box->w + 2 * env->opts->x_margin;
387 bbox.h = start_box->h + 2 * env->opts->y_margin;
389 be_assure_live_sets(irg);
390 be_assure_live_chk(irg);
392 p->vtab->begin(p, &bbox);
393 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
397 void draw_interval_tree(const draw_chordal_opts_t *opts,
398 const be_chordal_env_t *chordal_env, plotter_t *plotter)
400 draw_chordal_env_t env;
401 struct block_dims *start_dims;
402 ir_node *start_block = get_irg_start_block(chordal_env->irg);
405 env.block_dims = pmap_create();
406 env.plotter = plotter;
407 env.cls = chordal_env->cls;
409 env.chordal_env = chordal_env;
410 obstack_init(&env.obst);
412 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
413 layout(&env, start_block, opts->x_margin);
414 set_y(&env, start_block, opts->y_margin);
415 start_dims = pmap_get(struct block_dims, env.block_dims, start_block);
416 draw(&env, &start_dims->subtree_box);
418 pmap_destroy(env.block_dims);
419 obstack_free(&env.obst, NULL);