alias.c (adjust_offset_for_component_ref): Use component_ref_field_offset.
[platform/upstream/gcc.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 COMPOUND_EXPR:
540     case PREDECREMENT_EXPR:
541     case PREINCREMENT_EXPR:
542     case POSTDECREMENT_EXPR:
543     case POSTINCREMENT_EXPR:
544       /* These nodes are binary, but do not have code class `2'.  */
545       dump_child ("op 0", TREE_OPERAND (t, 0));
546       dump_child ("op 1", TREE_OPERAND (t, 1));
547       break;
548
549     case COMPONENT_REF:
550       dump_child ("op 0", TREE_OPERAND (t, 0));
551       dump_child ("op 1", TREE_OPERAND (t, 1));
552       dump_child ("op 2", TREE_OPERAND (t, 2));
553       break;
554
555     case ARRAY_REF:
556     case ARRAY_RANGE_REF:
557       dump_child ("op 0", TREE_OPERAND (t, 0));
558       dump_child ("op 1", TREE_OPERAND (t, 1));
559       dump_child ("op 2", TREE_OPERAND (t, 2));
560       dump_child ("op 3", TREE_OPERAND (t, 3));
561       break;
562
563     case COND_EXPR:
564       dump_child ("op 0", TREE_OPERAND (t, 0));
565       dump_child ("op 1", TREE_OPERAND (t, 1));
566       dump_child ("op 2", TREE_OPERAND (t, 2));
567       break;
568
569     case CALL_EXPR:
570       dump_child ("fn", TREE_OPERAND (t, 0));
571       dump_child ("args", TREE_OPERAND (t, 1));
572       break;
573
574     case CONSTRUCTOR:
575       dump_child ("elts", CONSTRUCTOR_ELTS (t));
576       break;
577
578     case BIND_EXPR:
579       dump_child ("vars", TREE_OPERAND (t, 0));
580       dump_child ("body", TREE_OPERAND (t, 1));
581       break;
582
583     case LOOP_EXPR:
584       dump_child ("body", TREE_OPERAND (t, 0));
585       break;
586
587     case EXIT_EXPR:
588       dump_child ("cond", TREE_OPERAND (t, 0));
589       break;
590
591     case TARGET_EXPR:
592       dump_child ("decl", TREE_OPERAND (t, 0));
593       dump_child ("init", TREE_OPERAND (t, 1));
594       dump_child ("clnp", TREE_OPERAND (t, 2));
595       /* There really are two possible places the initializer can be.
596          After RTL expansion, the second operand is moved to the
597          position of the fourth operand, and the second operand
598          becomes NULL.  */
599       dump_child ("init", TREE_OPERAND (t, 3));
600       break;
601
602     default:
603       /* There are no additional fields to print.  */
604       break;
605     }
606
607  done:
608   if (dump_flag (di, TDF_ADDRESS, NULL))
609     dump_pointer (di, "addr", (void *)t);
610
611   /* Terminate the line.  */
612   fprintf (di->stream, "\n");
613 }
614
615 /* Return nonzero if FLAG has been specified for the dump, and NODE
616    is not the root node of the dump.  */
617
618 int dump_flag (dump_info_p di, int flag, tree node)
619 {
620   return (di->flags & flag) && (node != di->node);
621 }
622
623 /* Dump T, and all its children, on STREAM.  */
624
625 void
626 dump_node (tree t, int flags, FILE *stream)
627 {
628   struct dump_info di;
629   dump_queue_p dq;
630   dump_queue_p next_dq;
631
632   /* Initialize the dump-information structure.  */
633   di.stream = stream;
634   di.index = 0;
635   di.column = 0;
636   di.queue = 0;
637   di.queue_end = 0;
638   di.free_list = 0;
639   di.flags = flags;
640   di.node = t;
641   di.nodes = splay_tree_new (splay_tree_compare_pointers, 0,
642                              (splay_tree_delete_value_fn) &free);
643
644   /* Queue up the first node.  */
645   queue (&di, t, DUMP_NONE);
646
647   /* Until the queue is empty, keep dumping nodes.  */
648   while (di.queue)
649     dequeue_and_dump (&di);
650
651   /* Now, clean up.  */
652   for (dq = di.free_list; dq; dq = next_dq)
653     {
654       next_dq = dq->next;
655       free (dq);
656     }
657   splay_tree_delete (di.nodes);
658 }
659
660 /* Define a tree dump switch.  */
661 struct dump_file_info
662 {
663   const char *suffix;           /* suffix to give output file.  */
664   const char *swtch;            /* command line switch */
665   int flags;                    /* user flags */
666   int state;                    /* state of play */
667 };
668
669 /* Table of tree dump switches. This must be consistent with the
670    TREE_DUMP_INDEX enumeration in tree.h */
671 static struct dump_file_info dump_files[TDI_end] =
672 {
673   {NULL, NULL, 0, 0},
674   {".tu", "translation-unit", 0, 0},
675   {".class", "class-hierarchy", 0, 0},
676   {".original", "tree-original", 0, 0},
677   {".generic", "tree-generic", 0, 0},
678   {".nested", "tree-nested", 0, 0},
679   {".inlined", "tree-inlined", 0, 0},
680   {".vcg", "tree-vcg", 0, 0},
681   {".xml", "call-graph", 0, 0},
682   {NULL, "tree-all", 0, 0},
683 };
684
685 /* Dynamically registered tree dump files and switches.  */
686 static struct dump_file_info *extra_dump_files;
687 static size_t extra_dump_files_in_use;
688 static size_t extra_dump_files_alloced;
689
690 /* Define a name->number mapping for a dump flag value.  */
691 struct dump_option_value_info
692 {
693   const char *const name;       /* the name of the value */
694   const int value;              /* the value of the name */
695 };
696
697 /* Table of dump options. This must be consistent with the TDF_* flags
698    in tree.h */
699 static const struct dump_option_value_info dump_options[] =
700 {
701   {"address", TDF_ADDRESS},
702   {"slim", TDF_SLIM},
703   {"raw", TDF_RAW},
704   {"details", TDF_DETAILS},
705   {"stats", TDF_STATS},
706   {"blocks", TDF_BLOCKS},
707   {"vops", TDF_VOPS},
708   {"lineno", TDF_LINENO},
709   {"uid", TDF_UID},
710   {"all", ~(TDF_RAW | TDF_SLIM | TDF_LINENO)},
711   {NULL, 0}
712 };
713
714 unsigned int
715 dump_register (const char *suffix, const char *swtch)
716 {
717   size_t this = extra_dump_files_in_use++;
718
719   if (this >= extra_dump_files_alloced)
720     {
721       if (extra_dump_files_alloced == 0)
722         extra_dump_files_alloced = 32;
723       else
724         extra_dump_files_alloced *= 2;
725       extra_dump_files = xrealloc (extra_dump_files,
726                                    sizeof (struct dump_file_info)
727                                    * extra_dump_files_alloced);
728     }
729
730   memset (&extra_dump_files[this], 0, sizeof (struct dump_file_info));
731   extra_dump_files[this].suffix = suffix;
732   extra_dump_files[this].swtch = swtch;
733
734   return this + TDI_end;
735 }
736
737 /* Return the dump_file_info for the given phase.  */
738
739 static struct dump_file_info *
740 get_dump_file_info (enum tree_dump_index phase)
741 {
742   if (phase < TDI_end)
743     return &dump_files[phase];
744   else if (phase - TDI_end >= extra_dump_files_in_use)
745     abort ();
746   else
747     return extra_dump_files + (phase - TDI_end);
748 }
749
750
751 /* Begin a tree dump for PHASE. Stores any user supplied flag in
752    *FLAG_PTR and returns a stream to write to. If the dump is not
753    enabled, returns NULL.
754    Multiple calls will reopen and append to the dump file.  */
755
756 FILE *
757 dump_begin (enum tree_dump_index phase, int *flag_ptr)
758 {
759   FILE *stream;
760   char *name;
761   char dump_id[10];
762   struct dump_file_info *dfi;
763
764   if (phase == TDI_none)
765     return NULL;
766
767   dfi = get_dump_file_info (phase);
768   if (dfi->state == 0)
769     return NULL;
770
771   if (snprintf (dump_id, sizeof (dump_id), ".t%02d", phase) < 0)
772     dump_id[0] = '\0';
773
774   name = concat (dump_base_name, dump_id, dfi->suffix, NULL);
775   stream = fopen (name, dfi->state < 0 ? "w" : "a");
776   if (!stream)
777     error ("could not open dump file `%s': %s", name, strerror (errno));
778   else
779     dfi->state = 1;
780   free (name);
781
782   if (flag_ptr)
783     *flag_ptr = dfi->flags;
784
785   return stream;
786 }
787
788 /* Returns nonzero if tree dump PHASE is enabled.  */
789
790 int
791 dump_enabled_p (enum tree_dump_index phase)
792 {
793   struct dump_file_info *dfi = get_dump_file_info (phase);
794   return dfi->state;
795 }
796
797 /* Returns the switch name of PHASE.  */
798
799 const char *
800 dump_flag_name (enum tree_dump_index phase)
801 {
802   struct dump_file_info *dfi = get_dump_file_info (phase);
803   return dfi->swtch;
804 }
805
806 /* Finish a tree dump for PHASE. STREAM is the stream created by
807    dump_begin.  */
808
809 void
810 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED, FILE *stream)
811 {
812   fclose (stream);
813 }
814
815 /* Enable all tree dumps.  */
816
817 static void
818 dump_enable_all (int flags)
819 {
820   size_t i;
821
822   for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
823     {
824       dump_files[i].state = -1;
825       dump_files[i].flags = flags;
826     }
827
828   for (i = 0; i < extra_dump_files_in_use; i++)
829     {
830       extra_dump_files[i].state = -1;
831       extra_dump_files[i].flags = flags;
832     }
833
834   /* FIXME  -fdump-call-graph is broken.  */
835   dump_files[TDI_xml].state = 0;
836   dump_files[TDI_xml].flags = 0;
837 }
838
839 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
840    relevant details in the dump_files array.  */
841
842 static int
843 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi)
844 {
845   const char *option_value;
846   const char *ptr;
847   int flags;
848
849   option_value = skip_leading_substring (arg, dfi->swtch);
850   if (!option_value)
851     return 0;
852
853   ptr = option_value;
854   flags = 0;
855
856   while (*ptr)
857     {
858       const struct dump_option_value_info *option_ptr;
859       const char *end_ptr;
860       unsigned length;
861
862       while (*ptr == '-')
863         ptr++;
864       end_ptr = strchr (ptr, '-');
865       if (!end_ptr)
866         end_ptr = ptr + strlen (ptr);
867       length = end_ptr - ptr;
868
869       for (option_ptr = dump_options; option_ptr->name; option_ptr++)
870         if (strlen (option_ptr->name) == length
871             && !memcmp (option_ptr->name, ptr, length))
872           {
873             flags |= option_ptr->value;
874             goto found;
875           }
876       warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
877                length, ptr, dfi->swtch);
878     found:;
879       ptr = end_ptr;
880     }
881
882   dfi->state = -1;
883   dfi->flags = flags;
884
885   /* Process -fdump-tree-all by enabling all the known dumps.  */
886   if (dfi->suffix == NULL)
887     dump_enable_all (flags);
888
889   return 1;
890 }
891
892 int
893 dump_switch_p (const char *arg)
894 {
895   size_t i;
896   int any = 0;
897
898   for (i = TDI_none + 1; i != TDI_end; i++)
899     any |= dump_switch_p_1 (arg, &dump_files[i]);
900
901   for (i = 0; i < extra_dump_files_in_use; i++)
902     any |= dump_switch_p_1 (arg, &extra_dump_files[i]);
903
904   return any;
905 }
906
907 /* Dump FUNCTION_DECL FN as tree dump PHASE.  */
908
909 void
910 dump_function (enum tree_dump_index phase, tree fn)
911 {
912   FILE *stream;
913   int flags;
914
915   stream = dump_begin (phase, &flags);
916   if (stream)
917     {
918       dump_function_to_file (fn, stream, flags);
919       dump_end (phase, stream);
920     }
921 }