25e011c1eb3c3aad4d357c2f22a34114f45a1794
[libfirm] / ir / ir / irdump.c
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Write vcg representation of firm to file.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Hubert Schmidt,
24  *          Matthias Braun
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdbool.h>
32 #include <errno.h>
33
34 #include "list.h"
35 #include "pset.h"
36
37 #include "irnode_t.h"
38 #include "irgraph_t.h"
39 #include "irprog_t.h"
40 #include "entity_t.h"
41 #include "irop.h"
42
43 #include "irdump_t.h"
44 #include "irpass_t.h"
45
46 #include "irgwalk.h"
47 #include "tv_t.h"
48 #include "irouts.h"
49 #include "iredges.h"
50 #include "irdom.h"
51 #include "irloop_t.h"
52 #include "callgraph.h"
53 #include "irhooks.h"
54 #include "dbginfo_t.h"
55 #include "irtools.h"
56 #include "irprintf.h"
57
58 #include "irverify.h"
59
60 #include "error.h"
61 #include "array.h"
62 #include "pmap.h"
63 #include "obst.h"
64 #include "pset.h"
65 #include "util.h"
66
67 typedef struct pns_lookup {
68         long       nr;      /**< the proj number */
69         const char *name;   /**< the name of the Proj */
70 } pns_lookup_t;
71
72 typedef struct proj_lookup {
73         unsigned           code;      /**< the opcode of the Proj predecessor */
74         unsigned           num_data;  /**< number of data entries */
75         const pns_lookup_t *data;     /**< the data */
76 } proj_lookup_t;
77
78 #include "gen_irdump.c.inl"
79
80 /** Dump only irgs with names that start with this prefix. */
81 static ident *dump_file_filter_id = NULL;
82
83 static ir_dump_flags_t flags =
84         ir_dump_flag_blocks_as_subgraphs |
85         ir_dump_flag_keepalive_edges |
86         ir_dump_flag_ld_names |
87         ir_dump_flag_back_edges |
88         ir_dump_flag_consts_local |
89         ir_dump_flag_analysed_types |
90         ir_dump_flag_entities_in_hierarchy |
91         ir_dump_flag_number_label;
92
93 static ird_color_t overrule_nodecolor = ird_color_default_node;
94
95 /** The vcg node attribute hook. */
96 static dump_node_vcgattr_func dump_node_vcgattr_hook = NULL;
97 /** The vcg edge attribute hook. */
98 static dump_edge_vcgattr_func dump_edge_vcgattr_hook = NULL;
99 /** The vcg dump block edge hook */
100 static dump_node_edge_func dump_block_edge_hook = NULL;
101 /** The vcg dump node edge hook. */
102 static dump_node_edge_func dump_node_edge_hook = NULL;
103
104 void set_dump_node_edge_hook(dump_node_edge_func func)
105 {
106         dump_node_edge_hook = func;
107 }
108
109 dump_node_edge_func get_dump_node_edge_hook(void)
110 {
111         return dump_node_edge_hook;
112 }
113
114 void set_dump_block_edge_hook(dump_node_edge_func func)
115 {
116         dump_block_edge_hook = func;
117 }
118
119 dump_node_edge_func get_dump_block_edge_hook(void)
120 {
121         return dump_node_edge_hook;
122 }
123
124 void set_dump_node_vcgattr_hook(dump_node_vcgattr_func hook)
125 {
126         dump_node_vcgattr_hook = hook;
127 }
128
129 void set_dump_edge_vcgattr_hook(dump_edge_vcgattr_func hook)
130 {
131         dump_edge_vcgattr_hook = hook;
132 }
133
134 void ir_set_dump_flags(ir_dump_flags_t new_flags)
135 {
136         flags = new_flags;
137 }
138
139 void ir_add_dump_flags(ir_dump_flags_t new_flags)
140 {
141         flags |= new_flags;
142 }
143
144 void ir_remove_dump_flags(ir_dump_flags_t to_remove)
145 {
146         flags &= ~to_remove;
147 }
148
149 ir_dump_flags_t ir_get_dump_flags(void)
150 {
151         return flags;
152 }
153
154 /** Returns 0 if dump_out_edge_flag or dump_loop_information_flag
155  * are set, else returns dump_const_local_flag.
156  */
157 static bool get_opt_dump_const_local(void)
158 {
159         return (flags & ir_dump_flag_out_edges)
160                 || (flags & ir_dump_flag_loops)
161                 || (flags & ir_dump_flag_consts_local)
162                 || (flags & ir_dump_flag_iredges);
163 }
164
165 static char *dump_filter;
166
167 void ir_set_dump_filter(const char *new_filter)
168 {
169         xfree(dump_filter);
170         dump_filter = xstrdup(new_filter);
171 }
172
173 const char *ir_get_dump_filter(void)
174 {
175         return dump_filter;
176 }
177
178 int ir_should_dump(const char *name)
179 {
180         const char *f, *n;
181
182         if (dump_filter == NULL || dump_filter[0] == '\0')
183                 return 1;
184
185         for (n = name, f = dump_filter; *f != '\0' && *n != '\0';
186                         ++n, ++f) {
187                 if (*n != *f)
188                         return 0;
189         }
190         return 1;
191 }
192
193 /* -------------- some extended helper functions ----------------- */
194
195 const char *get_mode_name_ex(const ir_mode *mode, int *bad)
196 {
197         if (is_mode(mode))
198                 return get_mode_name(mode);
199         if (bad != NULL)
200                 *bad |= 1;
201         return "<ERROR>";
202 }
203
204 #define CUSTOM_COLOR_BASE    100
205 static const char *color_names[ird_color_count];
206 static const char *color_rgb[ird_color_count];
207 static struct obstack color_obst;
208
209 /** define a custom color. */
210 static void custom_color(int num, const char *rgb_def)
211 {
212         assert(num < ird_color_count);
213         obstack_printf(&color_obst, "%d", CUSTOM_COLOR_BASE + num);
214         obstack_1grow(&color_obst, '\0');
215
216         color_rgb[num]   = rgb_def;
217         color_names[num] = (const char*)obstack_finish(&color_obst);
218 }
219
220 /** Define a named color. */
221 static void named_color(int num, const char *name)
222 {
223         assert(num < ird_color_count);
224         color_rgb[num]   = NULL;
225         color_names[num] = name;
226 }
227
228 /** Initializes the used colors. */
229 static void init_colors(void)
230 {
231         static bool initialized = 0;
232         if (initialized)
233                 return;
234
235         obstack_init(&color_obst);
236
237         custom_color(ird_color_prog_background,       "204 204 204");
238         custom_color(ird_color_block_background,      "255 255 0");
239         custom_color(ird_color_dead_block_background, "190 150 150");
240         named_color(ird_color_block_inout,            "lightblue");
241         named_color(ird_color_default_node,           "white");
242         custom_color(ird_color_memory,                "153 153 255");
243         custom_color(ird_color_controlflow,           "255 153 153");
244         custom_color(ird_color_const,                 "204 255 255");
245         custom_color(ird_color_proj,                  "255 255 153");
246         custom_color(ird_color_uses_memory,           "153 153 255");
247         custom_color(ird_color_phi,                   "105 255 105");
248         custom_color(ird_color_anchor,                "100 100 255");
249         named_color(ird_color_error,                  "red");
250         custom_color(ird_color_entity,                "204 204 255");
251
252         initialized = 1;
253 }
254
255 /**
256  * Prints the VCG color to a file.
257  */
258 static void print_vcg_color(FILE *out, ird_color_t color)
259 {
260         assert(color < ird_color_count);
261         fprintf(out, "color:%s", color_names[color]);
262 }
263
264 void print_nodeid(FILE *F, const ir_node *node)
265 {
266         fprintf(F, "\"n%ld\"", get_irn_node_nr(node));
267 }
268
269 void print_irgid(FILE *F, const ir_graph *irg)
270 {
271         fprintf(F, "\"g%ld\"", get_irg_graph_nr(irg));
272 }
273
274 void print_typeid(FILE *F, const ir_type *type)
275 {
276         fprintf(F, "\"t%ld\"", get_type_nr(type));
277 }
278
279 void print_entityid(FILE *F, const ir_entity *entity)
280 {
281         fprintf(F, "\"e%ld\"", get_entity_nr(entity));
282 }
283
284 static void print_itemid(FILE *F, const ir_type *tp, size_t itemnr)
285 {
286         fprintf(F, "\"i%ldT%zu\"", get_type_nr(tp), itemnr);
287 }
288
289 /**
290  * Prints the edge kind of a given IR node.
291  *
292  * Projs should be dumped near their predecessor, so they get "nearedge".
293  */
294 static void print_node_edge_kind(FILE *out, const ir_node *node)
295 {
296         if (is_Proj(node)) {
297                 fprintf(out, "nearedge: ");
298         } else {
299                 fprintf(out, "edge: ");
300         }
301 }
302
303 /**
304  * Prints the edge from a type S to a type T with additional info fmt, ...
305  * to the file F.
306  */
307 static void print_type_type_edge(FILE *F, const ir_type *S, const ir_type *T, const char *fmt, ...)
308 {
309         va_list ap;
310
311         va_start(ap, fmt);
312         fprintf(F, "edge: { sourcename: ");
313         print_typeid(F, S);
314         fprintf(F, " targetname: ");
315         print_typeid(F, T);
316         ir_vfprintf(F, fmt, ap);
317         fprintf(F,"}\n");
318         va_end(ap);
319 }
320
321 /**
322  * Prints the edge from a type tp to an entity ent with additional info fmt, ...
323  * to the file F.
324  */
325 static void print_type_ent_edge(FILE *F, const ir_type *tp, const ir_entity *ent, const char *fmt, ...)
326 {
327         va_list ap;
328
329         va_start(ap, fmt);
330         fprintf(F, "edge: { sourcename: ");
331         print_typeid(F, tp);
332         fprintf(F, " targetname: ");
333         print_entityid(F, ent);
334         ir_vfprintf(F, fmt, ap);
335         fprintf(F, "}\n");
336         va_end(ap);
337 }
338
339 /**
340  * Prints the edge from an entity ent1 to an entity ent2 with additional info fmt, ...
341  * to the file F.
342  */
343 static void print_ent_ent_edge(FILE *F, const ir_entity *ent1, const ir_entity *ent2, int backedge, ird_color_t color, const char *fmt, ...)
344 {
345         va_list ap;
346
347         va_start(ap, fmt);
348         if (backedge)
349                 fprintf(F, "backedge: { sourcename: ");
350         else
351                 fprintf(F, "edge: { sourcename: ");
352         print_entityid(F, ent1);
353         fprintf(F, " targetname: ");
354         print_entityid(F, ent2);
355         ir_vfprintf(F, fmt, ap);
356         fprintf(F, " ");
357         if (color != ird_color_none)
358                 print_vcg_color(F, color);
359         fprintf(F, "}\n");
360         va_end(ap);
361 }
362
363 /**
364  * Prints the edge from an entity ent to a type tp with additional info fmt, ...
365  * to the file F.
366  */
367 static void print_ent_type_edge(FILE *F, const ir_entity *ent, const ir_type *tp, const char *fmt, ...)
368 {
369         va_list ap;
370
371         va_start(ap, fmt);
372         fprintf(F, "edge: { sourcename: ");
373         print_entityid(F, ent);
374         fprintf(F, " targetname: ");
375         print_typeid(F, tp);
376         ir_vfprintf(F, fmt, ap);
377         fprintf(F,"}\n");
378         va_end(ap);
379 }
380
381 /**
382  * Prints the edge from a node irn to a type tp with additional info fmt, ...
383  * to the file F.
384  */
385 static void print_node_type_edge(FILE *F, const ir_node *irn, ir_type *tp, const char *fmt, ...)
386 {
387         va_list ap;
388
389         va_start(ap, fmt);
390         fprintf(F, "edge: { sourcename: ");
391         print_nodeid(F, irn);
392         fprintf(F, " targetname: ");
393         print_typeid(F, tp);
394         ir_vfprintf(F, fmt, ap);
395         fprintf(F,"}\n");
396         va_end(ap);
397 }
398
399 /**
400  * Prints the edge from a node irn to an entity ent with additional info fmt, ...
401  * to the file F.
402  */
403 static void print_node_ent_edge(FILE *F, const ir_node *irn, const ir_entity *ent, const char *fmt, ...)
404 {
405         va_list ap;
406
407         va_start(ap, fmt);
408         fprintf(F, "edge: { sourcename: ");
409         print_nodeid(F, irn);
410         fprintf(F, " targetname: ");
411         print_entityid(F, ent);
412         ir_vfprintf(F, fmt, ap);
413         fprintf(F,"}\n");
414         va_end(ap);
415 }
416
417 /**
418  * Prints the edge from a type tp to an enumeration item item with additional info fmt, ...
419  * to the file F.
420  */
421 static void print_enum_item_edge(FILE *F, const ir_type *tp, size_t item, const char *fmt, ...)
422 {
423         va_list ap;
424
425         va_start(ap, fmt);
426         fprintf(F, "edge: { sourcename: ");
427         print_typeid(F, tp);
428         fprintf(F, " targetname: ");
429         print_itemid(F, tp, item);
430         ir_vfprintf(F, fmt, ap);
431         fprintf(F,"}\n");
432         va_end(ap);
433 }
434
435 /*-----------------------------------------------------------------*/
436 /* global and ahead declarations                                   */
437 /*-----------------------------------------------------------------*/
438
439 static void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg);
440
441 /*-----------------------------------------------------------------*/
442 /* Helper functions.                                                */
443 /*-----------------------------------------------------------------*/
444
445 /**
446  * This map is used as a private link attr to be able to call dumper
447  * anywhere without destroying link fields.
448  */
449 static pmap *irdump_link_map = NULL;
450
451 /** NOT A STANDARD LIBFIRM INIT METHOD
452  *
453  * We do not want to integrate dumping into libfirm, i.e., if the dumpers
454  * are off, we want to have as few interferences as possible.  Therefore the
455  * initialization is performed lazily and not called from within init_firm.
456  *
457  * Creates the link attribute map. */
458 static void init_irdump(void)
459 {
460         /* We need a new, empty map. */
461         if (irdump_link_map) pmap_destroy(irdump_link_map);
462         irdump_link_map = pmap_create();
463         if (!dump_file_filter_id)
464                 dump_file_filter_id = new_id_from_str("");
465 }
466
467 /**
468  * Returns the private link field.
469  */
470 static void *ird_get_irn_link(const ir_node *n)
471 {
472         void *res = NULL;
473         if (irdump_link_map == NULL)
474                 return NULL;
475
476         if (pmap_contains(irdump_link_map, n))
477                 res = pmap_get(irdump_link_map, n);
478         return res;
479 }
480
481 /**
482  * Sets the private link field.
483  */
484 static void ird_set_irn_link(const ir_node *n, void *x)
485 {
486         if (irdump_link_map == NULL)
487                 init_irdump();
488         pmap_insert(irdump_link_map, n, x);
489 }
490
491 /**
492  * Gets the private link field of an irg.
493  */
494 static void *ird_get_irg_link(const ir_graph *irg)
495 {
496         void *res = NULL;
497         if (irdump_link_map == NULL)
498                 return NULL;
499
500         if (pmap_contains(irdump_link_map, irg))
501                 res = pmap_get(irdump_link_map, irg);
502         return res;
503 }
504
505 /**
506  * Sets the private link field of an irg.
507  */
508 static void ird_set_irg_link(const ir_graph *irg, void *x)
509 {
510         if (irdump_link_map == NULL)
511                 init_irdump();
512         pmap_insert(irdump_link_map, irg, x);
513 }
514
515 /**
516  * Walker, clears the private link field.
517  */
518 static void clear_link(ir_node *node, void *env)
519 {
520         (void) env;
521         ird_set_irn_link(node, NULL);
522 }
523
524 /**
525  * If the entity has a ld_name, returns it if the dump_ld_name is set,
526  * else returns the name of the entity.
527  */
528 static const char *get_ent_dump_name_(const ir_entity *ent, bool dump_ld_name)
529 {
530         if (ent == NULL)
531                 return "<NULL entity>";
532         if (dump_ld_name) {
533                 /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
534                 if (ent->ld_name != NULL)
535                         return get_id_str(ent->ld_name);
536         }
537         return get_id_str(ent->name);
538 }
539
540 const char *get_ent_dump_name(const ir_entity *ent)
541 {
542         return get_ent_dump_name_(ent, flags & ir_dump_flag_ld_names);
543 }
544
545 const char *get_irg_dump_name(const ir_graph *irg)
546 {
547         /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
548         return get_ent_dump_name_(get_irg_entity(irg), true);
549 }
550
551 /**
552  * Returns non-zero if a node is in floating state.
553  */
554 static int node_floats(const ir_node *n)
555 {
556         ir_graph *irg = get_irn_irg(n);
557         return ((get_irn_pinned(n) == op_pin_state_floats) &&
558                 (get_irg_pinned(irg) == op_pin_state_floats));
559 }
560
561 /**
562  *  Walker that visits the anchors
563  */
564 static void ird_walk_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
565 {
566         if ((flags & ir_dump_flag_all_anchors)
567                         || ((flags & ir_dump_flag_iredges) && edges_activated(irg))) {
568                 irg_walk_anchors(irg, pre, post, env);
569         } else {
570                 irg_walk_graph(irg, pre, post, env);
571         }
572 }
573
574 /**
575  * Walker, allocates an array for all blocks and puts their non-floating
576  * nodes into this array.
577  */
578 static void collect_node(ir_node *node, void *env)
579 {
580         (void) env;
581         if (is_Block(node)
582             || node_floats(node)
583             || (get_op_flags(get_irn_op(node)) & irop_flag_dump_noblock)) {
584                 ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
585                 if (!arr) arr = NEW_ARR_F(ir_node *, 0);
586                 ARR_APP1(ir_node *, arr, node);
587                 ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
588         } else {
589                 ir_node * block = get_nodes_block(node);
590
591                 if (is_Bad(block)) {
592                         /* this node is in a Bad block, so we must place it into the graph's list */
593                         ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
594                         if (!arr) arr = NEW_ARR_F(ir_node *, 0);
595                         ARR_APP1(ir_node *, arr, node);
596                         ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
597                 } else {
598                         ird_set_irn_link(node, ird_get_irn_link(block));
599                         ird_set_irn_link(block, node);
600                 }
601         }
602 }
603
604 /** Construct lists to walk ir block-wise.
605  *
606  * Collects all blocks, nodes not op_pin_state_pinned,
607  * Bad, NoMem and Unknown into a flexible array in link field of
608  * irg they belong to.  Sets the irg link field to NULL in all
609  * graphs not visited.
610  * Free the list with DEL_ARR_F().
611  */
612 static ir_node **construct_block_lists(ir_graph *irg)
613 {
614         size_t   i;
615         int      walk_flag = ir_resources_reserved(irg) & IR_RESOURCE_IRN_VISITED;
616
617         if (walk_flag) {
618                 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
619         }
620
621         for (i = get_irp_n_irgs(); i > 0;)
622                 ird_set_irg_link(get_irp_irg(--i), NULL);
623
624         ird_walk_graph(irg, clear_link, collect_node, irg);
625
626         if (walk_flag) {
627                 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
628         }
629
630         return (ir_node**)ird_get_irg_link(irg);
631 }
632
633 void dump_node_opcode(FILE *F, const ir_node *n)
634 {
635         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
636
637         /* call the dump_node operation if available */
638         if (ops->dump_node) {
639                 ops->dump_node(F, n, dump_node_opcode_txt);
640                 return;
641         }
642
643         /* implementation for default nodes */
644         switch (get_irn_opcode(n)) {
645         case iro_SymConst:
646                 switch (get_SymConst_kind(n)) {
647                 case symconst_addr_ent:
648                         fprintf(F, "SymC &%s", get_entity_name(get_SymConst_entity(n)));
649                         break;
650                 case symconst_ofs_ent:
651                         fprintf(F, "SymC %s offset", get_entity_name(get_SymConst_entity(n)));
652                         break;
653                 case symconst_type_size:
654                         ir_fprintf(F, "SymC %+F size", get_SymConst_type(n));
655                         break;
656                 case symconst_type_align:
657                         ir_fprintf(F, "SymC %+F align", get_SymConst_type(n));
658                         break;
659                 case symconst_enum_const:
660                         fprintf(F, "SymC %s enum", get_enumeration_const_name(get_SymConst_enum(n)));
661                         break;
662                 }
663                 break;
664
665         case iro_Load:
666                 if (get_Load_unaligned(n) == align_non_aligned)
667                         fprintf(F, "ua");
668                 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Load_mode(n), NULL));
669                 break;
670         case iro_Store:
671                 if (get_Store_unaligned(n) == align_non_aligned)
672                         fprintf(F, "ua");
673                 fprintf(F, "%s", get_irn_opname(n));
674                 break;
675         case iro_Block:
676                 if (n == get_irg_start_block(get_irn_irg(n)))
677                         fputs("Start ", F);
678                 if (n == get_irg_end_block(get_irn_irg(n)))
679                         fputs("End ", F);
680                 fprintf(F, "%s%s", get_irn_opname(n),
681                         (flags & ir_dump_flag_show_marks) ? (get_Block_mark(n) ? "*" : "") : "");
682                 break;
683         case iro_Conv:
684                 if (get_Conv_strict(n))
685                         fprintf(F, "strict");
686                 fprintf(F, "%s", get_irn_opname(n));
687                 break;
688         case iro_Div:
689                 fprintf(F, "%s", get_irn_opname(n));
690                 if (get_Div_no_remainder(n))
691                         fprintf(F, "RL");
692                 fprintf(F, "[%s]", get_mode_name_ex(get_Div_resmode(n), NULL));
693                 break;
694         case iro_Mod:
695                 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Mod_resmode(n), NULL));
696                 break;
697         case iro_Builtin:
698                 fprintf(F, "%s[%s]", get_irn_opname(n), get_builtin_kind_name(get_Builtin_kind(n)));
699                 break;
700
701         default:
702                 fprintf(F, "%s", get_irn_opname(n));
703         }
704 }
705
706 /**
707  * Dump the mode of a node n to a file F.
708  * Ignore modes that are "always known".
709  */
710 static void dump_node_mode(FILE *F, const ir_node *n)
711 {
712         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
713         unsigned         iro;
714         ir_mode         *mode;
715
716         /* call the dump_node operation if available */
717         if (ops->dump_node) {
718                 ops->dump_node(F, n, dump_node_mode_txt);
719                 return;
720         }
721
722         /* default implementation */
723         iro = get_irn_opcode(n);
724         switch (iro) {
725         case iro_SymConst:
726         case iro_Sel:
727         case iro_End:
728         case iro_Return:
729         case iro_Free:
730         case iro_Sync:
731         case iro_Jmp:
732         case iro_NoMem:
733                 break;
734         default:
735                 mode = get_irn_mode(n);
736
737                 if (mode != NULL && mode != mode_BB && mode != mode_ANY && mode != mode_BAD &&
738                         (mode != mode_T || iro == iro_Proj))
739                         fprintf(F, "%s", get_mode_name_ex(mode, NULL));
740         }
741 }
742
743 /**
744  * Dump the type of a node n to a file F if it's known.
745  */
746 static int dump_node_typeinfo(FILE *F, const ir_node *n)
747 {
748         ir_graph *irg = get_irn_irg(n);
749         int bad = 0;
750
751         if (ir_get_dump_flags() & ir_dump_flag_analysed_types) {
752                 if (get_irg_typeinfo_state(irg) == ir_typeinfo_consistent  ||
753                         get_irg_typeinfo_state(irg) == ir_typeinfo_inconsistent) {
754                         ir_type *tp = get_irn_typeinfo_type(n);
755                         if (tp != get_none_type()) {
756                                 ir_fprintf(F, "[%+F]", tp);
757                         } else {
758                                 fprintf(F, "[] ");
759                         }
760                 }
761         }
762         return bad;
763 }
764
765 /**
766  * Dump additional node attributes of some nodes to a file F.
767  */
768 static void dump_node_nodeattr(FILE *F, const ir_node *n)
769 {
770         ir_node *pred;
771         unsigned code;
772         long proj_nr;
773         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
774
775         /* call the dump_node operation if available */
776         if (ops->dump_node) {
777                 ops->dump_node(F, n, dump_node_nodeattr_txt);
778                 return;
779         }
780
781         switch (get_irn_opcode(n)) {
782         case iro_Const:
783                 ir_fprintf(F, "%T ", get_Const_tarval(n));
784                 break;
785
786         case iro_Proj:
787                 pred    = get_Proj_pred(n);
788                 proj_nr = get_Proj_proj(n);
789                 code    = get_irn_opcode(pred);
790
791                 if (code == iro_Proj && get_irn_opcode(get_Proj_pred(pred)) == iro_Start)
792                         fprintf(F, "Arg %ld ", proj_nr);
793                 else if (code == iro_Cond && get_irn_mode(get_Cond_selector(pred)) != mode_b)
794                         fprintf(F, "%ld ", proj_nr);
795                 else {
796                         unsigned i, j, f = 0;
797
798                         for (i = 0; i < ARRAY_SIZE(proj_lut); ++i) {
799                                 if (code == proj_lut[i].code) {
800                                         for (j = 0; j < proj_lut[i].num_data; ++j) {
801                                                 if (proj_nr == proj_lut[i].data[j].nr) {
802                                                         fprintf(F, "%s ", proj_lut[i].data[j].name);
803                                                         f = 1;
804                                                         break;
805                                                 }
806                                         }
807                                         break;
808                                 }
809                         }
810                         if (! f)
811                                 fprintf(F, "%ld ", proj_nr);
812                         if (code == iro_Cond && get_Cond_jmp_pred(pred) != COND_JMP_PRED_NONE) {
813                                 if (proj_nr == pn_Cond_false && get_Cond_jmp_pred(pred) == COND_JMP_PRED_FALSE)
814                                         fprintf(F, "PRED ");
815                                 if (proj_nr == pn_Cond_true && get_Cond_jmp_pred(pred) == COND_JMP_PRED_TRUE)
816                                         fprintf(F, "PRED ");
817                         }
818                 }
819                 break;
820         case iro_Sel:
821                 fprintf(F, "%s ", get_ent_dump_name(get_Sel_entity(n)));
822                 break;
823         case iro_Cast:
824                 ir_fprintf(F, "(%+F)", get_Cast_type(n));
825                 break;
826         case iro_Cmp:
827                 fprintf(F, "%s ", get_relation_string(get_Cmp_relation(n)));
828                 break;
829         case iro_Confirm:
830                 fprintf(F, "%s ", get_relation_string(get_Confirm_relation(n)));
831                 break;
832         case iro_CopyB:
833                 ir_fprintf(F, "(%+F)", get_CopyB_type(n));
834                 break;
835
836         default:
837                 break;
838         }
839 }
840
841 void dump_node_label(FILE *F, const ir_node *n)
842 {
843         dump_node_opcode(F, n);
844         fputs(" ", F);
845         dump_node_mode(F, n);
846         fprintf(F, " ");
847         dump_node_typeinfo(F, n);
848         dump_node_nodeattr(F, n);
849         if (flags & ir_dump_flag_number_label) {
850                 fprintf(F, "%ld", get_irn_node_nr(n));
851         }
852         if (flags & ir_dump_flag_idx_label) {
853                 fprintf(F, ":%u", get_irn_idx(n));
854         }
855 }
856
857 /**
858  * Dumps the attributes of a node n into the file F.
859  * Currently this is only the color of a node.
860  */
861 static void dump_node_vcgattr(FILE *F, const ir_node *node, const ir_node *local, bool bad)
862 {
863         ir_mode *mode;
864         const ir_node *n;
865
866         if (bad) {
867                 print_vcg_color(F, ird_color_error);
868                 return;
869         }
870
871         if (dump_node_vcgattr_hook != NULL) {
872                 dump_node_vcgattr_hook(F, node, local);
873                 return;
874         }
875
876         n = local ? local : node;
877
878         if (overrule_nodecolor != ird_color_default_node) {
879                 print_vcg_color(F, overrule_nodecolor);
880                 return;
881         }
882
883         mode = get_irn_mode(n);
884         if (mode == mode_M) {
885                 print_vcg_color(F, ird_color_memory);
886                 return;
887         }
888         if (mode == mode_X) {
889                 print_vcg_color(F, ird_color_controlflow);
890                 return;
891         }
892
893         switch (get_irn_opcode(n)) {
894         case iro_Start:
895         case iro_End:
896                 print_vcg_color(F, ird_color_anchor);
897                 break;
898         case iro_Bad:
899                 print_vcg_color(F, ird_color_error);
900                 break;
901         case iro_Block:
902                 print_vcg_color(F, ird_color_block_background);
903                 break;
904         case iro_Phi:
905                 print_vcg_color(F, ird_color_phi);
906                 break;
907         case iro_Pin:
908                 print_vcg_color(F, ird_color_memory);
909                 break;
910         case iro_SymConst:
911         case iro_Const:
912                 print_vcg_color(F, ird_color_const);
913                 break;
914         case iro_Proj:
915                 print_vcg_color(F, ird_color_proj);
916                 break;
917         default: {
918                 ir_op *op = get_irn_op(node);
919
920                 if (is_op_constlike(op)) {
921                         print_vcg_color(F, ird_color_const);
922                 } else if (is_op_uses_memory(op)) {
923                         print_vcg_color(F, ird_color_uses_memory);
924                 } else if (is_op_cfopcode(op) || is_op_forking(op)) {
925                         print_vcg_color(F, ird_color_controlflow);
926                 }
927         }
928         }
929 }
930
931 void *dump_add_node_info_callback(dump_node_info_cb_t *cb, void *data)
932 {
933         hook_entry_t *info = XMALLOCZ(hook_entry_t);
934
935         info->hook._hook_node_info = cb;
936         info->context              = data;
937         register_hook(hook_node_info, info);
938
939         return info;
940 }
941
942 void dump_remove_node_info_callback(void *handle)
943 {
944         hook_entry_t *info = (hook_entry_t*)handle;
945         unregister_hook(hook_node_info, info);
946         xfree(info);
947 }
948
949 /**
950  * Dump the node information of a node n to a file F.
951  */
952 static void dump_node_info(FILE *F, const ir_node *n)
953 {
954         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
955
956         fprintf(F, " info1: \"");
957         dump_irnode_to_file(F, n);
958         /* call the dump_node operation if available */
959         if (ops->dump_node)
960                 ops->dump_node(F, n, dump_node_info_txt);
961
962         /* allow additional info to be added */
963         hook_node_info(F, n);
964         fprintf(F, "\"\n");
965 }
966
967 static int is_constlike_node(const ir_node *node)
968 {
969         const ir_op *op = get_irn_op(node);
970         return is_op_constlike(op);
971 }
972
973 static void print_constid(FILE *F, const ir_node *user, const ir_node *node)
974 {
975         fprintf(F, "\"n%ldb%ld\"", get_irn_node_nr(user), get_irn_node_nr(node));
976 }
977
978 static void print_constblkid(FILE *F, const ir_node *node, const ir_node *block)
979 {
980         fprintf(F, "\"n%ldb%ld\"", get_irn_node_nr(node), get_irn_node_nr(block));
981 }
982
983
984 /** outputs the predecessors of n, that are constants, local.  I.e.,
985    generates a copy of the constant predecessors for each node called with. */
986 static void dump_const_node_local(FILE *F, const ir_node *n)
987 {
988         ir_graph *irg = get_irn_irg(n);
989         int i;
990         if (!get_opt_dump_const_local()) return;
991
992         /* Use visited flag to avoid outputting nodes twice.
993         initialize it first. */
994         for (i = 0; i < get_irn_arity(n); i++) {
995                 ir_node *con = get_irn_n(n, i);
996                 if (is_constlike_node(con)) {
997                         set_irn_visited(con, get_irg_visited(irg) - 1);
998                 }
999         }
1000
1001         for (i = 0; i < get_irn_arity(n); i++) {
1002                 ir_node *con = get_irn_n(n, i);
1003                 if (is_constlike_node(con) && !irn_visited_else_mark(con)) {
1004                         /* Generate a new name for the node by appending the names of
1005                         n and const. */
1006                         fprintf(F, "node: {title: ");
1007                         print_constid(F, n, con);
1008                         fprintf(F, " label: \"");
1009                         dump_node_label(F, con);
1010                         fprintf(F, "\" ");
1011                         dump_node_info(F, con);
1012                         dump_node_vcgattr(F, n, con, 0);
1013                         fprintf(F, "}\n");
1014                 }
1015         }
1016 }
1017
1018 /** If the block of an edge is a const_like node, dump it local with an edge */
1019 static void dump_const_block_local(FILE *F, const ir_node *n)
1020 {
1021         ir_node *blk;
1022
1023         if (!get_opt_dump_const_local()) return;
1024
1025         blk = get_nodes_block(n);
1026         if (is_constlike_node(blk)) {
1027                 /* Generate a new name for the node by appending the names of
1028                 n and blk. */
1029                 fprintf(F, "node: {title: ");
1030                 print_constblkid(F, n, blk);
1031                 fprintf(F, " label: \"");
1032                 dump_node_label(F, blk);
1033                 fprintf(F, "\" ");
1034                 dump_node_info(F, blk);
1035                 dump_node_vcgattr(F, n, blk, 0);
1036                 fprintf(F, "}\n");
1037
1038                 fprintf(F, "edge: { sourcename: ");
1039                 print_nodeid(F, n);
1040                 fprintf(F, " targetname: ");
1041                 print_constblkid(F, n, blk);
1042                 fprintf(F, " ");
1043
1044                 if (dump_edge_vcgattr_hook != NULL
1045                     && dump_edge_vcgattr_hook(F, n, -1)) {
1046                         fprintf(F, "}\n");
1047                         return;
1048                 }
1049
1050                 fprintf(F, BLOCK_EDGE_ATTR "}\n");
1051         }
1052 }
1053
1054 /**
1055  * prints the error message of a node to a file F as info2.
1056  */
1057 static void print_node_error(FILE *F, const char *err_msg)
1058 {
1059         if (! err_msg)
1060                 return;
1061
1062         fprintf(F, " info2: \"%s\"", err_msg);
1063 }
1064
1065 /**
1066  * prints debug messages of a node to file F as info3.
1067  */
1068 static void print_dbg_info(FILE *F, dbg_info *dbg)
1069 {
1070         char buf[1024];
1071
1072         ir_dbg_info_snprint(buf, sizeof(buf), dbg);
1073         if (buf[0] != 0) {
1074                 fprintf(F, " info3: \"%s\"\n", buf);
1075         }
1076 }
1077
1078 static void print_type_dbg_info(FILE *F, type_dbg_info *dbg)
1079 {
1080         (void) F;
1081         (void) dbg;
1082         /* TODO */
1083 }
1084
1085 /**
1086  * Dump a node
1087  */
1088 void dump_node(FILE *F, const ir_node *n)
1089 {
1090         int bad = 0;
1091         const char *p;
1092         ir_graph   *irg;
1093
1094         if (get_opt_dump_const_local() && is_constlike_node(n))
1095                 return;
1096
1097         /* dump this node */
1098         fputs("node: {title: ", F);
1099         print_nodeid(F, n);
1100
1101         fputs(" label: \"", F);
1102         irg = get_irn_irg(n);
1103         bad = ! irn_verify_irg_dump(n, irg, &p);
1104         dump_node_label(F, n);
1105         fputs("\" ", F);
1106
1107         dump_node_info(F, n);
1108         print_node_error(F, p);
1109         print_dbg_info(F, get_irn_dbg_info(n));
1110         dump_node_vcgattr(F, n, NULL, bad);
1111         fputs("}\n", F);
1112         dump_const_node_local(F, n);
1113
1114 }
1115
1116 /** dump the edge to the block this node belongs to */
1117 static void dump_ir_block_edge(FILE *F, const ir_node *n)
1118 {
1119         if (get_opt_dump_const_local() && is_constlike_node(n)) return;
1120         if (!is_Block(n)) {
1121                 ir_node *block = get_nodes_block(n);
1122
1123                 if (get_opt_dump_const_local() && is_constlike_node(block)) {
1124                         dump_const_block_local(F, n);
1125                 } else {
1126                         fprintf(F, "edge: { sourcename: ");
1127                         print_nodeid(F, n);
1128                         fprintf(F, " targetname: ");
1129                         print_nodeid(F, block);
1130                         fprintf(F, " ");
1131
1132                         if (dump_edge_vcgattr_hook && dump_edge_vcgattr_hook(F, n, -1)) {
1133                                 fprintf(F, "}\n");
1134                                 return;
1135                         }
1136
1137                         fprintf(F, BLOCK_EDGE_ATTR "}\n");
1138                 }
1139         }
1140 }
1141
1142 static void print_data_edge_vcgattr(FILE *F, const ir_node *from, int to)
1143 {
1144         /*
1145          * do not use get_nodes_block() here, will fail
1146          * if the irg is not pinned.
1147          */
1148         if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1149                 fprintf(F, INTRA_DATA_EDGE_ATTR);
1150         else
1151                 fprintf(F, INTER_DATA_EDGE_ATTR);
1152 }
1153
1154 static void print_mem_edge_vcgattr(FILE *F, const ir_node *from, int to)
1155 {
1156         /*
1157          * do not use get_nodes_block() here, will fail
1158          * if the irg is not pinned.
1159          */
1160         if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1161                 fprintf(F, INTRA_MEM_EDGE_ATTR);
1162         else
1163                 fprintf(F, INTER_MEM_EDGE_ATTR);
1164 }
1165
1166 /** Print the vcg attributes for the edge from node "from" to its "to"th input */
1167 static void print_edge_vcgattr(FILE *F, const ir_node *from, int to)
1168 {
1169         assert(from);
1170
1171         if (dump_edge_vcgattr_hook)
1172                 if (dump_edge_vcgattr_hook(F, from, to))
1173                         return;
1174
1175         if ((flags & ir_dump_flag_back_edges) && is_backedge(from, to))
1176                 fprintf(F, BACK_EDGE_ATTR);
1177
1178         switch (get_irn_opcode(from)) {
1179         case iro_Block:
1180                 fprintf(F, CF_EDGE_ATTR);
1181                 break;
1182         case iro_Start:  break;
1183         case iro_End:
1184                 if (to >= 0) {
1185                         if (get_irn_mode(get_End_keepalive(from, to)) == mode_BB)
1186                                 fprintf(F, KEEP_ALIVE_CF_EDGE_ATTR);
1187                         else
1188                                 fprintf(F, KEEP_ALIVE_DF_EDGE_ATTR);
1189                 }
1190                 break;
1191         default:
1192                 if (is_Proj(from)) {
1193                         if (get_irn_mode(from) == mode_M)
1194                                 print_mem_edge_vcgattr(F, from, to);
1195                         else if (get_irn_mode(from) == mode_X)
1196                                 fprintf(F, CF_EDGE_ATTR);
1197                         else
1198                                 print_data_edge_vcgattr(F, from, to);
1199                 }
1200                 else if (get_irn_mode(get_irn_n(from, to)) == mode_M)
1201                         print_mem_edge_vcgattr(F, from, to);
1202                 else if (get_irn_mode(get_irn_n(from, to)) == mode_X)
1203                         fprintf(F, CF_EDGE_ATTR);
1204                 else
1205                         print_data_edge_vcgattr(F, from, to);
1206         }
1207 }
1208
1209 /** dump edges to our inputs */
1210 static void dump_ir_data_edges(FILE *F, const ir_node *n)
1211 {
1212         int i, num;
1213
1214         if (dump_node_edge_hook)
1215                 dump_node_edge_hook(F, n);
1216
1217         if (!(flags & ir_dump_flag_keepalive_edges) && is_End(n)) {
1218                 /* the End node has only keep-alive edges */
1219                 return;
1220         }
1221
1222         /* dump the dependency edges. */
1223         num = get_irn_deps(n);
1224         for (i = 0; i < num; ++i) {
1225                 ir_node *dep = get_irn_dep(n, i);
1226
1227                 if (dep) {
1228                         print_node_edge_kind(F, n);
1229                         fprintf(F, "{sourcename: ");
1230                         print_nodeid(F, n);
1231                         fprintf(F, " targetname: ");
1232                         if ((get_opt_dump_const_local()) && is_constlike_node(dep)) {
1233                                 print_constid(F, n, dep);
1234                         } else {
1235                                 print_nodeid(F, dep);
1236                         }
1237                         fprintf(F, " label: \"%d\" ", i);
1238                         fprintf(F, " color: darkgreen}\n");
1239                 }
1240         }
1241
1242         num = get_irn_arity(n);
1243         for (i = 0; i < num; i++) {
1244                 ir_node *pred = get_irn_n(n, i);
1245                 assert(pred);
1246
1247                 if ((flags & ir_dump_flag_back_edges) && is_backedge(n, i)) {
1248                         fprintf(F, "backedge: {sourcename: ");
1249                 } else {
1250                         print_node_edge_kind(F, n);
1251                         fprintf(F, "{sourcename: ");
1252                 }
1253                 print_nodeid(F, n);
1254                 fprintf(F, " targetname: ");
1255                 if ((get_opt_dump_const_local()) && is_constlike_node(pred)) {
1256                         print_constid(F, n, pred);
1257                 } else {
1258                         print_nodeid(F, pred);
1259                 }
1260                 fprintf(F, " label: \"%d\" ", i);
1261                 print_edge_vcgattr(F, n, i);
1262                 fprintf(F, "}\n");
1263         }
1264 }
1265
1266 /**
1267  * Dump the ir_edges
1268  */
1269 static void dump_ir_edges(ir_node *node, void *env)
1270 {
1271         int              i = 0;
1272         FILE            *F = (FILE*)env;
1273         const ir_edge_t *edge;
1274
1275         foreach_out_edge(node, edge) {
1276                 ir_node *succ = get_edge_src_irn(edge);
1277
1278                 print_node_edge_kind(F, succ);
1279                 fprintf(F, "{sourcename: ");
1280                 print_nodeid(F, node);
1281                 fprintf(F, " targetname: ");
1282                 print_nodeid(F, succ);
1283
1284                 fprintf(F, " label: \"%d\" ", i);
1285                 fprintf(F, OUT_EDGE_ATTR);
1286                 fprintf(F, "}\n");
1287                 ++i;
1288         }
1289 }
1290
1291
1292 /** Dumps a node and its edges but not the block edge  */
1293 static void dump_node_wo_blockedge(FILE *F, const ir_node *n)
1294 {
1295         dump_node(F, n);
1296         dump_ir_data_edges(F, n);
1297 }
1298
1299 /** Dumps a node and its edges. */
1300 static void dump_node_with_edges(ir_node *n, void *env)
1301 {
1302         FILE *F = (FILE*)env;
1303         dump_node_wo_blockedge(F, n);
1304         if (!node_floats(n))
1305                 dump_ir_block_edge(F, n);
1306 }
1307
1308 /** Dumps a const-like node. */
1309 static void dump_const_node(ir_node *n, void *env)
1310 {
1311         FILE *F = (FILE*)env;
1312         if (is_Block(n)) return;
1313         dump_node_wo_blockedge(F, n);
1314 }
1315
1316 /***********************************************************************/
1317 /* the following routines dump the nodes/irgs bracketed to graphs.     */
1318 /***********************************************************************/
1319
1320 /** Dumps a constant expression as entity initializer, array bound ... */
1321 static void dump_const_expression(FILE *F, ir_node *value)
1322 {
1323         ir_graph *irg = get_const_code_irg();
1324         ir_dump_flags_t old_flags = ir_get_dump_flags();
1325         ir_remove_dump_flags(ir_dump_flag_consts_local);
1326
1327         irg_walk(value, dump_const_node, NULL, F);
1328         /* Decrease visited flag so that we walk with the same flag for the next
1329            expression.  This guarantees that we don't dump the same node twice,
1330            as for const expressions cse is performed to save memory. */
1331         set_irg_visited(irg, get_irg_visited(irg) -1);
1332
1333         ir_set_dump_flags(old_flags);
1334 }
1335
1336 /** Dump a block as graph containing its nodes.
1337  *
1338  *  Expects to find nodes belonging to the block as list in its
1339  *  link field.
1340  *  Dumps the edges of all nodes including itself. */
1341 static void dump_whole_block(FILE *F, const ir_node *block)
1342 {
1343         ir_node *node;
1344         ird_color_t color = ird_color_block_background;
1345
1346         assert(is_Block(block));
1347
1348         fprintf(F, "graph: { title: ");
1349         print_nodeid(F, block);
1350         fprintf(F, " label: \"");
1351         dump_node_label(F, block);
1352
1353         /* colorize blocks */
1354         if (! get_Block_matured(block))
1355                 color = ird_color_block_background;
1356
1357         fprintf(F, "\" status:clustered ");
1358         print_vcg_color(F, color);
1359         fprintf(F, "\n");
1360
1361         /* yComp can show attributes for blocks, XVCG parses but ignores them */
1362         dump_node_info(F, block);
1363         print_dbg_info(F, get_irn_dbg_info(block));
1364
1365         /* dump the blocks edges */
1366         dump_ir_data_edges(F, block);
1367
1368         if (dump_block_edge_hook)
1369                 dump_block_edge_hook(F, block);
1370
1371         /* dump the nodes that go into the block */
1372         for (node = (ir_node*)ird_get_irn_link(block); node; node = (ir_node*)ird_get_irn_link(node)) {
1373                 dump_node(F, node);
1374                 dump_ir_data_edges(F, node);
1375         }
1376
1377         /* Close the vcg information for the block */
1378         fprintf(F, "}\n");
1379         dump_const_node_local(F, block);
1380         fprintf(F, "\n");
1381 }
1382
1383 /** dumps a graph block-wise. Expects all blockless nodes in arr in irgs link.
1384  *  The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1385 static void dump_block_graph(FILE *F, ir_graph *irg)
1386 {
1387         size_t i, n;
1388         ir_node **arr = (ir_node**)ird_get_irg_link(irg);
1389
1390         for (i = 0, n = ARR_LEN(arr); i < n; ++i) {
1391                 ir_node *node = arr[i];
1392                 if (is_Block(node)) {
1393                 /* Dumps the block and all the nodes in the block, which are to
1394                         be found in Block->link. */
1395                         dump_whole_block(F, node);
1396                 } else {
1397                         /* Nodes that are not in a Block. */
1398                         dump_node(F, node);
1399                         if (!node_floats(node) && is_Bad(get_nodes_block(node))) {
1400                                 dump_const_block_local(F, node);
1401                         }
1402                         dump_ir_data_edges(F, node);
1403                 }
1404                 if ((flags & ir_dump_flag_iredges) && edges_activated(irg))
1405                         dump_ir_edges(node, F);
1406         }
1407
1408         if ((flags & ir_dump_flag_loops)
1409              && is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO))
1410                 dump_loop_nodes_into_graph(F, irg);
1411 }
1412
1413 /**
1414  * Dump the info for an irg.
1415  * Parsed by XVCG but not shown. use yComp.
1416  */
1417 static void dump_graph_info(FILE *F, ir_graph *irg)
1418 {
1419         fprintf(F, "info1: \"");
1420         dump_entity_to_file(F, get_irg_entity(irg));
1421         fprintf(F, "\n");
1422
1423         /* dump graph state */
1424         fprintf(F, "state:");
1425         if (is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP))
1426                 fprintf(F, " arch_dep");
1427         if (is_irg_state(irg, IR_GRAPH_STATE_MODEB_LOWERED))
1428                 fprintf(F, " modeb_lowered");
1429         if (is_irg_state(irg, IR_GRAPH_STATE_NORMALISATION2))
1430                 fprintf(F, " normalisation2");
1431         if (is_irg_state(irg, IR_GRAPH_STATE_OPTIMIZE_UNREACHABLE_CODE))
1432                 fprintf(F, " optimize_unreachable_code");
1433         if (is_irg_state(irg, IR_GRAPH_STATE_NO_CRITICAL_EDGES))
1434                 fprintf(F, " no_critical_edges");
1435         if (is_irg_state(irg, IR_GRAPH_STATE_NO_BADS))
1436                 fprintf(F, " no_bads");
1437         if (is_irg_state(irg, IR_GRAPH_STATE_NO_UNREACHABLE_CODE))
1438                 fprintf(F, " no_unreachable_code");
1439         if (is_irg_state(irg, IR_GRAPH_STATE_ONE_RETURN))
1440                 fprintf(F, " one_return");
1441         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE))
1442                 fprintf(F, " consistent_dominance");
1443         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE))
1444                 fprintf(F, " consistent_postdominance");
1445         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUT_EDGES))
1446                 fprintf(F, " consistent_out_edges");
1447         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS))
1448                 fprintf(F, " consistent_outs");
1449         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO))
1450                 fprintf(F, " consistent_loopinfo");
1451         if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE))
1452                 fprintf(F, " consistent_entity_usage");
1453         fprintf(F, "\"\n");
1454 }
1455
1456 /** Dumps an irg as a graph clustered by block nodes.
1457  *  If interprocedural view edges can point to nodes out of this graph.
1458  */
1459 static void dump_graph_from_list(FILE *F, ir_graph *irg)
1460 {
1461         ir_entity *ent = get_irg_entity(irg);
1462
1463         fprintf(F, "graph: { title: ");
1464         print_irgid(F, irg);
1465         fprintf(F, " label: \"%s\" status:clustered color:%s\n",
1466           get_ent_dump_name(ent), color_names[ird_color_prog_background]);
1467
1468         dump_graph_info(F, irg);
1469         print_dbg_info(F, get_entity_dbg_info(ent));
1470
1471         dump_block_graph(F, irg);
1472
1473         /* Close the vcg information for the irg */
1474         fprintf(F, "}\n\n");
1475 }
1476
1477 /*******************************************************************/
1478 /* Basic type and entity nodes and edges.                          */
1479 /*******************************************************************/
1480
1481 /** dumps the edges between nodes and their type or entity attributes. */
1482 static void dump_node2type_edges(ir_node *n, void *env)
1483 {
1484         FILE *F = (FILE*)env;
1485         assert(n);
1486
1487         switch (get_irn_opcode(n)) {
1488         case iro_Const :
1489                 /* @@@ some consts have an entity */
1490                 break;
1491         case iro_SymConst:
1492                 if (SYMCONST_HAS_TYPE(get_SymConst_kind(n)))
1493                         print_node_type_edge(F,n,get_SymConst_type(n),NODE2TYPE_EDGE_ATTR);
1494                 break;
1495         case iro_Sel:
1496                 print_node_ent_edge(F,n,get_Sel_entity(n),NODE2TYPE_EDGE_ATTR);
1497                 break;
1498         case iro_Call:
1499                 print_node_type_edge(F,n,get_Call_type(n),NODE2TYPE_EDGE_ATTR);
1500                 break;
1501         case iro_Alloc:
1502                 print_node_type_edge(F,n,get_Alloc_type(n),NODE2TYPE_EDGE_ATTR);
1503                 break;
1504         case iro_Free:
1505                 print_node_type_edge(F,n,get_Free_type(n),NODE2TYPE_EDGE_ATTR);
1506                 break;
1507         case iro_Cast:
1508                 print_node_type_edge(F,n,get_Cast_type(n),NODE2TYPE_EDGE_ATTR);
1509                 break;
1510         default:
1511                 break;
1512         }
1513 }
1514
1515 static void print_typespecific_vcgattr(FILE *F, ir_type *tp)
1516 {
1517         switch (get_type_tpop_code(tp)) {
1518         case tpo_class:
1519                 fprintf(F, " " TYPE_CLASS_NODE_ATTR);
1520                 break;
1521         case tpo_struct:
1522                 fprintf(F, " " TYPE_METH_NODE_ATTR);
1523                 break;
1524         case tpo_method:
1525                 break;
1526         case tpo_union:
1527                 break;
1528         case tpo_array:
1529                 break;
1530         case tpo_enumeration:
1531                 break;
1532         case tpo_pointer:
1533                 break;
1534         case tpo_primitive:
1535                 break;
1536         default:
1537                 break;
1538         }
1539 }
1540
1541 void dump_type_node(FILE *F, ir_type *tp)
1542 {
1543         fprintf(F, "node: {title: ");
1544         print_typeid(F, tp);
1545         fprintf(F, " label: \"");
1546         if (tp->dbi != NULL) {
1547                 char buf[1024];
1548                 ir_print_type(buf, sizeof(buf), tp);
1549                 fprintf(F, "%s '%s'", get_type_tpop_name(tp), buf);
1550         } else {
1551                 ir_fprintf(F, "%+F", tp);
1552         }
1553         fputs("\" info1: \"", F);
1554         dump_type_to_file(F, tp);
1555         fprintf(F, "\"\n");
1556         print_type_dbg_info(F, get_type_dbg_info(tp));
1557         print_typespecific_vcgattr(F, tp);
1558         fprintf(F, "}\n");
1559 }
1560
1561 static void dump_entity_node(FILE *F, ir_entity *ent)
1562 {
1563         fprintf(F, "node: {title: ");
1564         print_entityid(F, ent);
1565         fprintf(F, " label: ");
1566         fprintf(F, "\"%s\" ", get_ent_dump_name(ent));
1567
1568         print_vcg_color(F, ird_color_entity);
1569         fprintf(F, "\n info1: \"");
1570
1571         dump_entity_to_file(F, ent);
1572
1573         fprintf(F, "\"\n");
1574         print_dbg_info(F, get_entity_dbg_info(ent));
1575         fprintf(F, "}\n");
1576 }
1577
1578 static void dump_enum_item(FILE *F, ir_type *tp, size_t pos)
1579 {
1580         char buf[1024];
1581         ir_enum_const *ec = get_enumeration_const(tp, pos);
1582         ident         *id = get_enumeration_const_nameid(ec);
1583         ir_tarval     *tv = get_enumeration_value(ec);
1584
1585         if (tv)
1586                 tarval_snprintf(buf, sizeof(buf), tv);
1587         else
1588                 strncpy(buf, "<not set>", sizeof(buf));
1589         fprintf(F, "node: {title: ");
1590         print_itemid(F, tp, pos);
1591         fprintf(F, " label: ");
1592         fprintf(F, "\"enum item %s\" " ENUM_ITEM_NODE_ATTR, get_id_str(id));
1593         fprintf(F, "\n info1: \"value: %s\"}\n", buf);
1594 }
1595
1596 /**
1597  * Dumps a new style initializer.
1598  */
1599 static void dump_entity_initializer(FILE *F, const ir_entity *ent)
1600 {
1601         /* TODO */
1602         (void) F;
1603         (void) ent;
1604 }
1605
1606 /**
1607  * type-walker: Dumps a type or entity and its edges.
1608  */
1609 static void dump_type_info(type_or_ent tore, void *env)
1610 {
1611         FILE *F = (FILE*)env;
1612         size_t i = 0;  /* to shutup gcc */
1613
1614         /* dump this type or entity */
1615
1616         switch (get_kind(tore.ent)) {
1617         case k_entity: {
1618                 ir_entity *ent = tore.ent;
1619                 /* The node */
1620                 dump_entity_node(F, ent);
1621                 /* The Edges */
1622                 /* skip this to reduce graph.  Member edge of type is parallel to this edge. *
1623                 fprintf(F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
1624                 ENT_OWN_EDGE_ATTR "}\n", ent, get_entity_owner(ent));*/
1625                 print_ent_type_edge(F,ent, get_entity_type(ent), ENT_TYPE_EDGE_ATTR);
1626                 if (is_Class_type(get_entity_owner(ent))) {
1627                         for (i = get_entity_n_overwrites(ent); i > 0;)
1628                                 print_ent_ent_edge(F, ent, get_entity_overwrites(ent, --i), 0, ird_color_none, ENT_OVERWRITES_EDGE_ATTR);
1629                 }
1630                 /* attached subgraphs */
1631                 if (! (flags & ir_dump_flag_no_entity_values)) {
1632                         if (ent->initializer != NULL) {
1633                                 /* new style initializers */
1634                                 dump_entity_initializer(F, ent);
1635                         }
1636                 }
1637                 break;
1638         }
1639         case k_type: {
1640                 ir_type *tp = tore.typ;
1641                 dump_type_node(F, tp);
1642                 /* and now the edges */
1643                 switch (get_type_tpop_code(tp)) {
1644                 case tpo_class:
1645                         for (i = get_class_n_supertypes(tp); i > 0;) {
1646                                 --i;
1647                                 print_type_type_edge(F, tp, get_class_supertype(tp, i), TYPE_SUPER_EDGE_ATTR);
1648                         }
1649                         for (i = get_class_n_members(tp); i > 0;) {
1650                                 --i;
1651                                 print_type_ent_edge(F, tp, get_class_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
1652                         }
1653                         break;
1654                 case tpo_struct:
1655                         for (i = get_struct_n_members(tp); i > 0;) {
1656                                 --i;
1657                                 print_type_ent_edge(F, tp, get_struct_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
1658                         }
1659                         break;
1660                 case tpo_method:
1661                         for (i = get_method_n_params(tp); i > 0;) {
1662                                 --i;
1663                                 print_type_type_edge(F, tp, get_method_param_type(tp, i), METH_PAR_EDGE_ATTR,i);
1664                         }
1665                         for (i = get_method_n_ress(tp); i > 0;) {
1666                                  --i;
1667                                 print_type_type_edge(F, tp, get_method_res_type(tp, i), METH_RES_EDGE_ATTR,i);
1668                         }
1669                         break;
1670                 case tpo_union:
1671                         for (i = get_union_n_members(tp); i > 0;) {
1672                                  --i;
1673                                 print_type_ent_edge(F, tp, get_union_member(tp, i), UNION_EDGE_ATTR);
1674                         }
1675                         break;
1676                 case tpo_array:
1677                         print_type_type_edge(F, tp, get_array_element_type(tp), ARR_ELT_TYPE_EDGE_ATTR);
1678                         print_type_ent_edge(F, tp, get_array_element_entity(tp), ARR_ENT_EDGE_ATTR);
1679                         for (i = get_array_n_dimensions(tp); i > 0;) {
1680                                 ir_node *upper, *lower;
1681
1682                                  --i;
1683                                 upper = get_array_upper_bound(tp, i);
1684                                 lower = get_array_lower_bound(tp, i);
1685                                 print_node_type_edge(F, upper, tp, "label: \"upper %zu\"", get_array_order(tp, i));
1686                                 print_node_type_edge(F, lower, tp, "label: \"lower %zu\"", get_array_order(tp, i));
1687                                 dump_const_expression(F, upper);
1688                                 dump_const_expression(F, lower);
1689                         }
1690                         break;
1691                 case tpo_enumeration:
1692                         for (i = get_enumeration_n_enums(tp); i > 0;) {
1693                                  --i;
1694                                 dump_enum_item(F, tp, i);
1695                                 print_enum_item_edge(F, tp, i, "label: \"item %zu\"", i);
1696                         }
1697                         break;
1698                 case tpo_pointer:
1699                         print_type_type_edge(F, tp, get_pointer_points_to_type(tp), PTR_PTS_TO_EDGE_ATTR);
1700                         break;
1701                 case tpo_unknown:
1702                 case tpo_code:
1703                 case tpo_uninitialized:
1704                 case tpo_none:
1705                 case tpo_primitive:
1706                         break;
1707                 }
1708                 break; /* case k_type */
1709         }
1710         default:
1711                 printf(" *** irdump,  dump_type_info(l.%i), faulty type.\n", __LINE__);
1712         }
1713 }
1714
1715 /** For dumping class hierarchies.
1716  * Dumps a class type node and a superclass edge.
1717  */
1718 static void dump_class_hierarchy_node(type_or_ent tore, void *ctx)
1719 {
1720         FILE *F = (FILE*)ctx;
1721         size_t i = 0;  /* to shutup gcc */
1722
1723         /* dump this type or entity */
1724         switch (get_kind(tore.ent)) {
1725         case k_entity: {
1726                 ir_entity *ent = tore.ent;
1727                 if (get_entity_owner(ent) == get_glob_type()) break;
1728                 if (!is_Method_type(get_entity_type(ent)))
1729                         break;  /* GL */
1730                 if (flags & ir_dump_flag_entities_in_hierarchy
1731                                 && is_Class_type(get_entity_owner(ent))) {
1732                         /* The node */
1733                         dump_entity_node(F, ent);
1734                         /* The edges */
1735                         print_type_ent_edge(F, get_entity_owner(ent), ent, TYPE_MEMBER_EDGE_ATTR);
1736                         for (i = get_entity_n_overwrites(ent); i > 0;) {
1737                                  --i;
1738                                 print_ent_ent_edge(F, get_entity_overwrites(ent, i), ent, 0, ird_color_none, ENT_OVERWRITES_EDGE_ATTR);
1739                         }
1740                 }
1741                 break;
1742         }
1743         case k_type: {
1744                 ir_type *tp = tore.typ;
1745                 if (tp == get_glob_type())
1746                         break;
1747                 switch (get_type_tpop_code(tp)) {
1748                 case tpo_class:
1749                         dump_type_node(F, tp);
1750                         /* and now the edges */
1751                         for (i = get_class_n_supertypes(tp); i > 0;) {
1752                                  --i;
1753                                 print_type_type_edge(F,tp,get_class_supertype(tp, i),TYPE_SUPER_EDGE_ATTR);
1754                         }
1755                         break;
1756                 default: break;
1757                 }
1758                 break; /* case k_type */
1759         }
1760         default:
1761                 printf(" *** irdump,  dump_class_hierarchy_node(l.%i), faulty type.\n", __LINE__);
1762         }
1763 }
1764
1765 /*******************************************************************/
1766 /* dump analysis information that is expressed in graph terms.     */
1767 /*******************************************************************/
1768
1769 /* dump out edges */
1770 static void dump_out_edge(ir_node *n, void *env)
1771 {
1772         FILE *F = (FILE*)env;
1773         int i;
1774         for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
1775                 ir_node *succ = get_irn_out(n, i);
1776                 assert(succ);
1777                 print_node_edge_kind(F, succ);
1778                 fprintf(F, "{sourcename: ");
1779                 print_nodeid(F, n);
1780                 fprintf(F, " targetname: ");
1781                 print_nodeid(F, succ);
1782                 fprintf(F, " color: red linestyle: dashed");
1783                 fprintf(F, "}\n");
1784         }
1785 }
1786
1787 static void dump_loop_label(FILE *F, const ir_loop *loop)
1788 {
1789         fprintf(F, "loop %u", get_loop_depth(loop));
1790 }
1791
1792 static void dump_loop_info(FILE *F, const ir_loop *loop)
1793 {
1794         fprintf(F, " info1: \"");
1795         fprintf(F, " loop nr: %ld", get_loop_loop_nr(loop));
1796 #ifdef DEBUG_libfirm
1797         fprintf(F, "\n The loop was analyzed %ld times.", (long int) PTR_TO_INT(get_loop_link(loop)));
1798 #endif
1799         fprintf(F, "\"");
1800 }
1801
1802 void print_loopid(FILE *F, const ir_loop *loop)
1803 {
1804         fprintf(F, "\"l%ld\"", get_loop_loop_nr(loop));
1805 }
1806
1807 static void dump_loop_node(FILE *F, const ir_loop *loop)
1808 {
1809         fprintf(F, "node: {title: ");
1810         print_loopid(F, loop);
1811         fprintf(F, " label: \"");
1812         dump_loop_label(F, loop);
1813         fprintf(F, "\" ");
1814         dump_loop_info(F, loop);
1815         fprintf(F, "}\n");
1816 }
1817
1818 static void dump_loop_node_edge(FILE *F, const ir_loop *loop, size_t i)
1819 {
1820         assert(loop);
1821         fprintf(F, "edge: {sourcename: ");
1822         print_loopid(F, loop);
1823         fprintf(F, " targetname: ");
1824         print_nodeid(F, get_loop_element(loop, i).node);
1825         fprintf(F, " color: green");
1826         fprintf(F, "}\n");
1827 }
1828
1829 static void dump_loop_son_edge(FILE *F, const ir_loop *loop, size_t i)
1830 {
1831         assert(loop);
1832         fprintf(F, "edge: {sourcename: ");
1833         print_loopid(F, loop);
1834         fprintf(F, " targetname: ");
1835         print_loopid(F, get_loop_element(loop, i).son);
1836         ir_fprintf(F, " color: darkgreen label: \"%zu\"}\n", i);
1837 }
1838
1839 static void dump_loops(FILE *F, const ir_loop *loop)
1840 {
1841         size_t i;
1842         size_t n_elements = get_loop_n_elements(loop);
1843         /* dump this loop node */
1844         dump_loop_node(F, loop);
1845
1846         /* dump edges to nodes in loop -- only if it is a real loop */
1847         if (get_loop_depth(loop) != 0) {
1848                 for (i = n_elements; i > 0;) {
1849                         loop_element element;
1850                         --i;
1851                         element = get_loop_element(loop, i);
1852                         if (*element.kind != k_ir_node)
1853                                 continue;
1854                         dump_loop_node_edge(F, loop, i);
1855                 }
1856         }
1857         for (i = n_elements; i > 0;) {
1858                 loop_element element;
1859                 --i;
1860                 element = get_loop_element(loop, i);
1861                 if (*element.kind != k_ir_loop)
1862                         continue;
1863                 dump_loops(F, element.son);
1864                 dump_loop_son_edge(F, loop, i);
1865         }
1866 }
1867
1868 static void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg)
1869 {
1870         ir_loop *loop = get_irg_loop(irg);
1871         if (loop != NULL) {
1872                 dump_loops(F, loop);
1873         }
1874 }
1875
1876 void dump_vcg_header_colors(FILE *F)
1877 {
1878         int i;
1879         init_colors();
1880         for (i = 0; i < ird_color_count; ++i) {
1881                 if (color_rgb[i] != NULL) {
1882                         fprintf(F, "colorentry %s: %s\n", color_names[i], color_rgb[i]);
1883                 }
1884         }
1885 }
1886
1887 void dump_vcg_infonames(FILE *F)
1888 {
1889         fputs(
1890                 "infoname 1: \"Attribute\"\n"
1891                 "infoname 2: \"Verification errors\"\n"
1892                 "infoname 3: \"Debug info\"\n", F);
1893 }
1894
1895 /**
1896  * dumps the VCG header
1897  */
1898 void dump_vcg_header(FILE *F, const char *name, const char *layout, const char *orientation)
1899 {
1900         const char *label
1901                 = (flags & ir_dump_flag_disable_edge_labels) ? "no" : "yes";
1902
1903         if (! layout)     layout = "Compilergraph";
1904         if (!orientation) orientation = "bottom_to_top";
1905
1906         /* print header */
1907         fprintf(F,
1908                 "graph: { title: \"ir graph of %s\"\n"
1909                 "display_edge_labels: %s\n"
1910                 "layoutalgorithm: mindepth //$ \"%s\"\n"
1911                 "manhattan_edges: yes\n"
1912                 "port_sharing: no\n"
1913                 "orientation: %s\n"
1914                 "classname 1:  \"intrablock Data\"\n"
1915                 "classname 2:  \"Block\"\n"
1916                 "classname 3:  \"Entity type\"\n"
1917                 "classname 4:  \"Entity owner\"\n"
1918                 "classname 5:  \"Method Param\"\n"
1919                 "classname 6:  \"Method Res\"\n"
1920                 "classname 7:  \"Super\"\n"
1921                 "classname 8:  \"Union\"\n"
1922                 "classname 9:  \"Points-to\"\n"
1923                 "classname 10: \"Array Element Type\"\n"
1924                 "classname 11: \"Overwrites\"\n"
1925                 "classname 12: \"Member\"\n"
1926                 "classname 13: \"Control Flow\"\n"
1927                 "classname 14: \"intrablock Memory\"\n"
1928                 "classname 15: \"Dominators\"\n"
1929                 "classname 16: \"interblock Data\"\n"
1930                 "classname 17: \"interblock Memory\"\n"
1931                 "classname 18: \"Exception Control Flow for Interval Analysis\"\n"
1932                 "classname 19: \"Postdominators\"\n"
1933                 "classname 20: \"Keep Alive\"\n"
1934                 "classname 21: \"Out Edges\"\n"
1935                 "classname 22: \"Macro Block Edges\"\n",
1936                 name, label, layout, orientation);
1937         dump_vcg_infonames(F);
1938         dump_vcg_header_colors(F);
1939         fprintf(F, "\n");
1940 }
1941
1942 /**
1943  * Dumps the vcg file footer
1944  */
1945 void dump_vcg_footer(FILE *F)
1946 {
1947         fprintf(F, "}\n");
1948 }
1949
1950
1951
1952 static void dump_blocks_as_subgraphs(FILE *out, ir_graph *irg)
1953 {
1954         size_t i;
1955
1956         construct_block_lists(irg);
1957
1958         /*
1959          * If we are in the interprocedural view, we dump not
1960          * only the requested irg but also all irgs that can be reached
1961          * from irg.
1962          */
1963         for (i = get_irp_n_irgs(); i > 0;) {
1964                 ir_graph *other_irg = get_irp_irg(--i);
1965                 ir_node **arr = (ir_node**)ird_get_irg_link(other_irg);
1966                 if (arr == NULL)
1967                         continue;
1968
1969                 dump_graph_from_list(out, other_irg);
1970                 DEL_ARR_F(arr);
1971         }
1972 }
1973
1974 void dump_ir_graph_file(FILE *out, ir_graph *irg)
1975 {
1976         dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
1977
1978         /* dump nodes */
1979         if (flags & ir_dump_flag_blocks_as_subgraphs) {
1980                 dump_blocks_as_subgraphs(out, irg);
1981         } else {
1982                 /* dump_node_with_edges must be called in post visiting predecessors */
1983                 ird_walk_graph(irg, NULL, dump_node_with_edges, out);
1984         }
1985
1986         /* dump type info */
1987         if (flags & ir_dump_flag_with_typegraph) {
1988                 type_walk_irg(irg, dump_type_info, NULL, out);
1989                 inc_irg_visited(get_const_code_irg());
1990                 /* dump edges from graph to type info */
1991                 irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, out);
1992         }
1993
1994         /* dump iredges out edges */
1995         if ((flags & ir_dump_flag_iredges) && edges_activated(irg)) {
1996                 irg_walk_edges(get_irg_start_block(irg), dump_ir_edges, NULL, out);
1997         }
1998
1999         /* dump the out edges in a separate walk */
2000         if ((flags & ir_dump_flag_out_edges)
2001                         && (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_OUTS))) {
2002                 irg_out_walk(get_irg_start(irg), dump_out_edge, NULL, out);
2003         }
2004
2005         dump_vcg_footer(out);
2006 }
2007
2008 static void dump_block_to_cfg(ir_node *block, void *env)
2009 {
2010         FILE *F = (FILE*)env;
2011         int i;
2012
2013         if (is_Bad(block) && get_irn_mode(block) == mode_X) {
2014                 dump_node(F, block);
2015         }
2016
2017         if (is_Block(block)) {
2018                 /* This is a block. Dump a node for the block. */
2019                 fprintf(F, "node: {title: ");
2020                 print_nodeid(F, block);
2021                 fprintf(F, " label: \"");
2022                 if (block == get_irg_start_block(get_irn_irg(block)))
2023                         fprintf(F, "Start ");
2024                 if (block == get_irg_end_block(get_irn_irg(block)))
2025                         fprintf(F, "End ");
2026
2027                 fprintf(F, "%s ", get_op_name(get_irn_op(block)));
2028                 print_nodeid(F, block);
2029                 fprintf(F, "\" ");
2030                 fprintf(F, "info1:\"");
2031
2032                 /* the generic version. */
2033                 dump_irnode_to_file(F, block);
2034
2035                 fprintf(F, "\"");  /* closing quote of info */
2036
2037                 if ((block == get_irg_start_block(get_irn_irg(block))) ||
2038                         (block == get_irg_end_block(get_irn_irg(block)))     )
2039                         fprintf(F, " color:blue ");
2040
2041                 fprintf(F, "}\n");
2042
2043                 /* Dump the edges */
2044                 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) {
2045                         ir_node *pred = get_Block_cfgpred(block, i);
2046                         if (!is_Bad(pred))
2047                                 pred = get_nodes_block(pred);
2048                         fprintf(F, "edge: { sourcename: ");
2049                         print_nodeid(F, block);
2050                         fprintf(F, " targetname: ");
2051                         print_nodeid(F, pred);
2052                         fprintf(F, "\"}\n");
2053                 }
2054
2055                 /* Dump dominator/postdominator edge */
2056                 if (ir_get_dump_flags() & ir_dump_flag_dominance) {
2057                         if (is_irg_state(get_irn_irg(block), IR_GRAPH_STATE_CONSISTENT_DOMINANCE) && get_Block_idom(block)) {
2058                                 ir_node *pred = get_Block_idom(block);
2059                                 fprintf(F, "edge: { sourcename: ");
2060                                 print_nodeid(F, block);
2061                                 fprintf(F, " targetname: ");
2062                                 print_nodeid(F, pred);
2063                                 fprintf(F, " " DOMINATOR_EDGE_ATTR "}\n");
2064                         }
2065                         if (is_irg_state(get_irn_irg(block), IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE) && get_Block_ipostdom(block)) {
2066                                 ir_node *pred = get_Block_ipostdom(block);
2067                                 fprintf(F, "edge: { sourcename: ");
2068                                 print_nodeid(F, block);
2069                                 fprintf(F, " targetname: ");
2070                                 print_nodeid(F, pred);
2071                                 fprintf(F, " " POSTDOMINATOR_EDGE_ATTR "}\n");
2072                         }
2073                 }
2074         }
2075 }
2076
2077 void dump_cfg(FILE *F, ir_graph *irg)
2078 {
2079         dump_vcg_header(F, get_irg_dump_name(irg), NULL, NULL);
2080
2081         /* walk over the blocks in the graph */
2082         irg_walk_graph(irg, dump_block_to_cfg, NULL, F);
2083
2084         dump_vcg_footer(F);
2085 }
2086
2087 void dump_callgraph(FILE *F)
2088 {
2089         size_t          i;
2090         ir_dump_flags_t old_flags = ir_get_dump_flags();
2091
2092         ir_remove_dump_flags(ir_dump_flag_disable_edge_labels);
2093         dump_vcg_header(F, "Callgraph", "Hierarchic", NULL);
2094
2095         for (i = get_irp_n_irgs(); i > 0;) {
2096                 ir_graph *irg = get_irp_irg(--i);
2097                 ir_entity *ent = get_irg_entity(irg);
2098                 size_t j, n_callees = get_irg_n_callees(irg);
2099
2100                 dump_entity_node(F, ent);
2101                 for (j = 0; j < n_callees; ++j) {
2102                         ir_entity  *c    = get_irg_entity(get_irg_callee(irg, j));
2103                         int         be   = is_irg_callee_backedge(irg, j);
2104                         const char *attr = be
2105                                 ? "label:\"recursion %zu\""
2106                                 : "label:\"calls %zu\"";
2107                         print_ent_ent_edge(F, ent, c, be, ird_color_entity, attr,
2108                                            get_irg_callee_loop_depth(irg, j));
2109                 }
2110         }
2111
2112         ir_set_dump_flags(old_flags);
2113         dump_vcg_footer(F);
2114 }
2115
2116 void dump_typegraph(FILE *out)
2117 {
2118         dump_vcg_header(out, "All_types", NULL, NULL);
2119         type_walk(dump_type_info, NULL, out);
2120         dump_vcg_footer(out);
2121 }
2122
2123 void dump_class_hierarchy(FILE *out)
2124 {
2125         dump_vcg_header(out, "class_hierarchy", NULL, NULL);
2126         type_walk(dump_class_hierarchy_node, NULL, out);
2127         dump_vcg_footer(out);
2128 }
2129
2130 static void dump_loops_standalone(FILE *F, ir_loop *loop)
2131 {
2132         size_t i;
2133         bool   loop_node_started = false;
2134         size_t first      = 0;
2135         size_t son_number = 0;
2136         loop_element le;
2137         ir_loop *son = NULL;
2138
2139         /* Dump a new loop node. */
2140         dump_loop_node(F, loop);
2141
2142         /* Dump the loop elements. */
2143         for (i = 0; i < get_loop_n_elements(loop); i++) {
2144                 le = get_loop_element(loop, i);
2145                 son = le.son;
2146                 if (get_kind(son) == k_ir_loop) {
2147
2148                         /* We are a loop son -> Recurse */
2149
2150                         if (loop_node_started) { /* Close the "firm-nodes" node first if we started one. */
2151                                 fprintf(F, "\" }\n");
2152                                 fprintf(F, "edge: {sourcename: ");
2153                                 print_loopid(F, loop);
2154                                 fprintf(F, " targetname: \"l%ld-%lu-nodes\" label:\"%lu...%lu\"}\n",
2155                                         get_loop_loop_nr(loop),
2156                                                 (unsigned long) first,
2157                                                 (unsigned long) first,
2158                                         (unsigned long) i-1);
2159                                 loop_node_started = false;
2160                         }
2161                         dump_loop_son_edge(F, loop, son_number++);
2162                         dump_loops_standalone(F, son);
2163                 } else if (get_kind(son) == k_ir_node) {
2164                         /* We are a loop node -> Collect firm nodes */
2165
2166                         ir_node *n = le.node;
2167                         if (!loop_node_started) {
2168                                 /* Start a new node which contains all firm nodes of the current loop */
2169                                 fprintf(F, "node: { title: \"l%ld-%lu-nodes\" color: lightyellow label: \"",
2170                                         get_loop_loop_nr(loop),
2171                                         (unsigned long)i);
2172                                 loop_node_started = true;
2173                                 first = i;
2174                         } else
2175                                 fprintf(F, "\n");
2176
2177                         dump_node_label(F, n);
2178                         if (has_backedges(n)) fprintf(F, "\t loop head!");
2179                 } else { /* for callgraph loop tree */
2180                         ir_graph *n;
2181                         assert(get_kind(son) == k_ir_graph);
2182
2183                         /* We are a loop node -> Collect firm graphs */
2184                         n = le.irg;
2185                         if (!loop_node_started) {
2186                                 /* Start a new node which contains all firm nodes of the current loop */
2187                                 fprintf(F, "node: { title: \"l%ld-%lu-nodes\" color: lightyellow label: \"",
2188                                         get_loop_loop_nr(loop),
2189                                         (unsigned long)i);
2190                                 loop_node_started = true;
2191                                 first = i;
2192                         } else
2193                                 fprintf(F, "\n");
2194                         fprintf(F, " %s", get_irg_dump_name(n));
2195                         /* fprintf(F, " %s (depth %d)", get_irg_dump_name(n), n->callgraph_weighted_loop_depth); */
2196                 }
2197         }
2198
2199         if (loop_node_started) {
2200                 fprintf(F, "\" }\n");
2201                 fprintf(F, "edge: {sourcename: \"");
2202                 print_loopid(F, loop);
2203                 fprintf(F, "\" targetname: \"l%ld-%lu-nodes\" label:\"%lu...%lu\"}\n",
2204                         get_loop_loop_nr(loop),
2205                         (unsigned long) first,
2206                         (unsigned long) first,
2207                         (unsigned long) i-1);
2208                 loop_node_started = false;
2209         }
2210 }
2211
2212 void dump_loop_tree(FILE *out, ir_graph *irg)
2213 {
2214         ir_dump_flags_t old_flags = ir_get_dump_flags();
2215
2216         ir_remove_dump_flags(ir_dump_flag_disable_edge_labels);
2217
2218         dump_vcg_header(out, get_irg_dump_name(irg), "Tree", "top_to_bottom");
2219
2220         if (get_irg_loop(irg))
2221                 dump_loops_standalone(out, get_irg_loop(irg));
2222
2223         dump_vcg_footer(out);
2224
2225         ir_set_dump_flags(old_flags);
2226 }
2227
2228 void dump_callgraph_loop_tree(FILE *out)
2229 {
2230         dump_vcg_header(out, "callgraph looptree", "Tree", "top_to_bottom");
2231         dump_loops_standalone(out, irp->outermost_cg_loop);
2232         dump_vcg_footer(out);
2233 }
2234
2235 static void collect_nodeloop(FILE *F, ir_loop *loop, pset *loopnodes)
2236 {
2237         size_t i;
2238         int    son_number = 0;
2239         int    node_number = 0;
2240
2241         if (flags & ir_dump_flag_loops)
2242                 dump_loop_node(F, loop);
2243
2244         for (i = 0; i < get_loop_n_elements(loop); i++) {
2245                 loop_element le = get_loop_element(loop, i);
2246                 if (*(le.kind) == k_ir_loop) {
2247                         if (flags & ir_dump_flag_loops)
2248                                 dump_loop_son_edge(F, loop, son_number++);
2249                         /* Recur */
2250                         collect_nodeloop(F, le.son, loopnodes);
2251                 } else {
2252                         if (flags & ir_dump_flag_loops)
2253                                 dump_loop_node_edge(F, loop, node_number++);
2254                         pset_insert_ptr(loopnodes, le.node);
2255                 }
2256         }
2257 }
2258
2259 static void collect_nodeloop_external_nodes(ir_loop *loop, pset *loopnodes,
2260                                             pset *extnodes)
2261 {
2262         size_t i;
2263         int j, start;
2264
2265         for (i = 0; i < get_loop_n_elements(loop); i++) {
2266                 loop_element le = get_loop_element(loop, i);
2267                 if (*(le.kind) == k_ir_loop) {
2268                         /* Recur */
2269                         collect_nodeloop_external_nodes(le.son, loopnodes, extnodes);
2270                 } else {
2271                         if (is_Block(le.node)) start = 0; else start = -1;
2272                         for (j = start; j < get_irn_arity(le.node); j++) {
2273                                 ir_node *pred = get_irn_n(le.node, j);
2274                                 if (!pset_find_ptr(loopnodes, pred)) {
2275                                         pset_insert_ptr(extnodes, pred);
2276                                         if (!is_Block(pred)) {
2277                                                 pred = get_nodes_block(pred);
2278                                                 if (!pset_find_ptr(loopnodes, pred))
2279                                                         pset_insert_ptr(extnodes, pred);
2280                                         }
2281                                 }
2282                         }
2283                 }
2284         }
2285 }
2286
2287 void dump_loop(FILE *F, ir_loop *l)
2288 {
2289         pset *loopnodes = pset_new_ptr_default();
2290         pset *extnodes  = pset_new_ptr_default();
2291         ir_node *n, *b;
2292         char name[50];
2293
2294         snprintf(name, sizeof(name), "loop_%ld", get_loop_loop_nr(l));
2295         dump_vcg_header(F, name, NULL, NULL);
2296
2297         /* collect all nodes to dump */
2298         collect_nodeloop(F, l, loopnodes);
2299         collect_nodeloop_external_nodes(l, loopnodes, extnodes);
2300
2301         /* build block lists */
2302         foreach_pset(loopnodes, ir_node*, n) {
2303                 set_irn_link(n, NULL);
2304         }
2305         foreach_pset(extnodes, ir_node*, n) {
2306                 set_irn_link(n, NULL);
2307         }
2308         foreach_pset(loopnodes, ir_node*, n) {
2309                 if (!is_Block(n)) {
2310                         b = get_nodes_block(n);
2311                         set_irn_link(n, get_irn_link(b));
2312                         set_irn_link(b, n);
2313                 }
2314         }
2315         foreach_pset(extnodes, ir_node*, n) {
2316                 if (!is_Block(n)) {
2317                         b = get_nodes_block(n);
2318                         set_irn_link(n, get_irn_link(b));
2319                         set_irn_link(b, n);
2320                 }
2321         }
2322
2323         foreach_pset(loopnodes, ir_node*, b) {
2324                 if (is_Block(b)) {
2325                         fprintf(F, "graph: { title: ");
2326                         print_nodeid(F, b);
2327                         fprintf(F, "  label: \"");
2328                         dump_node_opcode(F, b);
2329                         fprintf(F, " %ld:%u", get_irn_node_nr(b), get_irn_idx(b));
2330                         fprintf(F, "\" status:clustered color:yellow\n");
2331
2332                         /* dump the blocks edges */
2333                         dump_ir_data_edges(F, b);
2334
2335                         /* dump the nodes that go into the block */
2336                         for (n = (ir_node*)get_irn_link(b); n; n = (ir_node*)get_irn_link(n)) {
2337                                 if (pset_find_ptr(extnodes, n))
2338                                         overrule_nodecolor = ird_color_block_inout;
2339                                 dump_node(F, n);
2340                                 overrule_nodecolor = ird_color_default_node;
2341                                 if (!pset_find_ptr(extnodes, n)) dump_ir_data_edges(F, n);
2342                         }
2343
2344                         /* Close the vcg information for the block */
2345                         fprintf(F, "}\n");
2346                         dump_const_node_local(F, b);
2347                         fprintf(F, "\n");
2348                 }
2349         }
2350         foreach_pset(extnodes, ir_node*, b) {
2351                 if (is_Block(b)) {
2352                         fprintf(F, "graph: { title: ");
2353                         print_nodeid(F, b);
2354                         fprintf(F, " label: \"");
2355                         dump_node_opcode(F, b);
2356                         fprintf(F, " %ld:%u", get_irn_node_nr(b), get_irn_idx(b));
2357                         fprintf(F, "\" status:clustered color:lightblue\n");
2358
2359                         /* dump the nodes that go into the block */
2360                         for (n = (ir_node*)get_irn_link(b); n; n = (ir_node*)get_irn_link(n)) {
2361                                 if (!pset_find_ptr(loopnodes, n))
2362                                         overrule_nodecolor = ird_color_block_inout;
2363                                 dump_node(F, n);
2364                                 overrule_nodecolor = ird_color_default_node;
2365                                 if (pset_find_ptr(loopnodes, n)) dump_ir_data_edges(F, n);
2366                         }
2367
2368                         /* Close the vcg information for the block */
2369                         fprintf(F, "}\n");
2370                         dump_const_node_local(F, b);
2371                         fprintf(F, "\n");
2372                 }
2373         }
2374         del_pset(loopnodes);
2375         del_pset(extnodes);
2376
2377         dump_vcg_footer(F);
2378 }
2379
2380 static bool   obstack_init;
2381 static struct obstack obst;
2382 static char  *dump_path;
2383
2384 void ir_set_dump_path(const char *path)
2385 {
2386         xfree(dump_path);
2387         dump_path = xstrdup(path);
2388 }
2389
2390 static void add_string_escaped(const char *string)
2391 {
2392         const char *p;
2393         for (p = string; *p != '\0'; ++p) {
2394                 char c = *p;
2395                 if (c == '/') {
2396                         obstack_1grow(&obst, '@');
2397                         obstack_1grow(&obst, '1');
2398                 } else if (c == '@') {
2399                         obstack_1grow(&obst, '@');
2400                         obstack_1grow(&obst, '2');
2401                 } else {
2402                         obstack_1grow(&obst, c);
2403                 }
2404         }
2405 }
2406
2407 static void add_dump_path(void)
2408 {
2409         if (!obstack_init) {
2410                 obstack_init(&obst);
2411                 obstack_init = true;
2412         }
2413
2414         if (dump_path != NULL) {
2415                 size_t len = strlen(dump_path);
2416                 obstack_grow(&obst, dump_path, len);
2417                 if (len > 0 && dump_path[len-1] != '/')
2418                         obstack_1grow(&obst, '/');
2419         }
2420 }
2421
2422 void dump_ir_graph_ext(ir_graph_dump_func func, ir_graph *graph,
2423                        const char *suffix)
2424 {
2425         const char *dump_name = get_irg_dump_name(graph);
2426         char       *file_name;
2427         FILE       *out;
2428
2429         if (!ir_should_dump(dump_name))
2430                 return;
2431
2432         add_dump_path();
2433
2434         add_string_escaped(dump_name);
2435         obstack_printf(&obst, "-%02u", graph->dump_nr++);
2436
2437         if (suffix != NULL) {
2438                 if (suffix[0] != '.')
2439                         obstack_1grow(&obst, '-');
2440                 add_string_escaped(suffix);
2441         }
2442         obstack_1grow(&obst, '\0');
2443
2444         file_name = (char*)obstack_finish(&obst);
2445         /* xvcg expects only <LF> so we need "b"inary mode (for win32) */
2446         out       = fopen(file_name, "wb");
2447         obstack_free(&obst, file_name);
2448
2449         if (out == NULL) {
2450                 fprintf(stderr, "Couldn't open '%s': %s\n", file_name, strerror(errno));
2451                 return;
2452         }
2453
2454         func(out, graph);
2455         fclose(out);
2456 }
2457
2458 void dump_ir_prog_ext(ir_prog_dump_func func, const char *suffix)
2459 {
2460         char *file_name;
2461         FILE *out;
2462
2463         add_dump_path();
2464
2465         obstack_printf(&obst, "%02u", irp->dump_nr++);
2466         if (suffix != NULL) {
2467                 if (suffix[0] != '.')
2468                         obstack_1grow(&obst, '-');
2469                 add_string_escaped(suffix);
2470         }
2471         obstack_1grow(&obst, '\0');
2472
2473         file_name = (char*)obstack_finish(&obst);
2474         out       = fopen(file_name, "wb");
2475         obstack_free(&obst, file_name);
2476
2477         if (out == NULL) {
2478                 fprintf(stderr, "Couldn't open '%s': %s\n", file_name, strerror(errno));
2479                 return;
2480         }
2481         func(out);
2482         fclose(out);
2483 }
2484
2485 void dump_ir_graph(ir_graph *graph, const char *suffix)
2486 {
2487         char buf[256];
2488
2489         snprintf(buf, sizeof(buf), "%s.vcg", suffix);
2490         dump_ir_graph_ext(dump_ir_graph_file, graph, buf);
2491 }
2492
2493 void dump_all_ir_graphs(const char *suffix)
2494 {
2495         size_t i, n_irgs = get_irp_n_irgs();
2496
2497         for (i = 0; i < n_irgs; ++i) {
2498                 ir_graph *irg = get_irp_irg(i);
2499                 dump_ir_graph(irg, suffix);
2500         }
2501 }
2502
2503 typedef struct pass_t {
2504         ir_prog_pass_t pass;
2505         char           suffix[1];
2506 } pass_t;
2507
2508 /**
2509  * Wrapper around dump_all_ir_graphs().
2510  */
2511 static int dump_all_ir_graphs_wrapper(ir_prog *irp, void *context)
2512 {
2513         pass_t *pass = (pass_t*)context;
2514
2515         (void)irp;
2516         dump_all_ir_graphs(pass->suffix);
2517         return 0;
2518 }
2519
2520 ir_prog_pass_t *dump_all_ir_graph_pass(const char *name, const char *suffix)
2521 {
2522         size_t  len  = strlen(suffix) + 1;
2523         pass_t *pass = XMALLOCF(pass_t, suffix, len);
2524         ir_prog_pass_t *res  = def_prog_pass_constructor(
2525                 &pass->pass, name ? name : "dump_all_graphs", dump_all_ir_graphs_wrapper);
2526
2527         /* this pass does not change anything, so neither dump nor verify is needed. */
2528         res->dump_irprog   = ir_prog_no_dump;
2529         res->verify_irprog = ir_prog_no_verify;
2530
2531         memcpy(pass->suffix, suffix, len);
2532
2533         return res;
2534 }