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
13 # include "irnode_t.h"
14 # include "irgraph_t.h"
24 # include "type_or_entity.h"
26 # include "typewalk.h"
28 /* Attributes of nodes */
29 #define DEFAULT_NODE_ATTR ""
30 #define DEFAULT_TYPE_ATTRIBUTE ""
32 /* Attributes of edges between Firm nodes */
33 #define BLOCK_EDGE_ATTR "class: 2 priority: 2 linestyle: dotted"
34 #define CF_EDGE_ATTR "color: red"
35 #define MEM_EDGE_ATTR "color: blue"
37 /* Attributes of edges between Firm nodes and type/entity nodes */
38 #define NODE2TYPE_EDGE_ATTR ""
40 /* Attributes of edges in type/entity graphs. */
41 #define TYPE_METH_NODE_ATTR "color: lightyellow"
42 #define TYPE_CLASS_NODE_ATTR "color: green"
43 #define ENTITY_NODE_ATTR "color: yellow"
44 #define ENT_TYPE_EDGE_ATTR "label: \"type\" color: red"
45 #define ENT_OWN_EDGE_ATTR "label: \"owner\" color: black"
46 #define METH_PAR_EDGE_ATTR "label: \"param %d\" color: green"
47 #define METH_RES_EDGE_ATTR "label: \"res %d\" color: green"
48 #define TYPE_SUPER_EDGE_ATTR "label: \"supertype\" color: blue"
49 #define UNION_EDGE_ATTR "label: \"component\" color: blue"
50 #define PTR_PTS_TO_EDGE_ATTR "label: \"points to\" color:green"
51 #define ARR_ELT_TYPE_EDGE_ATTR "label: \"arr elt\" color:green"
53 #define PRINT_NODEID(X) fprintf(F, "%p", X)
60 /*******************************************************************/
61 /* routines to dump information about a single node */
62 /*******************************************************************/
67 dump_node_opcode (ir_node *n)
70 if (n->op->code == iro_Const) {
71 xfprintf (F, "%v", n->attr.con);
73 } else if (n->op->code == iro_SymConst) {
74 if (get_SymConst_kind(n) == linkage_ptr_info) {
75 xfprintf (F, "%I", get_SymConst_ptrinfo(n));
77 assert(get_kind(get_SymConst_type(n)) == k_type);
78 assert(get_type_nameid(get_SymConst_type(n)));
79 xfprintf (F, "%s ", id_to_str(get_type_nameid(get_SymConst_type(n))));
80 if (get_SymConst_kind == type_tag)
87 xfprintf (F, "%I", get_irn_opident(n));
92 dump_node_mode (ir_node *n)
94 switch (n->op->code) {
111 xfprintf (F, "%I", get_mode_ident(n->mode));
118 dump_node_nodeattr (ir_node *n)
120 switch (n->op->code) {
122 if (n->in[1]->op->code == iro_Cmp) {
123 xfprintf (F, "%s", get_pnc_string(n->attr.proj));
125 xfprintf (F, "%ld", n->attr.proj);
129 assert(get_kind(get_Sel_entity(n)) == k_entity);
130 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
137 dump_node_vcgattr (ir_node *n)
139 switch (n->op->code) {
142 xfprintf (F, "color: blue");
145 xfprintf (F, "color: lightyellow");
148 xfprintf (F, "color: green");
153 xfprintf (F, "color: yellow");
156 xfprintf (F, DEFAULT_NODE_ATTR);
161 dump_node (ir_node *n) {
164 xfprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: \"");
169 dump_node_nodeattr(n);
171 xfprintf (F, " %ld", get_irn_node_nr(n));
174 dump_node_vcgattr(n);
179 dump_ir_node (ir_node *n)
182 fprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: ");
184 switch (n->op->code) { /* node label */
186 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
187 xfprintf (F, DEFAULT_NODE_ATTR);
190 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
191 xfprintf (F, DEFAULT_NODE_ATTR);
194 xfprintf (F, "\"%I\" color: lightyellow ", get_irn_opident(n));
195 xfprintf (F, DEFAULT_NODE_ATTR);
198 xfprintf (F, "\"%I%I\" color: green", get_irn_opident(n), get_irn_modeident(n));
199 if (get_irn_modecode(n) == irm_M)
200 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
202 xfprintf (F, DEFAULT_NODE_ATTR);
205 xfprintf (F, "\"%v%I\" color: yellow ", n->attr.con, get_irn_modeident(n));
206 xfprintf (F, DEFAULT_NODE_ATTR);
209 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
210 xfprintf (F, DEFAULT_NODE_ATTR);
213 if (n->in[1]->op->code == iro_Cmp) {
214 xfprintf (F, "\"%I%I %s\" color: yellow", get_irn_opident(n), get_irn_modeident(n),
215 get_pnc_string(n->attr.proj));
217 xfprintf (F, "\"%I%I %ld\"", get_irn_opident(n), get_irn_modeident(n), n->attr.proj);
219 xfprintf (F, DEFAULT_NODE_ATTR);
222 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
223 xfprintf (F, DEFAULT_NODE_ATTR);
226 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
227 xfprintf (F, DEFAULT_NODE_ATTR);
230 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
231 xfprintf (F, DEFAULT_NODE_ATTR);
234 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
235 xfprintf (F, DEFAULT_NODE_ATTR);
238 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
239 xfprintf (F, DEFAULT_NODE_ATTR);
242 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
243 xfprintf (F, DEFAULT_NODE_ATTR);
246 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
247 xfprintf (F, DEFAULT_NODE_ATTR);
250 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
251 xfprintf (F, DEFAULT_NODE_ATTR);
254 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
255 xfprintf (F, DEFAULT_NODE_ATTR);
258 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
259 xfprintf (F, DEFAULT_NODE_ATTR);
262 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
263 xfprintf (F, DEFAULT_NODE_ATTR);
266 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
267 xfprintf (F, DEFAULT_NODE_ATTR);
270 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
271 xfprintf (F, DEFAULT_NODE_ATTR);
274 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
275 xfprintf (F, DEFAULT_NODE_ATTR);
278 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
279 xfprintf (F, DEFAULT_NODE_ATTR);
282 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
283 xfprintf (F, DEFAULT_NODE_ATTR);
286 xfprintf (F, "\"%I\"", get_irn_opident(n));
287 xfprintf (F, DEFAULT_NODE_ATTR);
290 xfprintf (F, "\"%I\"", get_irn_opident(n));
291 xfprintf (F, DEFAULT_NODE_ATTR);
294 xfprintf (F, "\"%I\"", get_irn_opident(n));
295 xfprintf (F, DEFAULT_NODE_ATTR);
298 xfprintf (F, "\"%I\"", get_irn_opident(n));
299 xfprintf (F, DEFAULT_NODE_ATTR);
302 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
303 xfprintf (F, DEFAULT_NODE_ATTR);
307 xfprintf (F, "\"%R\"", n);
308 xfprintf (F, DEFAULT_NODE_ATTR);
311 xfprintf (F, "\"%I\" ", get_irn_opident(n));
312 xfprintf (F, DEFAULT_NODE_ATTR);
315 assert(get_kind(get_Sel_entity(n)) == k_entity);
316 xfprintf (F, "\"%I ", get_irn_opident(n));
317 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
318 xfprintf (F, DEFAULT_NODE_ATTR);
321 assert(get_kind(get_SymConst_type(n)) == k_type);
322 assert(get_type_nameid(get_SymConst_type(n)));
323 xfprintf (F, "\"%s ", get_type_name(get_SymConst_type(n)));
324 switch (n->attr.i.num){
326 xfprintf (F, "tag\" ");
329 xfprintf (F, "size\" ");
335 xfprintf (F, DEFAULT_NODE_ATTR);
338 xfprintf (F, "\"%I\" ", get_irn_opident(n));
339 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
342 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
343 xfprintf (F, DEFAULT_NODE_ATTR);
346 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
348 xfprintf (F, "}\n"); /* footer */
352 /* dump the edge to the block this node belongs to */
354 dump_ir_block_edge(ir_node *n) {
356 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
357 BLOCK_EDGE_ATTR "}\n", n, get_nodes_Block(n));
360 void print_edge_vcgattr(ir_node *from, int to) {
363 switch (get_irn_opcode(from)) {
365 xfprintf (F, CF_EDGE_ATTR);
367 case iro_Start: break;
370 case iro_Cond: break;
373 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
375 case iro_Const: break;
376 case iro_SymConst:break;
379 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
383 case iro_Minus: break;
389 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
397 case iro_Shrs: break;
400 case iro_Conv: break;
402 if (get_irn_modecode(from) == irm_M) xfprintf (F, MEM_EDGE_ATTR);
408 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
411 xfprintf (F, MEM_EDGE_ATTR);
413 case iro_Tuple: break;
415 switch (get_irn_modecode(from)) {
417 xfprintf (F, CF_EDGE_ATTR);
420 xfprintf (F, MEM_EDGE_ATTR);
431 /* dump edges to our inputs */
433 dump_ir_data_edges(ir_node *n) {
436 for (i = 0; i < get_irn_arity(n); i++) {
437 assert(get_irn_n(n, i));
438 xfprintf (F, "edge: {sourcename: \"%p\" targetname: \"%p\"",
440 fprintf (F, " label: \"%d\" ", i+1);
441 print_edge_vcgattr(n, i);
446 /* dumps the edges between nodes and their type or entity attributes. */
447 void dump_node2type_edges (ir_node *n, void *env)
451 switch (get_irn_opcode(n)) {
453 if ( (get_SymConst_kind(n) == type_tag)
454 || (get_SymConst_kind(n) == size))
455 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
456 NODE2TYPE_EDGE_ATTR "}\n", n, get_SymConst_type(n));
459 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
460 NODE2TYPE_EDGE_ATTR "}\n", n, get_Sel_entity(n));
463 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
464 NODE2TYPE_EDGE_ATTR "}\n", n, get_Call_type(n));
467 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
468 NODE2TYPE_EDGE_ATTR "}\n", n, get_Alloc_type(n));
471 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
472 NODE2TYPE_EDGE_ATTR "}\n", n, get_Free_type(n));
480 /* dumps a type or entity and it's edges. */
482 dump_type_info (type_or_ent *tore, void *env) {
483 int i = 0; /* to shutup gcc */
485 /* dump this type or entity */
486 xfprintf (F, "node: {title: \"%p\" ", tore);
487 xfprintf (F, DEFAULT_TYPE_ATTRIBUTE);
488 xfprintf (F, "label: ");
490 switch (get_kind(tore)) {
493 entity *ent = (entity *)tore;
494 xfprintf (F, "\"ent %I\" " ENTITY_NODE_ATTR "}\n", get_entity_ident(ent));
495 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
496 ENT_OWN_EDGE_ATTR "}\n", tore, get_entity_owner(ent));
497 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
498 ENT_TYPE_EDGE_ATTR "}\n", tore, get_entity_type(ent));
502 /* why can't I cast here??? @@@ */
504 xfprintf (F, "\"%I %I", get_type_tpop_nameid(type), get_type_nameid(type));
506 switch (get_type_tpop_code(type)) {
509 xfprintf (F, "\" " TYPE_CLASS_NODE_ATTR "}\n");
510 for (i=0; i < get_class_n_supertype(type); i++)
511 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
512 TYPE_SUPER_EDGE_ATTR "}\n",
513 type, get_class_supertype(type, i));
517 xfprintf (F, "\"}\n");
521 xfprintf (F, "\" " TYPE_METH_NODE_ATTR "}\n");
522 for (i = 0; i < get_method_n_params(type); i++)
523 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
524 METH_PAR_EDGE_ATTR "}\n",
525 type, get_method_param_type(type, i), i);
526 for (i = 0; i < get_method_n_res(type); i++)
527 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
528 METH_RES_EDGE_ATTR "}\n",
529 type, get_method_res_type(type, i), i);
533 xfprintf (F, "\"}\n");
534 for (i = 0; i < get_union_n_members(type); i++)
535 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
536 "label: \"\"f" UNION_EDGE_ATTR "}\n",
537 type, get_union_member(type, i));
541 xfprintf (F, "\"}\n");
542 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
543 ARR_ELT_TYPE_EDGE_ATTR "}\n", type, get_array_element_type(type), i);
545 case tpo_enumeration:
547 xfprintf (F, "\"}\n");
551 xfprintf (F, "\"}\n");
552 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
553 PTR_PTS_TO_EDGE_ATTR "}\n", type,
554 get_pointer_points_to_type(type), i);
558 xfprintf (F, "mode %I\"}\n", get_mode_ident(get_type_mode(type)));
563 } break; /* case k_type */
566 xfprintf (F, "\" faulty type \"}\n");
567 printf(" *** irdump, %s(l.%i), faulty type.\n", __FUNCTION__, __LINE__);
569 } /* switch kind_or_entity */
572 /************************************************************************/
573 /* open and close vcg file */
574 /************************************************************************/
576 void vcg_open (ir_graph *irg, char *suffix) {
577 char *fname; /* filename to put the vcg information in */
583 /** open file for vcg graph */
584 id = get_entity_ld_name (get_irg_ent(irg));
585 len = id_to_strlen (id);
588 fname = malloc (len + 5 + strlen(suffix));
589 strncpy (fname, cp, len); /* copy the filename */
591 strcat (fname, suffix); /* append file suffix */
593 fname = malloc (len + 5 + strlen(suffix));
594 strncpy (fname, cp, len); /* copy the filename */
595 fname[len] = '\0'; /* ensure string termination */
596 /*strcpy (fname, cp); * copy the filename *
597 this produces wrong, too long strings in conjuction with the
598 jocca frontend. The \0 seems to be missing. */
599 strcat (fname, suffix); /* append file suffix */
600 strcat (fname, ".vcg"); /* append the .vcg suffix */
601 F = fopen (fname, "w"); /* open file for writing */
603 panic ("cannot open %s for writing (%m)", fname); /* not reached */
607 strcpy(label, "yes");
609 strcpy (label, "no");
614 "graph: { title: \"ir graph of %s\"\n"
615 "display_edge_labels: %s\n"
616 "layoutalgorithm: mindepth\n"
617 "manhattan_edges: yes\n"
619 "orientation: bottom_to_top\n"
620 "classname 1: \"Data\"\n"
621 "classname 2: \"Block\"\n", cp, label);
623 xfprintf (F, "\n"); /* a separator */
626 void vcg_open_name (const char *name) {
627 char *fname; /* filename to put the vcg information in */
631 /** open file for vcg graph */
633 fname = malloc (len + 5);
634 strcpy (fname, name); /* copy the filename */
635 strcat (fname, ".vcg"); /* append the .vcg suffix */
636 F = fopen (fname, "w"); /* open file for writing */
638 panic ("cannot open %s for writing (%m)", fname); /* not reached */
642 strcpy(label, "yes");
644 strcpy (label, "no");
649 "graph: { title: \"ir graph of %s\"\n"
650 "display_edge_labels: %s\n"
651 "layoutalgorithm: mindepth\n"
652 "manhattan_edges: yes\n"
654 "orientation: bottom_to_top\n"
655 "classname 1: \"Data\"\n"
656 "classname 2: \"Block\"\n", name, label);
658 xfprintf (F, "\n"); /* a separator */
663 xfprintf (F, "}\n"); /* print footer */
664 fclose (F); /* close vcg file */
667 /************************************************************************/
668 /* routines to dump a graph, blocks as conventional nodes. */
669 /************************************************************************/
672 dump_whole_node (ir_node *n, void* env) {
674 dump_ir_block_edge(n);
675 dump_ir_data_edges(n);
679 dump_ir_graph (ir_graph *irg)
682 rem = current_ir_graph;
683 current_ir_graph = irg;
687 /* walk over the graph */
688 irg_walk(irg->end, dump_whole_node, NULL, NULL);
692 current_ir_graph = rem;
695 /***********************************************************************/
696 /* the following routines dump the nodes as attached to the blocks. */
697 /***********************************************************************/
700 dump_ir_blocks_nodes (ir_node *n, void *env) {
701 ir_node *block = (ir_node *)env;
703 if (is_no_Block(n) && get_nodes_Block(n) == block) {
705 dump_ir_data_edges(n);
710 dump_ir_block (ir_node *block, void *env) {
711 ir_graph *irg = (ir_graph *)env;
713 if (get_irn_opcode(block) == iro_Block) {
715 /* This is a block. So dump the vcg information to make a block. */
716 xfprintf(F, "graph: { title: \""); PRINT_NODEID(block); fprintf(F, "\" label: \"");
718 xfprintf (F, "%ld", get_irn_node_nr(block));
720 xfprintf (F, "%I", block->op->name);
722 xfprintf(F, "\" status:clustered color:lightyellow \n");
723 /* dump the blocks edges */
724 dump_ir_data_edges(block);
726 /* dump the nodes that go into the block */
727 irg_walk(irg->end, dump_ir_blocks_nodes, NULL, block);
729 /* Close the vcg information for the block */
730 xfprintf(F, "}\n\n");
735 dump_ir_block_graph (ir_graph *irg)
738 rem = current_ir_graph;
739 current_ir_graph = irg;
743 /* walk over the blocks in the graph */
744 irg_block_walk(irg->end, dump_ir_block, NULL, irg);
747 current_ir_graph = rem;
751 /***********************************************************************/
752 /* the following routines dump a control flow graph */
753 /***********************************************************************/
757 dump_block_to_cfg (ir_node *block, void *env) {
761 if (get_irn_opcode(block) == iro_Block) {
762 /* This is a block. Dump a node for the block. */
763 xfprintf (F, "node: {title: \"%p\" label: \"%I\"}", block,
766 for ( i = 0; i < get_Block_n_cfgpreds(block); i++) {
767 pred = get_nodes_Block(skip_Proj(get_Block_cfgpred(block, i)));
768 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" }\n",
775 dump_cfg (ir_graph *irg)
777 vcg_open (irg, "-cfg");
779 /* walk over the blocks in the graph */
780 irg_block_walk(irg->end, dump_block_to_cfg, NULL, NULL);
786 /***********************************************************************/
787 /* the following routine dumps all type information reachable from an */
789 /***********************************************************************/
793 dump_type_graph (ir_graph *irg)
796 rem = current_ir_graph;
797 current_ir_graph = irg;
799 vcg_open (irg, "-type");
801 /* walk over the blocks in the graph */
802 type_walk_irg(irg, dump_type_info, NULL, NULL);
805 current_ir_graph = rem;
808 /***********************************************************************/
809 /* the following routine dumps all type information */
810 /***********************************************************************/
814 dump_all_types (void)
816 vcg_open_name ("All_types");
817 type_walk(dump_type_info, NULL, NULL);
821 /***********************************************************************/
822 /* dumps a graph with type information */
823 /***********************************************************************/
827 dump_ir_graph_w_types (ir_graph *irg)
830 rem = current_ir_graph;
831 current_ir_graph = irg;
833 vcg_open (irg, "-all");
835 /* dump common ir graph */
836 /* irg_block_walk(irg->end, dump_ir_block, NULL, irg); */
837 irg_walk(irg->end, dump_whole_node, NULL, NULL);
839 type_walk_irg(irg, dump_type_info, NULL, NULL);
840 /* dump edges from graph to type info */
841 irg_walk(irg->end, dump_node2type_edges, NULL, NULL);
844 current_ir_graph = rem;
847 /***********************************************************************/
848 /* dumps all graphs with the graph-dumper passed. Possible dumpers: */
850 /* dump_ir_block_graph */
852 /* dump_type_graph */
853 /* dump_ir_graph_w_types */
854 /***********************************************************************/
855 void dump_all_ir_graphs (void dump_graph(ir_graph*)) {
857 for (i=0; i < get_irp_n_irgs(); i++) {
858 dump_graph(get_irp_irg(i));
863 /* To turn off display of edge labels. Edge labels offen cause xvcg to
864 abort with a segmentation fault. */
865 void turn_of_edge_labels() {