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)
71 if (n->op->code == iro_Const) {
72 xfprintf (F, "%v", n->attr.con);
75 } else if (n->op->code == iro_SymConst) {
76 if (get_SymConst_kind(n) == linkage_ptr_info) {
77 xfprintf (F, "%I", get_SymConst_ptrinfo(n));
79 assert(get_kind(get_SymConst_type(n)) == k_type);
80 assert(get_type_nameid(get_SymConst_type(n)));
81 xfprintf (F, "%s ", id_to_str(get_type_nameid(get_SymConst_type(n))));
82 if (get_SymConst_kind == type_tag)
89 xfprintf (F, "%I", get_irn_opident(n));
94 dump_node_mode (ir_node *n)
96 switch (n->op->code) {
113 xfprintf (F, "%I", get_mode_ident(n->mode));
120 dump_node_nodeattr (ir_node *n)
122 switch (n->op->code) {
124 if (n->in[1]->op->code == iro_Cmp) {
125 xfprintf (F, "%s", get_pnc_string(n->attr.proj));
127 xfprintf (F, "%ld", n->attr.proj);
131 assert(get_kind(get_Sel_entity(n)) == k_entity);
132 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
139 dump_node_vcgattr (ir_node *n)
141 switch (n->op->code) {
144 xfprintf (F, "color: blue");
147 xfprintf (F, "color: lightyellow");
150 xfprintf (F, "color: green");
155 xfprintf (F, "color: yellow");
158 xfprintf (F, DEFAULT_NODE_ATTR);
163 dump_node (ir_node *n) {
166 xfprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: \"");
171 dump_node_nodeattr(n);
173 xfprintf (F, " %ld", get_irn_node_nr(n));
176 dump_node_vcgattr(n);
181 dump_ir_node (ir_node *n)
184 fprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: ");
186 switch (n->op->code) { /* node label */
188 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
189 xfprintf (F, DEFAULT_NODE_ATTR);
192 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
193 xfprintf (F, DEFAULT_NODE_ATTR);
196 xfprintf (F, "\"%I\" color: lightyellow ", get_irn_opident(n));
197 xfprintf (F, DEFAULT_NODE_ATTR);
200 xfprintf (F, "\"%I%I\" color: green", get_irn_opident(n), get_irn_modeident(n));
201 if (get_irn_modecode(n) == irm_M)
202 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
204 xfprintf (F, DEFAULT_NODE_ATTR);
207 xfprintf (F, "\"%v%I\" color: yellow ", n->attr.con, get_irn_modeident(n));
208 xfprintf (F, DEFAULT_NODE_ATTR);
211 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
212 xfprintf (F, DEFAULT_NODE_ATTR);
215 if (n->in[1]->op->code == iro_Cmp) {
216 xfprintf (F, "\"%I%I %s\" color: yellow", get_irn_opident(n), get_irn_modeident(n),
217 get_pnc_string(n->attr.proj));
219 xfprintf (F, "\"%I%I %ld\"", get_irn_opident(n), get_irn_modeident(n), n->attr.proj);
221 xfprintf (F, DEFAULT_NODE_ATTR);
224 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
225 xfprintf (F, DEFAULT_NODE_ATTR);
228 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
229 xfprintf (F, DEFAULT_NODE_ATTR);
232 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
233 xfprintf (F, DEFAULT_NODE_ATTR);
236 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
237 xfprintf (F, DEFAULT_NODE_ATTR);
240 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
241 xfprintf (F, DEFAULT_NODE_ATTR);
244 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
245 xfprintf (F, DEFAULT_NODE_ATTR);
248 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
249 xfprintf (F, DEFAULT_NODE_ATTR);
252 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
253 xfprintf (F, DEFAULT_NODE_ATTR);
256 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
257 xfprintf (F, DEFAULT_NODE_ATTR);
260 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
261 xfprintf (F, DEFAULT_NODE_ATTR);
264 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
265 xfprintf (F, DEFAULT_NODE_ATTR);
268 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
269 xfprintf (F, DEFAULT_NODE_ATTR);
272 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
273 xfprintf (F, DEFAULT_NODE_ATTR);
276 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
277 xfprintf (F, DEFAULT_NODE_ATTR);
280 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
281 xfprintf (F, DEFAULT_NODE_ATTR);
284 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
285 xfprintf (F, DEFAULT_NODE_ATTR);
288 xfprintf (F, "\"%I\"", get_irn_opident(n));
289 xfprintf (F, DEFAULT_NODE_ATTR);
292 xfprintf (F, "\"%I\"", get_irn_opident(n));
293 xfprintf (F, DEFAULT_NODE_ATTR);
296 xfprintf (F, "\"%I\"", get_irn_opident(n));
297 xfprintf (F, DEFAULT_NODE_ATTR);
300 xfprintf (F, "\"%I\"", get_irn_opident(n));
301 xfprintf (F, DEFAULT_NODE_ATTR);
304 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
305 xfprintf (F, DEFAULT_NODE_ATTR);
309 xfprintf (F, "\"%R\"", n);
310 xfprintf (F, DEFAULT_NODE_ATTR);
313 xfprintf (F, "\"%I\" ", get_irn_opident(n));
314 xfprintf (F, DEFAULT_NODE_ATTR);
317 assert(get_kind(get_Sel_entity(n)) == k_entity);
318 xfprintf (F, "\"%I ", get_irn_opident(n));
319 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
320 xfprintf (F, DEFAULT_NODE_ATTR);
323 assert(get_kind(get_SymConst_type(n)) == k_type);
324 assert(get_type_nameid(get_SymConst_type(n)));
325 xfprintf (F, "\"%s ", get_type_name(get_SymConst_type(n)));
326 switch (n->attr.i.num){
328 xfprintf (F, "tag\" ");
331 xfprintf (F, "size\" ");
337 xfprintf (F, DEFAULT_NODE_ATTR);
340 xfprintf (F, "\"%I\" ", get_irn_opident(n));
341 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
344 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
345 xfprintf (F, DEFAULT_NODE_ATTR);
348 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
350 xfprintf (F, "}\n"); /* footer */
354 /* dump the edge to the block this node belongs to */
356 dump_ir_block_edge(ir_node *n) {
358 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
359 BLOCK_EDGE_ATTR "}\n", n, get_nodes_Block(n));
362 void print_edge_vcgattr(ir_node *from, int to) {
365 switch (get_irn_opcode(from)) {
367 xfprintf (F, CF_EDGE_ATTR);
369 case iro_Start: break;
372 case iro_Cond: break;
375 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
377 case iro_Const: break;
378 case iro_SymConst:break;
381 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
385 case iro_Minus: break;
391 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
399 case iro_Shrs: break;
402 case iro_Conv: break;
404 if (get_irn_modecode(from) == irm_M) xfprintf (F, MEM_EDGE_ATTR);
410 if (to == 0) xfprintf (F, MEM_EDGE_ATTR);
413 xfprintf (F, MEM_EDGE_ATTR);
415 case iro_Tuple: break;
417 switch (get_irn_modecode(from)) {
419 xfprintf (F, CF_EDGE_ATTR);
422 xfprintf (F, MEM_EDGE_ATTR);
433 /* dump edges to our inputs */
435 dump_ir_data_edges(ir_node *n) {
438 for (i = 0; i < get_irn_arity(n); i++) {
439 assert(get_irn_n(n, i));
440 xfprintf (F, "edge: {sourcename: \"%p\" targetname: \"%p\"",
442 fprintf (F, " label: \"%d\" ", i+1);
443 print_edge_vcgattr(n, i);
448 /* dumps the edges between nodes and their type or entity attributes. */
449 void dump_node2type_edges (ir_node *n, void *env)
453 switch (get_irn_opcode(n)) {
455 if ( (get_SymConst_kind(n) == type_tag)
456 || (get_SymConst_kind(n) == size))
457 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
458 NODE2TYPE_EDGE_ATTR "}\n", n, get_SymConst_type(n));
461 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
462 NODE2TYPE_EDGE_ATTR "}\n", n, get_Sel_entity(n));
465 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
466 NODE2TYPE_EDGE_ATTR "}\n", n, get_Call_type(n));
469 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
470 NODE2TYPE_EDGE_ATTR "}\n", n, get_Alloc_type(n));
473 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
474 NODE2TYPE_EDGE_ATTR "}\n", n, get_Free_type(n));
482 /* dumps a type or entity and it's edges. */
484 dump_type_info (type_or_ent *tore, void *env) {
485 int i = 0; /* to shutup gcc */
487 /* dump this type or entity */
488 xfprintf (F, "node: {title: \"%p\" ", tore);
489 xfprintf (F, DEFAULT_TYPE_ATTRIBUTE);
490 xfprintf (F, "label: ");
492 switch (get_kind(tore)) {
495 entity *ent = (entity *)tore;
496 xfprintf (F, "\"ent %I\" " ENTITY_NODE_ATTR "}\n", get_entity_ident(ent));
497 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
498 ENT_OWN_EDGE_ATTR "}\n", tore, get_entity_owner(ent));
499 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
500 ENT_TYPE_EDGE_ATTR "}\n", tore, get_entity_type(ent));
504 /* why can't I cast here??? @@@ */
506 xfprintf (F, "\"%I %I", get_type_tpop_nameid(type), get_type_nameid(type));
508 switch (get_type_tpop_code(type)) {
511 xfprintf (F, "\" " TYPE_CLASS_NODE_ATTR "}\n");
512 for (i=0; i < get_class_n_supertype(type); i++)
513 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
514 TYPE_SUPER_EDGE_ATTR "}\n",
515 type, get_class_supertype(type, i));
519 xfprintf (F, "\"}\n");
523 xfprintf (F, "\" " TYPE_METH_NODE_ATTR "}\n");
524 for (i = 0; i < get_method_n_params(type); i++)
525 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
526 METH_PAR_EDGE_ATTR "}\n",
527 type, get_method_param_type(type, i), i);
528 for (i = 0; i < get_method_n_res(type); i++)
529 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
530 METH_RES_EDGE_ATTR "}\n",
531 type, get_method_res_type(type, i), i);
535 xfprintf (F, "\"}\n");
536 for (i = 0; i < get_union_n_members(type); i++)
537 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
538 "label: \"\"f" UNION_EDGE_ATTR "}\n",
539 type, get_union_member(type, i));
543 xfprintf (F, "\"}\n");
544 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
545 ARR_ELT_TYPE_EDGE_ATTR "}\n", type, get_array_element_type(type), i);
547 case tpo_enumeration:
549 xfprintf (F, "\"}\n");
553 xfprintf (F, "\"}\n");
554 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
555 PTR_PTS_TO_EDGE_ATTR "}\n", type,
556 get_pointer_points_to_type(type), i);
560 xfprintf (F, "mode %I\"}\n", get_mode_ident(get_type_mode(type)));
565 } break; /* case k_type */
568 xfprintf (F, "\" faulty type \"}\n");
569 printf(" *** irdump, %s(l.%i), faulty type.\n", __FUNCTION__, __LINE__);
571 } /* switch kind_or_entity */
574 /************************************************************************/
575 /* open and close vcg file */
576 /************************************************************************/
578 void vcg_open (ir_graph *irg, char *suffix) {
579 char *fname; /* filename to put the vcg information in */
585 /** open file for vcg graph */
586 id = get_entity_ld_ident (get_irg_ent(irg));
587 len = id_to_strlen (id);
590 fname = malloc (len + 5 + strlen(suffix));
591 strncpy (fname, cp, len); /* copy the filename */
593 strcat (fname, suffix); /* append file suffix */
595 fname = malloc (len + 5 + strlen(suffix));
596 strncpy (fname, cp, len); /* copy the filename */
597 fname[len] = '\0'; /* ensure string termination */
598 /*strcpy (fname, cp); * copy the filename *
599 this produces wrong, too long strings in conjuction with the
600 jocca frontend. The \0 seems to be missing. */
601 strcat (fname, suffix); /* append file suffix */
602 strcat (fname, ".vcg"); /* append the .vcg suffix */
603 F = fopen (fname, "w"); /* open file for writing */
605 panic ("cannot open %s for writing (%m)", fname); /* not reached */
609 strcpy(label, "yes");
611 strcpy (label, "no");
616 "graph: { title: \"ir graph of %s\"\n"
617 "display_edge_labels: %s\n"
618 "layoutalgorithm: mindepth\n"
619 "manhattan_edges: yes\n"
621 "orientation: bottom_to_top\n"
622 "classname 1: \"Data\"\n"
623 "classname 2: \"Block\"\n", cp, label);
625 xfprintf (F, "\n"); /* a separator */
628 void vcg_open_name (const char *name) {
629 char *fname; /* filename to put the vcg information in */
633 /** open file for vcg graph */
635 fname = malloc (len + 5);
636 strcpy (fname, name); /* copy the filename */
637 strcat (fname, ".vcg"); /* append the .vcg suffix */
638 F = fopen (fname, "w"); /* open file for writing */
640 panic ("cannot open %s for writing (%m)", fname); /* not reached */
644 strcpy(label, "yes");
646 strcpy (label, "no");
651 "graph: { title: \"ir graph of %s\"\n"
652 "display_edge_labels: %s\n"
653 "layoutalgorithm: mindepth\n"
654 "manhattan_edges: yes\n"
656 "orientation: bottom_to_top\n"
657 "classname 1: \"Data\"\n"
658 "classname 2: \"Block\"\n", name, label);
660 xfprintf (F, "\n"); /* a separator */
665 xfprintf (F, "}\n"); /* print footer */
666 fclose (F); /* close vcg file */
669 /************************************************************************/
670 /* routines to dump a graph, blocks as conventional nodes. */
671 /************************************************************************/
674 dump_whole_node (ir_node *n, void* env) {
676 dump_ir_block_edge(n);
677 dump_ir_data_edges(n);
681 dump_ir_graph (ir_graph *irg)
684 rem = current_ir_graph;
685 current_ir_graph = irg;
689 /* walk over the graph */
690 irg_walk(irg->end, dump_whole_node, NULL, NULL);
694 current_ir_graph = rem;
697 /***********************************************************************/
698 /* the following routines dump the nodes as attached to the blocks. */
699 /***********************************************************************/
702 dump_ir_blocks_nodes (ir_node *n, void *env) {
703 ir_node *block = (ir_node *)env;
705 if (is_no_Block(n) && get_nodes_Block(n) == block) {
707 dump_ir_data_edges(n);
712 dump_ir_block (ir_node *block, void *env) {
713 ir_graph *irg = (ir_graph *)env;
715 if (get_irn_opcode(block) == iro_Block) {
717 /* This is a block. So dump the vcg information to make a block. */
718 xfprintf(F, "graph: { title: \""); PRINT_NODEID(block); fprintf(F, "\" label: \"");
720 xfprintf (F, "%ld", get_irn_node_nr(block));
722 xfprintf (F, "%I", block->op->name);
724 xfprintf(F, "\" status:clustered color:lightyellow \n");
725 /* dump the blocks edges */
726 dump_ir_data_edges(block);
728 /* dump the nodes that go into the block */
729 irg_walk(irg->end, dump_ir_blocks_nodes, NULL, block);
731 /* Close the vcg information for the block */
732 xfprintf(F, "}\n\n");
737 dump_ir_block_graph (ir_graph *irg)
740 rem = current_ir_graph;
741 current_ir_graph = irg;
745 /* walk over the blocks in the graph */
746 irg_block_walk(irg->end, dump_ir_block, NULL, irg);
749 current_ir_graph = rem;
753 /***********************************************************************/
754 /* the following routines dump a control flow graph */
755 /***********************************************************************/
759 dump_block_to_cfg (ir_node *block, void *env) {
763 if (get_irn_opcode(block) == iro_Block) {
764 /* This is a block. Dump a node for the block. */
765 xfprintf (F, "node: {title: \"%p\" label: \"%I\"}", block,
768 for ( i = 0; i < get_Block_n_cfgpreds(block); i++) {
769 pred = get_nodes_Block(skip_Proj(get_Block_cfgpred(block, i)));
770 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" }\n",
777 dump_cfg (ir_graph *irg)
779 vcg_open (irg, "-cfg");
781 /* walk over the blocks in the graph */
782 irg_block_walk(irg->end, dump_block_to_cfg, NULL, NULL);
788 /***********************************************************************/
789 /* the following routine dumps all type information reachable from an */
791 /***********************************************************************/
795 dump_type_graph (ir_graph *irg)
798 rem = current_ir_graph;
799 current_ir_graph = irg;
801 vcg_open (irg, "-type");
803 /* walk over the blocks in the graph */
804 type_walk_irg(irg, dump_type_info, NULL, NULL);
807 current_ir_graph = rem;
810 /***********************************************************************/
811 /* the following routine dumps all type information */
812 /***********************************************************************/
816 dump_all_types (void)
818 vcg_open_name ("All_types");
819 type_walk(dump_type_info, NULL, NULL);
823 /***********************************************************************/
824 /* dumps a graph with type information */
825 /***********************************************************************/
829 dump_ir_graph_w_types (ir_graph *irg)
832 rem = current_ir_graph;
833 current_ir_graph = irg;
835 vcg_open (irg, "-all");
837 /* dump common ir graph */
838 /* irg_block_walk(irg->end, dump_ir_block, NULL, irg); */
839 irg_walk(irg->end, dump_whole_node, NULL, NULL);
841 type_walk_irg(irg, dump_type_info, NULL, NULL);
842 /* dump edges from graph to type info */
843 irg_walk(irg->end, dump_node2type_edges, NULL, NULL);
846 current_ir_graph = rem;
849 /***********************************************************************/
850 /* dumps all graphs with the graph-dumper passed. Possible dumpers: */
852 /* dump_ir_block_graph */
854 /* dump_type_graph */
855 /* dump_ir_graph_w_types */
856 /***********************************************************************/
857 void dump_all_ir_graphs (void dump_graph(ir_graph*)) {
859 for (i=0; i < get_irp_n_irgs(); i++) {
860 dump_graph(get_irp_irg(i));
865 /* To turn off display of edge labels. Edge labels offen cause xvcg to
866 abort with a segmentation fault. */
867 void turn_of_edge_labels() {