1 /* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Martin Trapp, Christian Schaefer
6 ** irdump.h: dumping of an intermediate representation graph
10 # include "irgraph_t.h"
20 # include "type_or_entity.h"
22 # include "typewalk.h"
24 #define DEFAULT_NODE_ATTR ""
25 #define BLOCK_EDGE_ATTR "class: 2 priority: 2 linestyle: dotted"
26 #define NODE2TYPE_EDGE_ATTR ""
27 #define DEFAULT_TYPE_ATTRIBUTE ""
28 #define TYPE_EDGE_ATTR ""
34 /*******************************************************************/
35 /* routines to dump information about a single node */
36 /*******************************************************************/
41 dump_node_opcode (ir_node *n)
44 if (n->op->code == iro_Const) {
45 xfprintf (F, "%v", n->attr.con);
47 } else if (n->op->code == iro_SymConst) {
48 if (get_SymConst_kind(n) == linkage_ptr_info) {
49 xfprintf (F, "%I", get_SymConst_ptrinfo(n));
51 assert(get_kind(get_SymConst_type(n)) == k_type_class);
52 assert(get_class_ident((type_class *)get_SymConst_type(n)));
53 xfprintf (F, "%s ", id_to_str(get_class_ident((type_class *)get_SymConst_type(n))));
54 if (get_SymConst_kind == type_tag)
61 xfprintf (F, "%I", n->op->name);
66 dump_node_mode (ir_node *n)
68 switch (n->op->code) {
85 xfprintf (F, "%I", n->mode->name);
92 dump_node_nodeattr (ir_node *n)
94 switch (n->op->code) {
96 if (n->in[1]->op->code == iro_Cmp) {
97 xfprintf (F, "%s", get_pnc_string(n->attr.proj));
99 xfprintf (F, "%ld", n->attr.proj);
103 /*assert(n->attr.s.ent->kind == k_entity);*/
104 assert(get_kind(get_Sel_entity(n)) == k_entity);
105 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
107 /* xdoesn't work for some reason.
108 fprintf (F, "\"%I %I\" ", n->op->name, n->attr.s.ent); */
115 dump_node_vcgattr (ir_node *n)
117 switch (n->op->code) {
120 xfprintf (F, "color: blue");
123 xfprintf (F, "color: lightyellow");
126 xfprintf (F, "color: green");
131 xfprintf (F, "color: yellow");
134 xfprintf (F, DEFAULT_NODE_ATTR);
139 dump_node (ir_node *n) {
142 xfprintf (F, "node: {title: \"%p\" label: \"", n);
146 dump_node_nodeattr(n);
148 xfprintf (F, " %ld", get_irn_node_nr(n));
151 dump_node_vcgattr(n);
156 dump_ir_node (ir_node *n)
159 xfprintf (F, "node: {title: \"%p\" label: ", n);
161 switch (n->op->code) { /* node label */
163 xfprintf (F, "\"%I\" color: blue ", n->op->name);
164 xfprintf (F, DEFAULT_NODE_ATTR);
167 xfprintf (F, "\"%I\" color: blue ", n->op->name);
168 xfprintf (F, DEFAULT_NODE_ATTR);
171 xfprintf (F, "\"%I\" color: lightyellow ", n->op->name);
172 xfprintf (F, DEFAULT_NODE_ATTR);
175 xfprintf (F, "\"%I%I\" color: green", n->op->name, n->mode->name);
176 if (n->mode->code == irm_M)
177 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
179 xfprintf (F, DEFAULT_NODE_ATTR);
182 xfprintf (F, "\"%v%I\" color: yellow ", n->attr.con, n->mode->name);
183 xfprintf (F, DEFAULT_NODE_ATTR);
186 xfprintf (F, "\"%I%I\" ", n->op->name, n->mode->name);
187 xfprintf (F, DEFAULT_NODE_ATTR);
190 if (n->in[1]->op->code == iro_Cmp) {
191 xfprintf (F, "\"%I%I %s\" color: yellow", n->op->name, n->mode->name,
192 get_pnc_string(n->attr.proj));
194 xfprintf (F, "\"%I%I %ld\"", n->op->name, n->mode->name, n->attr.proj);
196 xfprintf (F, DEFAULT_NODE_ATTR);
199 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
200 xfprintf (F, DEFAULT_NODE_ATTR);
203 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
204 xfprintf (F, DEFAULT_NODE_ATTR);
207 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
208 xfprintf (F, DEFAULT_NODE_ATTR);
211 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
212 xfprintf (F, DEFAULT_NODE_ATTR);
215 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
216 xfprintf (F, DEFAULT_NODE_ATTR);
219 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
220 xfprintf (F, DEFAULT_NODE_ATTR);
223 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
224 xfprintf (F, DEFAULT_NODE_ATTR);
227 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
228 xfprintf (F, DEFAULT_NODE_ATTR);
231 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
232 xfprintf (F, DEFAULT_NODE_ATTR);
235 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
236 xfprintf (F, DEFAULT_NODE_ATTR);
239 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
240 xfprintf (F, DEFAULT_NODE_ATTR);
243 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
244 xfprintf (F, DEFAULT_NODE_ATTR);
247 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
248 xfprintf (F, DEFAULT_NODE_ATTR);
251 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
252 xfprintf (F, DEFAULT_NODE_ATTR);
255 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
256 xfprintf (F, DEFAULT_NODE_ATTR);
259 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
260 xfprintf (F, DEFAULT_NODE_ATTR);
263 xfprintf (F, "\"%I\"", n->op->name);
264 xfprintf (F, DEFAULT_NODE_ATTR);
267 xfprintf (F, "\"%I\"", n->op->name);
268 xfprintf (F, DEFAULT_NODE_ATTR);
271 xfprintf (F, "\"%I\"", n->op->name);
272 xfprintf (F, DEFAULT_NODE_ATTR);
275 xfprintf (F, "\"%I\"", n->op->name);
276 xfprintf (F, DEFAULT_NODE_ATTR);
279 xfprintf (F, "\"%I%I\"", n->op->name, n->mode->name);
280 xfprintf (F, DEFAULT_NODE_ATTR);
284 xfprintf (F, "\"%R\"", n);
285 xfprintf (F, DEFAULT_NODE_ATTR);
288 xfprintf (F, "\"%I\" ", n->op->name);
289 xfprintf (F, DEFAULT_NODE_ATTR);
292 assert(get_kind(get_Sel_entity(n)) == k_entity);
293 xfprintf (F, "\"%I ", n->op->name);
294 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
295 /* xdoesn't work for some reason.
296 fprintf (F, "\"%I %I\" ", n->op->name, get_entity_ident(get_Sel_entity(n))); */
297 xfprintf (F, DEFAULT_NODE_ATTR);
300 assert(get_kind(get_SymConst_type(n)) == k_type_class);
301 assert(get_class_ident((type_class *)get_SymConst_type(n)));
302 xfprintf (F, "\"%s ",
303 id_to_str(get_class_ident((type_class *)get_SymConst_type(n))));
304 switch (n->attr.i.num){
306 xfprintf (F, "tag\" ");
309 xfprintf (F, "size\" ");
315 xfprintf (F, DEFAULT_NODE_ATTR);
318 xfprintf (F, "\"%I\" ", n->op->name);
319 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
322 xfprintf (F, "\"%I%I\" ", n->op->name, n->mode->name);
323 xfprintf (F, DEFAULT_NODE_ATTR);
326 xfprintf (F, "\"%I%I\" ", n->op->name, n->mode->name);
328 xfprintf (F, "}\n"); /* footer */
332 /* dump the edge to the block this node belongs to */
334 dump_ir_block_edge(ir_node *n) {
336 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
337 BLOCK_EDGE_ATTR "}\n", n, get_nodes_Block(n));
341 /* dump edges to our inputs */
343 dump_ir_data_edges(ir_node *n) {
346 for (i = 0; i < get_irn_arity(n); i++) {
347 assert(get_irn_n(n, i));
348 xfprintf (F, "edge: {sourcename: \"%p\" targetname: \"%p\"",
350 fprintf (F, " label: \"%d\"", i+1);
355 /* dumps the edges between nodes and their type or entity attributes. */
356 void dump_node2type_edges (ir_node *n, void *env)
360 switch (get_irn_opcode(n)) {
362 if ( (get_SymConst_kind(n) == type_tag)
363 || (get_SymConst_kind(n) == size))
364 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
365 NODE2TYPE_EDGE_ATTR "}\n", n, get_SymConst_type(n));
368 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
369 NODE2TYPE_EDGE_ATTR "}\n", n, get_Sel_entity(n));
372 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
373 NODE2TYPE_EDGE_ATTR "}\n", n, get_Call_type(n));
376 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
377 NODE2TYPE_EDGE_ATTR "}\n", n, get_Alloc_type(n));
380 printf(" in irdum\n");
381 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
382 NODE2TYPE_EDGE_ATTR "}\n", n, get_Free_type(n));
390 /* dumps a type or entity and it's edges. */
392 dump_type_info (type_or_ent *tore, void *env) {
393 int i = 0; /* to shutup gcc */
395 /* dump this type or entity */
396 xfprintf (F, "node: {title: \"%p\" ", tore);
397 xfprintf (F, DEFAULT_TYPE_ATTRIBUTE);
398 xfprintf (F, "label: ");
400 switch (get_kind(tore)) {
403 entity *ent = (entity *)tore;
404 xfprintf (F, "\"ent %I\"}\n", get_entity_ident(ent));
405 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
407 TYPE_EDGE_ATTR "}\n", tore, get_entity_owner(ent));
408 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
410 TYPE_EDGE_ATTR "}\n", tore, get_entity_type(ent));
415 type_class *type = (type_class *)tore;
416 xfprintf (F, "\"class %I\"}\n", get_class_ident(type));
417 for (i=0; i < get_class_n_supertype(type); i++)
418 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
419 " label: \"supertype\" " TYPE_EDGE_ATTR "}\n",
420 type, get_class_supertype(type, i));
425 type_strct *type = (type_strct *)tore;
426 xfprintf (F, "\"strct %I\"}\n", get_strct_ident(type));
431 type_method *type = (type_method *)tore;
432 xfprintf (F, "\"meth %I\"}\n", get_method_ident(type));
433 for (i = 0; i < get_method_arity(type); i++)
434 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
435 " label: \"param %d\" " TYPE_EDGE_ATTR "}\n",
436 tore, get_method_param_type(type, i), i);
437 for (i = 0; i < get_method_n_res(type); i++)
438 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
439 " label: \"res %d\" " TYPE_EDGE_ATTR "}\n",
440 tore, get_method_res_type(type, i), i);
445 type_union *type = (type_union *)tore;
446 xfprintf (F, "\"union %I\"}\n", get_union_ident(type));
452 type_array *type = (type_array *)tore;
453 xfprintf (F, "\"array %I\"}\n", get_array_ident(type));
454 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
455 TYPE_EDGE_ATTR "}\n", tore, get_array_element_type(type), i);
458 case k_type_enumeration:
460 type_enumeration *type = (type_enumeration *)tore;
461 xfprintf (F, "\"enum %I\"}\n", get_enumeration_ident(type));
466 type_pointer *type = (type_pointer *)tore;
467 xfprintf (F, "\"ptr %I\"}\n", get_pointer_ident(type));
468 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
469 TYPE_EDGE_ATTR "}\n", tore,
470 get_pointer_points_to_type(type), i);
473 case k_type_primitive:
475 type_primitive *type = (type_primitive *)tore;
476 xfprintf (F, "\"prim %I, mode %I\"}\n", get_primitive_ident(type),
477 get_mode_ident(get_primitive_mode(type)));
486 /************************************************************************/
487 /* open and close vcg file */
488 /************************************************************************/
490 void vcg_open (ir_graph *irg, char *suffix) {
491 char *fname; /* filename to put the vcg information in */
496 /** open file for vcg graph */
497 id = get_entity_ld_name (get_irg_ent(irg));
498 len = id_to_strlen (id);
500 fname = malloc (len + 5 + strlen(suffix));
501 strcpy (fname, cp); /* copy the filename */
502 strcat (fname, suffix); /* append file suffix */
503 strcat (fname, ".vcg"); /* append the .vcg suffix */
504 F = fopen (fname, "w"); /* open file for writing */
506 panic ("cannot open %s for writing (%m)", fname); /* not reached */
511 "graph: { title: \"ir graph of %s\"\n"
512 "display_edge_labels: yes\n"
513 "layoutalgorithm: mindepth\n"
514 "manhattan_edges: yes\n"
516 "orientation: bottom_to_top\n"
517 "classname 1: \"Data\"\n"
518 "classname 2: \"Block\"\n", cp);
520 xfprintf (F, "\n"); /* a separator */
523 void vcg_open_name (const char *name) {
524 char *fname; /* filename to put the vcg information in */
527 /** open file for vcg graph */
529 fname = malloc (len + 5);
530 strcpy (fname, name); /* copy the filename */
531 strcat (fname, ".vcg"); /* append the .vcg suffix */
532 F = fopen (fname, "w"); /* open file for writing */
534 panic ("cannot open %s for writing (%m)", fname); /* not reached */
539 "graph: { title: \"ir graph of %s\"\n"
540 "display_edge_labels: yes\n"
541 "layoutalgorithm: mindepth\n"
542 "manhattan_edges: yes\n"
544 "orientation: bottom_to_top\n"
545 "classname 1: \"Data\"\n"
546 "classname 2: \"Block\"\n", name);
548 xfprintf (F, "\n"); /* a separator */
553 xfprintf (F, "}\n"); /* print footer */
554 fclose (F); /* close vcg file */
557 /************************************************************************/
558 /* routines to dump a graph, blocks as conventional nodes. */
559 /************************************************************************/
562 dump_whole_node (ir_node *n, void* env) {
564 dump_ir_block_edge(n);
565 dump_ir_data_edges(n);
569 dump_ir_graph (ir_graph *irg)
572 rem = current_ir_graph;
573 current_ir_graph = irg;
577 /* walk over the graph */
578 irg_walk(irg->end, dump_whole_node, NULL, NULL);
582 current_ir_graph = rem;
585 /***********************************************************************/
586 /* the following routines dump the nodes as attached to the blocks. */
587 /***********************************************************************/
590 dump_ir_blocks_nodes (ir_node *n, void *env) {
591 ir_node *block = (ir_node *)env;
593 if (is_no_Block(n) && get_nodes_Block(n) == block) {
595 dump_ir_data_edges(n);
600 dump_ir_block (ir_node *block, void *env) {
601 ir_graph *irg = (ir_graph *)env;
603 if (get_irn_opcode(block) == iro_Block) {
605 /* This is a block. So dump the vcg information to make a block. */
606 xfprintf(F, "graph: { title: \"%p\" label: \"", block);
608 xfprintf (F, "%ld", get_irn_node_nr(block));
610 xfprintf (F, "%I", block->op->name);
612 xfprintf(F, "\" status:clustered color:lightyellow \n");
613 /* dump the blocks edges */
614 dump_ir_data_edges(block);
616 /* dump the nodes that go into the block */
617 irg_walk(irg->end, dump_ir_blocks_nodes, NULL, block);
619 /* Close the vcg information for the block */
620 xfprintf(F, "}\n\n");
625 dump_ir_block_graph (ir_graph *irg)
628 rem = current_ir_graph;
629 current_ir_graph = irg;
633 /* walk over the blocks in the graph */
634 irg_block_walk(irg->end, dump_ir_block, NULL, irg);
637 current_ir_graph = rem;
641 /***********************************************************************/
642 /* the following routines dump a control flow graph */
643 /***********************************************************************/
647 dump_block_to_cfg (ir_node *block, void *env) {
651 if (get_irn_opcode(block) == iro_Block) {
652 /* This is a block. Dump a node for the block. */
653 xfprintf (F, "node: {title: \"%p\" label: \"%I\"}", block,
656 for ( i = 0; i < get_Block_n_cfgpreds(block); i++) {
657 pred = get_nodes_Block(skip_Proj(get_Block_cfgpred(block, i)));
658 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" }\n",
665 dump_cfg (ir_graph *irg)
667 vcg_open (irg, "-cfg");
669 /* walk over the blocks in the graph */
670 irg_block_walk(irg->end, dump_block_to_cfg, NULL, NULL);
676 /***********************************************************************/
677 /* the following routine dumps all type information reachable from an */
679 /***********************************************************************/
683 dump_type_graph (ir_graph *irg)
686 rem = current_ir_graph;
687 current_ir_graph = irg;
689 vcg_open (irg, "-type");
691 /* walk over the blocks in the graph */
692 type_walk_irg(irg, dump_type_info, NULL, NULL);
695 current_ir_graph = rem;
698 /***********************************************************************/
699 /* the following routine dumps all type information */
700 /***********************************************************************/
704 dump_all_types (void)
706 vcg_open_name ("All_types");
707 type_walk(dump_type_info, NULL, NULL);
711 /***********************************************************************/
712 /* dumps a graph with type information */
713 /***********************************************************************/
717 dump_ir_graph_w_types (ir_graph *irg)
720 rem = current_ir_graph;
721 current_ir_graph = irg;
723 vcg_open (irg, "-all");
725 /* dump common ir graph */
726 /* irg_block_walk(irg->end, dump_ir_block, NULL, irg); */
727 irg_walk(irg->end, dump_whole_node, NULL, NULL);
729 type_walk_irg(irg, dump_type_info, NULL, NULL);
730 /* dump edges from graph to type info */
731 irg_walk(irg->end, dump_node2type_edges, NULL, NULL);
734 current_ir_graph = rem;
737 /***********************************************************************/
738 /* dumps all graphs with the graph-dumper passed. Possible dumpers: */
740 /* dump_ir_block_graph */
742 /* dump_type_graph */
743 /* dump_ir_graph_w_types */
744 /***********************************************************************/
745 void dump_all_ir_graphs (void dump_graph(ir_graph*)) {
747 for (i=0; i < get_irp_n_irgs(); i++) {
748 dump_graph(get_irp_irg(i));