OSDN Git Service

.:
[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 base;
255       VEC (tree) *accesses = BINFO_BASE_ACCESSES (t);
256
257       dump_child ("type", BINFO_TYPE (t));
258
259       if (BINFO_VIRTUAL_P (t))
260         dump_string (di, "virt");
261
262       dump_int (di, "bases", BINFO_N_BASE_BINFOS (t));
263       for (ix = 0; BINFO_BASE_ITERATE (t, ix, base); ix++)
264         {
265           tree access = (accesses ? VEC_index (tree, accesses, ix)
266                          : access_public_node);
267           const char *string = NULL;
268
269           if (access == access_public_node)
270             string = "pub";
271           else if (access == access_protected_node)
272             string = "prot";
273           else if (access == access_private_node)
274             string = "priv";
275           else
276             abort ();
277
278           dump_string (di, string);
279           queue_and_dump_index (di, "binf", base, DUMP_BINFO);
280         }
281
282       goto done;
283     }
284
285   /* We can knock off a bunch of expression nodes in exactly the same
286      way.  */
287   if (IS_EXPR_CODE_CLASS (code_class))
288     {
289       /* If we're dumping children, dump them now.  */
290       queue_and_dump_type (di, t);
291
292       switch (code_class)
293         {
294         case '1':
295           dump_child ("op 0", TREE_OPERAND (t, 0));
296           break;
297
298         case '2':
299         case '<':
300           dump_child ("op 0", TREE_OPERAND (t, 0));
301           dump_child ("op 1", TREE_OPERAND (t, 1));
302           break;
303
304         case 'e':
305         case 'r':
306         case 's':
307           /* These nodes are handled explicitly below.  */
308           break;
309
310         default:
311           abort ();
312         }
313     }
314   else if (DECL_P (t))
315     {
316       expanded_location xloc;
317       /* All declarations have names.  */
318       if (DECL_NAME (t))
319         dump_child ("name", DECL_NAME (t));
320       if (DECL_ASSEMBLER_NAME_SET_P (t)
321           && DECL_ASSEMBLER_NAME (t) != DECL_NAME (t))
322         dump_child ("mngl", DECL_ASSEMBLER_NAME (t));
323       /* And types.  */
324       queue_and_dump_type (di, t);
325       dump_child ("scpe", DECL_CONTEXT (t));
326       /* And a source position.  */
327       xloc = expand_location (DECL_SOURCE_LOCATION (t));
328       if (xloc.file)
329         {
330           const char *filename = strrchr (xloc.file, '/');
331           if (!filename)
332             filename = xloc.file;
333           else
334             /* Skip the slash.  */
335             ++filename;
336
337           dump_maybe_newline (di);
338           fprintf (di->stream, "srcp: %s:%-6d ", filename,
339                    xloc.line);
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     case REALPART_EXPR:
532     case IMAGPART_EXPR:
533       /* These nodes are unary, but do not have code class `1'.  */
534       dump_child ("op 0", TREE_OPERAND (t, 0));
535       break;
536
537     case TRUTH_ANDIF_EXPR:
538     case TRUTH_ORIF_EXPR:
539     case INIT_EXPR:
540     case MODIFY_EXPR:
541     case COMPOUND_EXPR:
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 COMPONENT_REF:
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 ARRAY_REF:
558     case ARRAY_RANGE_REF:
559       dump_child ("op 0", TREE_OPERAND (t, 0));
560       dump_child ("op 1", TREE_OPERAND (t, 1));
561       dump_child ("op 2", TREE_OPERAND (t, 2));
562       dump_child ("op 3", TREE_OPERAND (t, 3));
563       break;
564
565     case COND_EXPR:
566       dump_child ("op 0", TREE_OPERAND (t, 0));
567       dump_child ("op 1", TREE_OPERAND (t, 1));
568       dump_child ("op 2", TREE_OPERAND (t, 2));
569       break;
570
571     case CALL_EXPR:
572       dump_child ("fn", TREE_OPERAND (t, 0));
573       dump_child ("args", TREE_OPERAND (t, 1));
574       break;
575
576     case CONSTRUCTOR:
577       dump_child ("elts", CONSTRUCTOR_ELTS (t));
578       break;
579
580     case BIND_EXPR:
581       dump_child ("vars", TREE_OPERAND (t, 0));
582       dump_child ("body", TREE_OPERAND (t, 1));
583       break;
584
585     case LOOP_EXPR:
586       dump_child ("body", TREE_OPERAND (t, 0));
587       break;
588
589     case EXIT_EXPR:
590       dump_child ("cond", TREE_OPERAND (t, 0));
591       break;
592
593     case TARGET_EXPR:
594       dump_child ("decl", TREE_OPERAND (t, 0));
595       dump_child ("init", TREE_OPERAND (t, 1));
596       dump_child ("clnp", TREE_OPERAND (t, 2));
597       /* There really are two possible places the initializer can be.
598          After RTL expansion, the second operand is moved to the
599          position of the fourth operand, and the second operand
600          becomes NULL.  */
601       dump_child ("init", TREE_OPERAND (t, 3));
602       break;
603
604     default:
605       /* There are no additional fields to print.  */
606       break;
607     }
608
609  done:
610   if (dump_flag (di, TDF_ADDRESS, NULL))
611     dump_pointer (di, "addr", (void *)t);
612
613   /* Terminate the line.  */
614   fprintf (di->stream, "\n");
615 }
616
617 /* Return nonzero if FLAG has been specified for the dump, and NODE
618    is not the root node of the dump.  */
619
620 int dump_flag (dump_info_p di, int flag, tree node)
621 {
622   return (di->flags & flag) && (node != di->node);
623 }
624
625 /* Dump T, and all its children, on STREAM.  */
626
627 void
628 dump_node (tree t, int flags, FILE *stream)
629 {
630   struct dump_info di;
631   dump_queue_p dq;
632   dump_queue_p next_dq;
633
634   /* Initialize the dump-information structure.  */
635   di.stream = stream;
636   di.index = 0;
637   di.column = 0;
638   di.queue = 0;
639   di.queue_end = 0;
640   di.free_list = 0;
641   di.flags = flags;
642   di.node = t;
643   di.nodes = splay_tree_new (splay_tree_compare_pointers, 0,
644                              (splay_tree_delete_value_fn) &free);
645
646   /* Queue up the first node.  */
647   queue (&di, t, DUMP_NONE);
648
649   /* Until the queue is empty, keep dumping nodes.  */
650   while (di.queue)
651     dequeue_and_dump (&di);
652
653   /* Now, clean up.  */
654   for (dq = di.free_list; dq; dq = next_dq)
655     {
656       next_dq = dq->next;
657       free (dq);
658     }
659   splay_tree_delete (di.nodes);
660 }
661
662 /* Define a tree dump switch.  */
663 struct dump_file_info
664 {
665   const char *suffix;           /* suffix to give output file.  */
666   const char *swtch;            /* command line switch */
667   int flags;                    /* user flags */
668   int state;                    /* state of play */
669 };
670
671 /* Table of tree dump switches. This must be consistent with the
672    TREE_DUMP_INDEX enumeration in tree.h */
673 static struct dump_file_info dump_files[TDI_end] =
674 {
675   {NULL, NULL, 0, 0},
676   {".tu", "translation-unit", 0, 0},
677   {".class", "class-hierarchy", 0, 0},
678   {".original", "tree-original", 0, 0},
679   {".generic", "tree-generic", 0, 0},
680   {".nested", "tree-nested", 0, 0},
681   {".inlined", "tree-inlined", 0, 0},
682   {".vcg", "tree-vcg", 0, 0},
683   {".xml", "call-graph", 0, 0},
684   {NULL, "tree-all", 0, 0},
685 };
686
687 /* Dynamically registered tree dump files and switches.  */
688 static struct dump_file_info *extra_dump_files;
689 static size_t extra_dump_files_in_use;
690 static size_t extra_dump_files_alloced;
691
692 /* Define a name->number mapping for a dump flag value.  */
693 struct dump_option_value_info
694 {
695   const char *const name;       /* the name of the value */
696   const int value;              /* the value of the name */
697 };
698
699 /* Table of dump options. This must be consistent with the TDF_* flags
700    in tree.h */
701 static const struct dump_option_value_info dump_options[] =
702 {
703   {"address", TDF_ADDRESS},
704   {"slim", TDF_SLIM},
705   {"raw", TDF_RAW},
706   {"details", TDF_DETAILS},
707   {"stats", TDF_STATS},
708   {"blocks", TDF_BLOCKS},
709   {"vops", TDF_VOPS},
710   {"lineno", TDF_LINENO},
711   {"uid", TDF_UID},
712   {"all", ~(TDF_RAW | TDF_SLIM | TDF_LINENO)},
713   {NULL, 0}
714 };
715
716 unsigned int
717 dump_register (const char *suffix, const char *swtch)
718 {
719   size_t this = extra_dump_files_in_use++;
720
721   if (this >= extra_dump_files_alloced)
722     {
723       if (extra_dump_files_alloced == 0)
724         extra_dump_files_alloced = 32;
725       else
726         extra_dump_files_alloced *= 2;
727       extra_dump_files = xrealloc (extra_dump_files,
728                                    sizeof (struct dump_file_info)
729                                    * extra_dump_files_alloced);
730     }
731
732   memset (&extra_dump_files[this], 0, sizeof (struct dump_file_info));
733   extra_dump_files[this].suffix = suffix;
734   extra_dump_files[this].swtch = swtch;
735
736   return this + TDI_end;
737 }
738
739 /* Return the dump_file_info for the given phase.  */
740
741 static struct dump_file_info *
742 get_dump_file_info (enum tree_dump_index phase)
743 {
744   if (phase < TDI_end)
745     return &dump_files[phase];
746   else if (phase - TDI_end >= extra_dump_files_in_use)
747     abort ();
748   else
749     return extra_dump_files + (phase - TDI_end);
750 }
751
752
753 /* Begin a tree dump for PHASE. Stores any user supplied flag in
754    *FLAG_PTR and returns a stream to write to. If the dump is not
755    enabled, returns NULL.
756    Multiple calls will reopen and append to the dump file.  */
757
758 FILE *
759 dump_begin (enum tree_dump_index phase, int *flag_ptr)
760 {
761   FILE *stream;
762   char *name;
763   char dump_id[10];
764   struct dump_file_info *dfi;
765
766   if (phase == TDI_none)
767     return NULL;
768
769   dfi = get_dump_file_info (phase);
770   if (dfi->state == 0)
771     return NULL;
772
773   if (snprintf (dump_id, sizeof (dump_id), ".t%02d", phase) < 0)
774     dump_id[0] = '\0';
775
776   name = concat (dump_base_name, dump_id, dfi->suffix, NULL);
777   stream = fopen (name, dfi->state < 0 ? "w" : "a");
778   if (!stream)
779     error ("could not open dump file `%s': %s", name, strerror (errno));
780   else
781     dfi->state = 1;
782   free (name);
783
784   if (flag_ptr)
785     *flag_ptr = dfi->flags;
786
787   return stream;
788 }
789
790 /* Returns nonzero if tree dump PHASE is enabled.  */
791
792 int
793 dump_enabled_p (enum tree_dump_index phase)
794 {
795   struct dump_file_info *dfi = get_dump_file_info (phase);
796   return dfi->state;
797 }
798
799 /* Returns the switch name of PHASE.  */
800
801 const char *
802 dump_flag_name (enum tree_dump_index phase)
803 {
804   struct dump_file_info *dfi = get_dump_file_info (phase);
805   return dfi->swtch;
806 }
807
808 /* Finish a tree dump for PHASE. STREAM is the stream created by
809    dump_begin.  */
810
811 void
812 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED, FILE *stream)
813 {
814   fclose (stream);
815 }
816
817 /* Enable all tree dumps.  */
818
819 static void
820 dump_enable_all (int flags)
821 {
822   size_t i;
823
824   for (i = TDI_none + 1; i < (size_t) TDI_end; i++)
825     {
826       dump_files[i].state = -1;
827       dump_files[i].flags = flags;
828     }
829
830   for (i = 0; i < extra_dump_files_in_use; i++)
831     {
832       extra_dump_files[i].state = -1;
833       extra_dump_files[i].flags = flags;
834     }
835
836   /* FIXME  -fdump-call-graph is broken.  */
837   dump_files[TDI_xml].state = 0;
838   dump_files[TDI_xml].flags = 0;
839 }
840
841 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
842    relevant details in the dump_files array.  */
843
844 static int
845 dump_switch_p_1 (const char *arg, struct dump_file_info *dfi)
846 {
847   const char *option_value;
848   const char *ptr;
849   int flags;
850
851   option_value = skip_leading_substring (arg, dfi->swtch);
852   if (!option_value)
853     return 0;
854
855   ptr = option_value;
856   flags = 0;
857
858   while (*ptr)
859     {
860       const struct dump_option_value_info *option_ptr;
861       const char *end_ptr;
862       unsigned length;
863
864       while (*ptr == '-')
865         ptr++;
866       end_ptr = strchr (ptr, '-');
867       if (!end_ptr)
868         end_ptr = ptr + strlen (ptr);
869       length = end_ptr - ptr;
870
871       for (option_ptr = dump_options; option_ptr->name; option_ptr++)
872         if (strlen (option_ptr->name) == length
873             && !memcmp (option_ptr->name, ptr, length))
874           {
875             flags |= option_ptr->value;
876             goto found;
877           }
878       warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
879                length, ptr, dfi->swtch);
880     found:;
881       ptr = end_ptr;
882     }
883
884   dfi->state = -1;
885   dfi->flags = flags;
886
887   /* Process -fdump-tree-all by enabling all the known dumps.  */
888   if (dfi->suffix == NULL)
889     dump_enable_all (flags);
890
891   return 1;
892 }
893
894 int
895 dump_switch_p (const char *arg)
896 {
897   size_t i;
898   int any = 0;
899
900   for (i = TDI_none + 1; i != TDI_end; i++)
901     any |= dump_switch_p_1 (arg, &dump_files[i]);
902
903   for (i = 0; i < extra_dump_files_in_use; i++)
904     any |= dump_switch_p_1 (arg, &extra_dump_files[i]);
905
906   return any;
907 }
908
909 /* Dump FUNCTION_DECL FN as tree dump PHASE.  */
910
911 void
912 dump_function (enum tree_dump_index phase, tree fn)
913 {
914   FILE *stream;
915   int flags;
916
917   stream = dump_begin (phase, &flags);
918   if (stream)
919     {
920       dump_function_to_file (fn, stream, flags);
921       dump_end (phase, stream);
922     }
923 }