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
36 #include "iredges_t.h"
40 #include "bechordal_t.h"
41 #include "besched_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) {
56 decl_self(base_plotter_t, _self);
60 static const color_t *get_color(const plotter_t *_self) {
61 decl_self(const base_plotter_t, _self);
65 static void set_width(plotter_t *_self, int width) {
66 decl_self(base_plotter_t, _self);
70 static int get_width(const plotter_t *_self) {
71 decl_self(const base_plotter_t, _self);
75 static void plotter_default_free(plotter_t *self) {
88 | _ \/ ___| | _ \| | ___ | |_| |_ ___ _ __
89 | |_) \___ \ | |_) | |/ _ \| __| __/ _ \ '__|
90 | __/ ___) | | __/| | (_) | |_| || __/ |
91 |_| |____/ |_| |_|\___/ \__|\__\___|_|
95 static void ps_begin(plotter_t *_self, const rect_t *vis)
98 decl_self(ps_plotter_t, _self);
100 f = self->f = fopen(self->filename, "wt");
101 fprintf(f, "%%!PS-Adobe-2.0\n");
102 fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
104 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
105 fprintf(f, "mainfont setfont\n");
109 static void ps_setcolor(plotter_t *_self, const color_t *color)
111 decl_self(ps_plotter_t, _self);
112 set_color(_self, color);
114 fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
115 color->r, color->g, color->b);
118 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
120 decl_self(ps_plotter_t, _self);
122 fprintf(self->f, "%d %d moveto\n", x1, y1);
123 fprintf(self->f, "%d %d lineto\n", x2, y2);
124 fprintf(self->f, "stroke\n");
127 static void ps_box(plotter_t *_self, const rect_t *rect)
129 decl_self(ps_plotter_t, _self);
131 fprintf(self->f, "%d %d %d %d rectstroke\n",
132 rect->x, rect->y, rect->w, rect->h);
135 void ps_text(plotter_t *_self, int x, int y, const char *str)
137 decl_self(ps_plotter_t, _self);
139 fprintf(self->f, "%d %d moveto\n", x, y);
140 fprintf(self->f, "(%s) show\n", str);
143 static void ps_finish(plotter_t *_self)
145 decl_self(ps_plotter_t, _self);
149 const plotter_if_t ps_plotter_vtab = {
162 plotter_t *new_plotter_ps(const char *filename)
164 ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
165 plotter_t *p = (plotter_t *) ps_plotter;
167 ps_plotter->filename = filename;
168 p->vtab = &ps_plotter_vtab;
173 _____ _ _ _____ ____ _ _ _
174 |_ _(_) | __|__ / | _ \| | ___ | |_| |_ ___ _ __
175 | | | | |/ / / / | |_) | |/ _ \| __| __/ _ \ '__|
176 | | | | < / /_ | __/| | (_) | |_| || __/ |
177 |_| |_|_|\_\/____| |_| |_|\___/ \__|\__\___|_|
181 /* chriswue: the following seems to be unused and can be deleted? */
185 const char *filename;
189 static void tikz_begin(plotter_t *_self, const rect_t *vis)
192 decl_self(tikz_plotter_t, _self);
194 f = self->f = fopen(self->filename, "wt");
195 fprintf(f, "\\begin{tikzpicture}\n");
198 static void tikz_setcolor(plotter_t *_self, const color_t *color)
200 set_color(_self, color);
203 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
205 decl_self(tikz_plotter_t, _self);
206 fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
209 static void tikz_box(plotter_t *_self, const rect_t *rect)
211 decl_self(tikz_plotter_t, _self);
213 fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
214 rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
217 void tikz_text(plotter_t *_self, int x, int y, const char *str)
219 decl_self(tikz_plotter_t, _self);
220 fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
223 static void tikz_finish(plotter_t *_self)
225 decl_self(tikz_plotter_t, _self);
231 extern void plotter_free(plotter_t *self)
233 self->vtab->free(self);
237 const draw_chordal_opts_t draw_chordal_def_opts = {
238 10, 10, 30, 8, 10, 10
241 typedef struct _draw_chordal_env_t {
242 const be_chordal_env_t *chordal_env;
243 const arch_register_class_t *cls;
246 const draw_chordal_opts_t *opts;
249 } draw_chordal_env_t;
259 #define doz(a, b) MAX((a) - (b), 0)
261 static void block_dims_walker(ir_node *block, void *data)
263 draw_chordal_env_t *env = data;
264 struct list_head *head = get_block_border_head(env->chordal_env, block);
265 const draw_chordal_opts_t *opts = env->opts;
266 struct block_dims *dims = obstack_alloc(&env->obst, sizeof(*dims));
269 memset(dims, 0, sizeof(*dims));
270 dims->min_step = INT_MAX;
272 list_for_each_entry_reverse(border_t, b, head, list) {
273 ir_node *irn = b->irn;
274 const arch_register_t *reg = arch_get_irn_register(irn);
275 int col = arch_register_get_index(reg);
277 dims->max_step = MAX(dims->max_step, b->step);
278 dims->max_color = MAX(dims->max_color, col);
279 env->max_color = MAX(env->max_color, col);
285 dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
286 dims->box.h = dims->max_step * opts->v_inter_gap;
288 dims->box.w = dims->box.h = 10;
291 pmap_insert(env->block_dims, block, dims);
294 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
296 const draw_chordal_opts_t *opts = env->opts;
297 struct block_dims *dims = pmap_get(env->block_dims, bl);
298 rect_t *rect = &dims->subtree_box;
303 memset(rect, 0, sizeof(*rect));
306 dominates_for_each(bl, sub) {
307 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
309 layout(env, sub, rect->x + rect->w);
311 rect->w += h_space + bl_dim->subtree_box.w;
312 rect->h = MAX(rect->h, bl_dim->subtree_box.h);
314 h_space = opts->h_gap;
315 v_space = opts->v_gap;
318 rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
320 dims->box.x = x + doz(rect->w, dims->box.w) / 2;
321 dims->box.y = rect->h + v_space;
323 rect->h = dims->box.y + dims->box.h;
326 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
328 const draw_chordal_opts_t *opts = env->opts;
329 struct block_dims *dims = pmap_get(env->block_dims, bl);
330 int max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
333 dominates_for_each(bl, sub) {
334 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
335 int height_diff = max_height - bl_dim->subtree_box.h;
337 set_y(env, sub, up + height_diff);
340 dims->subtree_box.y += up;
344 static color_t *reg_to_color(const draw_chordal_env_t *env,
345 ir_node *rel_bl, ir_node *irn, color_t *color)
348 const ir_edge_t *edge;
352 foreach_out_edge(irn, edge)
353 phi_arg |= is_Phi(edge->src);
356 color->r = is_Phi(irn) ? 0.5 : 0.0;
357 color->g = phi_arg ? 0.5 : 0.0;
361 int live_in = is_live_in(rel_bl, irn);
362 int live_out = is_live_out(rel_bl, irn);
373 static void draw_block(ir_node *bl, void *data)
375 static const color_t black = { 0, 0, 0 };
376 const draw_chordal_env_t *env = data;
377 const be_lv_t *lv = be_get_birg_liveness(env->chordal_env->birg);
378 struct list_head *head = get_block_border_head(env->chordal_env, bl);
379 ir_node *dom = get_Block_idom(bl);
380 const draw_chordal_opts_t *opts = env->opts;
381 struct block_dims *dims = pmap_get(env->block_dims, bl);
386 ir_snprintf(buf, sizeof(buf), "%F", bl);
388 env->plotter->vtab->set_color(env->plotter, &black);
389 env->plotter->vtab->box(env->plotter, &dims->box);
392 env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
395 list_for_each_entry(border_t, b, head, list) {
397 const arch_register_t *reg = arch_get_irn_register(b->irn);
398 int col = arch_register_get_index(reg);
399 int live_out = be_is_live_out(lv, bl, b->irn);
400 int x = (col + 1) * opts->h_inter_gap;
401 int ystart = (b->step) * opts->v_inter_gap;
402 int ystop = (b->other_end->step) * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
405 reg_to_color(env, bl, b->irn, &color);
408 ystart += dims->box.y;
409 ystop += dims->box.y;
411 env->plotter->vtab->set_color(env->plotter, &color);
412 env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
414 env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
415 env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
420 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
422 be_lv_foreach(lv, bl, be_lv_state_in, idx) {
423 ir_node *irn = be_lv_get_irn(lv, bl, idx);
424 if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
425 const arch_register_t *reg = arch_get_irn_register(irn);
426 int col = arch_register_get_index(reg);
427 int x = (col + 1) * opts->h_inter_gap;
430 reg_to_color(env, bl, irn, &color);
432 env->plotter->vtab->set_color(env->plotter, &color);
433 env->plotter->vtab->line(env->plotter,
435 dims->box.y + dims->box.h,
443 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
445 plotter_t *p = env->plotter;
450 bbox.w = start_box->w + 2 * env->opts->x_margin;
451 bbox.h = start_box->h + 2 * env->opts->y_margin;
453 lv = be_assure_liveness(env->chordal_env->birg);
454 be_liveness_assure_sets(lv);
455 be_liveness_assure_chk(lv);
457 p->vtab->begin(p, &bbox);
458 irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
462 void draw_interval_tree(const draw_chordal_opts_t *opts, const be_chordal_env_t *chordal_env, plotter_t *plotter) {
463 draw_chordal_env_t env;
464 struct block_dims *start_dims;
465 ir_node *start_block = get_irg_start_block(chordal_env->irg);
468 env.block_dims = pmap_create();
469 env.plotter = plotter;
470 env.cls = chordal_env->cls;
472 env.chordal_env = chordal_env;
473 obstack_init(&env.obst);
475 irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
476 layout(&env, start_block, opts->x_margin);
477 set_y(&env, start_block, opts->y_margin);
478 start_dims = pmap_get(env.block_dims, start_block);
479 draw(&env, &start_dims->subtree_box);
481 pmap_destroy(env.block_dims);
482 obstack_free(&env.obst, NULL);