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>
5 This file is part of GCC.
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
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
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
24 #include "coretypes.h"
27 #include "splay-tree.h"
28 #include "diagnostic.h"
30 #include "tree-dump.h"
31 #include "langhooks.h"
32 #include "tree-iterator.h"
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);
42 /* Add T to the end of the queue of nodes to dump. Returns the index
46 queue (dump_info_p di
, tree t
, int flags
)
52 /* Assign the next available index to T. */
55 /* Obtain a new queue node. */
59 di
->free_list
= dq
->next
;
62 dq
= xmalloc (sizeof (struct dump_queue
));
64 /* Create a new entry in the splay-tree. */
65 dni
= xmalloc (sizeof (struct dump_node_info
));
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
);
71 /* Add it to the end of the queue. */
76 di
->queue_end
->next
= dq
;
79 /* Return the index. */
84 dump_index (dump_info_p di
, unsigned int index
)
86 fprintf (di
->stream
, "@%-6u ", index
);
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. */
95 queue_and_dump_index (dump_info_p di
, const char *field
, tree t
, int flags
)
100 /* If there's no node, just return. This makes for fewer checks in
105 /* See if we've already queued or dumped this node. */
106 n
= splay_tree_lookup (di
->nodes
, (splay_tree_key
) t
);
108 index
= ((dump_node_info_p
) n
->value
)->index
;
110 /* If we haven't, add it to the queue. */
111 index
= queue (di
, t
, flags
);
113 /* Print the index of the node. */
114 dump_maybe_newline (di
);
115 fprintf (di
->stream
, "%-4s: ", field
);
117 dump_index (di
, index
);
120 /* Dump the type of T. */
123 queue_and_dump_type (dump_info_p di
, tree t
)
125 queue_and_dump_index (di
, "type", TREE_TYPE (t
), DUMP_NONE
);
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. */
133 /* Insert a new line in the dump output, and indent to an appropriate
134 place to start printing more fields. */
137 dump_new_line (dump_info_p di
)
139 fprintf (di
->stream
, "\n%*s", SOL_COLUMN
, "");
140 di
->column
= SOL_COLUMN
;
143 /* If necessary, insert a new line. */
146 dump_maybe_newline (dump_info_p di
)
150 /* See if we need a new line. */
151 if (di
->column
> EOL_COLUMN
)
153 /* See if we need any padding. */
154 else if ((extra
= (di
->column
- SOL_COLUMN
) % COLUMN_ALIGNMENT
) != 0)
156 fprintf (di
->stream
, "%*s", COLUMN_ALIGNMENT
- extra
, "");
157 di
->column
+= COLUMN_ALIGNMENT
- extra
;
161 /* Dump pointer PTR using FIELD to identify it. */
164 dump_pointer (dump_info_p di
, const char *field
, void *ptr
)
166 dump_maybe_newline (di
);
167 fprintf (di
->stream
, "%-4s: %-8lx ", field
, (long) ptr
);
171 /* Dump integer I using FIELD to identify it. */
174 dump_int (dump_info_p di
, const char *field
, int i
)
176 dump_maybe_newline (di
);
177 fprintf (di
->stream
, "%-4s: %-7d ", field
, i
);
181 /* Dump the string S. */
184 dump_string (dump_info_p di
, const char *string
)
186 dump_maybe_newline (di
);
187 fprintf (di
->stream
, "%-13s ", string
);
188 if (strlen (string
) > 13)
189 di
->column
+= strlen (string
) + 1;
194 /* Dump the string field S. */
197 dump_string_field (dump_info_p di
, const char *field
, const char *string
)
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;
207 /* Dump the next node in the queue. */
210 dequeue_and_dump (dump_info_p di
)
214 dump_node_info_p dni
;
219 const char* code_name
;
221 /* Get the next node from the queue. */
225 dni
= (dump_node_info_p
) stn
->value
;
228 /* Remove the node from the queue, and put it on the free list. */
229 di
->queue
= dq
->next
;
232 dq
->next
= di
->free_list
;
235 /* Print the node index. */
236 dump_index (di
, index
);
237 /* And the type of node this is. */
241 code_name
= tree_code_name
[(int) TREE_CODE (t
)];
242 fprintf (di
->stream
, "%-16s ", code_name
);
245 /* Figure out what kind of node this is. */
246 code
= TREE_CODE (t
);
247 code_class
= TREE_CODE_CLASS (code
);
249 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
254 tree bases
= BINFO_BASETYPES (t
);
255 unsigned n_bases
= bases
? TREE_VEC_LENGTH (bases
): 0;
256 tree accesses
= BINFO_BASEACCESSES (t
);
258 dump_child ("type", BINFO_TYPE (t
));
260 if (TREE_VIA_VIRTUAL (t
))
261 dump_string (di
, "virt");
263 dump_int (di
, "bases", n_bases
);
264 for (ix
= 0; ix
!= n_bases
; ix
++)
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
;
271 if (access
== access_public_node
)
273 else if (access
== access_protected_node
)
275 else if (access
== access_private_node
)
280 dump_string (di
, string
);
281 queue_and_dump_index (di
, "binf", base
, DUMP_BINFO
);
287 /* We can knock off a bunch of expression nodes in exactly the same
289 if (IS_EXPR_CODE_CLASS (code_class
))
291 /* If we're dumping children, dump them now. */
292 queue_and_dump_type (di
, t
);
297 dump_child ("op 0", TREE_OPERAND (t
, 0));
302 dump_child ("op 0", TREE_OPERAND (t
, 0));
303 dump_child ("op 1", TREE_OPERAND (t
, 1));
309 /* These nodes are handled explicitly below. */
318 /* All declarations have names. */
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
));
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
))
330 const char *filename
= strrchr (DECL_SOURCE_FILE (t
), '/');
332 filename
= DECL_SOURCE_FILE (t
);
334 /* Skip the slash. */
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;
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
));
348 else if (code_class
== 't')
350 /* All types have qualifiers. */
351 int quals
= lang_hooks
.tree_dump
.type_quals (t
);
353 if (quals
!= TYPE_UNQUALIFIED
)
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' : ' ');
362 /* All types have associated declarations. */
363 dump_child ("name", TYPE_NAME (t
));
365 /* All types have a main variant. */
366 if (TYPE_MAIN_VARIANT (t
) != t
)
367 dump_child ("unql", TYPE_MAIN_VARIANT (t
));
370 dump_child ("size", TYPE_SIZE (t
));
372 /* All types have alignments. */
373 dump_int (di
, "algn", TYPE_ALIGN (t
));
375 else if (code_class
== 'c')
376 /* All constants can have types. */
377 queue_and_dump_type (di
, t
);
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
))
385 /* Now handle the various kinds of nodes. */
390 case IDENTIFIER_NODE
:
391 dump_string_field (di
, "strg", IDENTIFIER_POINTER (t
));
392 dump_int (di
, "lngt", IDENTIFIER_LENGTH (t
));
396 dump_child ("purp", TREE_PURPOSE (t
));
397 dump_child ("valu", TREE_VALUE (t
));
398 dump_child ("chan", TREE_CHAIN (t
));
403 tree_stmt_iterator it
;
404 for (i
= 0, it
= tsi_start (t
); !tsi_end_p (it
); tsi_next (&it
), i
++)
407 sprintf (buffer
, "%u", i
);
408 dump_child (buffer
, tsi_stmt (it
));
414 dump_int (di
, "lngt", TREE_VEC_LENGTH (t
));
415 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
418 sprintf (buffer
, "%u", i
);
419 dump_child (buffer
, TREE_VEC_ELT (t
, i
));
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
));
431 if (code
== ENUMERAL_TYPE
)
432 dump_child ("csts", TYPE_VALUES (t
));
436 dump_int (di
, "prec", TYPE_PRECISION (t
));
440 dump_child ("ptd", TREE_TYPE (t
));
444 dump_child ("refd", TREE_TYPE (t
));
448 dump_child ("clas", TYPE_METHOD_BASETYPE (t
));
452 dump_child ("retn", TREE_TYPE (t
));
453 dump_child ("prms", TYPE_ARG_TYPES (t
));
457 dump_child ("elts", TREE_TYPE (t
));
458 dump_child ("domn", TYPE_DOMAIN (t
));
463 if (TREE_CODE (t
) == RECORD_TYPE
)
464 dump_string (di
, "struct");
466 dump_string (di
, "union");
468 dump_child ("flds", TYPE_FIELDS (t
));
469 dump_child ("fncs", TYPE_METHODS (t
));
470 queue_and_dump_index (di
, "binf", TYPE_BINFO (t
),
475 dump_child ("cnst", DECL_INITIAL (t
));
482 if (TREE_CODE (t
) == PARM_DECL
)
483 dump_child ("argt", DECL_ARG_TYPE (t
));
485 dump_child ("init", DECL_INITIAL (t
));
486 dump_child ("size", DECL_SIZE (t
));
487 dump_int (di
, "algn", DECL_ALIGN (t
));
489 if (TREE_CODE (t
) == FIELD_DECL
)
491 if (DECL_FIELD_OFFSET (t
))
492 dump_child ("bpos", bit_position (t
));
494 else if (TREE_CODE (t
) == VAR_DECL
495 || TREE_CODE (t
) == PARM_DECL
)
497 dump_int (di
, "used", TREE_USED (t
));
498 if (DECL_REGISTER (t
))
499 dump_string (di
, "register");
504 dump_child ("args", DECL_ARGUMENTS (t
));
505 if (DECL_EXTERNAL (t
))
506 dump_string (di
, "undefined");
508 dump_string (di
, "extern");
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
));
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
));
522 fprintf (di
->stream
, "strg: %-7s ", TREE_STRING_POINTER (t
));
523 dump_int (di
, "lngt", TREE_STRING_LENGTH (t
));
529 case CLEANUP_POINT_EXPR
:
531 /* These nodes are unary, but do not have code class `1'. */
532 dump_child ("op 0", TREE_OPERAND (t
, 0));
535 case TRUTH_ANDIF_EXPR
:
536 case TRUTH_ORIF_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));
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));
558 dump_child ("fn", TREE_OPERAND (t
, 0));
559 dump_child ("args", TREE_OPERAND (t
, 1));
563 dump_child ("elts", CONSTRUCTOR_ELTS (t
));
567 dump_child ("vars", TREE_OPERAND (t
, 0));
568 dump_child ("body", TREE_OPERAND (t
, 1));
572 dump_child ("body", TREE_OPERAND (t
, 0));
576 dump_child ("cond", TREE_OPERAND (t
, 0));
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
587 dump_child ("init", TREE_OPERAND (t
, 3));
591 /* There are no additional fields to print. */
596 if (dump_flag (di
, TDF_ADDRESS
, NULL
))
597 dump_pointer (di
, "addr", (void *)t
);
599 /* Terminate the line. */
600 fprintf (di
->stream
, "\n");
603 /* Return nonzero if FLAG has been specified for the dump, and NODE
604 is not the root node of the dump. */
606 int dump_flag (dump_info_p di
, int flag
, tree node
)
608 return (di
->flags
& flag
) && (node
!= di
->node
);
611 /* Dump T, and all its children, on STREAM. */
614 dump_node (tree t
, int flags
, FILE *stream
)
618 dump_queue_p next_dq
;
620 /* Initialize the dump-information structure. */
629 di
.nodes
= splay_tree_new (splay_tree_compare_pointers
, 0,
630 (splay_tree_delete_value_fn
) &free
);
632 /* Queue up the first node. */
633 queue (&di
, t
, DUMP_NONE
);
635 /* Until the queue is empty, keep dumping nodes. */
637 dequeue_and_dump (&di
);
640 for (dq
= di
.free_list
; dq
; dq
= next_dq
)
645 splay_tree_delete (di
.nodes
);
648 /* Define a tree dump switch. */
649 struct dump_file_info
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 */
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
] =
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},
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
;
678 /* Define a name->number mapping for a dump flag value. */
679 struct dump_option_value_info
681 const char *const name
; /* the name of the value */
682 const int value
; /* the value of the name */
685 /* Table of dump options. This must be consistent with the TDF_* flags
687 static const struct dump_option_value_info dump_options
[] =
689 {"address", TDF_ADDRESS
},
692 {"details", TDF_DETAILS
},
693 {"stats", TDF_STATS
},
694 {"blocks", TDF_BLOCKS
},
696 {"lineno", TDF_LINENO
},
698 {"all", ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
)},
703 dump_register (const char *suffix
, const char *swtch
)
705 size_t this = extra_dump_files_in_use
++;
707 if (this >= extra_dump_files_alloced
)
709 if (extra_dump_files_alloced
== 0)
710 extra_dump_files_alloced
= 32;
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
);
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
;
722 return this + TDI_end
;
725 /* Return the dump_file_info for the given phase. */
727 static struct dump_file_info
*
728 get_dump_file_info (enum tree_dump_index phase
)
731 return &dump_files
[phase
];
732 else if (phase
- TDI_end
>= extra_dump_files_in_use
)
735 return extra_dump_files
+ (phase
- TDI_end
);
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. */
745 dump_begin (enum tree_dump_index phase
, int *flag_ptr
)
750 struct dump_file_info
*dfi
;
752 if (phase
== TDI_none
)
755 dfi
= get_dump_file_info (phase
);
759 if (snprintf (dump_id
, sizeof (dump_id
), ".t%02d", phase
) < 0)
762 name
= concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
763 stream
= fopen (name
, dfi
->state
< 0 ? "w" : "a");
765 error ("could not open dump file `%s': %s", name
, strerror (errno
));
771 *flag_ptr
= dfi
->flags
;
776 /* Returns nonzero if tree dump PHASE is enabled. */
779 dump_enabled_p (enum tree_dump_index phase
)
781 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
785 /* Returns the switch name of PHASE. */
788 dump_flag_name (enum tree_dump_index phase
)
790 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
794 /* Finish a tree dump for PHASE. STREAM is the stream created by
798 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED
, FILE *stream
)
803 /* Enable all tree dumps. */
806 dump_enable_all (int flags
)
810 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
812 dump_files
[i
].state
= -1;
813 dump_files
[i
].flags
= flags
;
816 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
818 extra_dump_files
[i
].state
= -1;
819 extra_dump_files
[i
].flags
= flags
;
822 /* FIXME -fdump-call-graph is broken. */
823 dump_files
[TDI_xml
].state
= 0;
824 dump_files
[TDI_xml
].flags
= 0;
827 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
828 relevant details in the dump_files array. */
831 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
)
833 const char *option_value
;
837 option_value
= skip_leading_substring (arg
, dfi
->swtch
);
846 const struct dump_option_value_info
*option_ptr
;
852 end_ptr
= strchr (ptr
, '-');
854 end_ptr
= ptr
+ strlen (ptr
);
855 length
= end_ptr
- ptr
;
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
))
861 flags
|= option_ptr
->value
;
864 warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
865 length
, ptr
, dfi
->swtch
);
873 /* Process -fdump-tree-all by enabling all the known dumps. */
874 if (dfi
->suffix
== NULL
)
875 dump_enable_all (flags
);
881 dump_switch_p (const char *arg
)
886 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
887 any
|= dump_switch_p_1 (arg
, &dump_files
[i
]);
889 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
890 any
|= dump_switch_p_1 (arg
, &extra_dump_files
[i
]);
895 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
898 dump_function (enum tree_dump_index phase
, tree fn
)
903 stream
= dump_begin (phase
, &flags
);
906 dump_function_to_file (fn
, stream
, flags
);
907 dump_end (phase
, stream
);