Fixed extblock dumper (did not work outside debugger Bug)
[libfirm] / ir / ir / irdump.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irdump.c
4  * Purpose:     Write vcg representation of firm to file.
5  * Author:      Martin Trapp, Christian Schaefer
6  * Modified by: Goetz Lindenmaier, Hubert Schmidt
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2003 Universit�t Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #ifdef HAVE_STRING_H
17 #include <string.h>
18 #endif
19 #ifdef HAVE_STDLIB_H
20 #include <stdlib.h>
21 #endif
22 #include <stdarg.h>
23
24 #include "firm_common_t.h"
25
26 #include "irnode.h"
27 #include "irgraph.h"
28 #include "irprog_t.h"
29 #include "entity_t.h"
30 #include "irop.h"
31
32 #include "irdump_t.h"
33
34 #include "irgwalk.h"
35 #include "typewalk.h"
36 #include "tv_t.h"
37 #include "type_or_entity.h"
38 #include "irouts.h"
39 #include "irdom.h"
40 #include "irloop_t.h"
41 #include "callgraph.h"
42 #include "irextbb_t.h"
43 #include "dbginfo_t.h"
44 #include "irtools.h"
45
46 #include "irvrfy.h"
47
48 #include "error.h"
49 #include "array.h"
50 #include "pmap.h"
51 #include "eset.h"
52 #include "pset.h"
53
54 #if DO_HEAPANALYSIS
55 extern void dump_irn_chi_term(FILE *FL, ir_node *n);
56 extern void dump_irn_state(FILE *FL, ir_node *n);
57 extern int  get_opt_dump_abstvals(void);
58 typedef unsigned long SeqNo;
59 extern SeqNo get_Block_seqno(ir_node *n);
60 #endif
61
62 /* basis for a color range for vcg */
63 static int n_colors   = 0;
64 static int base_color = 0;
65
66 /** Dump only irgs with names that start with this string */
67 static ident *dump_file_filter_id = NULL;
68
69 #define ERROR_TXT       "<ERROR>"
70
71 /*******************************************************************/
72 /* flags to steer output                                           */
73 /*******************************************************************/
74
75 /** An option to turn off edge labels */
76 static int edge_label = 1;
77 /** An option to turn off dumping values of constant entities */
78 static int const_entities = 1;
79 /** An option to dump the keep alive edges */
80 static int dump_keepalive = 0;
81 /** An option to dump ld_names instead of names. */
82 static int dump_ld_name = 1;
83 /** Compiler options to dump analysis information in dump_ir_graph */
84 int dump_out_edge_flag = 0;
85 int dump_dominator_information_flag = 0;
86 int dump_loop_information_flag = 0;
87 int dump_backedge_information_flag = 1;
88 int dump_const_local = 0;
89 int opt_dump_analysed_type_info = 1;
90 int opt_dump_pointer_values_to_info = 0;  /* default off: for test compares!! */
91
92 static dumper_colors overrule_nodecolor = ird_color_default;
93
94 /** An additional edge hook. */
95 static DUMP_NODE_EDGE_FUNC dump_node_edge_hook = NULL;
96
97 void set_dump_node_edge_hook(DUMP_NODE_EDGE_FUNC func)
98 {
99   dump_node_edge_hook = func;
100 }
101
102 DUMP_NODE_EDGE_FUNC get_dump_node_edge_hook(void)
103 {
104   return dump_node_edge_hook;
105 }
106
107
108 /** The vcg node attribute hook. */
109 static DUMP_IR_GRAPH_FUNC dump_ir_graph_hook = NULL;
110 /** The vcg node attribute hook. */
111 static DUMP_NODE_VCGATTR_FUNC dump_node_vcgattr_hook = NULL;
112 /** The vcg edge attribute hook. */
113 static DUMP_EDGE_VCGATTR_FUNC dump_edge_vcgattr_hook = NULL;
114
115 /* set the ir graph hook */
116 void set_dump_ir_graph_hook(DUMP_IR_GRAPH_FUNC hook) {
117   dump_ir_graph_hook = hook;
118 }
119 /* set the node attribute hook */
120 void set_dump_node_vcgattr_hook(DUMP_NODE_VCGATTR_FUNC hook) {
121   dump_node_vcgattr_hook = hook;
122 }
123 /* set the edge attribute hook */
124 void set_dump_edge_vcgattr_hook(DUMP_EDGE_VCGATTR_FUNC hook) {
125   dump_edge_vcgattr_hook = hook;
126 }
127
128 INLINE int get_opt_dump_const_local(void) {
129   if (!dump_out_edge_flag && !dump_loop_information_flag)
130     return dump_const_local;
131   else
132     return 0;
133 }
134
135 void only_dump_method_with_name(ident *name) {
136   dump_file_filter_id = name;
137 }
138
139 ident *get_dump_file_filter_ident(void) {
140   return dump_file_filter_id;
141 }
142
143 /** Returns non-zero if dump file filter is not set, or if it is a
144  *  prefix of name. */
145 int is_filtered_dump_name(ident *name) {
146   if (!dump_file_filter_id) return 1;
147   return id_is_prefix(dump_file_filter_id, name);
148 }
149
150 /* To turn off display of edge labels.  Edge labels often cause xvcg to
151    abort with a segmentation fault. */
152 void turn_off_edge_labels(void) {
153   edge_label = 0;
154 }
155
156 void dump_consts_local(int flag) {
157   dump_const_local = flag;
158 }
159
160 void dump_constant_entity_values(int flag) {
161   const_entities = flag;
162 }
163
164 void dump_keepalive_edges(int flag) {
165   dump_keepalive = flag;
166 }
167
168 int get_opt_dump_keepalive_edges(void) {
169   return dump_keepalive;
170 }
171
172 void dump_out_edges(int flag) {
173   dump_out_edge_flag = flag;
174 }
175
176 void dump_dominator_information(int flag) {
177   dump_dominator_information_flag = flag;
178 }
179
180 void dump_loop_information(int flag) {
181   dump_loop_information_flag = flag;
182 }
183
184 void dump_backedge_information(int flag) {
185   dump_backedge_information_flag = flag;
186 }
187
188 /* Dump the information of type field specified in ana/irtypeinfo.h.
189  * If the flag is set, the type name is output in [] in the node label,
190  * else it is output as info.
191  */
192 void set_opt_dump_analysed_type_info(int flag) {
193   opt_dump_analysed_type_info = flag;
194 }
195
196 void dump_pointer_values_to_info(int flag) {
197   opt_dump_pointer_values_to_info = flag;
198 }
199
200 void dump_ld_names(int flag) {
201   dump_ld_name = flag;
202 }
203
204 /* -------------- some extended helper functions ----------------- */
205
206 /**
207  * returns the name of a mode or <ERROR> if mode is NOT a mode object.
208  * in the later case, sets bad
209  */
210 const char *get_mode_name_ex(ir_mode *mode, int *bad)
211 {
212   if (is_mode(mode))
213     return get_mode_name(mode);
214   *bad |= 1;
215   return ERROR_TXT;
216 }
217
218 /**
219  * returns the name of a type or <ERROR> if mode is NOT a mode object.
220  * in the later case, sets bad
221  */
222 const char *get_type_name_ex(ir_type *tp, int *bad)
223 {
224   if (is_type(tp))
225     return get_type_name(tp);
226   *bad |= 1;
227   return ERROR_TXT;
228 }
229
230 /**
231  * printf the VCG color
232  */
233 static void print_vcg_color(FILE *F, dumper_colors color) {
234   static const char *color_names[32] = {
235     "white",        "blue",      "red",        "green",
236     "yellow",       "magenta",   "cyan",       "darkgray",
237     "darkblue",     "darkred",   "darkgreen",  "darkyellow",
238     "darkmagenta",  "darkcyan",  "gold",       "lightgray",
239     "lightblue",    "lightred",  "lightgreen", "lightyellow",
240     "lightmagenta", "lightcyan", "lilac",      "turquoise",
241     "aquamarine",   "khaki",     "purple",     "yellowgreen",
242     "pink",         "orange",    "orchid",     "black"
243   };
244
245   if (color != ird_color_default)
246     fprintf(F, "color:%s", color_names[color]);
247 }
248
249 /**
250  * prints the edge from a type S to a type T with additional info fmt, ...
251  * to the file F
252  */
253 static void print_type_type_edge(FILE *F, ir_type *S, ir_type *T, const char *fmt, ...)
254 {
255   va_list ap;
256
257   va_start(ap, fmt);
258   fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(S);
259   fprintf(F, " targetname: "); PRINT_TYPEID(T);
260   vfprintf(F, fmt, ap);
261   fprintf(F,"}\n");
262   va_end(ap);
263 }
264
265 /**
266  * prints the edge from a type T to an entity E with additional info fmt, ...
267  * to the file F
268  */
269 static void print_type_ent_edge(FILE *F, ir_type *T, entity *E, const char *fmt, ...)
270 {
271   va_list ap;
272
273   va_start(ap, fmt);
274   fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(T);
275   fprintf(F, " targetname: \""); PRINT_ENTID(E); fprintf(F, "\"");
276   vfprintf(F, fmt, ap);
277   fprintf(F, "}\n");
278   va_end(ap);
279 }
280
281 /**
282  * prints the edge from an entity E to an entity T with additional info fmt, ...
283  * to the file F
284  */
285 static void print_ent_ent_edge(FILE *F, entity *E, entity *T, int backedge, const char *fmt, ...)
286 {
287   va_list ap;
288
289   va_start(ap, fmt);
290   if (backedge)
291     fprintf(F, "backedge: { sourcename: \"");
292    else
293     fprintf(F, "edge: { sourcename: \"");
294   PRINT_ENTID(E);
295   fprintf(F, "\" targetname: \""); PRINT_ENTID(T);  fprintf(F, "\"");
296   vfprintf(F, fmt, ap);
297   fprintf(F, "}\n");
298   va_end(ap);
299 }
300
301 /**
302  * prints the edge from an entity E to a type T with additional info fmt, ...
303  * to the file F
304  */
305 static void print_ent_type_edge(FILE *F, entity *E, ir_type *T, const char *fmt, ...)
306 {
307   va_list ap;
308
309   va_start(ap, fmt);
310   fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(E);
311   fprintf(F, "\" targetname: "); PRINT_TYPEID(T);
312   vfprintf(F, fmt, ap);
313   fprintf(F,"}\n");
314   va_end(ap);
315 }
316
317 /**
318  * prints the edge from a node N to a type T with additional info fmt, ...
319  * to the file F
320  */
321 static void print_node_type_edge(FILE *F, const ir_node *N, ir_type *T, const char *fmt, ...)
322 {
323   va_list ap;
324
325   va_start(ap, fmt);
326   fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(N);
327   fprintf(F, "\" targetname: "); PRINT_TYPEID(T);
328   vfprintf(F, fmt, ap);
329   fprintf(F,"}\n");
330   va_end(ap);
331 }
332
333 /**
334  * prints the edge from a node N to an entity E with additional info fmt, ...
335  * to the file F
336  */
337 static void print_node_ent_edge(FILE *F, const ir_node *N, entity *E, const char *fmt, ...)
338 {
339   va_list ap;
340
341   va_start(ap, fmt);
342   fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(N);
343   fprintf(F, "\" targetname: \""); PRINT_ENTID(E);
344   fprintf(F, "\"");
345   vfprintf(F, fmt, ap);
346   fprintf(F,"}\n");
347   va_end(ap);
348 }
349
350 /**
351  * prints the edge from an entity E to a node N with additional info fmt, ...
352  * to the file F
353  */
354 static void print_ent_node_edge(FILE *F, entity *E, const ir_node *N, const char *fmt, ...)
355 {
356   va_list ap;
357
358   va_start(ap, fmt);
359   fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(E);
360   fprintf(F, "\" targetname: \""); PRINT_NODEID(N); fprintf(F, "\"");
361   vfprintf(F, fmt, ap);
362   fprintf(F,"}\n");
363   va_end(ap);
364 }
365
366 /**
367  * prints the edge from a type E to an enumeration item item with additional info fmt, ...
368  * to the file F
369  */
370 static void print_enum_item_edge(FILE *F, ir_type *E, int item, const char *fmt, ...)
371 {
372   va_list ap;
373
374   va_start(ap, fmt);
375   fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(E);
376   fprintf(F, " targetname: \""); PRINT_ITEMID(E, item); fprintf(F, "\" ");
377   vfprintf(F, fmt, ap);
378   fprintf(F,"}\n");
379   va_end(ap);
380 }
381
382 /*-----------------------------------------------------------------*/
383 /* global and ahead declarations                                   */
384 /*-----------------------------------------------------------------*/
385
386 static void dump_whole_node(ir_node *n, void *env);
387 static INLINE void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg);
388
389 /*-----------------------------------------------------------------*/
390 /* Helper functions.                                                */
391 /*-----------------------------------------------------------------*/
392
393 /**
394  * This map is used as a private link attr to be able to call dumper
395  * anywhere without destroying link fields.
396  */
397 static pmap *irdump_link_map = NULL;
398
399 /** NOT A STANDARD LIBFIRM INIT METHOD
400  *
401  * We do not want to integrate dumping into libfirm, i.e., if the dumpers
402  * are off, we want to have as few interferences as possible.  Therefore the
403  * initialization is performed lazily and not called from within init_firm.
404  *
405  * Creates the link attribute map. */
406 static void init_irdump(void) {
407   /* We need a new, empty map. */
408   if (irdump_link_map) pmap_destroy(irdump_link_map);
409   irdump_link_map = pmap_create();
410   if (!dump_file_filter_id)
411     dump_file_filter_id = new_id_from_str("");
412 }
413 /**
414  * Returns the private link field.
415  */
416 static void *ird_get_irn_link(ir_node *n) {
417   void *res = NULL;
418   if (!irdump_link_map) return NULL;
419
420   if (pmap_contains(irdump_link_map, (void *)n))
421     res = pmap_get(irdump_link_map, (void *)n);
422   return res;
423 }
424
425 /**
426  * Sets the private link field.
427  */
428 static void ird_set_irn_link(ir_node *n, void *x) {
429   if (!irdump_link_map)
430     init_irdump();
431   pmap_insert(irdump_link_map, (void *)n, x);
432 }
433
434 /**
435  * Gets the private link field of an irg.
436  */
437 static void *ird_get_irg_link(ir_graph *irg) {
438   void *res = NULL;
439   if (!irdump_link_map) return NULL;
440
441   if (pmap_contains(irdump_link_map, (void *)irg))
442     res = pmap_get(irdump_link_map, (void *)irg);
443   return res;
444 }
445
446 /**
447  * Sets the private link field of an irg.
448  */
449 static void ird_set_irg_link(ir_graph *irg, void *x) {
450   if (!irdump_link_map) init_irdump();
451   pmap_insert(irdump_link_map, (void *)irg, x);
452 }
453
454 /**
455  * Walker, clears the private link field.
456  */
457 static void clear_link(ir_node * node, void * env) {
458   ird_set_irn_link(node, NULL);
459 }
460
461 /**
462  * If the entity has a ld_name, returns it if the dump_ld_name is set,
463  * else returns the name of the entity.
464  */
465 static const char *_get_ent_dump_name(entity *ent, int dump_ld_name) {
466   if (!ent)
467     return "<NULL entity>";
468   if (dump_ld_name) {
469     /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
470     if (ent->ld_name) return get_id_str(ent->ld_name);
471   }
472   return get_id_str(ent->name);
473 }
474
475 /**
476  * If the entity has a ld_name, returns it if the option dump_ld_name is set,
477  * else returns the name of the entity.
478  */
479 const char *get_ent_dump_name(entity *ent) {
480   return _get_ent_dump_name(ent, dump_ld_name);
481 }
482
483 /* Returns the name of an IRG. */
484 const char *get_irg_dump_name(ir_graph *irg) {
485   /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
486   return _get_ent_dump_name(get_irg_entity(irg), 1);
487 }
488
489 /**
490  * Returns non-zero if a node is in floating state.
491  */
492 static int node_floats(ir_node *n) {
493   return ((get_irn_pinned(n) == op_pin_state_floats) &&
494       (get_irg_pinned(current_ir_graph) == op_pin_state_floats));
495 }
496
497 /**
498  * Walker, allocates an array for all blocks and puts it's nodes non-floating nodes into this array.
499  */
500 static void collect_node(ir_node * node, void *env) {
501   if (is_Block(node)
502       || node_floats(node)
503       || get_irn_op(node) == op_Bad
504       || get_irn_op(node) == op_Unknown
505       || get_irn_op(node) == op_NoMem) {
506     ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
507     if (!arr) arr = NEW_ARR_F(ir_node *, 0);
508     ARR_APP1(ir_node *, arr, node);
509     ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
510   } else {
511     ir_node * block = get_nodes_block(node);
512
513     if (is_Bad(block)) {
514       /* this node is in a Bad block, so we must place it into the graph's list */
515       ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
516       if (!arr) arr = NEW_ARR_F(ir_node *, 0);
517       ARR_APP1(ir_node *, arr, node);
518       ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
519     }
520     else {
521       ird_set_irn_link(node, ird_get_irn_link(block));
522       ird_set_irn_link(block, node);
523     }
524   }
525 }
526
527 /** Construct lists to walk ir block-wise.
528  *
529  * Collects all blocks, nodes not op_pin_state_pinned,
530  * Bad, NoMem and Unknown into a flexible array in link field of
531  * irg they belong to.  Sets the irg link field to NULL in all
532  * graphs not visited.
533  * Free the list with DEL_ARR_F().
534  */
535 static ir_node **construct_block_lists(ir_graph *irg) {
536   int i, rem_view = get_interprocedural_view();
537   ir_graph *rem = current_ir_graph;
538   current_ir_graph = irg;
539
540   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
541     ird_set_irg_link(get_irp_irg(i), NULL);
542
543   irg_walk_graph(current_ir_graph, clear_link, collect_node, current_ir_graph);
544
545   /* Collect also EndReg and EndExcept. We do not want to change the walker. */
546   set_interprocedural_view(0);
547
548   set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
549   irg_walk(get_irg_end_reg(current_ir_graph), clear_link, collect_node, current_ir_graph);
550   set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
551   irg_walk(get_irg_end_except(current_ir_graph), clear_link, collect_node, current_ir_graph);
552
553   set_interprocedural_view(rem_view);
554
555   current_ir_graph = rem;
556   return ird_get_irg_link(irg);
557 }
558
559 typedef struct _list_tuple {
560   ir_node **blk_list;
561   ir_extblk **extbb_list;
562 } list_tuple;
563
564 /** Construct lists to walk IR extended block-wise.
565  * Free the lists in the tuple with DEL_ARR_F().
566  * Sets the irg link field to NULL in all
567  * graphs not visited.
568  */
569 static list_tuple *construct_extblock_lists(ir_graph *irg) {
570   ir_node **blk_list = construct_block_lists(irg);
571   int i;
572   ir_graph *rem = current_ir_graph;
573   list_tuple *lists = xmalloc(sizeof(*lists));
574
575   current_ir_graph = irg;
576
577   lists->blk_list   = NEW_ARR_F(ir_node *, 0);
578   lists->extbb_list = NEW_ARR_F(ir_extblk *, 0);
579
580   inc_irg_block_visited(irg);
581   for (i = ARR_LEN(blk_list) - 1; i >= 0; --i) {
582     ir_extblk *ext;
583
584     if (is_Block(blk_list[i])) {
585       ext = get_Block_extbb(blk_list[i]);
586
587       if (extbb_not_visited(ext)) {
588         ARR_APP1(ir_extblk *, lists->extbb_list, ext);
589         mark_extbb_visited(ext);
590       }
591     }
592     else
593       ARR_APP1(ir_node *, lists->blk_list, blk_list[i]);
594   }
595   DEL_ARR_F(blk_list);
596
597   current_ir_graph = rem;
598   ird_set_irg_link(irg, lists);
599   return lists;
600 }
601
602 /*-----------------------------------------------------------------*/
603 /* Routines to dump information about a single ir node.            */
604 /*-----------------------------------------------------------------*/
605
606 /*
607  * dump the name of a node n to the File F.
608  */
609 int dump_node_opcode(FILE *F, ir_node *n)
610 {
611   int bad = 0;
612   const ir_op_ops *ops = get_op_ops(get_irn_op(n));
613
614   /* call the dump_node operation if available */
615   if (ops->dump_node)
616     return ops->dump_node(n, F, dump_node_opcode_txt);
617
618   /* implementation for default nodes */
619   switch (get_irn_opcode(n)) {
620
621   case iro_Const: {
622     int res;
623     char buf[1024];
624     res = tarval_snprintf(buf, sizeof(buf), get_Const_tarval(n));
625     assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
626     fprintf(F, buf);
627   } break;
628
629   case iro_SymConst: {
630     if (get_SymConst_kind(n) == symconst_addr_name) {
631       /* don't use get_SymConst_ptr_info as it mangles the name. */
632       fprintf (F, "SymC %s", get_id_str(get_SymConst_name(n)));
633     } else if (get_SymConst_kind(n) == symconst_addr_ent) {
634       assert(get_SymConst_entity(n));
635       assert(is_entity(get_SymConst_entity(n)));
636       fprintf (F, "SymC &%s", get_entity_name(get_SymConst_entity(n)));
637     } else {
638       assert(get_kind(get_SymConst_type(n)) == k_type);
639       assert(get_type_ident(get_SymConst_type(n)));
640       fprintf (F, "SymC %s ", get_type_name_ex(get_SymConst_type(n), &bad));
641       if (get_SymConst_kind(n) == symconst_type_tag)
642         fprintf (F, "tag");
643       else
644         fprintf (F, "size");
645     }
646   } break;
647
648   case iro_Filter: {
649     if (!get_interprocedural_view())
650       fprintf(F, "Proj'");
651     else
652       goto default_case;
653   } break;
654
655   case iro_Proj: {
656     ir_node *pred = get_Proj_pred(n);
657
658     if (get_irn_opcode(pred) == iro_Cond
659         && get_Proj_proj(n) == get_Cond_defaultProj(pred)
660         && get_irn_mode(get_Cond_selector(pred)) != mode_b)
661       fprintf (F, "defProj");
662     else
663       goto default_case;
664   } break;
665   case iro_Start:
666   case iro_End:
667   case iro_EndExcept:
668   case iro_EndReg: {
669     if (get_interprocedural_view()) {
670       fprintf(F, "%s %s", get_irn_opname(n), get_ent_dump_name(get_irg_entity(get_irn_irg(n))));
671       break;
672     } else
673       goto default_case;
674   }
675   case iro_CallBegin: {
676     ir_node *addr = get_CallBegin_ptr(n);
677     entity *ent = NULL;
678     if (get_irn_op(addr) == op_Sel)
679       ent = get_Sel_entity(addr);
680     else if ((get_irn_op(addr) == op_SymConst) && (get_SymConst_kind(addr) == symconst_addr_ent))
681       ent = get_SymConst_entity(addr);
682     fprintf (F, "%s", get_irn_opname(n));
683     if (ent) fprintf (F, " %s", get_entity_name(ent));
684     break;
685   }
686   case iro_Load:
687     fprintf (F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Load_mode(n), &bad));
688     break;
689   case iro_Block:
690     fprintf (F, "%s%s", is_Block_dead(n) ? "Dead " : "", get_irn_opname(n));
691     break;
692
693   default:
694 default_case:
695     fprintf (F, "%s", get_irn_opname(n));
696
697   }  /* end switch */
698   return bad;
699 }
700
701 /**
702  * Dump the mode of a node n to a file F.
703  * Ignore modes that are "always known".
704  */
705 static int dump_node_mode(FILE *F, ir_node *n)
706 {
707   int bad = 0;
708   const ir_op_ops *ops = get_op_ops(get_irn_op(n));
709   opcode iro;
710
711   /* call the dump_node operation if available */
712   if (ops->dump_node)
713     return ops->dump_node(n, F, dump_node_mode_txt);
714
715   /* default implementation */
716   iro = get_irn_opcode(n);
717   switch (iro) {
718     case iro_SymConst:
719     case iro_Sel:
720     case iro_End:
721     case iro_Return:
722     case iro_Free:
723     case iro_Sync:
724     case iro_Jmp:
725     case iro_NoMem:
726       break;
727     default: {
728       ir_mode *mode = get_irn_mode(n);
729
730       if (mode && mode != mode_BB && mode != mode_ANY && mode != mode_BAD &&
731           (mode != mode_T || iro == iro_Proj))
732         fprintf(F, "%s", get_mode_name_ex(mode, &bad));
733     }
734   }
735
736   return bad;
737 }
738
739 /**
740  * Dump the type of a node n to a file F if it's known.
741  */
742 static int dump_node_typeinfo(FILE *F, ir_node *n) {
743   int bad = 0;
744
745   if (opt_dump_analysed_type_info) {
746     if (get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_consistent  ||
747         get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_inconsistent) {
748       ir_type *tp = get_irn_typeinfo_type(n);
749       if (tp != firm_none_type)
750         fprintf(F, "[%s] ", get_type_name_ex(tp, &bad));
751       else
752         fprintf(F, "[] ");
753     }
754   }
755   return bad;
756 }
757
758 typedef struct _pns_lookup {
759   long       nr;      /**< the proj number */
760   const char *name;   /**< the name of the Proj */
761 } pns_lookup_t;
762
763 typedef struct _proj_lookup {
764   opcode             code;      /**< the opcode of the Proj predecessor */
765   unsigned           num_data;  /**< number of data entries */
766   const pns_lookup_t *data;     /**< the data */
767 } proj_lookup_t;
768
769 #define ARR_SIZE(a)       (sizeof(a)/sizeof(a[0]))
770
771 /** the lookup table for Proj(Start) names */
772 static const pns_lookup_t start_lut[] = {
773 #define X(a)    { pn_Start_##a, #a }
774   X(X_initial_exec),
775   X(P_frame_base),
776   X(P_globals),
777   X(T_args),
778   X(P_value_arg_base)
779 #undef X
780 };
781
782 /** the lookup table for Proj(Cond) names */
783 static const pns_lookup_t cond_lut[] = {
784 #define X(a)    { pn_Cond_##a, #a }
785   X(false),
786   X(true)
787 #undef X
788 };
789
790 /** the lookup table for Proj(Call) names */
791 static const pns_lookup_t call_lut[] = {
792 #define X(a)    { pn_Call_##a, #a }
793   X(M_regular),
794   X(T_result),
795   X(P_value_res_base),
796   X(X_except),
797   X(M_except)
798 #undef X
799 };
800
801 /** the lookup table for Proj(Quot) names */
802 static const pns_lookup_t quot_lut[] = {
803 #define X(a)    { pn_Quot_##a, #a }
804   X(M),
805   X(X_except),
806   X(res)
807 #undef X
808 };
809
810 /** the lookup table for Proj(DivMod) names */
811 static const pns_lookup_t divmod_lut[] = {
812 #define X(a)    { pn_DivMod_##a, #a }
813   X(M),
814   X(X_except),
815   X(res_div),
816   X(res_mod)
817 #undef X
818 };
819
820 /** the lookup table for Proj(Div) names */
821 static const pns_lookup_t div_lut[] = {
822 #define X(a)    { pn_Div_##a, #a }
823   X(M),
824   X(X_except),
825   X(res)
826 #undef X
827 };
828
829 /** the lookup table for Proj(Mod) names */
830 static const pns_lookup_t mod_lut[] = {
831 #define X(a)    { pn_Mod_##a, #a }
832   X(M),
833   X(X_except),
834   X(res)
835 #undef X
836 };
837
838 /** the lookup table for Proj(Load) names */
839 static const pns_lookup_t load_lut[] = {
840 #define X(a)    { pn_Load_##a, #a }
841   X(M),
842   X(X_except),
843   X(res)
844 #undef X
845 };
846
847 /** the lookup table for Proj(Store) names */
848 static const pns_lookup_t store_lut[] = {
849 #define X(a)    { pn_Store_##a, #a }
850   X(M),
851   X(X_except)
852 #undef X
853 };
854
855 /** the lookup table for Proj(Alloc) names */
856 static const pns_lookup_t alloc_lut[] = {
857 #define X(a)    { pn_Alloc_##a, #a }
858   X(M),
859   X(X_except),
860   X(res)
861 #undef X
862 };
863
864 /** the lookup table for Proj(CopyB) names */
865 static const pns_lookup_t copyb_lut[] = {
866 #define X(a)    { pn_CopyB_##a, #a }
867   X(M),
868   X(X_except),
869   X(M_except)
870 #undef X
871 };
872
873 /** the lookup table for Proj(InstOf) names */
874 static const pns_lookup_t instof_lut[] = {
875 #define X(a)    { pn_InstOf_##a, #a }
876   X(M),
877   X(X_except),
878   X(res),
879   X(M_except),
880 #undef X
881 };
882
883 /** the lookup table for Proj(Raise) names */
884 static const pns_lookup_t raise_lut[] = {
885 #define X(a)    { pn_Raise_##a, #a }
886   X(M),
887   X(X),
888 #undef X
889 };
890
891 /** the lookup table for Proj(Bound) names */
892 static const pns_lookup_t bound_lut[] = {
893 #define X(a)    { pn_Bound_##a, #a }
894   X(M),
895   X(X_except),
896   X(res),
897   X(M_except)
898 #undef X
899 };
900
901 /** the Proj lookup table */
902 static const proj_lookup_t proj_lut[] = {
903 #define E(a)  ARR_SIZE(a), a
904   { iro_Start,   E(start_lut) },
905   { iro_Cond,    E(cond_lut) },
906   { iro_Call,    E(call_lut) },
907   { iro_Quot,    E(quot_lut) },
908   { iro_DivMod,  E(divmod_lut) },
909   { iro_Div,     E(div_lut) },
910   { iro_Mod,     E(mod_lut) },
911   { iro_Load,    E(load_lut) },
912   { iro_Store,   E(store_lut) },
913   { iro_Alloc,   E(alloc_lut) },
914   { iro_CopyB,   E(copyb_lut) },
915   { iro_InstOf,  E(instof_lut) },
916   { iro_Raise,   E(raise_lut) },
917   { iro_Bound,   E(bound_lut) }
918 #undef E
919 };
920
921 /**
922  * Dump additional node attributes of some nodes to a file F.
923  */
924 static int
925 dump_node_nodeattr(FILE *F, ir_node *n)
926 {
927   int bad = 0;
928   ir_node *pred;
929   opcode code;
930   long proj_nr;
931   const ir_op_ops *ops = get_op_ops(get_irn_op(n));
932
933   /* call the dump_node operation if available */
934   if (ops->dump_node)
935     return ops->dump_node(n, F, dump_node_nodeattr_txt);
936
937   switch (get_irn_opcode(n)) {
938   case iro_Start:
939     if (0 && get_interprocedural_view()) {
940       fprintf (F, "%s ", get_ent_dump_name(get_irg_entity(current_ir_graph)));
941     }
942     break;
943
944   case iro_Proj:
945     pred    = get_Proj_pred(n);
946     proj_nr = get_Proj_proj(n);
947 handle_lut:
948     code    = get_irn_opcode(pred);
949
950     if (code == iro_Cmp)
951       fprintf (F, "%s ", get_pnc_string(get_Proj_proj(n)));
952     else if (code == iro_Proj && get_irn_opcode(get_Proj_pred(pred)) == iro_Start)
953       fprintf (F, "Arg %ld ", proj_nr);
954     else if (code == iro_Cond && get_irn_mode(get_Cond_selector(pred)) != mode_b)
955       fprintf (F, "%ld ", proj_nr);
956     else {
957       unsigned i, j, f = 0;
958
959       for (i = 0; i < ARR_SIZE(proj_lut); ++i) {
960         if (code == proj_lut[i].code) {
961           for (j = 0; j < proj_lut[i].num_data; ++j) {
962             if (proj_nr == proj_lut[i].data[j].nr) {
963               fprintf (F, "%s ", proj_lut[i].data[j].name);
964               f = 1;
965               break;
966             }
967           }
968           break;
969         }
970       }
971       if (! f)
972         fprintf (F, "%ld ", proj_nr);
973       if (code == iro_Cond && get_Cond_jmp_pred(pred) != COND_JMP_PRED_NONE) {
974         if (proj_nr == pn_Cond_false && get_Cond_jmp_pred(pred) == COND_JMP_PRED_FALSE)
975           fprintf(F, "PRED ");
976         if (proj_nr == pn_Cond_true && get_Cond_jmp_pred(pred) == COND_JMP_PRED_TRUE)
977           fprintf(F, "PRED ");
978       }
979     }
980     break;
981   case iro_Filter:
982     proj_nr = get_Filter_proj(n);
983     if (! get_interprocedural_view()) {
984       /* it's a Proj' */
985       pred    = get_Filter_pred(n);
986       goto handle_lut;
987     }
988     else
989       fprintf (F, "%ld ", proj_nr);
990     break;
991   case iro_Sel:
992     fprintf (F, "%s ", get_ent_dump_name(get_Sel_entity(n)));
993     break;
994   case iro_Cast:
995     fprintf (F, "(%s) ", get_type_name_ex(get_Cast_type(n), &bad));
996     break;
997   case iro_Confirm:
998     fprintf (F, "%s ", get_pnc_string(get_Confirm_cmp(n)));
999     break;
1000   case iro_CopyB:
1001     fprintf (F, "(%s) ", get_type_name_ex(get_CopyB_type(n), &bad));
1002     break;
1003
1004   default:
1005     ;
1006   } /* end switch */
1007
1008   return bad;
1009 }
1010
1011 #include <math.h>
1012 #include "execution_frequency.h"
1013 #include "callgraph.h"
1014
1015 void dump_node_ana_vals(FILE *F, ir_node *n) {
1016   return;
1017   fprintf(F, " %lf*(%2.0lf + %2.0lf) = %2.0lf ",
1018           get_irn_exec_freq(n),
1019           get_irg_method_execution_frequency(get_irn_irg(n)),
1020           pow(5, get_irg_recursion_depth(get_irn_irg(n))),
1021           get_irn_exec_freq(n) * (get_irg_method_execution_frequency(get_irn_irg(n)) + pow(5, get_irg_recursion_depth(get_irn_irg(n))))
1022           );
1023 }
1024
1025
1026 /* Dumps a node label without the enclosing ". */
1027 int dump_node_label(FILE *F, ir_node *n) {
1028   int bad = 0;
1029
1030   bad |= dump_node_opcode(F, n);
1031   bad |= dump_node_mode(F, n);
1032   fprintf (F, " ");
1033   bad |= dump_node_typeinfo(F, n);
1034   bad |= dump_node_nodeattr(F, n);
1035   fprintf(F, "%ld", get_irn_node_nr(n));
1036
1037   return bad;
1038 }
1039
1040
1041 /**
1042  * Dumps the attributes of a node n into the file F.
1043  * Currently this is only the color of a node.
1044  */
1045 static void dump_node_vcgattr(FILE *F, ir_node *node, ir_node *local, int bad)
1046 {
1047   ir_node *n;
1048
1049   if (bad) {
1050     print_vcg_color(F, ird_color_red);
1051     return;
1052   }
1053
1054   if (dump_node_vcgattr_hook)
1055     if (dump_node_vcgattr_hook(F, node, local))
1056       return;
1057
1058   n = local ? local : node;
1059
1060   if (overrule_nodecolor != ird_color_default) {
1061     print_vcg_color(F, overrule_nodecolor);
1062     return;
1063   }
1064
1065   switch (get_irn_opcode(n)) {
1066   case iro_Start:
1067   case iro_EndReg:
1068     /* fall through */
1069   case iro_EndExcept:
1070     /* fall through */
1071   case iro_End:
1072     print_vcg_color(F, ird_color_blue);
1073     break;
1074   case iro_Block:
1075     if (is_Block_dead(n))
1076       print_vcg_color(F, ird_color_lightred);
1077     else
1078       print_vcg_color(F, ird_color_lightyellow);
1079     break;
1080   case iro_Phi:
1081     print_vcg_color(F, ird_color_green);
1082     break;
1083   case iro_Const:
1084   case iro_Proj:
1085   case iro_Filter:
1086   case iro_Tuple:
1087     print_vcg_color(F, ird_color_yellow);
1088     break;
1089   default:
1090     PRINT_DEFAULT_NODE_ATTR;
1091   }
1092 }
1093
1094
1095 /**
1096  * Dump the node information of a node n to a file F.
1097  */
1098 static INLINE int dump_node_info(FILE *F, ir_node *n)
1099 {
1100   int bad = 0;
1101   const ir_op_ops *ops = get_op_ops(get_irn_op(n));
1102
1103   fprintf (F, " info1: \"");
1104   bad = dump_irnode_to_file(F, n);
1105   /* call the dump_node operation if available */
1106   if (ops->dump_node)
1107     bad = ops->dump_node(n, F, dump_node_info_txt);
1108   fprintf(F, "\"\n");
1109
1110   return bad;
1111 }
1112
1113 /**
1114  * checks whether a node is "constant-like" ie can be treated "block-less"
1115  */
1116 static INLINE
1117 int is_constlike_node(ir_node *n) {
1118   ir_op *op = get_irn_op(n);
1119   return (op == op_Const || op == op_Bad || op == op_NoMem || op == op_SymConst || op == op_Unknown);
1120 }
1121
1122
1123 /** outputs the predecessors of n, that are constants, local.  I.e.,
1124    generates a copy of the constant predecessors for each node called with. */
1125 static void dump_const_node_local(FILE *F, ir_node *n) {
1126   int i;
1127   if (!get_opt_dump_const_local()) return;
1128
1129   /* Use visited flag to avoid outputting nodes twice.
1130      initialize it first. */
1131   for (i = 0; i < get_irn_arity(n); i++) {
1132     ir_node *con = get_irn_n(n, i);
1133     if (is_constlike_node(con)) {
1134       set_irn_visited(con, get_irg_visited(current_ir_graph) - 1);
1135     }
1136   }
1137
1138   for (i = 0; i < get_irn_arity(n); i++) {
1139     ir_node *con = get_irn_n(n, i);
1140     if (is_constlike_node(con) && irn_not_visited(con)) {
1141       int bad = 0;
1142
1143       mark_irn_visited(con);
1144       /* Generate a new name for the node by appending the names of
1145          n and const. */
1146       fprintf(F, "node: {title: "); PRINT_CONSTID(n, con);
1147       fprintf(F, " label: \"");
1148       bad |= dump_node_label(F, con);
1149       fprintf(F, "\" ");
1150       bad |= dump_node_info(F, con);
1151       dump_node_vcgattr(F, n, con, bad);
1152       fprintf(F, "}\n");
1153     }
1154   }
1155 }
1156
1157 /** If the block of an edge is a const_like node, dump it local with an edge */
1158 static void dump_const_block_local(FILE *F, ir_node *n) {
1159   ir_node *blk;
1160
1161   if (!get_opt_dump_const_local()) return;
1162
1163   blk = get_nodes_block(n);
1164   if (is_constlike_node(blk)) {
1165     int bad = 0;
1166
1167     /* Generate a new name for the node by appending the names of
1168        n and blk. */
1169     fprintf(F, "node: {title: \""); PRINT_CONSTBLKID(n, blk);
1170     fprintf(F, "\" label: \"");
1171     bad |= dump_node_label(F, blk);
1172     fprintf(F, "\" ");
1173     bad |= dump_node_info(F, blk);
1174     dump_node_vcgattr(F, n, blk, bad);
1175     fprintf(F, "}\n");
1176
1177     fprintf (F, "edge: { sourcename: \"");
1178     PRINT_NODEID(n);
1179     fprintf (F, "\" targetname: \""); PRINT_CONSTBLKID(n,blk);
1180
1181     if (dump_edge_vcgattr_hook) {
1182       fprintf (F, "\" ");
1183       if (dump_edge_vcgattr_hook(F, n, -1)) {
1184         fprintf (F, "}\n");
1185         return;
1186       }
1187       else {
1188         fprintf (F, " " BLOCK_EDGE_ATTR "}\n");
1189         return;
1190       }
1191     }
1192
1193     fprintf (F, "\" "   BLOCK_EDGE_ATTR "}\n");
1194   }
1195 }
1196
1197 /**
1198  * prints the error message of a node to a file F as info2.
1199  */
1200 static void INLINE print_node_error(FILE *F, const char *err_msg)
1201 {
1202   if (! err_msg)
1203     return;
1204
1205   fprintf (F, " info2: \"%s\"", err_msg);
1206 }
1207
1208 /**
1209  * prints debug messages of a node to file F as info3.
1210  */
1211 static void print_dbg_info(FILE *F, dbg_info *dbg)
1212 {
1213   char buf[1024];
1214
1215   if (__dbg_info_snprint) {
1216     buf[0] = '\0';
1217     if (__dbg_info_snprint(buf, sizeof(buf), dbg) > 0)
1218       fprintf (F, " info3: \"%s\"\n", buf);
1219   }
1220 }
1221
1222 /**
1223  * Dump a node
1224  */
1225 static void dump_node(FILE *F, ir_node *n)
1226 {
1227   int bad = 0;
1228   const char *p;
1229
1230   if (get_opt_dump_const_local() && is_constlike_node(n))
1231     return;
1232
1233   /* dump this node */
1234   fprintf(F, "node: {title: \""); PRINT_NODEID(n); fprintf(F, "\" label: \"");
1235
1236   bad = ! irn_vrfy_irg_dump(n, current_ir_graph, &p);
1237   bad |= dump_node_label(F, n);
1238   dump_node_ana_vals(F, n);
1239   //dump_node_ana_info(F, n);
1240   fprintf(F, "\" ");
1241   bad |= dump_node_info(F, n);
1242   print_node_error(F, p);
1243   print_dbg_info(F, get_irn_dbg_info(n));
1244   dump_node_vcgattr(F, n, NULL, bad);
1245   fprintf(F, "}\n");
1246   dump_const_node_local(F, n);
1247
1248   if(dump_node_edge_hook)
1249     dump_node_edge_hook(F, n);
1250 #if DO_HEAPANALYSIS
1251   dump_irn_chi_term(F, n);
1252   dump_irn_state(F, n);
1253 #endif
1254 }
1255
1256 /** dump the edge to the block this node belongs to */
1257 static void
1258 dump_ir_block_edge(FILE *F, ir_node *n)  {
1259   if (get_opt_dump_const_local() && is_constlike_node(n)) return;
1260   if (is_no_Block(n)) {
1261     ir_node *block = get_nodes_block(n);
1262
1263     if (get_opt_dump_const_local() && is_constlike_node(block)) {
1264       dump_const_block_local(F, n);
1265     }
1266     else {
1267       fprintf (F, "edge: { sourcename: \"");
1268       PRINT_NODEID(n);
1269       fprintf (F, "\" targetname: ");
1270       fprintf(F, "\""); PRINT_NODEID(block); fprintf(F, "\"");
1271
1272       if (dump_edge_vcgattr_hook) {
1273         fprintf (F, " ");
1274         if (dump_edge_vcgattr_hook(F, n, -1)) {
1275           fprintf (F, "}\n");
1276           return;
1277         }
1278         else {
1279           fprintf (F, " "  BLOCK_EDGE_ATTR "}\n");
1280           return;
1281         }
1282       }
1283
1284       fprintf (F, " "   BLOCK_EDGE_ATTR "}\n");
1285     }
1286   }
1287 }
1288
1289 static void
1290 print_data_edge_vcgattr(FILE *F, ir_node *from, int to) {
1291   /*
1292    * do not use get_nodes_block() here, will fail
1293    * if the irg is not pinned.
1294    */
1295   if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1296     fprintf (F, INTRA_DATA_EDGE_ATTR);
1297   else
1298     fprintf (F, INTER_DATA_EDGE_ATTR);
1299 }
1300
1301 static void
1302 print_mem_edge_vcgattr(FILE *F, ir_node *from, int to) {
1303   /*
1304    * do not use get_nodes_block() here, will fail
1305    * if the irg is not pinned.
1306    */
1307   if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1308     fprintf (F, INTRA_MEM_EDGE_ATTR);
1309   else
1310     fprintf (F, INTER_MEM_EDGE_ATTR);
1311 }
1312
1313 static void
1314 print_edge_vcgattr(FILE *F, ir_node *from, int to) {
1315   assert(from);
1316
1317   if (dump_edge_vcgattr_hook)
1318     if (dump_edge_vcgattr_hook(F, from, to))
1319       return;
1320
1321   if (dump_backedge_information_flag && is_backedge(from, to))
1322     fprintf (F, BACK_EDGE_ATTR);
1323
1324   switch (get_irn_opcode(from)) {
1325   case iro_Block:
1326     fprintf (F, CF_EDGE_ATTR);
1327     break;
1328   case iro_Start:  break;
1329   case iro_End:
1330     if (to >= 0) {
1331       if (get_irn_mode(get_End_keepalive(from, to)) == mode_BB)
1332     fprintf (F, CF_EDGE_ATTR);
1333       if (get_irn_mode(get_End_keepalive(from, to)) == mode_X)
1334     fprintf (F, INTER_MEM_EDGE_ATTR);
1335     }
1336     break;
1337   default:
1338     if (is_Proj(from)) {
1339       if (get_irn_mode(from) == mode_M)
1340         print_mem_edge_vcgattr(F, from, to);
1341       else if (get_irn_mode(from) == mode_X)
1342         fprintf(F, CF_EDGE_ATTR);
1343       else
1344         print_data_edge_vcgattr(F, from, to);
1345     }
1346     else if (get_irn_mode(get_irn_n(from, to)) == mode_M)
1347       print_mem_edge_vcgattr(F, from, to);
1348     else if (get_irn_mode(get_irn_n(from, to)) == mode_X)
1349       fprintf (F, CF_EDGE_ATTR);
1350     else
1351       print_data_edge_vcgattr(F, from, to);
1352   }
1353 }
1354
1355 /* dump edges to our inputs */
1356 static void
1357 dump_ir_data_edges(FILE *F, ir_node *n)  {
1358   int i;
1359   unsigned long visited = get_irn_visited(n);
1360
1361   if ((get_irn_op(n) == op_End) && (!dump_keepalive))
1362     return;
1363
1364   for (i = 0; i < get_irn_arity(n); i++) {
1365     ir_node * pred = get_irn_n(n, i);
1366     assert(pred);
1367
1368     if ((get_interprocedural_view() && get_irn_visited(pred) < visited))
1369       continue; /* pred not dumped */
1370
1371     if (dump_backedge_information_flag && is_backedge(n, i))
1372       fprintf (F, "backedge: {sourcename: \"");
1373     else
1374       fprintf (F, "edge: {sourcename: \"");
1375     PRINT_NODEID(n);
1376     fprintf (F, "\" targetname: ");
1377     if ((get_opt_dump_const_local()) && is_constlike_node(pred)) {
1378       PRINT_CONSTID(n, pred);
1379     } else {
1380       fprintf(F, "\""); PRINT_NODEID(pred); fprintf(F, "\"");
1381     }
1382     fprintf (F, " label: \"%d\" ", i);
1383     print_edge_vcgattr(F, n, i);
1384     fprintf (F, "}\n");
1385   }
1386 }
1387
1388 /** Dumps a node and its edges but not the block edge
1389  */
1390 static INLINE void
1391 dump_node_wo_blockedge (ir_node *n, void *env) {
1392   FILE *F = env;
1393   dump_node(F, n);
1394   dump_ir_data_edges(F, n);
1395 }
1396
1397 /** Dumps a node and its edges.
1398  */
1399 static void
1400 dump_whole_node (ir_node *n, void *env) {
1401   FILE *F = env;
1402   dump_node_wo_blockedge(n, env);
1403   if (!node_floats(n)) dump_ir_block_edge(F, n);
1404 }
1405
1406 static void
1407 dump_const_node(ir_node *n, void *env) {
1408   if (is_Block(n)) return;
1409   dump_node_wo_blockedge(n, env);
1410 }
1411
1412 /***********************************************************************/
1413 /* the following routines dump the nodes/irgs bracketed to graphs.     */
1414 /***********************************************************************/
1415
1416 /** Dumps a constant expression as entity initializer, array bound ...
1417  */
1418 static void dump_const_expression(FILE *F, ir_node *value) {
1419   ir_graph *rem = current_ir_graph;
1420   int rem_dump_const_local = dump_const_local;
1421   dump_const_local = 0;
1422   current_ir_graph = get_const_code_irg();
1423   irg_walk(value, dump_const_node, NULL, F);
1424   /* Decrease visited flag so that we walk with the same flag for the next
1425      expression.  This guarantees that we don't dump the same node twice,
1426      as for const expressions cse is performed to save memory. */
1427   set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph) -1);
1428   current_ir_graph = rem;
1429   dump_const_local = rem_dump_const_local;
1430 }
1431
1432 /** Dump a block as graph containing its nodes.
1433  *
1434  *  Expects to find nodes belonging to the block as list in its
1435  *  link field.
1436  *  Dumps the edges of all nodes including itself. */
1437 static void
1438 dump_whole_block(FILE *F, ir_node *block) {
1439   ir_node *node;
1440   dumper_colors color = ird_color_yellow;
1441
1442   assert(is_Block(block));
1443
1444   fprintf(F, "graph: { title: \"");
1445   PRINT_NODEID(block);
1446   fprintf(F, "\"  label: \"");
1447   dump_node_label(F, block);
1448 #if DO_HEAPANALYSIS
1449   if (get_opt_dump_abstvals())
1450     fprintf (F, " seqno: %d", (int)get_Block_seqno(block));
1451 #endif
1452
1453   /* colorize blocks */
1454   if (! get_Block_matured(block))
1455     color = ird_color_red;
1456   if (is_Block_dead(block))
1457     color = ird_color_orange;
1458
1459   fprintf(F, "\" status:clustered ");
1460   print_vcg_color(F, color);
1461   fprintf(F, "\n");
1462
1463   /* yComp can show attributes for blocks, XVCG parses but ignores them */
1464   dump_node_info(F, block);
1465   print_dbg_info(F, get_irn_dbg_info(block));
1466
1467   /* dump the blocks edges */
1468   dump_ir_data_edges(F, block);
1469
1470   /* dump the nodes that go into the block */
1471   for (node = ird_get_irn_link(block); node; node = ird_get_irn_link(node)) {
1472     dump_node(F, node);
1473     dump_ir_data_edges(F, node);
1474   }
1475
1476   /* Close the vcg information for the block */
1477   fprintf(F, "}\n");
1478   dump_const_node_local(F, block);
1479 #if DO_HEAPANALYSIS
1480   dump_irn_chi_term(F, block);
1481 #endif
1482   fprintf(F, "\n");
1483 }
1484
1485 /** dumps a graph block-wise. Expects all blockless nodes in arr in irgs link.
1486  *  The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1487 static void
1488 dump_block_graph(FILE *F, ir_graph *irg) {
1489   int i;
1490   ir_graph *rem = current_ir_graph;
1491   ir_node **arr = ird_get_irg_link(irg);
1492   current_ir_graph = irg;
1493
1494   for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
1495     ir_node * node = arr[i];
1496     if (is_Block(node)) {
1497       /* Dumps the block and all the nodes in the block, which are to
1498          be found in Block->link. */
1499       dump_whole_block(F, node);
1500     } else {
1501       /* Nodes that are not in a Block. */
1502       dump_node(F, node);
1503       if (!node_floats(node) && is_Bad(get_nodes_block(node))) {
1504         dump_const_block_local(F, node);
1505       }
1506       dump_ir_data_edges(F, node);
1507     }
1508   }
1509
1510   if (dump_loop_information_flag && (get_irg_loopinfo_state(irg) & loopinfo_valid))
1511     dump_loop_nodes_into_graph(F, irg);
1512
1513   current_ir_graph = rem;
1514 }
1515
1516 /**
1517  * Dump the info for an irg.
1518  * Parsed by XVCG but not shown. use yComp.
1519  */
1520 static void dump_graph_info(FILE *F, ir_graph *irg) {
1521   fprintf(F, "info1: \"");
1522   dump_entity_to_file(F, get_irg_entity(irg), dump_verbosity_entattrs | dump_verbosity_entconsts);
1523   fprintf(F, "\"\n");
1524 }
1525
1526 /** Dumps an irg as a graph clustered by block nodes.
1527  *  If interprocedural view edges can point to nodes out of this graph.
1528  */
1529 static void dump_graph_from_list(FILE *F, ir_graph *irg) {
1530   entity *ent = get_irg_entity(irg);
1531
1532   fprintf(F, "graph: { title: \"");
1533   PRINT_IRGID(irg);
1534   fprintf(F, "\" label: \"%s\" status:clustered color:white \n",
1535       get_ent_dump_name(ent));
1536
1537   dump_graph_info(F, irg);
1538   print_dbg_info(F, get_entity_dbg_info(ent));
1539
1540   dump_block_graph(F, irg);
1541
1542   /* Close the vcg information for the irg */
1543   fprintf(F, "}\n\n");
1544 }
1545
1546 /** dumps a graph extended block-wise. Expects all blockless nodes in arr in irgs link.
1547  *  The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1548 static void
1549 dump_extblock_graph(FILE *F, ir_graph *irg) {
1550   int i;
1551   ir_graph *rem = current_ir_graph;
1552   ir_extblk **arr = ird_get_irg_link(irg);
1553   current_ir_graph = irg;
1554
1555   for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
1556     ir_extblk *extbb = arr[i];
1557     ir_node *leader = get_extbb_leader(extbb);
1558     int j;
1559
1560     fprintf(F, "graph: { title: \"");
1561     PRINT_EXTBBID(leader);
1562     fprintf(F, "\"  label: \"ExtBB %ld\" status:clustered color:lightgreen\n",
1563             get_irn_node_nr(leader));
1564
1565     for (j = ARR_LEN(extbb->blks) - 1; j >= 0; --j) {
1566       ir_node * node = extbb->blks[j];
1567       if (is_Block(node)) {
1568         /* Dumps the block and all the nodes in the block, which are to
1569            be found in Block->link. */
1570         dump_whole_block(F, node);
1571       } else {
1572         /* Nodes that are not in a Block. */
1573         dump_node(F, node);
1574         if (is_Bad(get_nodes_block(node)) && !node_floats(node)) {
1575           dump_const_block_local(F, node);
1576         }
1577         dump_ir_data_edges(F, node);
1578       }
1579     }
1580     fprintf(F, "}\n");
1581   }
1582
1583   if (dump_loop_information_flag && (get_irg_loopinfo_state(irg) & loopinfo_valid))
1584     dump_loop_nodes_into_graph(F, irg);
1585
1586   current_ir_graph = rem;
1587   free_extbb(irg);
1588 }
1589
1590
1591 /*******************************************************************/
1592 /* Basic type and entity nodes and edges.                          */
1593 /*******************************************************************/
1594
1595 /** dumps the edges between nodes and their type or entity attributes. */
1596 static void dump_node2type_edges(ir_node *n, void *env)
1597 {
1598   FILE *F = env;
1599   assert(n);
1600
1601   switch (get_irn_opcode(n)) {
1602   case iro_Const :
1603     /* @@@ some consts have an entity */
1604     break;
1605   case iro_SymConst:
1606     if (   (get_SymConst_kind(n) ==symconst_type_tag)
1607        || (get_SymConst_kind(n) ==symconst_size))
1608       {
1609         print_node_type_edge(F,n,get_SymConst_type(n),NODE2TYPE_EDGE_ATTR);
1610       }
1611     break;
1612   case iro_Sel: {
1613       print_node_ent_edge(F,n,get_Sel_entity(n),NODE2TYPE_EDGE_ATTR);
1614     } break;
1615   case iro_Call: {
1616       print_node_type_edge(F,n,get_Call_type(n),NODE2TYPE_EDGE_ATTR);
1617     } break;
1618   case iro_Alloc: {
1619       print_node_type_edge(F,n,get_Alloc_type(n),NODE2TYPE_EDGE_ATTR);
1620     } break;
1621   case iro_Free: {
1622       print_node_type_edge(F,n,get_Free_type(n),NODE2TYPE_EDGE_ATTR);
1623     } break;
1624   case iro_Cast: {
1625       print_node_type_edge(F,n,get_Cast_type(n),NODE2TYPE_EDGE_ATTR);
1626     } break;
1627   default:
1628     break;
1629   }
1630 }
1631
1632 #if 0
1633 static int print_type_info(FILE *F, ir_type *tp) {
1634   int bad = 0;
1635
1636   if (get_type_state(tp) == layout_undefined) {
1637     fprintf(F, "state: layout_undefined\n");
1638   } else {
1639     fprintf(F, "state: layout_fixed,\n");
1640   }
1641   if (get_type_mode(tp))
1642     fprintf(F, "mode: %s,\n", get_mode_name_ex(get_type_mode(tp), &bad));
1643   fprintf(F, "size: %db,\n", get_type_size_bits(tp));
1644
1645   return bad;
1646 }
1647
1648 static void print_typespecific_info(FILE *F, ir_type *tp) {
1649   switch (get_type_tpop_code(tp)) {
1650   case tpo_class:
1651     {
1652       fprintf(F, "peculiarity: %s\n", get_peculiarity_string(get_class_peculiarity(tp)));
1653     } break;
1654   case tpo_struct:
1655     {
1656     } break;
1657   case tpo_method:
1658     {
1659       fprintf(F, "variadicity: %s\n", get_variadicity_name(get_method_variadicity(tp)));
1660       fprintf(F, "params: %d\n", get_method_n_params(tp));
1661       fprintf(F, "results: %d\n", get_method_n_ress(tp));
1662     } break;
1663   case tpo_union:
1664     {
1665     } break;
1666   case tpo_array:
1667     {
1668     } break;
1669   case tpo_enumeration:
1670     {
1671     } break;
1672   case tpo_pointer:
1673     {
1674     } break;
1675   case tpo_primitive:
1676     {
1677     } break;
1678   default: break;
1679   } /* switch type */
1680 }
1681 #endif
1682
1683 static void print_typespecific_vcgattr(FILE *F, ir_type *tp) {
1684   switch (get_type_tpop_code(tp)) {
1685   case tpo_class:
1686     {
1687       if (peculiarity_existent == get_class_peculiarity(tp))
1688     fprintf (F, " " TYPE_CLASS_NODE_ATTR);
1689       else
1690     fprintf (F, " " TYPE_DESCRIPTION_NODE_ATTR);
1691     } break;
1692   case tpo_struct:
1693     {
1694       fprintf (F, " " TYPE_METH_NODE_ATTR);
1695     } break;
1696   case tpo_method:
1697     {
1698     } break;
1699   case tpo_union:
1700     {
1701     } break;
1702   case tpo_array:
1703     {
1704     } break;
1705   case tpo_enumeration:
1706     {
1707     } break;
1708   case tpo_pointer:
1709     {
1710     } break;
1711   case tpo_primitive:
1712     {
1713     } break;
1714   default: break;
1715   } /* switch type */
1716 }
1717
1718
1719 int dump_type_node(FILE *F, ir_type *tp)
1720 {
1721   int bad = 0;
1722
1723   fprintf (F, "node: {title: ");
1724   PRINT_TYPEID(tp);
1725   fprintf (F, " label: \"%s %s\"", get_type_tpop_name(tp), get_type_name_ex(tp, &bad));
1726   fprintf (F, " info1: \"");
1727 #if 0
1728   bad |= print_type_info(F, tp);
1729   print_typespecific_info(F, tp);
1730 #else
1731   dump_type_to_file(F, tp, dump_verbosity_max);
1732 #endif
1733   fprintf (F, "\"\n");
1734   print_dbg_info(F, get_type_dbg_info(tp));
1735   print_typespecific_vcgattr(F, tp);
1736   fprintf (F, "}\n");
1737
1738   return bad;
1739 }
1740
1741
1742 #define X(a)    case a: fprintf(F, #a); break
1743 void dump_entity_node(FILE *F, entity *ent, int color)
1744 {
1745   fprintf (F, "node: {title: \"");
1746   PRINT_ENTID(ent); fprintf(F, "\"");
1747   fprintf (F, DEFAULT_TYPE_ATTRIBUTE);
1748   fprintf (F, "label: ");
1749   fprintf (F, "\"ent %s\" ", get_ent_dump_name(ent));
1750   if (color)
1751     fprintf(F, "color:%d", color);
1752   else
1753     fprintf (F, ENTITY_NODE_ATTR);
1754   fprintf (F, "\n info1: \"");
1755
1756   dump_entity_to_file(F, ent, dump_verbosity_entattrs | dump_verbosity_entconsts);
1757
1758   fprintf(F, "\"\n");
1759   print_dbg_info(F, get_entity_dbg_info(ent));
1760   fprintf(F, "}\n");
1761 }
1762 #undef X
1763
1764 static void dump_enum_item(FILE *F, ir_type *tp, int pos)
1765 {
1766   char buf[1024];
1767   ident *id  = get_enumeration_nameid(tp, pos);
1768   tarval *tv = get_enumeration_enum(tp, pos);
1769
1770   tarval_snprintf(buf, sizeof(buf), tv);
1771   fprintf (F, "node: {title: \"");
1772   PRINT_ITEMID(tp, pos); fprintf(F, "\"");
1773   fprintf (F, DEFAULT_ENUM_ITEM_ATTRIBUTE);
1774   fprintf (F, "label: ");
1775   fprintf (F, "\"enum item %s\" " ENUM_ITEM_NODE_ATTR, get_id_str(id));
1776   fprintf (F, "\n info1: \"value: %s\"}\n", buf);
1777 }
1778
1779 /* dumps a type or entity and it's edges. */
1780 static void
1781 dump_type_info(type_or_ent *tore, void *env) {
1782   FILE *F = env;
1783   int i = 0;  /* to shutup gcc */
1784
1785   /* dump this type or entity */
1786
1787   switch (get_kind(tore)) {
1788   case k_entity:
1789     {
1790       entity *ent = (entity *)tore;
1791       ir_node *value;
1792       /* The node */
1793       dump_entity_node(F, ent, 0);
1794       /* The Edges */
1795       /* skip this to reduce graph.  Member edge of type is parallel to this edge. *
1796       fprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
1797                 ENT_OWN_EDGE_ATTR "}\n", ent, get_entity_owner(ent));*/
1798       print_ent_type_edge(F,ent, get_entity_type(ent), ENT_TYPE_EDGE_ATTR);
1799       if (is_Class_type(get_entity_owner(ent))) {
1800         for(i = 0; i < get_entity_n_overwrites(ent); i++)
1801           print_ent_ent_edge(F,ent, get_entity_overwrites(ent, i), 0, ENT_OVERWRITES_EDGE_ATTR);
1802       }
1803       /* attached subgraphs */
1804       if (const_entities && (get_entity_variability(ent) != variability_uninitialized)) {
1805         if (is_atomic_entity(ent)) {
1806           value = get_atomic_ent_value(ent);
1807           if (value) {
1808             print_ent_node_edge(F, ent, value, ENT_VALUE_EDGE_ATTR, i);
1809             /* DDMN(value);  $$$ */
1810             dump_const_expression(F, value);
1811           }
1812         }
1813         if (is_compound_entity(ent)) {
1814           for (i = 0; i < get_compound_ent_n_values(ent); i++) {
1815             value = get_compound_ent_value(ent, i);
1816             if (value) {
1817               print_ent_node_edge(F, ent, value, ENT_VALUE_EDGE_ATTR, i);
1818               dump_const_expression(F, value);
1819               print_ent_ent_edge(F, ent, get_compound_ent_value_member(ent, i), 0, ENT_CORR_EDGE_ATTR, i);
1820               /*
1821               fprintf (F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
1822               ENT_CORR_EDGE_ATTR  "}\n", GET_ENTID(ent),
1823               get_compound_ent_value_member(ent, i), i);
1824               */
1825             }
1826           }
1827         }
1828       }
1829     } break;
1830   case k_type:
1831     {
1832       ir_type *tp = (ir_type *)tore;
1833       dump_type_node(F, tp);
1834       /* and now the edges */
1835       switch (get_type_tpop_code(tp)) {
1836       case tpo_class:
1837         {
1838           for (i=0; i < get_class_n_supertypes(tp); i++)
1839             print_type_type_edge(F, tp,get_class_supertype(tp, i),TYPE_SUPER_EDGE_ATTR);
1840           for (i=0; i < get_class_n_members(tp); i++)
1841             print_type_ent_edge(F,tp,get_class_member(tp, i),TYPE_MEMBER_EDGE_ATTR);
1842         } break;
1843       case tpo_struct:
1844         {
1845           for (i=0; i < get_struct_n_members(tp); i++)
1846             print_type_ent_edge(F,tp,get_struct_member(tp, i),TYPE_MEMBER_EDGE_ATTR);
1847         } break;
1848       case tpo_method:
1849         {
1850           for (i = 0; i < get_method_n_params(tp); i++)
1851             print_type_type_edge(F,tp,get_method_param_type(tp, i),METH_PAR_EDGE_ATTR,i);
1852           for (i = 0; i < get_method_n_ress(tp); i++)
1853             print_type_type_edge(F,tp,get_method_res_type(tp, i),METH_RES_EDGE_ATTR,i);
1854         } break;
1855       case tpo_union:
1856         {
1857           for (i = 0; i < get_union_n_members(tp); i++)
1858             print_type_ent_edge(F,tp,get_union_member(tp, i),UNION_EDGE_ATTR);
1859         } break;
1860       case tpo_array:
1861         {
1862           print_type_type_edge(F,tp,get_array_element_type(tp),ARR_ELT_TYPE_EDGE_ATTR);
1863           print_type_ent_edge(F,tp,get_array_element_entity(tp),ARR_ENT_EDGE_ATTR);
1864           for (i = 0; i < get_array_n_dimensions(tp); i++) {
1865             ir_node *upper = get_array_upper_bound(tp, i);
1866             ir_node *lower = get_array_lower_bound(tp, i);
1867             print_node_type_edge(F, upper, tp, "label: \"upper %d\"", get_array_order(tp, i));
1868             print_node_type_edge(F, lower, tp, "label: \"lower %d\"", get_array_order(tp, i));
1869             dump_const_expression(F, upper);
1870             dump_const_expression(F, lower);
1871           }
1872
1873         } break;
1874       case tpo_enumeration:
1875         {
1876           for (i = 0; i < get_enumeration_n_enums(tp); ++i) {
1877             dump_enum_item(F, tp, i);
1878             print_enum_item_edge(F, tp, i, "label: \"item %d\"", i);
1879           }
1880         } break;
1881       case tpo_pointer:
1882         {
1883           print_type_type_edge(F,tp,get_pointer_points_to_type(tp), PTR_PTS_TO_EDGE_ATTR);
1884         } break;
1885       case tpo_primitive:
1886         {
1887         } break;
1888       default: break;
1889       } /* switch type */
1890     }
1891     break; /* case k_type */
1892   default:
1893     {
1894       printf(" *** irdump,  dump_type_info(l.%i), faulty type.\n", __LINE__);
1895     } break;
1896   } /* switch kind_or_entity */
1897 }
1898
1899 typedef struct _h_env {
1900   int dump_ent;
1901   FILE *f;
1902 } h_env_t;
1903
1904 /** For dumping class hierarchies.
1905  * Dumps a class type node and a superclass edge.
1906  * If env->dump_ent dumps entities of classes and overwrites edges.
1907  */
1908 static void
1909 dump_class_hierarchy_node (type_or_ent *tore, void *ctx) {
1910   h_env_t *env = ctx;
1911   FILE *F = env->f;
1912   int i = 0;  /* to shutup gcc */
1913
1914   /* dump this type or entity */
1915   switch (get_kind(tore)) {
1916   case k_entity: {
1917     entity *ent = (entity *)tore;
1918     if (get_entity_owner(ent) == get_glob_type()) break;
1919     if (!is_Method_type(get_entity_type(ent))) break;  /* GL */
1920     if (env->dump_ent && is_Class_type(get_entity_owner(ent))) {
1921       /* The node */
1922       dump_entity_node(F, ent, 0);
1923       /* The edges */
1924       print_type_ent_edge(F,get_entity_owner(ent),ent,TYPE_MEMBER_EDGE_ATTR);
1925       for(i = 0; i < get_entity_n_overwrites(ent); i++)
1926         print_ent_ent_edge(F, get_entity_overwrites(ent, i), ent, 0, ENT_OVERWRITES_EDGE_ATTR);
1927     }
1928   } break; /* case k_entity */
1929   case k_type:
1930     {
1931       ir_type *tp = (ir_type *)tore;
1932       if (tp == get_glob_type()) break;
1933       switch (get_type_tpop_code(tp)) {
1934         case tpo_class: {
1935           dump_type_node(F, tp);
1936           /* and now the edges */
1937           for (i=0; i < get_class_n_supertypes(tp); i++)
1938           {
1939               print_type_type_edge(F,tp,get_class_supertype(tp, i),TYPE_SUPER_EDGE_ATTR);
1940           }
1941         } break;
1942         default: break;
1943       } /* switch type */
1944     }
1945     break; /* case k_type */
1946   default:
1947     {
1948       printf(" *** irdump,  dump_class_hierarchy_node(l.%i), faulty type.\n", __LINE__);
1949     } break;
1950   } /* switch kind_or_entity */
1951 }
1952
1953 /*******************************************************************/
1954 /* dump analysis information that is expressed in graph terms.     */
1955 /*******************************************************************/
1956
1957 /* dump out edges */
1958 static void
1959 dump_out_edge(ir_node *n, void *env) {
1960   FILE *F = env;
1961   int i;
1962   for (i = 0; i < get_irn_n_outs(n); i++) {
1963     assert(get_irn_out(n, i));
1964     fprintf (F, "edge: {sourcename: \"");
1965     PRINT_NODEID(n);
1966     fprintf (F, "\" targetname: \"");
1967     PRINT_NODEID(get_irn_out(n, i));
1968     fprintf (F, "\" color: red linestyle: dashed");
1969     fprintf (F, "}\n");
1970   }
1971 }
1972
1973 static INLINE void
1974 dump_loop_label(FILE *F, ir_loop *loop) {
1975   fprintf (F, "loop %d, %d sons, %d nodes",
1976        get_loop_depth(loop), get_loop_n_sons(loop), get_loop_n_nodes(loop));
1977 }
1978
1979 static INLINE void dump_loop_info(FILE *F, ir_loop *loop) {
1980   fprintf (F, " info1: \"");
1981   fprintf (F, " loop nr: %d", get_loop_loop_nr(loop));
1982 #if DEBUG_libfirm   /* GL @@@ debug analyses */
1983   fprintf (F, "\n The loop was analyzed %d times.", PTR_TO_INT(get_loop_link(loop)));
1984 #endif
1985   fprintf (F, "\"");
1986 }
1987
1988 static INLINE void
1989 dump_loop_node(FILE *F, ir_loop *loop) {
1990   fprintf (F, "node: {title: \"");
1991   PRINT_LOOPID(loop);
1992   fprintf (F, "\" label: \"");
1993   dump_loop_label(F, loop);
1994   fprintf (F, "\" ");
1995   dump_loop_info(F, loop);
1996   fprintf (F, "}\n");
1997
1998 }
1999
2000 static INLINE void
2001 dump_loop_node_edge(FILE *F, ir_loop *loop, int i) {
2002   assert(loop);
2003   fprintf (F, "edge: {sourcename: \"");
2004   PRINT_LOOPID(loop);
2005   fprintf (F, "\" targetname: \"");
2006   PRINT_NODEID(get_loop_node(loop, i));
2007   fprintf (F, "\" color: green");
2008   fprintf (F, "}\n");
2009 }
2010
2011 static INLINE void
2012 dump_loop_son_edge(FILE *F, ir_loop *loop, int i) {
2013   assert(loop);
2014   fprintf (F, "edge: {sourcename: \"");
2015   PRINT_LOOPID(loop);
2016   fprintf (F, "\" targetname: \"");
2017   PRINT_LOOPID(get_loop_son(loop, i));
2018   fprintf (F, "\" color: darkgreen label: \"%d\"}\n",
2019        get_loop_element_pos(loop, get_loop_son(loop, i)));
2020 }
2021
2022 static
2023 void dump_loops(FILE *F, ir_loop *loop) {
2024   int i;
2025   /* dump this loop node */
2026   dump_loop_node(F, loop);
2027
2028   /* dump edges to nodes in loop -- only if it is a real loop */
2029   if (get_loop_depth(loop) != 0) {
2030     for (i = 0; i < get_loop_n_nodes(loop); i++) {
2031       dump_loop_node_edge(F, loop, i);
2032     }
2033   }
2034   for (i = 0; i < get_loop_n_sons(loop); i++) {
2035     dump_loops(F, get_loop_son(loop, i));
2036     dump_loop_son_edge(F, loop, i);
2037   }
2038 }
2039
2040 static INLINE
2041 void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg) {
2042   ir_graph *rem = current_ir_graph;
2043   current_ir_graph = irg;
2044
2045   if (get_irg_loop(irg)) dump_loops(F, get_irg_loop(irg));
2046
2047   current_ir_graph = rem;
2048 }
2049
2050
2051 /**
2052  * dumps the VCG header
2053  */
2054 void dump_vcg_header(FILE *F, const char *name, const char *orientation) {
2055   char *label;
2056
2057   if (edge_label) {
2058     label = "yes";
2059   } else {
2060     label = "no";
2061   }
2062
2063   if (!orientation) orientation = "bottom_to_top";
2064
2065   /* print header */
2066   fprintf (F,
2067        "graph: { title: \"ir graph of %s\"\n"
2068        "display_edge_labels: %s\n"
2069        "layoutalgorithm: mindepth\n"
2070        "manhattan_edges: yes\n"
2071        "port_sharing: no\n"
2072        "orientation: %s\n"
2073        "classname 1:  \"intrablock Data\"\n"
2074        "classname 2:  \"Block\"\n"
2075        "classname 3:  \"Entity type\"\n"
2076        "classname 4:  \"Entity owner\"\n"
2077        "classname 5:  \"Method Param\"\n"
2078        "classname 6:  \"Method Res\"\n"
2079        "classname 7:  \"Super\"\n"
2080        "classname 8:  \"Union\"\n"
2081        "classname 9:  \"Points-to\"\n"
2082        "classname 10: \"Array Element Type\"\n"
2083        "classname 11: \"Overwrites\"\n"
2084        "classname 12: \"Member\"\n"
2085        "classname 13: \"Control Flow\"\n"
2086        "classname 14: \"intrablock Memory\"\n"
2087        "classname 15: \"Dominators\"\n"
2088        "classname 16: \"interblock Data\"\n"
2089        "classname 17: \"interblock Memory\"\n"
2090        "classname 18: \"Exception Control Flow for Interval Analysis\"\n"
2091        "classname 19: \"Postdominators\"\n"
2092        "infoname 1: \"Attribute\"\n"
2093        "infoname 2: \"Verification errors\"\n"
2094        "infoname 3: \"Debug info\"\n",
2095        name, label, orientation);
2096
2097   /* don't use all, the range is too whith/black. */
2098   n_colors   = 18;
2099   base_color = 105;
2100   fprintf (F,
2101        "colorentry 100:    0   0    0\n"
2102        "colorentry 101:   20   0    0\n"
2103        "colorentry 102:   40   0    0\n"
2104        "colorentry 103:   60   0    0\n"
2105        "colorentry 104:   80   0    0\n"
2106        "colorentry 105:  100   0    0\n"
2107        "colorentry 106:  120   0    0\n"
2108        "colorentry 107:  140   0    0\n"
2109        "colorentry 108:  150   0    0\n"
2110        "colorentry 109:  180   0    0\n"
2111        "colorentry 110:  200   0    0\n"
2112        "colorentry 111:  220   0    0\n"
2113        "colorentry 112:  240   0    0\n"
2114        "colorentry 113:  255   0    0\n"
2115        "colorentry 113:  255  20   20\n"
2116        "colorentry 114:  255  40   40\n"
2117        "colorentry 115:  255  60   60\n"
2118        "colorentry 116:  255  80   80\n"
2119        "colorentry 117:  255 100  100\n"
2120        "colorentry 118:  255 120  120\n"
2121        "colorentry 119:  255 140  140\n"
2122        "colorentry 120:  255 150  150\n"
2123        "colorentry 121:  255 180  180\n"
2124        "colorentry 122:  255 200  200\n"
2125        "colorentry 123:  255 220  220\n"
2126        "colorentry 124:  255 240  240\n"
2127        "colorentry 125:  255 250  250\n"
2128        );
2129
2130   fprintf (F, "\n");        /* a separator */
2131 }
2132
2133 /**
2134  * open a vcg file
2135  *
2136  * @param irg     The graph to be dumped
2137  * @param suffix1 first filename suffix
2138  * @param suffix2 second filename suffix
2139  */
2140 FILE *vcg_open (ir_graph *irg, const char * suffix1, const char *suffix2) {
2141   FILE *F;
2142   const char *nm = get_irg_dump_name(irg);
2143   int len = strlen(nm), i, j;
2144   char *fname;  /* filename to put the vcg information in */
2145
2146   if (!suffix1) suffix1 = "";
2147   if (!suffix2) suffix2 = "";
2148
2149   /* open file for vcg graph */
2150   fname = malloc (len * 2 + strlen(suffix1) + strlen(suffix2) + 5);
2151
2152   /* strncpy (fname, nm, len); */     /* copy the filename */
2153   j = 0;
2154   for (i = 0; i < len; ++i) {  /* replace '/' in the name: escape by @. */
2155     if (nm[i] == '/') {
2156       fname[j] = '@'; j++; fname[j] = '1'; j++;
2157     } else if (nm[i] == '@') {
2158       fname[j] = '@'; j++; fname[j] = '2'; j++;
2159     } else {
2160       fname[j] = nm[i]; j++;
2161     }
2162   }
2163   fname[j] = '\0';
2164   strcat (fname, suffix1);  /* append file suffix */
2165   strcat (fname, suffix2);  /* append file suffix */
2166   strcat (fname, ".vcg");   /* append the .vcg suffix */
2167
2168   /* vcg really expect only a <CR> at end of line, so
2169    * the "b"inary mode is what you mean (and even needed for Win32)
2170    */
2171   F = fopen (fname, "wb");  /* open file for writing */
2172   if (!F) {
2173     panic("cannot open %s for writing (%m)", fname);  /* not reached */
2174   }
2175   free(fname);
2176
2177   return F;
2178 }
2179
2180 /**
2181  * open a vcg file
2182  *
2183  * @param name    prefix file name
2184  * @param suffix  filename suffix
2185  */
2186 FILE *vcg_open_name (const char *name, const char *suffix) {
2187   FILE *F;
2188   char *fname;  /* filename to put the vcg information in */
2189   int i, j, len = strlen(name);
2190
2191   if (!suffix) suffix = "";
2192
2193   /** open file for vcg graph */
2194   fname = xmalloc(len * 2 + 5 + strlen(suffix));
2195   /* strcpy (fname, name);*/    /* copy the filename */
2196   j = 0;
2197   for (i = 0; i < len; ++i) {  /* replace '/' in the name: escape by @. */
2198     if (name[i] == '/') {
2199       fname[j] = '@'; j++; fname[j] = '1'; j++;
2200     } else if (name[i] == '@') {
2201       fname[j] = '@'; j++; fname[j] = '2'; j++;
2202     } else {
2203       fname[j] = name[i]; j++;
2204     }
2205   }
2206   fname[j] = '\0';
2207   strcat (fname, suffix);
2208   strcat (fname, ".vcg");  /* append the .vcg suffix */
2209
2210   /* vcg really expect only a <CR> at end of line, so
2211    * the "b"inary mode is what you mean (and even needed for Win32)
2212    */
2213   F = fopen (fname, "wb");  /* open file for writing */
2214   if (!F) {
2215     panic ("cannot open %s for writing (%m)", fname);  /* not reached */
2216   }
2217   free(fname);
2218
2219   return F;
2220 }
2221
2222 /**
2223  * Dumps the vcg file footer
2224  */
2225 static INLINE void dump_vcg_footer (FILE *F) {
2226   fprintf (F, "}\n");
2227 }
2228
2229 /**
2230  * close the vcg file
2231  */
2232 void vcg_close (FILE *F) {
2233   dump_vcg_footer(F);    /* print footer */
2234   fclose (F);           /* close vcg file */
2235 }
2236
2237 /************************************************************************/
2238 /************************************************************************/
2239 /* Routines that dump all or parts of the firm representation to a file */
2240 /************************************************************************/
2241 /************************************************************************/
2242
2243 /************************************************************************/
2244 /* Dump ir graphs, different formats and additional information.        */
2245 /************************************************************************/
2246
2247 /** Routine to dump a graph, blocks as conventional nodes.  */
2248 void
2249 dump_ir_graph (ir_graph *irg, const char *suffix )
2250 {
2251   FILE *f;
2252   ir_graph *rem;
2253   char *suffix1;
2254   rem = current_ir_graph;
2255
2256   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg)))) return;
2257
2258   current_ir_graph = irg;
2259   if (get_interprocedural_view()) suffix1 = "-pure-ip";
2260   else                            suffix1 = "-pure";
2261   f = vcg_open(irg, suffix, suffix1);
2262   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2263
2264   /* call the dump graph hook */
2265   if (dump_ir_graph_hook)
2266     if (dump_ir_graph_hook(f, irg))
2267       return;
2268
2269   /* walk over the graph */
2270   /* dump_whole_node must be called in post visiting predecessors */
2271   irg_walk(get_irg_end(irg), NULL, dump_whole_node, f);
2272
2273   /* dump the out edges in a separate walk */
2274   if ((dump_out_edge_flag) && (get_irg_outs_state(irg) != outs_none)) {
2275     irg_out_walk(get_irg_start(irg), dump_out_edge, NULL, f);
2276   }
2277
2278   vcg_close(f);
2279
2280   current_ir_graph = rem;
2281 }
2282
2283 /* Dump a firm graph without explicit block nodes. */
2284 void dump_ir_block_graph (ir_graph *irg, const char *suffix)
2285 {
2286   FILE *f;
2287   int i;
2288   char *suffix1;
2289
2290   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2291     return;
2292
2293   if (get_interprocedural_view()) suffix1 = "-ip";
2294   else                            suffix1 = "";
2295   f = vcg_open(irg, suffix, suffix1);
2296   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2297
2298   construct_block_lists(irg);
2299
2300   /*
2301    * If we are in the interprocedural view, we dump not
2302    * only the requested irg but also all irgs that can be reached
2303    * from irg.
2304    */
2305   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2306     ir_node **arr = ird_get_irg_link(get_irp_irg(i));
2307     if (arr) {
2308       dump_graph_from_list(f, get_irp_irg(i));
2309       DEL_ARR_F(arr);
2310     }
2311   }
2312
2313   vcg_close(f);
2314 }
2315
2316 /* Dump a firm graph without explicit block nodes but grouped in extended blocks. */
2317 void dump_ir_extblock_graph (ir_graph *irg, const char *suffix)
2318 {
2319   FILE *F;
2320   int i;
2321   char *suffix1;
2322   entity *ent;
2323
2324   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2325     return;
2326
2327   if (get_irg_extblk_state(irg) != extblk_valid)
2328     compute_extbb(irg);
2329
2330   if (get_interprocedural_view()) suffix1 = "-ip";
2331   else                            suffix1 = "";
2332
2333   ent = get_irg_entity(irg);
2334
2335   F = vcg_open(irg, suffix, suffix1);
2336   dump_vcg_header(F, get_irg_dump_name(irg), NULL);
2337
2338   construct_extblock_lists(irg);
2339
2340   fprintf(F, "graph: { title: \"");
2341   PRINT_IRGID(irg);
2342   fprintf(F, "\" label: \"%s\" status:clustered color: white \n",
2343     get_ent_dump_name(ent));
2344
2345   dump_graph_info(F, irg);
2346   print_dbg_info(F, get_entity_dbg_info(ent));
2347
2348   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2349     ir_graph *irg     = get_irp_irg(i);
2350     list_tuple *lists = ird_get_irg_link(irg);
2351
2352     if (lists) {
2353       /* dump the extended blocks first */
2354       if (ARR_LEN(lists->extbb_list)) {
2355         ird_set_irg_link(irg, lists->extbb_list);
2356         dump_extblock_graph(F, irg);
2357       }
2358
2359       /* we may have blocks without extended blocks, bad for instance */
2360       if (ARR_LEN(lists->blk_list)) {
2361         ird_set_irg_link(irg, lists->blk_list);
2362         dump_block_graph(F, irg);
2363       }
2364
2365       DEL_ARR_F(lists->extbb_list);
2366       DEL_ARR_F(lists->blk_list);
2367       xfree(lists);
2368     }
2369   }
2370
2371   /* Close the vcg information for the irg */
2372   fprintf(F, "}\n\n");
2373
2374   vcg_close(F);
2375   free_extbb(irg);
2376 }
2377
2378 /* dumps a graph with type information */
2379 void
2380 dump_ir_graph_w_types (ir_graph *irg, const char *suffix)
2381 {
2382   FILE *f;
2383   ir_graph *rem = current_ir_graph;
2384   char *suffix1;
2385   int rem_dump_const_local;
2386
2387   /* if a filter is set, dump only the irg's that match the filter */
2388   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2389     return;
2390
2391   current_ir_graph = irg;
2392   rem_dump_const_local = dump_const_local;
2393   /* dumping types does not work with local nodes */
2394   dump_const_local = 0;
2395
2396   if (get_interprocedural_view()) suffix1 = "-pure-wtypes-ip";
2397   else                            suffix1 = "-pure-wtypes";
2398   f = vcg_open(irg,suffix, suffix1);
2399   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2400
2401   /* dump common ir graph */
2402   irg_walk(get_irg_end(irg), NULL, dump_whole_node, f);
2403   /* dump type info */
2404   type_walk_irg(irg, dump_type_info, NULL, f);
2405   inc_irg_visited(get_const_code_irg());
2406   /* dump edges from graph to type info */
2407   irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, f);
2408
2409   vcg_close(f);
2410   dump_const_local = rem_dump_const_local;
2411   current_ir_graph = rem;
2412 }
2413
2414 void
2415 dump_ir_block_graph_w_types (ir_graph *irg, const char *suffix)
2416 {
2417   FILE *f;
2418   int i;
2419   char *suffix1;
2420   ir_graph *rem = current_ir_graph;
2421   int rem_dump_const_local;
2422
2423   /* if a filter is set, dump only the irg's that match the filter */
2424   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2425     return;
2426
2427   rem_dump_const_local = dump_const_local;
2428   /* dumping types does not work with local nodes */
2429   dump_const_local = 0;
2430
2431   if (get_interprocedural_view()) suffix1 = "-wtypes-ip";
2432   else                            suffix1 = "-wtypes";
2433   f = vcg_open(irg, suffix, suffix1);
2434   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2435
2436   /* dump common blocked ir graph */
2437   construct_block_lists(irg);
2438
2439   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2440     ir_node **arr = ird_get_irg_link(get_irp_irg(i));
2441     if (arr) {
2442       dump_graph_from_list(f, get_irp_irg(i));
2443       DEL_ARR_F(arr);
2444     }
2445   }
2446
2447   /* dump type info */
2448   current_ir_graph = irg;
2449   type_walk_irg(irg, dump_type_info, NULL, f);
2450   inc_irg_visited(get_const_code_irg());
2451
2452   /* dump edges from graph to type info */
2453   irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, f);
2454
2455   vcg_close(f);
2456   dump_const_local = rem_dump_const_local;
2457   current_ir_graph = rem;
2458 }
2459
2460 /*---------------------------------------------------------------------*/
2461 /* The following routines dump a control flow graph.                   */
2462 /*---------------------------------------------------------------------*/
2463
2464 static void
2465 dump_block_to_cfg(ir_node *block, void *env) {
2466   FILE *F = env;
2467   int i, fl = 0;
2468   ir_node *pred;
2469
2470   if (is_Block(block)) {
2471     /* This is a block. Dump a node for the block. */
2472     fprintf (F, "node: {title: \""); PRINT_NODEID(block);
2473     fprintf (F, "\" label: \"");
2474     if (block == get_irg_start_block(get_irn_irg(block)))
2475       fprintf(F, "Start ");
2476     if (block == get_irg_end_block(get_irn_irg(block)))
2477       fprintf(F, "End ");
2478
2479     fprintf (F, "%s ", get_op_name(get_irn_op(block)));
2480     PRINT_NODEID(block);
2481     fprintf (F, "\" ");
2482     fprintf(F, "info1:\"");
2483
2484     /* the generic version. */
2485     dump_irnode_to_file(F, block);
2486
2487     /* Check whether we have bad predecessors to color the block. */
2488     for (i = 0; i < get_Block_n_cfgpreds(block); ++i)
2489       if ((fl = is_Bad(get_Block_cfgpred(block, i))))
2490         break;
2491
2492     fprintf (F, "\"");  /* closing quote of info */
2493
2494     if ((block == get_irg_start_block(get_irn_irg(block))) ||
2495     (block == get_irg_end_block(get_irn_irg(block)))     )
2496       fprintf(F, " color:blue ");
2497     else if (fl)
2498       fprintf(F, " color:yellow ");
2499
2500     fprintf (F, "}\n");
2501     /* Dump the edges */
2502     for ( i = 0; i < get_Block_n_cfgpreds(block); i++)
2503       if (get_irn_op(skip_Proj(get_Block_cfgpred(block, i))) != op_Bad) {
2504         pred = get_nodes_block(skip_Proj(get_Block_cfgpred(block, i)));
2505         fprintf (F, "edge: { sourcename: \"");
2506         PRINT_NODEID(block);
2507         fprintf (F, "\" targetname: \"");
2508         PRINT_NODEID(pred);
2509         fprintf (F, "\"}\n");
2510       }
2511
2512     /* Dump dominator/postdominator edge */
2513     if (dump_dominator_information_flag) {
2514       if (get_irg_dom_state(current_ir_graph) == dom_consistent && get_Block_idom(block)) {
2515         pred = get_Block_idom(block);
2516         fprintf (F, "edge: { sourcename: \"");
2517         PRINT_NODEID(block);
2518         fprintf (F, "\" targetname: \"");
2519         PRINT_NODEID(pred);
2520         fprintf (F, "\" " DOMINATOR_EDGE_ATTR "}\n");
2521       }
2522       if (get_irg_postdom_state(current_ir_graph) == dom_consistent && get_Block_ipostdom(block)) {
2523         pred = get_Block_ipostdom(block);
2524         fprintf (F, "edge: { sourcename: \"");
2525         PRINT_NODEID(block);
2526         fprintf (F, "\" targetname: \"");
2527         PRINT_NODEID(pred);
2528         fprintf (F, "\" " POSTDOMINATOR_EDGE_ATTR "}\n");
2529       }
2530     }
2531   }
2532 }
2533
2534 void
2535 dump_cfg (ir_graph *irg, const char *suffix)
2536 {
2537   FILE *f;
2538   ir_graph *rem = current_ir_graph;
2539   int ipv = get_interprocedural_view();
2540
2541   /* if a filter is set, dump only the irg's that match the filter */
2542   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2543     return;
2544
2545   current_ir_graph = irg;
2546
2547   f = vcg_open(irg, suffix, "-cfg");
2548   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2549
2550   if (ipv) {
2551     printf("Warning: dumping cfg not in interprocedural view!\n");
2552     set_interprocedural_view(0);
2553   }
2554
2555   /* walk over the blocks in the graph */
2556   irg_block_walk(get_irg_end(irg), dump_block_to_cfg, NULL, f);
2557   dump_node(f, get_irg_bad(irg));
2558
2559   set_interprocedural_view(ipv);
2560   vcg_close(f);
2561   current_ir_graph = rem;
2562 }
2563
2564
2565 static void descend_and_dump(FILE *F, ir_node *n, int depth, pset *mark_set) {
2566   if (pset_find_ptr(mark_set, n)) return;
2567
2568   pset_insert_ptr(mark_set, n);
2569
2570   if (depth > 0) {
2571     int i, start = is_Block(n) ? 0 : -1;
2572     dump_whole_node(n, F);
2573     for (i = start; i < get_irn_arity(n); ++i)
2574       descend_and_dump(F, get_irn_n(n, i), depth-1, mark_set);
2575   } else {
2576     dump_node(F, n);
2577     /* Don't dump edges to nodes further out.  These might be edges to
2578        nodes we already dumped, if there is a shorter path to these. */
2579   }
2580 }
2581
2582 static int subgraph_counter = 0;
2583 void dump_subgraph (ir_node *root, int depth, const char *suffix) {
2584   FILE *F;
2585   char buf[32];
2586   pset *mark_set = pset_new_ptr(1);
2587   sprintf(buf, "-subg_%03d", subgraph_counter++);
2588   F = vcg_open(get_irn_irg(root), suffix, buf);
2589   dump_vcg_header(F, get_irg_dump_name(get_irn_irg(root)), NULL);
2590   descend_and_dump(F, root, depth, mark_set);
2591   vcg_close(F);
2592   del_pset(mark_set);
2593 }
2594
2595
2596 static int weight_overall(int rec, int loop) {
2597   return 2*rec + loop;
2598 }
2599
2600 static int compute_color (int my, int max) {
2601   int color;
2602   if (!max) {
2603     color = 0;
2604   } else {
2605     int step;
2606
2607     /* if small, scale to the full color range. */
2608     if (max < n_colors)
2609       my = my * (n_colors/max);
2610
2611     step = 1 + (max / n_colors);
2612
2613     color = my/step;
2614   }
2615   return base_color + n_colors - color;
2616 }
2617
2618 static int get_entity_color(entity *ent) {
2619   ir_graph *irg = get_entity_irg(ent);
2620   assert(irg);
2621
2622   {
2623     int rec_depth     = get_irg_recursion_depth(irg);
2624     int loop_depth    = get_irg_loop_depth(irg);
2625     int overall_depth = weight_overall(rec_depth, loop_depth);
2626
2627     int max_rec_depth     = irp->max_callgraph_recursion_depth;
2628     int max_loop_depth    = irp->max_callgraph_loop_depth;
2629     int max_overall_depth = weight_overall(max_rec_depth, max_loop_depth);
2630
2631     /* int my_rec_color     = compute_color(rec_depth, max_rec_depth); */
2632     /* int my_loop_color    = compute_color(loop_depth, max_loop_depth); */
2633     int my_overall_color = compute_color(overall_depth, max_overall_depth);;
2634
2635     return my_overall_color;
2636   }
2637 }
2638
2639 void dump_callgraph(const char *suffix) {
2640   FILE *F;
2641   int i;
2642   int rem = edge_label;
2643   edge_label = 1;
2644   //ident *prefix = new_id_from_str("java/");
2645
2646   F = vcg_open_name("Callgraph", suffix);
2647   dump_vcg_header(F, "Callgraph", NULL);
2648
2649   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2650     ir_graph *irg = get_irp_irg(i);
2651     entity *ent = get_irg_entity(irg);
2652     int j, n_callees = get_irg_n_callees(irg);
2653
2654     /* Do not dump runtime system. */
2655     //if (id_is_prefix(prefix, get_entity_ld_ident(ent))) continue;
2656
2657     dump_entity_node(F, ent, get_entity_color(ent));
2658     for (j = 0; j < n_callees; ++j) {
2659       entity *c = get_irg_entity(get_irg_callee(irg, j));
2660       //if (id_is_prefix(prefix, get_entity_ld_ident(c))) continue;
2661       int be = is_irg_callee_backedge(irg, j);
2662       char *attr;
2663       attr = (be) ?
2664         "label:\"recursion %d\" color:%d" :
2665         "label:\"calls %d\" color:%d";
2666       print_ent_ent_edge(F, ent, c, be, attr, get_irg_callee_loop_depth(irg, j), get_entity_color(ent));
2667     }
2668   }
2669
2670   edge_label = rem;
2671   vcg_close(F);
2672 }
2673
2674 /* Dump all irgs in interprocedural view to a single file. */
2675 void dump_all_cg_block_graph(const char *suffix) {
2676   FILE *f;
2677   int i;
2678   int rem_view = get_interprocedural_view();
2679   set_interprocedural_view(1);
2680
2681   f = vcg_open_name("All_graphs", suffix);
2682   dump_vcg_header(f, "All_graphs", NULL);
2683
2684   /* collect nodes in all irgs reachable in call graph*/
2685   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
2686     ird_set_irg_link(get_irp_irg(i), NULL);
2687
2688   cg_walk(clear_link, collect_node, NULL);
2689
2690   /* dump all graphs */
2691   for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2692     current_ir_graph = get_irp_irg(i);
2693     assert(ird_get_irg_link(current_ir_graph));
2694     dump_graph_from_list(f, current_ir_graph);
2695     DEL_ARR_F(ird_get_irg_link(current_ir_graph));
2696   }
2697
2698   vcg_close(f);
2699   set_interprocedural_view(rem_view);
2700 }
2701
2702 /*---------------------------------------------------------------------*/
2703 /* the following routines dumps type information without any ir nodes. */
2704 /*---------------------------------------------------------------------*/
2705
2706 void
2707 dump_type_graph (ir_graph *irg, const char *suffix)
2708 {
2709   FILE *f;
2710   ir_graph *rem;
2711   rem = current_ir_graph;
2712
2713   /* if a filter is set, dump only the irg's that match the filter */
2714   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg)))) return;
2715
2716   current_ir_graph = irg;
2717
2718   f = vcg_open(irg, suffix, "-type");
2719   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
2720
2721   /* walk over the blocks in the graph */
2722   type_walk_irg(irg, dump_type_info, NULL, f);
2723   /* The walker for the const code can be called several times for the
2724      same (sub) expression.  So that no nodes are dumped several times
2725      we decrease the visited flag of the corresponding graph after each
2726      walk.  So now increase it finally. */
2727   inc_irg_visited(get_const_code_irg());
2728
2729   vcg_close(f);
2730   current_ir_graph = rem;
2731 }
2732
2733 void
2734 dump_all_types (const char *suffix)
2735 {
2736   FILE *f = vcg_open_name("All_types", suffix);
2737   dump_vcg_header(f, "All_types", NULL);
2738   type_walk(dump_type_info, NULL, f);
2739   inc_irg_visited(get_const_code_irg());
2740   vcg_close(f);
2741 }
2742
2743 void
2744 dump_class_hierarchy (int entities, const char *suffix)
2745 {
2746   FILE *f = vcg_open_name("class_hierarchy", suffix);
2747   h_env_t env;
2748
2749   env.f        = f;
2750   env.dump_ent = entities;
2751   dump_vcg_header(f, "class_hierarchy", NULL);
2752   type_walk(dump_class_hierarchy_node, NULL, &env);
2753   vcg_close(f);
2754 }
2755
2756 /*---------------------------------------------------------------------*/
2757 /* dumps all graphs with the graph-dumper passed. Possible dumpers:    */
2758 /*  dump_ir_graph                                                      */
2759 /*  dump_ir_block_graph                                                */
2760 /*  dump_cfg                                                           */
2761 /*  dump_type_graph                                                    */
2762 /*  dump_ir_graph_w_types                                              */
2763 /*---------------------------------------------------------------------*/
2764
2765 void dump_all_ir_graphs(dump_graph_func *dmp_grph, const char *suffix) {
2766   int i;
2767   for (i = get_irp_n_irgs() - 1; i >= 0; --i)
2768     dmp_grph(get_irp_irg(i), suffix);
2769 }
2770
2771
2772 /*--------------------------------------------------------------------------------*
2773  * Dumps a stand alone loop graph with firm nodes which belong to one loop node   *
2774  * packed together in one subgraph/box                                            *
2775  *--------------------------------------------------------------------------------*/
2776
2777 void dump_loops_standalone(FILE *F, ir_loop *loop) {
2778   int i = 0, loop_node_started = 0, son_number = 0, first = 0;
2779   loop_element le;
2780   ir_loop *son = NULL;
2781
2782   /* Dump a new loop node. */
2783   dump_loop_node(F, loop);
2784
2785   /* Dump the loop elements. */
2786
2787   for(i = 0; i < get_loop_n_elements(loop); i++) {
2788     le = get_loop_element(loop, i);
2789     son = le.son;
2790     if (get_kind(son) == k_ir_loop) {
2791
2792       /* We are a loop son -> Recurse */
2793
2794       if(loop_node_started) { /* Close the "firm-nodes" node first if we started one. */
2795         fprintf(F, "\" }\n");
2796         fprintf (F, "edge: {sourcename: \"");
2797         PRINT_LOOPID(loop);
2798         fprintf (F, "\" targetname: \"");
2799         PRINT_LOOPID(loop);
2800         fprintf (F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
2801         loop_node_started = 0;
2802       }
2803       dump_loop_son_edge(F, loop, son_number++);
2804       dump_loops_standalone(F, son);
2805     } else if (get_kind(son) == k_ir_node) {
2806       /* We are a loop node -> Collect firm nodes */
2807
2808       ir_node *n = le.node;
2809       int bad = 0;
2810
2811       if (!loop_node_started) {
2812         /* Start a new node which contains all firm nodes of the current loop */
2813         fprintf (F, "node: { title: \"");
2814         PRINT_LOOPID(loop);
2815         fprintf (F, "-%d-nodes\" color: lightyellow label: \"", i);
2816         loop_node_started = 1;
2817         first = i;
2818       }
2819       else
2820         fprintf(F, "\n");
2821
2822       bad |= dump_node_label(F, n);
2823       /* Causes indeterministic output: if (is_Block(n)) fprintf (F, "\t ->%d", (int)get_irn_link(n)); */
2824       if (has_backedges(n)) fprintf(F, "\t loop head!");
2825     } else { /* for callgraph loop tree */
2826       ir_graph *n;
2827       assert(get_kind(son) == k_ir_graph);
2828
2829       /* We are a loop node -> Collect firm graphs */
2830       n = (ir_graph *)le.node;
2831       if (!loop_node_started) {
2832         /* Start a new node which contains all firm nodes of the current loop */
2833         fprintf (F, "node: { title: \"");
2834         PRINT_LOOPID(loop);
2835         fprintf (F, "-%d-nodes\" color: lightyellow label: \"", i);
2836         loop_node_started = 1;
2837         first = i;
2838       }
2839       else
2840         fprintf(F, "\n");
2841       fprintf (F, " %s", get_irg_dump_name(n));
2842       /* fprintf (F, " %s (depth %d)", get_irg_dump_name(n), n->callgraph_weighted_loop_depth); */
2843     }
2844   }
2845
2846   if (loop_node_started) {
2847     fprintf(F, "\" }\n");
2848     fprintf (F, "edge: {sourcename: \"");
2849     PRINT_LOOPID(loop);
2850     fprintf (F, "\" targetname: \"");
2851     PRINT_LOOPID(loop);
2852     fprintf (F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
2853     loop_node_started = 0;
2854   }
2855 }
2856
2857 void dump_loop_tree(ir_graph *irg, const char *suffix)
2858 {
2859   FILE *f;
2860   ir_graph *rem = current_ir_graph;
2861   int el_rem = edge_label;
2862   edge_label = 1;
2863
2864   /* if a filter is set, dump only the irg's that match the filter */
2865   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg)))) return;
2866
2867   current_ir_graph = irg;
2868
2869   f = vcg_open(irg, suffix, "-looptree");
2870   dump_vcg_header(f, get_irg_dump_name(irg), "top_to_bottom");
2871
2872   if (get_irg_loop(irg)) dump_loops_standalone(f, get_irg_loop(irg));
2873
2874   vcg_close(f);
2875
2876   edge_label = el_rem;
2877   current_ir_graph = rem;
2878 }
2879
2880 void dump_callgraph_loop_tree(const char *suffix) {
2881   FILE *F;
2882   F = vcg_open_name("Callgraph_looptree", suffix);
2883   dump_vcg_header(F, "callgraph looptree", "top_to_bottom");
2884   dump_loops_standalone(F, irp->outermost_cg_loop);
2885   vcg_close(F);
2886 }
2887
2888
2889 /*-----------------------------------------------------------------------------*/
2890 /* Dumps the firm nodes in the loop tree to a graph along with the loop nodes. */
2891 /*-----------------------------------------------------------------------------*/
2892
2893 void collect_nodeloop(FILE *F, ir_loop *loop, eset *loopnodes) {
2894   int i, son_number = 0, node_number = 0;
2895
2896   if (dump_loop_information_flag) dump_loop_node(F, loop);
2897
2898   for (i = 0; i < get_loop_n_elements(loop); i++) {
2899     loop_element le = get_loop_element(loop, i);
2900     if (*(le.kind) == k_ir_loop) {
2901       if (dump_loop_information_flag) dump_loop_son_edge(F, loop, son_number++);
2902       /* Recur */
2903       collect_nodeloop(F, le.son, loopnodes);
2904     } else {
2905       if (dump_loop_information_flag) dump_loop_node_edge(F, loop, node_number++);
2906       eset_insert(loopnodes, le.node);
2907     }
2908   }
2909 }
2910
2911 void collect_nodeloop_external_nodes(ir_loop *loop, eset *loopnodes, eset *extnodes) {
2912   int i, j, start;
2913
2914   for(i = 0; i < get_loop_n_elements(loop); i++) {
2915     loop_element le = get_loop_element(loop, i);
2916     if (*(le.kind) == k_ir_loop) {
2917       /* Recur */
2918       collect_nodeloop_external_nodes(le.son, loopnodes, extnodes);
2919     } else {
2920       if (is_Block(le.node)) start = 0; else start = -1;
2921       for (j = start; j < get_irn_arity(le.node); j++) {
2922         ir_node *pred = get_irn_n(le.node, j);
2923         if (!eset_contains(loopnodes, pred)) {
2924           eset_insert(extnodes, pred);
2925           if (!is_Block(pred)) {
2926             pred = get_nodes_block(pred);
2927             if (!eset_contains(loopnodes, pred)) eset_insert(extnodes, pred);
2928           }
2929         }
2930       }
2931     }
2932   }
2933 }
2934
2935 void dump_loop(ir_loop *l, const char *suffix) {
2936   FILE *F;
2937   char name[50];
2938   eset *loopnodes = eset_create();
2939   eset *extnodes = eset_create();
2940   ir_node *n, *b;
2941
2942   snprintf(name, sizeof(name), "loop_%d", get_loop_loop_nr(l));
2943   F = vcg_open_name (name, suffix);
2944   dump_vcg_header(F, name, NULL);
2945
2946   /* collect all nodes to dump */
2947   collect_nodeloop(F, l, loopnodes);
2948   collect_nodeloop_external_nodes(l, loopnodes, extnodes);
2949
2950   /* build block lists */
2951   for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes))
2952     set_irn_link(n, NULL);
2953   for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes))
2954     set_irn_link(n, NULL);
2955   for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes))
2956     if (!is_Block(n)) {
2957       b = get_nodes_block(n);
2958       set_irn_link(n, get_irn_link(b));
2959       set_irn_link(b, n);
2960     }
2961   for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes))
2962     if (!is_Block(n)) {
2963       b = get_nodes_block(n);
2964       set_irn_link(n, get_irn_link(b));
2965       set_irn_link(b, n);
2966     }
2967
2968   for (b = eset_first(loopnodes); b != NULL; b = eset_next(loopnodes))
2969     if (is_Block(b)) {
2970       fprintf(F, "graph: { title: \"");
2971       PRINT_NODEID(b);
2972       fprintf(F, "\"  label: \"");
2973       dump_node_opcode(F, b);
2974       fprintf (F, " %ld", get_irn_node_nr(b));
2975       fprintf(F, "\" status:clustered color:yellow\n");
2976
2977       /* dump the blocks edges */
2978       dump_ir_data_edges(F, b);
2979
2980       /* dump the nodes that go into the block */
2981       for (n = get_irn_link(b); n; n = get_irn_link(n)) {
2982         if (eset_contains(extnodes, n))
2983           overrule_nodecolor = ird_color_lightblue;
2984         dump_node(F, n);
2985         overrule_nodecolor = ird_color_default;
2986         if (!eset_contains(extnodes, n)) dump_ir_data_edges(F, n);
2987       }
2988
2989       /* Close the vcg information for the block */
2990       fprintf(F, "}\n");
2991       dump_const_node_local(F, b);
2992       fprintf(F, "\n");
2993     }
2994   for (b = eset_first(extnodes); b != NULL; b = eset_next(extnodes))
2995     if (is_Block(b)) {
2996       fprintf(F, "graph: { title: \"");
2997       PRINT_NODEID(b);
2998       fprintf(F, "\"  label: \"");
2999       dump_node_opcode(F, b);
3000       fprintf (F, " %ld", get_irn_node_nr(b));
3001       fprintf(F, "\" status:clustered color:lightblue\n");
3002
3003       /* dump the nodes that go into the block */
3004       for (n = get_irn_link(b); n; n = get_irn_link(n)) {
3005         if (!eset_contains(loopnodes, n))
3006           overrule_nodecolor = ird_color_lightblue;
3007         dump_node(F, n);
3008         overrule_nodecolor = ird_color_default;
3009         if (eset_contains(loopnodes, n)) dump_ir_data_edges(F, n);
3010       }
3011
3012       /* Close the vcg information for the block */
3013       fprintf(F, "}\n");
3014       dump_const_node_local(F, b);
3015       fprintf(F, "\n");
3016     }
3017
3018   eset_destroy(loopnodes);
3019   eset_destroy(extnodes);
3020   vcg_close(F);
3021 }