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 PTR_PTS_TO_EDGE_ATTR "label: \"points to\" color:green"
50 #define ARR_ELT_TYPE_EDGE_ATTR "label: \"arr elt\" color:green"
52 #define PRINT_NODEID(X) fprintf(F, "%p", X)
59 /*******************************************************************/
60 /* routines to dump information about a single node */
61 /*******************************************************************/
66 dump_node_opcode (ir_node *n)
69 if (n->op->code == iro_Const) {
70 xfprintf (F, "%v", n->attr.con);
72 } else if (n->op->code == iro_SymConst) {
73 if (get_SymConst_kind(n) == linkage_ptr_info) {
74 xfprintf (F, "%I", get_SymConst_ptrinfo(n));
76 assert(get_kind(get_SymConst_type(n)) == k_type_class);
77 assert(get_class_ident((type_class *)get_SymConst_type(n)));
78 xfprintf (F, "%s ", id_to_str(get_class_ident((type_class *)get_SymConst_type(n))));
79 if (get_SymConst_kind == type_tag)
86 xfprintf (F, "%I", get_irn_opident(n));
91 dump_node_mode (ir_node *n)
93 switch (n->op->code) {
110 xfprintf (F, "%I", get_mode_ident(n->mode));
117 dump_node_nodeattr (ir_node *n)
119 switch (n->op->code) {
121 if (n->in[1]->op->code == iro_Cmp) {
122 xfprintf (F, "%s", get_pnc_string(n->attr.proj));
124 xfprintf (F, "%ld", n->attr.proj);
128 assert(get_kind(get_Sel_entity(n)) == k_entity);
129 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
136 dump_node_vcgattr (ir_node *n)
138 switch (n->op->code) {
141 xfprintf (F, "color: blue");
144 xfprintf (F, "color: lightyellow");
147 xfprintf (F, "color: green");
152 xfprintf (F, "color: yellow");
155 xfprintf (F, DEFAULT_NODE_ATTR);
160 dump_node (ir_node *n) {
163 xfprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: \"");
168 dump_node_nodeattr(n);
170 xfprintf (F, " %ld", get_irn_node_nr(n));
173 dump_node_vcgattr(n);
178 dump_ir_node (ir_node *n)
181 fprintf (F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: ");
183 switch (n->op->code) { /* node label */
185 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
186 xfprintf (F, DEFAULT_NODE_ATTR);
189 xfprintf (F, "\"%I\" color: blue ", get_irn_opident(n));
190 xfprintf (F, DEFAULT_NODE_ATTR);
193 xfprintf (F, "\"%I\" color: lightyellow ", get_irn_opident(n));
194 xfprintf (F, DEFAULT_NODE_ATTR);
197 xfprintf (F, "\"%I%I\" color: green", get_irn_opident(n), get_irn_modeident(n));
198 if (get_irn_modecode(n) == irm_M)
199 xfprintf (F, DEFAULT_NODE_ATTR " color: green");
201 xfprintf (F, DEFAULT_NODE_ATTR);
204 xfprintf (F, "\"%v%I\" color: yellow ", n->attr.con, get_irn_modeident(n));
205 xfprintf (F, DEFAULT_NODE_ATTR);
208 xfprintf (F, "\"%I%I\" ", get_irn_opident(n), get_irn_modeident(n));
209 xfprintf (F, DEFAULT_NODE_ATTR);
212 if (n->in[1]->op->code == iro_Cmp) {
213 xfprintf (F, "\"%I%I %s\" color: yellow", get_irn_opident(n), get_irn_modeident(n),
214 get_pnc_string(n->attr.proj));
216 xfprintf (F, "\"%I%I %ld\"", get_irn_opident(n), get_irn_modeident(n), n->attr.proj);
218 xfprintf (F, DEFAULT_NODE_ATTR);
221 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
222 xfprintf (F, DEFAULT_NODE_ATTR);
225 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
226 xfprintf (F, DEFAULT_NODE_ATTR);
229 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
230 xfprintf (F, DEFAULT_NODE_ATTR);
233 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
234 xfprintf (F, DEFAULT_NODE_ATTR);
237 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
238 xfprintf (F, DEFAULT_NODE_ATTR);
241 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
242 xfprintf (F, DEFAULT_NODE_ATTR);
245 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
246 xfprintf (F, DEFAULT_NODE_ATTR);
249 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
250 xfprintf (F, DEFAULT_NODE_ATTR);
253 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
254 xfprintf (F, DEFAULT_NODE_ATTR);
257 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
258 xfprintf (F, DEFAULT_NODE_ATTR);
261 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
262 xfprintf (F, DEFAULT_NODE_ATTR);
265 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
266 xfprintf (F, DEFAULT_NODE_ATTR);
269 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
270 xfprintf (F, DEFAULT_NODE_ATTR);
273 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
274 xfprintf (F, DEFAULT_NODE_ATTR);
277 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
278 xfprintf (F, DEFAULT_NODE_ATTR);
281 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
282 xfprintf (F, DEFAULT_NODE_ATTR);
285 xfprintf (F, "\"%I\"", get_irn_opident(n));
286 xfprintf (F, DEFAULT_NODE_ATTR);
289 xfprintf (F, "\"%I\"", get_irn_opident(n));
290 xfprintf (F, DEFAULT_NODE_ATTR);
293 xfprintf (F, "\"%I\"", get_irn_opident(n));
294 xfprintf (F, DEFAULT_NODE_ATTR);
297 xfprintf (F, "\"%I\"", get_irn_opident(n));
298 xfprintf (F, DEFAULT_NODE_ATTR);
301 xfprintf (F, "\"%I%I\"", get_irn_opident(n), get_irn_modeident(n));
302 xfprintf (F, DEFAULT_NODE_ATTR);
306 xfprintf (F, "\"%R\"", n);
307 xfprintf (F, DEFAULT_NODE_ATTR);
310 xfprintf (F, "\"%I\" ", get_irn_opident(n));
311 xfprintf (F, DEFAULT_NODE_ATTR);
314 assert(get_kind(get_Sel_entity(n)) == k_entity);
315 xfprintf (F, "\"%I ", get_irn_opident(n));
316 xfprintf (F, "%s", id_to_str(get_entity_ident(get_Sel_entity(n))));
317 xfprintf (F, DEFAULT_NODE_ATTR);
320 assert(get_kind(get_SymConst_type(n)) == k_type_class);
321 assert(get_class_ident((type_class *)get_SymConst_type(n)));
322 xfprintf (F, "\"%s ",
323 id_to_str(get_class_ident((type_class *)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 type_class *type = (type_class *)tore;
503 xfprintf (F, "\"class %I\" " TYPE_CLASS_NODE_ATTR "}\n", get_class_ident(type));
504 for (i=0; i < get_class_n_supertype(type); i++)
505 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
506 TYPE_SUPER_EDGE_ATTR "}\n",
507 type, get_class_supertype(type, i));
511 type_strct *type = (type_strct *)tore;
512 xfprintf (F, "\"strct %I\"}\n", get_strct_ident(type));
517 type_method *type = (type_method *)tore;
518 xfprintf (F, "\"meth %I\" " TYPE_METH_NODE_ATTR "}\n", get_method_ident(type));
519 for (i = 0; i < get_method_arity(type); i++)
520 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
521 METH_PAR_EDGE_ATTR "}\n",
522 tore, get_method_param_type(type, i), i);
523 for (i = 0; i < get_method_n_res(type); i++)
524 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
525 METH_RES_EDGE_ATTR "}\n",
526 tore, get_method_res_type(type, i), i);
530 type_union *type = (type_union *)tore;
531 xfprintf (F, "\"union %I\"}\n", get_union_ident(type));
536 type_array *type = (type_array *)tore;
537 xfprintf (F, "\"array %I\"}\n", get_array_ident(type));
538 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
539 ARR_ELT_TYPE_EDGE_ATTR "}\n", tore, get_array_element_type(type), i);
542 case k_type_enumeration:
544 type_enumeration *type = (type_enumeration *)tore;
545 xfprintf (F, "\"enum %I\"}\n", get_enumeration_ident(type));
549 type_pointer *type = (type_pointer *)tore;
550 xfprintf (F, "\"ptr %I\"}\n", get_pointer_ident(type));
551 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
552 PTR_PTS_TO_EDGE_ATTR "}\n", tore,
553 get_pointer_points_to_type(type), i);
555 case k_type_primitive:
557 type_primitive *type = (type_primitive *)tore;
558 xfprintf (F, "\"prim %I, mode %I\"}\n", get_primitive_ident(type),
559 get_mode_ident(get_primitive_mode(type)));
563 xfprintf (F, "\" faulty type \"}\n");
564 printf(" *** irdump, %s(l.%i), faulty type.\n", __FUNCTION__, __LINE__);
569 /************************************************************************/
570 /* open and close vcg file */
571 /************************************************************************/
573 void vcg_open (ir_graph *irg, char *suffix) {
574 char *fname; /* filename to put the vcg information in */
580 /** open file for vcg graph */
581 id = get_entity_ld_name (get_irg_ent(irg));
582 len = id_to_strlen (id);
585 fname = malloc (len + 5 + strlen(suffix));
586 strncpy (fname, cp, len); /* copy the filename */
588 strcat (fname, suffix); /* append file suffix */
590 fname = malloc (len + 5 + strlen(suffix));
591 strncpy (fname, cp, len); /* copy the filename */
592 fname[len] = '\0'; /* ensure string termination */
593 /*strcpy (fname, cp); * copy the filename *
594 this produces wrong, too long strings in conjuction with the
595 jocca frontend. The \0 seems to be missing. */
596 strcat (fname, suffix); /* append file suffix */
597 strcat (fname, ".vcg"); /* append the .vcg suffix */
598 F = fopen (fname, "w"); /* open file for writing */
600 panic ("cannot open %s for writing (%m)", fname); /* not reached */
604 strcpy(label, "yes");
606 strcpy (label, "no");
611 "graph: { title: \"ir graph of %s\"\n"
612 "display_edge_labels: %s\n"
613 "layoutalgorithm: mindepth\n"
614 "manhattan_edges: yes\n"
616 "orientation: bottom_to_top\n"
617 "classname 1: \"Data\"\n"
618 "classname 2: \"Block\"\n", cp, label);
620 xfprintf (F, "\n"); /* a separator */
623 void vcg_open_name (const char *name) {
624 char *fname; /* filename to put the vcg information in */
628 /** open file for vcg graph */
630 fname = malloc (len + 5);
631 strcpy (fname, name); /* copy the filename */
632 strcat (fname, ".vcg"); /* append the .vcg suffix */
633 F = fopen (fname, "w"); /* open file for writing */
635 panic ("cannot open %s for writing (%m)", fname); /* not reached */
639 strcpy(label, "yes");
641 strcpy (label, "no");
646 "graph: { title: \"ir graph of %s\"\n"
647 "display_edge_labels: %s\n"
648 "layoutalgorithm: mindepth\n"
649 "manhattan_edges: yes\n"
651 "orientation: bottom_to_top\n"
652 "classname 1: \"Data\"\n"
653 "classname 2: \"Block\"\n", name, label);
655 xfprintf (F, "\n"); /* a separator */
660 xfprintf (F, "}\n"); /* print footer */
661 fclose (F); /* close vcg file */
664 /************************************************************************/
665 /* routines to dump a graph, blocks as conventional nodes. */
666 /************************************************************************/
669 dump_whole_node (ir_node *n, void* env) {
671 dump_ir_block_edge(n);
672 dump_ir_data_edges(n);
676 dump_ir_graph (ir_graph *irg)
679 rem = current_ir_graph;
680 current_ir_graph = irg;
684 /* walk over the graph */
685 irg_walk(irg->end, dump_whole_node, NULL, NULL);
689 current_ir_graph = rem;
692 /***********************************************************************/
693 /* the following routines dump the nodes as attached to the blocks. */
694 /***********************************************************************/
697 dump_ir_blocks_nodes (ir_node *n, void *env) {
698 ir_node *block = (ir_node *)env;
700 if (is_no_Block(n) && get_nodes_Block(n) == block) {
702 dump_ir_data_edges(n);
707 dump_ir_block (ir_node *block, void *env) {
708 ir_graph *irg = (ir_graph *)env;
710 if (get_irn_opcode(block) == iro_Block) {
712 /* This is a block. So dump the vcg information to make a block. */
713 xfprintf(F, "graph: { title: \""); PRINT_NODEID(block); fprintf(F, "\" label: \"");
715 xfprintf (F, "%ld", get_irn_node_nr(block));
717 xfprintf (F, "%I", block->op->name);
719 xfprintf(F, "\" status:clustered color:lightyellow \n");
720 /* dump the blocks edges */
721 dump_ir_data_edges(block);
723 /* dump the nodes that go into the block */
724 irg_walk(irg->end, dump_ir_blocks_nodes, NULL, block);
726 /* Close the vcg information for the block */
727 xfprintf(F, "}\n\n");
732 dump_ir_block_graph (ir_graph *irg)
735 rem = current_ir_graph;
736 current_ir_graph = irg;
740 /* walk over the blocks in the graph */
741 irg_block_walk(irg->end, dump_ir_block, NULL, irg);
744 current_ir_graph = rem;
748 /***********************************************************************/
749 /* the following routines dump a control flow graph */
750 /***********************************************************************/
754 dump_block_to_cfg (ir_node *block, void *env) {
758 if (get_irn_opcode(block) == iro_Block) {
759 /* This is a block. Dump a node for the block. */
760 xfprintf (F, "node: {title: \"%p\" label: \"%I\"}", block,
763 for ( i = 0; i < get_Block_n_cfgpreds(block); i++) {
764 pred = get_nodes_Block(skip_Proj(get_Block_cfgpred(block, i)));
765 xfprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" }\n",
772 dump_cfg (ir_graph *irg)
774 vcg_open (irg, "-cfg");
776 /* walk over the blocks in the graph */
777 irg_block_walk(irg->end, dump_block_to_cfg, NULL, NULL);
783 /***********************************************************************/
784 /* the following routine dumps all type information reachable from an */
786 /***********************************************************************/
790 dump_type_graph (ir_graph *irg)
793 rem = current_ir_graph;
794 current_ir_graph = irg;
796 vcg_open (irg, "-type");
798 /* walk over the blocks in the graph */
799 type_walk_irg(irg, dump_type_info, NULL, NULL);
802 current_ir_graph = rem;
805 /***********************************************************************/
806 /* the following routine dumps all type information */
807 /***********************************************************************/
811 dump_all_types (void)
813 vcg_open_name ("All_types");
814 type_walk(dump_type_info, NULL, NULL);
818 /***********************************************************************/
819 /* dumps a graph with type information */
820 /***********************************************************************/
824 dump_ir_graph_w_types (ir_graph *irg)
827 rem = current_ir_graph;
828 current_ir_graph = irg;
830 vcg_open (irg, "-all");
832 /* dump common ir graph */
833 /* irg_block_walk(irg->end, dump_ir_block, NULL, irg); */
834 irg_walk(irg->end, dump_whole_node, NULL, NULL);
836 type_walk_irg(irg, dump_type_info, NULL, NULL);
837 /* dump edges from graph to type info */
838 irg_walk(irg->end, dump_node2type_edges, NULL, NULL);
841 current_ir_graph = rem;
844 /***********************************************************************/
845 /* dumps all graphs with the graph-dumper passed. Possible dumpers: */
847 /* dump_ir_block_graph */
849 /* dump_type_graph */
850 /* dump_ir_graph_w_types */
851 /***********************************************************************/
852 void dump_all_ir_graphs (void dump_graph(ir_graph*)) {
854 for (i=0; i < get_irp_n_irgs(); i++) {
855 dump_graph(get_irp_irg(i));
860 /* To turn off display of edge labels. Edge labels offen cause xvcg to
861 abort with a segmentation fault. */
862 void turn_of_edge_labels() {