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"
39 #include "bechordal_t.h"
41 #include "bechordal_draw.h"
45 be_chordal_env_t *env;
51 #define decl_self(type, from) \
52 type *self = (type *) from
54 static void set_color(plotter_t *_self, const color_t *color)
56 decl_self(base_plotter_t, _self);
60 static const color_t *get_color(const plotter_t *_self)
62 decl_self(const base_plotter_t, _self);
66 static void set_width(plotter_t *_self, int width)
68 decl_self(base_plotter_t, _self);
72 static int get_width(const plotter_t *_self)
74 decl_self(const base_plotter_t, _self);
78 static void plotter_default_free(plotter_t *self)
92 | _ \/ ___| | _ \| | ___ | |_| |_ ___ _ __
93 | |_) \___ \ | |_) | |/ _ \| __| __/ _ \ '__|
94 | __/ ___) | | __/| | (_) | |_| || __/ |
95 |_| |____/ |_| |_|\___/ \__|\__\___|_|
99 static void ps_begin(plotter_t *_self, const rect_t *vis)
102 decl_self(ps_plotter_t, _self);
104 f = self->f = fopen(self->filename, "wt");
105 fprintf(f, "%%!PS-Adobe-2.0\n");
106 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
108 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
109 fprintf(f, "mainfont setfont\n");
113 static void ps_setcolor(plotter_t *_self, const color_t *color)
115 decl_self(ps_plotter_t, _self);
116 set_color(_self, color);
118 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
119 color->r, color->g, color->b);
122 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
124 decl_self(ps_plotter_t, _self);
126 fprintf(self->f, "%d %d moveto\n", x1, y1);
127 fprintf(self->f, "%d %d lineto\n", x2, y2);
128 fprintf(self->f, "stroke\n");
131 static void ps_box(plotter_t *_self, const rect_t *rect)
133 decl_self(ps_plotter_t, _self);
135 fprintf(self->f, "%d %d %d %d rectstroke\n",
136 rect->x, rect->y, rect->w, rect->h);
139 static void ps_text(plotter_t *_self, int x, int y, const char *str)
141 decl_self(ps_plotter_t, _self);
143 fprintf(self->f, "%d %d moveto\n", x, y);
144 fprintf(self->f, "(%s) show\n", str);
147 static void ps_finish(plotter_t *_self)
149 decl_self(ps_plotter_t, _self);
153 static const plotter_if_t ps_plotter_vtab = {
166 plotter_t *new_plotter_ps(const char *filename)
168 ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
169 plotter_t *p = (plotter_t *) ps_plotter;
171 ps_plotter->filename = filename;
172 p->vtab = &ps_plotter_vtab;
177 _____ _ _ _____ ____ _ _ _
178 |_ _(_) | __|__ / | _ \| | ___ | |_| |_ ___ _ __
179 | | | | |/ / / / | |_) | |/ _ \| __| __/ _ \ '__|
180 | | | | < / /_ | __/| | (_) | |_| || __/ |
181 |_| |_|_|\_\/____| |_| |_|\___/ \__|\__\___|_|
185 /* chriswue: the following seems to be unused and can be deleted? */
189 const char *filename;
193 static void tikz_begin(plotter_t *_self, const rect_t *vis)
196 decl_self(tikz_plotter_t, _self);
198 f = self->f = fopen(self->filename, "wt");
199 fprintf(f, "\\begin{tikzpicture}\n");
202 static void tikz_setcolor(plotter_t *_self, const color_t *color)
204 set_color(_self, color);
207 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
209 decl_self(tikz_plotter_t, _self);
210 fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
213 static void tikz_box(plotter_t *_self, const rect_t *rect)
215 decl_self(tikz_plotter_t, _self);
217 fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
218 rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
221 void tikz_text(plotter_t *_self, int x, int y, const char *str)
223 decl_self(tikz_plotter_t, _self);
224 fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
227 static void tikz_finish(plotter_t *_self)
229 decl_self(tikz_plotter_t, _self);
235 extern void plotter_free(plotter_t *self)
237 self->vtab->free(self);
241 const draw_chordal_opts_t draw_chordal_def_opts = {
242 10, 10, 30, 8, 10, 10
245 typedef struct draw_chordal_env_t {
246 const be_chordal_env_t *chordal_env;
247 const arch_register_class_t *cls;
250 const draw_chordal_opts_t *opts;
253 } draw_chordal_env_t;
263 #define doz(a, b) MAX((a) - (b), 0)
265 static void block_dims_walker(ir_node *block, void *data)
267 draw_chordal_env_t *env = (draw_chordal_env_t*)data;
268 struct list_head *head = get_block_border_head(env->chordal_env, block);
269 const draw_chordal_opts_t *opts = env->opts;
270 struct block_dims *dims = OALLOCZ(&env->obst, struct block_dims);
273 dims->min_step = INT_MAX;
275 list_for_each_entry_reverse(border_t, b, head, list) {
276 ir_node *irn = b->irn;
277 const arch_register_t *reg = arch_get_irn_register(irn);
278 int col = arch_register_get_index(reg);
280 dims->max_step = MAX(dims->max_step, b->step);
281 dims->max_color = MAX(dims->max_color, col);
282 env->max_color = MAX(env->max_color, col);
288 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
289 dims->box.h = dims->max_step * opts->v_inter_gap;
291 dims->box.w = dims->box.h = 10;
294 pmap_insert(env->block_dims, block, dims);
297 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
299 const draw_chordal_opts_t *opts = env->opts;
300 struct block_dims *dims = (struct block_dims*)pmap_get(env->block_dims, bl);
301 rect_t *rect = &dims->subtree_box;
306 memset(rect, 0, sizeof(*rect));
309 dominates_for_each(bl, sub) {
310 struct block_dims *bl_dim = (struct block_dims*)pmap_get(env->block_dims, sub);
312 layout(env, sub, rect->x + rect->w);
314 rect->w += h_space + bl_dim->subtree_box.w;
315 rect->h = MAX(rect->h, bl_dim->subtree_box.h);
317 h_space = opts->h_gap;
318 v_space = opts->v_gap;
321 rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
323 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
324 dims->box.y = rect->h + v_space;
326 rect->h = dims->box.y + dims->box.h;
329 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
331 const draw_chordal_opts_t *opts = env->opts;
332 struct block_dims *dims = (struct block_dims*)pmap_get(env->block_dims, bl);
333 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
336 dominates_for_each(bl, sub) {
337 struct block_dims *bl_dim = (struct block_dims*)pmap_get(env->block_dims, sub);
338 int height_diff = max_height - bl_dim->subtree_box.h;
340 set_y(env, sub, up + height_diff);
343 dims->subtree_box.y += up;
347 static color_t *reg_to_color(const draw_chordal_env_t *env,
348 ir_node *rel_bl, ir_node *irn, color_t *color)
351 const ir_edge_t *edge;
355 foreach_out_edge(irn, edge)
356 phi_arg |= is_Phi(edge->src);
359 color->r = is_Phi(irn) ? 0.5 : 0.0;
360 color->g = phi_arg ? 0.5 : 0.0;
364 int live_in = is_live_in(rel_bl, irn);
365 int live_out = is_live_out(rel_bl, irn);
376 static void draw_block(ir_node *bl, void *data)
378 static const color_t black = { 0, 0, 0 };
379 const draw_chordal_env_t *env = (const draw_chordal_env_t*)data;
380 const be_lv_t *lv = be_get_irg_liveness(env->chordal_env->irg);
381 struct list_head *head = get_block_border_head(env->chordal_env, bl);
382 ir_node *dom = get_Block_idom(bl);
383 const draw_chordal_opts_t *opts = env->opts;
384 struct block_dims *dims = (struct block_dims*)pmap_get(env->block_dims, bl);
389 ir_snprintf(buf, sizeof(buf), "%F", bl);
391 env->plotter->vtab->set_color(env->plotter, &black);
392 env->plotter->vtab->box(env->plotter, &dims->box);
395 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
398 list_for_each_entry(border_t, b, head, list) {
400 const arch_register_t *reg = arch_get_irn_register(b->irn);
401 int col = arch_register_get_index(reg);
402 int live_out = be_is_live_out(lv, bl, b->irn);
403 int x = (col + 1) * opts->h_inter_gap;
404 int ystart = (b->step) * opts->v_inter_gap;
405 int ystop = (b->other_end->step) * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
408 reg_to_color(env, bl, b->irn, &color);
411 ystart += dims->box.y;
412 ystop += dims->box.y;
414 env->plotter->vtab->set_color(env->plotter, &color);
415 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
417 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
418 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
423 struct block_dims *dom_dims = (struct block_dims*)pmap_get(env->block_dims, dom);
425 be_lv_foreach(lv, bl, be_lv_state_in, idx) {
426 ir_node *irn = be_lv_get_irn(lv, bl, idx);
427 if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
428 const arch_register_t *reg = arch_get_irn_register(irn);
429 int col = arch_register_get_index(reg);
430 int x = (col + 1) * opts->h_inter_gap;
433 reg_to_color(env, bl, irn, &color);
435 env->plotter->vtab->set_color(env->plotter, &color);
436 env->plotter->vtab->line(env->plotter,
438 dims->box.y + dims->box.h,
446 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
448 plotter_t *p = env->plotter;
453 bbox.w = start_box->w + 2 * env->opts->x_margin;
454 bbox.h = start_box->h + 2 * env->opts->y_margin;
456 lv = be_assure_liveness(env->chordal_env->irg);
457 be_liveness_assure_sets(lv);
458 be_liveness_assure_chk(lv);
460 p->vtab->begin(p, &bbox);
461 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
465 void draw_interval_tree(const draw_chordal_opts_t *opts,
466 const be_chordal_env_t *chordal_env, plotter_t *plotter)
468 draw_chordal_env_t env;
469 struct block_dims *start_dims;
470 ir_node *start_block = get_irg_start_block(chordal_env->irg);
473 env.block_dims = pmap_create();
474 env.plotter = plotter;
475 env.cls = chordal_env->cls;
477 env.chordal_env = chordal_env;
478 obstack_init(&env.obst);
480 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
481 layout(&env, start_block, opts->x_margin);
482 set_y(&env, start_block, opts->y_margin);
483 start_dims = (struct block_dims*)pmap_get(env.block_dims, start_block);
484 draw(&env, &start_dims->subtree_box);
486 pmap_destroy(env.block_dims);
487 obstack_free(&env.obst, NULL);