OSDN Git Service

* config/i386/winnt.c (i386_pe_output_labelref); Correct
[pf3gnuchains/gcc-fork.git] / gcc / tree-dump.c
1 /* Tree-dumping functionality for intermediate representation.
2    Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "splay-tree.h"
28 #include "diagnostic.h"
29 #include "toplev.h"
30 #include "tree-dump.h"
31 #include "langhooks.h"
32 #include "tree-iterator.h"
33
34 static unsigned int queue (dump_info_p, tree, int);
35 static void dump_index (dump_info_p, unsigned int);
36 static void dequeue_and_dump (dump_info_p);
37 static void dump_new_line (dump_info_p);
38 static void dump_maybe_newline (dump_info_p);
39 static void dump_string_field (dump_info_p, const char *, const char *);
40 static void dump_enable_all (int);
41
42 /* Add T to the end of the queue of nodes to dump.  Returns the index
43    assigned to T.  */
44
45 static unsigned int
46 queue (dump_info_p di, tree t, int flags)
47 {
48   dump_queue_p dq;
49   dump_node_info_p dni;
50   unsigned int index;
51
52   /* Assign the next available index to T.  */
53   index = ++di->index;
54
55   /* Obtain a new queue node.  */
56   if (di->free_list)
57     {
58       dq = di->free_list;
59       di->free_list = dq->next;
60     }
61   else
62     dq = xmalloc (sizeof (struct dump_queue));
63
64   /* Create a new entry in the splay-tree.  */
65   dni = xmalloc (sizeof (struct dump_node_info));
66   dni->index = index;
67   dni->binfo_p = ((flags & DUMP_BINFO) != 0);
68   dq->node = splay_tree_insert (di->nodes, (splay_tree_key) t,
69                                 (splay_tree_value) dni);
70
71   /* Add it to the end of the queue.  */
72   dq->next = 0;
73   if (!di->queue_end)
74     di->queue = dq;
75   else
76     di->queue_end->next = dq;
77   di->queue_end = dq;
78
79   /* Return the index.  */
80   return index;
81 }
82
83 static void
84 dump_index (dump_info_p di, unsigned int index)
85 {
86   fprintf (di->stream, "@%-6u ", index);
87   di->column += 8;
88 }
89
90 /* If T has not already been output, queue it for subsequent output.
91    FIELD is a string to print before printing the index.  Then, the
92    index of T is printed.  */
93
94 void
95 queue_and_dump_index (dump_info_p di, const char *field, tree t, int flags)
96 {
97   unsigned int index;
98   splay_tree_node n;
99
100   /* If there's no node, just return.  This makes for fewer checks in
101      our callers.  */
102   if (!t)
103     return;
104
105   /* See if we've already queued or dumped this node.  */
106   n = splay_tree_lookup (di->nodes, (splay_tree_key) t);
107   if (n)
108     index = ((dump_node_info_p) n->value)->index;
109   else
110     /* If we haven't, add it to the queue.  */
111     index = queue (di, t, flags);
112
113   /* Print the index of the node.  */
114   dump_maybe_newline (di);
115   fprintf (di->stream, "%-4s: ", field);
116   di->column += 6;
117   dump_index (di, index);
118 }
119
120 /* Dump the type of T.  */
121
122 void
123 queue_and_dump_type (dump_info_p di, tree t)
124 {
125   queue_and_dump_index (di, "type", TREE_TYPE (t), DUMP_NONE);
126 }
127
128 /* Dump column control */
129 #define SOL_COLUMN 25           /* Start of line column.  */
130 #define EOL_COLUMN 55           /* End of line column.  */
131 #define COLUMN_ALIGNMENT 15     /* Alignment.  */
132
133 /* Insert a new line in the dump output, and indent to an appropriate
134    place to start printing more fields.  */
135
136 static void
137 dump_new_line (dump_info_p di)
138 {
139   fprintf (di->stream, "\n%*s", SOL_COLUMN, "");
140   di->column = SOL_COLUMN;
141 }
142
143 /* If necessary, insert a new line.  */
144
145 static void
146 dump_maybe_newline (dump_info_p di)
147 {
148   int extra;
149
150   /* See if we need a new line.  */
151   if (di->column > EOL_COLUMN)
152     dump_new_line (di);
153   /* See if we need any padding.  */
154   else if ((extra = (di->column - SOL_COLUMN) % COLUMN_ALIGNMENT) != 0)
155     {
156       fprintf (di->stream, "%*s", COLUMN_ALIGNMENT - extra, "");
157       di->column += COLUMN_ALIGNMENT - extra;
158     }
159 }
160
161 /* Dump pointer PTR using FIELD to identify it.  */
162
163 void
164 dump_pointer (dump_info_p di, const char *field, void *ptr)
165 {
166   dump_maybe_newline (di);
167   fprintf (di->stream, "%-4s: %-8lx ", field, (long) ptr);
168   di->column += 15;
169 }
170
171 /* Dump integer I using FIELD to identify it.  */
172
173 void
174 dump_int (dump_info_p di, const char *field, int i)
175 {
176   dump_maybe_newline (di);
177   fprintf (di->stream, "%-4s: %-7d ", field, i);
178   di->column += 14;
179 }
180
181 /* Dump the string S.  */
182
183 void
184 dump_string (dump_info_p di, const char *string)
185 {
186   dump_maybe_newline (di);
187   fprintf (di->stream, "%-13s ", string);
188   if (strlen (string) > 13)
189     di->column += strlen (string) + 1;
190   else
191     di->column += 14;
192 }
193
194 /* Dump the string field S.  */
195
196 static void
197 dump_string_field (dump_info_p di, const char *field, const char *string)
198 {
199   dump_maybe_newline (di);
200   fprintf (di->stream, "%-4s: %-7s ", field, string);
201   if (strlen (string) > 7)
202     di->column += 6 + strlen (string) + 1;
203   else
204     di->column += 14;
205 }
206
207 /* Dump the next node in the queue.  */
208
209 static void
210 dequeue_and_dump (dump_info_p di)
211 {
212   dump_queue_p dq;
213   splay_tree_node stn;
214   dump_node_info_p dni;
215   tree t;
216   unsigned int index;
217   enum tree_code code;
218   char code_class;
219   const char* code_name;
220
221   /* Get the next node from the queue.  */
222   dq = di->queue;
223   stn = dq->node;
224   t = (tree) stn->key;
225   dni = (dump_node_info_p) stn->value;
226   index = dni->index;
227
228   /* Remove the node from the queue, and put it on the free list.  */
229   di->queue = dq->next;
230   if (!di->queue)
231     di->queue_end = 0;
232   dq->next = di->free_list;
233   di->free_list = dq;
234
235   /* Print the node index.  */
236   dump_index (di, index);
237   /* And the type of node this is.  */
238   if (dni->binfo_p)
239     code_name = "binfo";
240   else
241     code_name = tree_code_name[(int) TREE_CODE (t)];
242   fprintf (di->stream, "%-16s ", code_name);
243   di->column = 25;
244
245   /* Figure out what kind of node this is.  */
246   code = TREE_CODE (t);
247   code_class = TREE_CODE_CLASS (code);
248
249   /* Although BINFOs are TREE_VECs, we dump them specially so as to be
250      more informative.  */
251   if (dni->binfo_p)
252     {
253       unsigned ix;
254       tree bases = BINFO_BASETYPES (t);
255       unsigned n_bases = bases ? TREE_VEC_LENGTH (bases): 0;
256       tree accesses = BINFO_BASEACCESSES (t);
257
258       dump_child ("type", BINFO_TYPE (t));
259
260       if (TREE_VIA_VIRTUAL (t))
261         dump_string (di, "virt");
262
263       dump_int (di, "bases", n_bases);
264       for (ix = 0; ix != n_bases; ix++)
265         {
266           tree base = TREE_VEC_ELT (bases, ix);
267           tree access = (accesses ? TREE_VEC_ELT (accesses, ix)
268                          : access_public_node);
269           const char *string = NULL;
270
271           if (access == access_public_node)
272             string = "pub";
273           else if (access == access_protected_node)
274             string = "prot";
275           else if (access == access_private_node)
276             string = "priv";
277           else
278             abort ();
279
280           dump_string (di, string);
281           queue_and_dump_index (di, "binf", base, DUMP_BINFO);
282         }
283
284       goto done;
285     }
286
287   /* We can knock off a bunch of expression nodes in exactly the same
288      way.  */
289   if (IS_EXPR_CODE_CLASS (code_class))
290     {
291       /* If we're dumping children, dump them now.  */
292       queue_and_dump_type (di, t);
293
294       switch (code_class)
295         {
296         case '1':
297           dump_child ("op 0", TREE_OPERAND (t, 0));
298           break;
299
300         case '2':
301         case '<':
302           dump_child ("op 0", TREE_OPERAND (t, 0));
303           dump_child ("op 1", TREE_OPERAND (t, 1));
304           break;
305
306         case 'e':
307         case 'r':
308         case 's':
309           /* These nodes are handled explicitly below.  */
310           break;
311
312         default:
313           abort ();
314         }
315     }
316   else if (DECL_P (t))
317     {
318       /* All declarations have names.  */
319       if (DECL_NAME (t))
320         dump_child ("name", DECL_NAME (t));
321       if (DECL_ASSEMBLER_NAME_SET_P (t)
322           && DECL_ASSEMBLER_NAME (t) != DECL_NAME (t))
323         dump_child ("mngl", DECL_ASSEMBLER_NAME (t));
324       /* And types.  */
325       queue_and_dump_type (di, t);
326       dump_child ("scpe", DECL_CONTEXT (t));
327       /* And a source position.  */
328       if (DECL_SOURCE_FILE (t))
329         {
330           const char *filename = strrchr (DECL_SOURCE_FILE (t), '/');
331           if (!filename)
332             filename = DECL_SOURCE_FILE (t);
333           else
334             /* Skip the slash.  */
335             ++filename;
336
337           dump_maybe_newline (di);
338           fprintf (di->stream, "srcp: %s:%-6d ", filename,
339                    DECL_SOURCE_LINE (t));
340           di->column += 6 + strlen (filename) + 8;
341         }
342       /* And any declaration can be compiler-generated.  */
343       if (DECL_ARTIFICIAL (t))
344         dump_string (di, "artificial");
345       if (TREE_CHAIN (t) && !dump_flag (di, TDF_SLIM, NULL))
346         dump_child ("chan", TREE_CHAIN (t));
347     }
348   else if (code_class == 't')
349     {
350       /* All types have qualifiers.  */
351       int quals = lang_hooks.tree_dump.type_quals (t);
352
353       if (quals != TYPE_UNQUALIFIED)
354         {
355           fprintf (di->stream, "qual: %c%c%c     ",
356                    (quals & TYPE_QUAL_CONST) ? 'c' : ' ',
357                    (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ',
358                    (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' ');
359           di->column += 14;
360         }
361
362       /* All types have associated declarations.  */
363       dump_child ("name", TYPE_NAME (t));
364
365       /* All types have a main variant.  */
366       if (TYPE_MAIN_VARIANT (t) != t)
367         dump_child ("unql", TYPE_MAIN_VARIANT (t));
368
369       /* And sizes.  */
370       dump_child ("size", TYPE_SIZE (t));
371
372       /* All types have alignments.  */
373       dump_int (di, "algn", TYPE_ALIGN (t));
374     }
375   else if (code_class == 'c')
376     /* All constants can have types.  */
377     queue_and_dump_type (di, t);
378
379   /* Give the language-specific code a chance to print something.  If
380      it's completely taken care of things, don't bother printing
381      anything more ourselves.  */
382   if (lang_hooks.tree_dump.dump_tree (di, t))
383     goto done;
384
385   /* Now handle the various kinds of nodes.  */
386   switch (code)
387     {
388       int i;
389
390     case IDENTIFIER_NODE:
391       dump_string_field (di, "strg", IDENTIFIER_POINTER (t));
392       dump_int (di, "lngt", IDENTIFIER_LENGTH (t));
393       break;
394
395     case TREE_LIST:
396       dump_child ("purp", TREE_PURPOSE (t));
397       dump_child ("valu", TREE_VALUE (t));
398       dump_child ("chan", TREE_CHAIN (t));
399       break;
400
401     case STATEMENT_LIST:
402       {
403         tree_stmt_iterator it;
404         for (i = 0, it = tsi_start (t); !tsi_end_p (it); tsi_next (&it), i++)
405           {
406             char buffer[32];
407             sprintf (buffer, "%u", i);
408             dump_child (buffer, tsi_stmt (it));
409           }
410       }
411       break;
412
413     case TREE_VEC:
414       dump_int (di, "lngt", TREE_VEC_LENGTH (t));
415       for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
416         {
417           char buffer[32];
418           sprintf (buffer, "%u", i);
419           dump_child (buffer, TREE_VEC_ELT (t, i));
420         }
421       break;
422
423     case INTEGER_TYPE:
424     case ENUMERAL_TYPE:
425       dump_int (di, "prec", TYPE_PRECISION (t));
426       if (TYPE_UNSIGNED (t))
427         dump_string (di, "unsigned");
428       dump_child ("min", TYPE_MIN_VALUE (t));
429       dump_child ("max", TYPE_MAX_VALUE (t));
430
431       if (code == ENUMERAL_TYPE)
432         dump_child ("csts", TYPE_VALUES (t));
433       break;
434
435     case REAL_TYPE:
436       dump_int (di, "prec", TYPE_PRECISION (t));
437       break;
438
439     case POINTER_TYPE:
440       dump_child ("ptd", TREE_TYPE (t));
441       break;
442
443     case REFERENCE_TYPE:
444       dump_child ("refd", TREE_TYPE (t));
445       break;
446
447     case METHOD_TYPE:
448       dump_child ("clas", TYPE_METHOD_BASETYPE (t));
449       /* Fall through.  */
450
451     case FUNCTION_TYPE:
452       dump_child ("retn", TREE_TYPE (t));
453       dump_child ("prms", TYPE_ARG_TYPES (t));
454       break;
455
456     case ARRAY_TYPE:
457       dump_child ("elts", TREE_TYPE (t));
458       dump_child ("domn", TYPE_DOMAIN (t));
459       break;
460
461     case RECORD_TYPE:
462     case UNION_TYPE:
463       if (TREE_CODE (t) == RECORD_TYPE)
464         dump_string (di, "struct");
465       else
466         dump_string (di, "union");
467
468       dump_child ("flds", TYPE_FIELDS (t));
469       dump_child ("fncs", TYPE_METHODS (t));
470       queue_and_dump_index (di, "binf", TYPE_BINFO (t),
471                             DUMP_BINFO);
472       break;
473
474     case CONST_DECL:
475       dump_child ("cnst", DECL_INITIAL (t));
476       break;
477
478     case VAR_DECL:
479     case PARM_DECL:
480     case FIELD_DECL:
481     case RESULT_DECL:
482       if (TREE_CODE (t) == PARM_DECL)
483         dump_child ("argt", DECL_ARG_TYPE (t));
484       else
485         dump_child ("init", DECL_INITIAL (t));
486       dump_child ("size", DECL_SIZE (t));
487       dump_int (di, "algn", DECL_ALIGN (t));
488
489       if (TREE_CODE (t) == FIELD_DECL)
490         {
491           if (DECL_FIELD_OFFSET (t))
492             dump_child ("bpos", bit_position (t));
493         }
494       else if (TREE_CODE (t) == VAR_DECL
495                || TREE_CODE (t) == PARM_DECL)
496         {
497           dump_int (di, "used", TREE_USED (t));
498           if (DECL_REGISTER (t))
499             dump_string (di, "register");
500         }
501       break;
502
503     case FUNCTION_DECL:
504       dump_child ("args", DECL_ARGUMENTS (t));
505       if (DECL_EXTERNAL (t))
506         dump_string (di, "undefined");
507       if (TREE_PUBLIC (t))
508         dump_string (di, "extern");
509       else
510         dump_string (di, "static");
511       if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
512         dump_child ("body", DECL_SAVED_TREE (t));
513       break;
514
515     case INTEGER_CST:
516       if (TREE_INT_CST_HIGH (t))
517         dump_int (di, "high", TREE_INT_CST_HIGH (t));
518       dump_int (di, "low", TREE_INT_CST_LOW (t));
519       break;
520
521     case STRING_CST:
522       fprintf (di->stream, "strg: %-7s ", TREE_STRING_POINTER (t));
523       dump_int (di, "lngt", TREE_STRING_LENGTH (t));
524       break;
525
526     case TRUTH_NOT_EXPR:
527     case ADDR_EXPR:
528     case INDIRECT_REF:
529     case CLEANUP_POINT_EXPR:
530     case SAVE_EXPR:
531       /* These nodes are unary, but do not have code class `1'.  */
532       dump_child ("op 0", TREE_OPERAND (t, 0));
533       break;
534
535     case TRUTH_ANDIF_EXPR:
536     case TRUTH_ORIF_EXPR:
537     case INIT_EXPR:
538     case MODIFY_EXPR:
539     case COMPONENT_REF:
540     case COMPOUND_EXPR:
541     case ARRAY_REF:
542     case PREDECREMENT_EXPR:
543     case PREINCREMENT_EXPR:
544     case POSTDECREMENT_EXPR:
545     case POSTINCREMENT_EXPR:
546       /* These nodes are binary, but do not have code class `2'.  */
547       dump_child ("op 0", TREE_OPERAND (t, 0));
548       dump_child ("op 1", TREE_OPERAND (t, 1));
549       break;
550
551     case COND_EXPR:
552       dump_child ("op 0", TREE_OPERAND (t, 0));
553       dump_child ("op 1", TREE_OPERAND (t, 1));
554       dump_child ("op 2", TREE_OPERAND (t, 2));
555       break;
556
557     case CALL_EXPR:
558       dump_child ("fn", TREE_OPERAND (t, 0));
559       dump_child ("args", TREE_OPERAND (t, 1));
560       break;
561
562     case CONSTRUCTOR:
563       dump_child ("elts", CONSTRUCTOR_ELTS (t));
564       break;
565
566     case BIND_EXPR:
567       dump_child ("vars", TREE_OPERAND (t, 0));
568       dump_child ("body", TREE_OPERAND (t, 1));
569       break;
570
571     case LOOP_EXPR:
572       dump_child ("body", TREE_OPERAND (t, 0));
573       break;
574
575     case EXIT_EXPR:
576       dump_child ("cond", TREE_OPERAND (t, 0));
577       break;
578
579     case TARGET_EXPR:
580       dump_child ("decl", TREE_OPERAND (t, 0));
581       dump_child ("init", TREE_OPERAND (t, 1));
582       dump_child ("clnp", TREE_OPERAND (t, 2));
583       /* There really are two possible places the initializer can be.
584          After RTL expansion, the second operand is moved to the
585          position of the fourth operand, and the second operand
586          becomes NULL.  */
587       dump_child ("init", TREE_OPERAND (t, 3));
588       break;
589
590     default:
591       /* There are no additional fields to print.  */
592       break;
593     }
594
595  done:
596   if (dump_flag (di, TDF_ADDRESS, NULL))
597     dump_pointer (di, "addr", (void *)t);
598
599   /* Terminate the line.  */
600   fprintf (di->stream, "\n");
601 }
602
603 /* Return nonzero if FLAG has been specified for the dump, and NODE
604    is not the root node of the dump.  */
605
606 int dump_flag (dump_info_p di, int flag, tree node)
607 {
608   return (di->flags & flag) && (node != di->node);
609 }
610
611 /* Dump T, and all its children, on STREAM.  */
612
613 void
614 dump_node (tree t, int flags, FILE *stream)
615 {
616   struct dump_info di;
617   dump_queue_p dq;
618   dump_queue_p next_dq;
619
620   /* Initialize the dump-information structure.  */
621   di.stream = stream;
622   di.index = 0;
623   di.column = 0;
624   di.queue = 0;
625   di.queue_end = 0;
626   di.free_list = 0;
627   di.flags = flags;
628   di.node = t;
629   di.nodes = splay_tree_new (splay_tree_compare_pointers, 0,
630                              (splay_tree_delete_value_fn) &free);
631
632   /* Queue up the first node.  */
633   queue (&di, t, DUMP_NONE);
634
635   /* Until the queue is empty, keep dumping nodes.  */
636   while (di.queue)
637     dequeue_and_dump (&di);
638
639   /* Now, clean up.  */
640   for (dq = di.free_list; dq; dq = next_dq)
641     {
642       next_dq = dq->next;
643       free (dq);
644     }
645   splay_tree_delete (di.nodes);
646 }
647
648 /* Define a tree dump switch.  */
649 struct dump_file_info
650 {
651   const char *suffix;           /* suffix to give output file.  */
652   const char *swtch;            /* command line switch */
653   int flags;                    /* user flags */
654   int state;                    /* state of play */
655 };
656
657 /* Table of tree dump switches. This must be consistent with the
658    TREE_DUMP_INDEX enumeration in tree.h */
659 static struct dump_file_info dump_files[TDI_end] =
660 {
661   {NULL, NULL, 0, 0},
662   {".tu", "translation-unit", 0, 0},
663   {".class", "class-hierarchy", 0, 0},
664   {".original", "tree-original", 0, 0},
665   {".generic", "tree-generic", 0, 0},
666   {".nested", "tree-nested", 0, 0},
667   {".inlined", "tree-inlined", 0, 0},
668   {".vcg", "tree-vcg", 0, 0},
669   {".xml", "call-graph", 0, 0},
670   {NULL, "tree-all", 0, 0},
671 };
672
673 /* Dynamically registered tree dump files and switches.  */
674 static struct dump_file_info *extra_dump_files;
675 static size_t extra_dump_files_in_use;
676 static size_t extra_dump_files_alloced;
677
678 /* Define a name->number mapping for a dump flag value.  */
679 struct dump_option_value_info
680 {
681   const char *const name;       /* the name of the value */
682   const int value;              /* the value of the name */
683 };
684
685 /* Table of dump options. This must be consistent with the TDF_* flags
686    in tree.h */
687 static const struct dump_option_value_info dump_options[] =
688 {
689   {"address", TDF_ADDRESS},
690   {"slim", TDF_SLIM},
691   {"raw", TDF_RAW},
692   {"details", TDF_DETAILS},
693   {"stats", TDF_STATS},
694   {"blocks", TDF_BLOCKS},
695   {"vops", TDF_VOPS},
696   {"lineno", TDF_LINENO},
697   {"uid", TDF_UID},
698   {"all", ~(TDF_RAW | TDF_SLIM | TDF_LINENO)},
699   {NULL, 0}
700 };
701
702 unsigned int
703 dump_register (const char *suffix, const char *swtch)
704 {
705   size_t this = extra_dump_files_in_use++;
706
707   if (this >= extra_dump_files_alloced)
708     {
709       if (extra_dump_files_alloced == 0)
710         extra_dump_files_alloced = 32;
711       else
712         extra_dump_files_alloced *= 2;
713       extra_dump_files = xrealloc (extra_dump_files,
714                                    sizeof (struct dump_file_info)
715                                    * extra_dump_files_alloced);
716     }
717
718   memset (&extra_dump_files[this], 0, sizeof (struct dump_file_info));
719   extra_dump_files[this].suffix = suffix;
720   extra_dump_files[this].swtch = swtch;
721
722   return this + TDI_end;
723 }
724
725 /* Return the dump_file_info for the given phase.  */
726
727 static struct dump_file_info *
728 get_dump_file_info (enum tree_dump_index phase)
729 {
730   if (phase < TDI_end)
731     return &dump_files[phase];
732   else if (phase - TDI_end >= extra_dump_files_in_use)
733     abort ();
734   else
735     return extra_dump_files + (phase - TDI_end);
736 }
737
738
739 /* Begin a tree dump for PHASE. Stores any user supplied flag in
740    *FLAG_PTR and returns a stream to write to. If the dump is not
741    enabled, returns NULL.
742    Multiple calls will reopen and append to the dump file.  */
743
744 FILE *
745 dump_begin (enum tree_dump_index phase, int *flag_ptr)
746 {
747   FILE *stream;
748   char *name;
749   char dump_id[10];
750   struct dump_file_info *dfi;
751
752   if (phase == TDI_none)
753     return NULL;
754
755   dfi = get_dump_file_info (phase);
756   if (dfi->state == 0)
757     return NULL;
758
759   if (snprintf (dump_id, sizeof (dump_id), ".t%02d", phase) < 0)
760     dump_id[0] = '\0';
761
762   name = concat (dump_base_name, dump_id, dfi->suffix, NULL);
763   stream = fopen (name, dfi->state < 0 ? "w" : "a");
764   if (!stream)
765     error ("could not open dump file `%s': %s", name, strerror (errno));
766   else
767     dfi->state = 1;
768   free (name);
769
770   if (flag_ptr)
771     *flag_ptr = dfi->flags;
772
773   return stream;
774 }
775
776 /* Returns nonzero if tree dump PHASE is enabled.  */
777
778 int
779 dump_enabled_p (enum tree_dump_index phase)
780 {
781   struct dump_file_info *dfi = get_dump_file_info (phase);
782   return dfi->state;
783 }
784
785 /* Returns the switch name of PHASE.  */
786
787 const char *
788 dump_flag_name (enum tree_dump_index phase)
789 {
790   struct dump_file_info *dfi = get_dump_file_info (phase);
791   return dfi->swtch;
792 }
793
794 /* Finish a tree dump for PHASE. STREAM is the stream created by
795    dump_begin.  */
796
797 void
798 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED, FILE *stream)
799 {
800   fclose (stream);
801 }
802
803 /* Enable all tree dumps.  */
804
805 static void
806 dump_enable_all (int flags)
807 {
808   size_t i;
809
810   for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
811     {
812       dump_files[i].state = -1;
813       dump_files[i].flags = flags;
814     }
815
816   for (i = 0; i < extra_dump_files_in_use; i++)
817     {
818       extra_dump_files[i].state = -1;
819       extra_dump_files[i].flags = flags;
820     }
821
822   /* FIXME  -fdump-call-graph is broken.  */
823   dump_files[TDI_xml].state = 0;
824   dump_files[TDI_xml].flags = 0;
825 }
826
827 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
828    relevant details in the dump_files array.  */
829
830 static int
831 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi)
832 {
833   const char *option_value;
834   const char *ptr;
835   int flags;
836
837   option_value = skip_leading_substring (arg, dfi->swtch);
838   if (!option_value)
839     return 0;
840
841   ptr = option_value;
842   flags = 0;
843
844   while (*ptr)
845     {
846       const struct dump_option_value_info *option_ptr;
847       const char *end_ptr;
848       unsigned length;
849
850       while (*ptr == '-')
851         ptr++;
852       end_ptr = strchr (ptr, '-');
853       if (!end_ptr)
854         end_ptr = ptr + strlen (ptr);
855       length = end_ptr - ptr;
856
857       for (option_ptr = dump_options; option_ptr->name; option_ptr++)
858         if (strlen (option_ptr->name) == length
859             && !memcmp (option_ptr->name, ptr, length))
860           {
861             flags |= option_ptr->value;
862             goto found;
863           }
864       warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
865                length, ptr, dfi->swtch);
866     found:;
867       ptr = end_ptr;
868     }
869
870   dfi->state = -1;
871   dfi->flags = flags;
872
873   /* Process -fdump-tree-all by enabling all the known dumps.  */
874   if (dfi->suffix == NULL)
875     dump_enable_all (flags);
876
877   return 1;
878 }
879
880 int
881 dump_switch_p (const char *arg)
882 {
883   size_t i;
884   int any = 0;
885
886   for (i = TDI_none + 1; i != TDI_end; i++)
887     any |= dump_switch_p_1 (arg, &dump_files[i]);
888
889   for (i = 0; i < extra_dump_files_in_use; i++)
890     any |= dump_switch_p_1 (arg, &extra_dump_files[i]);
891
892   return any;
893 }
894
895 /* Dump FUNCTION_DECL FN as tree dump PHASE.  */
896
897 void
898 dump_function (enum tree_dump_index phase, tree fn)
899 {
900   FILE *stream;
901   int flags;
902
903   stream = dump_begin (phase, &flags);
904   if (stream)
905     {
906       dump_function_to_file (fn, stream, flags);
907       dump_end (phase, stream);
908     }
909 }