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"
33 static unsigned int queue (dump_info_p
, tree
, int);
34 static void dump_index (dump_info_p
, unsigned int);
35 static void dequeue_and_dump (dump_info_p
);
36 static void dump_new_line (dump_info_p
);
37 static void dump_maybe_newline (dump_info_p
);
38 static void dump_string_field (dump_info_p
, const char *, const char *);
39 static void dump_enable_all (int);
41 /* Add T to the end of the queue of nodes to dump. Returns the index
45 queue (dump_info_p di
, tree t
, int flags
)
51 /* Assign the next available index to T. */
54 /* Obtain a new queue node. */
58 di
->free_list
= dq
->next
;
61 dq
= xmalloc (sizeof (struct dump_queue
));
63 /* Create a new entry in the splay-tree. */
64 dni
= xmalloc (sizeof (struct dump_node_info
));
66 dni
->binfo_p
= ((flags
& DUMP_BINFO
) != 0);
67 dq
->node
= splay_tree_insert (di
->nodes
, (splay_tree_key
) t
,
68 (splay_tree_value
) dni
);
70 /* Add it to the end of the queue. */
75 di
->queue_end
->next
= dq
;
78 /* Return the index. */
83 dump_index (dump_info_p di
, unsigned int index
)
85 fprintf (di
->stream
, "@%-6u ", index
);
89 /* If T has not already been output, queue it for subsequent output.
90 FIELD is a string to print before printing the index. Then, the
91 index of T is printed. */
94 queue_and_dump_index (dump_info_p di
, const char *field
, tree t
, int flags
)
99 /* If there's no node, just return. This makes for fewer checks in
104 /* See if we've already queued or dumped this node. */
105 n
= splay_tree_lookup (di
->nodes
, (splay_tree_key
) t
);
107 index
= ((dump_node_info_p
) n
->value
)->index
;
109 /* If we haven't, add it to the queue. */
110 index
= queue (di
, t
, flags
);
112 /* Print the index of the node. */
113 dump_maybe_newline (di
);
114 fprintf (di
->stream
, "%-4s: ", field
);
116 dump_index (di
, index
);
119 /* Dump the type of T. */
122 queue_and_dump_type (dump_info_p di
, tree t
)
124 queue_and_dump_index (di
, "type", TREE_TYPE (t
), DUMP_NONE
);
127 /* Dump column control */
128 #define SOL_COLUMN 25 /* Start of line column. */
129 #define EOL_COLUMN 55 /* End of line column. */
130 #define COLUMN_ALIGNMENT 15 /* Alignment. */
132 /* Insert a new line in the dump output, and indent to an appropriate
133 place to start printing more fields. */
136 dump_new_line (dump_info_p di
)
138 fprintf (di
->stream
, "\n%*s", SOL_COLUMN
, "");
139 di
->column
= SOL_COLUMN
;
142 /* If necessary, insert a new line. */
145 dump_maybe_newline (dump_info_p di
)
149 /* See if we need a new line. */
150 if (di
->column
> EOL_COLUMN
)
152 /* See if we need any padding. */
153 else if ((extra
= (di
->column
- SOL_COLUMN
) % COLUMN_ALIGNMENT
) != 0)
155 fprintf (di
->stream
, "%*s", COLUMN_ALIGNMENT
- extra
, "");
156 di
->column
+= COLUMN_ALIGNMENT
- extra
;
160 /* Dump pointer PTR using FIELD to identify it. */
163 dump_pointer (dump_info_p di
, const char *field
, void *ptr
)
165 dump_maybe_newline (di
);
166 fprintf (di
->stream
, "%-4s: %-8lx ", field
, (long) ptr
);
170 /* Dump integer I using FIELD to identify it. */
173 dump_int (dump_info_p di
, const char *field
, int i
)
175 dump_maybe_newline (di
);
176 fprintf (di
->stream
, "%-4s: %-7d ", field
, i
);
180 /* Dump the string S. */
183 dump_string (dump_info_p di
, const char *string
)
185 dump_maybe_newline (di
);
186 fprintf (di
->stream
, "%-13s ", string
);
187 if (strlen (string
) > 13)
188 di
->column
+= strlen (string
) + 1;
193 /* Dump the string field S. */
196 dump_string_field (dump_info_p di
, const char *field
, const char *string
)
198 dump_maybe_newline (di
);
199 fprintf (di
->stream
, "%-4s: %-7s ", field
, string
);
200 if (strlen (string
) > 7)
201 di
->column
+= 6 + strlen (string
) + 1;
206 /* Dump the next node in the queue. */
209 dequeue_and_dump (dump_info_p di
)
213 dump_node_info_p dni
;
218 const char* code_name
;
220 /* Get the next node from the queue. */
224 dni
= (dump_node_info_p
) stn
->value
;
227 /* Remove the node from the queue, and put it on the free list. */
228 di
->queue
= dq
->next
;
231 dq
->next
= di
->free_list
;
234 /* Print the node index. */
235 dump_index (di
, index
);
236 /* And the type of node this is. */
240 code_name
= tree_code_name
[(int) TREE_CODE (t
)];
241 fprintf (di
->stream
, "%-16s ", code_name
);
244 /* Figure out what kind of node this is. */
245 code
= TREE_CODE (t
);
246 code_class
= TREE_CODE_CLASS (code
);
248 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
253 tree bases
= BINFO_BASETYPES (t
);
254 unsigned n_bases
= bases
? TREE_VEC_LENGTH (bases
): 0;
255 tree accesses
= BINFO_BASEACCESSES (t
);
257 dump_child ("type", BINFO_TYPE (t
));
259 if (TREE_VIA_VIRTUAL (t
))
260 dump_string (di
, "virt");
262 dump_int (di
, "bases", n_bases
);
263 for (ix
= 0; ix
!= n_bases
; ix
++)
265 tree base
= TREE_VEC_ELT (bases
, ix
);
266 tree access
= (accesses
? TREE_VEC_ELT (accesses
, ix
)
267 : access_public_node
);
268 const char *string
= NULL
;
270 if (access
== access_public_node
)
272 else if (access
== access_protected_node
)
274 else if (access
== access_private_node
)
279 dump_string (di
, string
);
280 queue_and_dump_index (di
, "binf", base
, DUMP_BINFO
);
286 /* We can knock off a bunch of expression nodes in exactly the same
288 if (IS_EXPR_CODE_CLASS (code_class
))
290 /* If we're dumping children, dump them now. */
291 queue_and_dump_type (di
, t
);
296 dump_child ("op 0", TREE_OPERAND (t
, 0));
301 dump_child ("op 0", TREE_OPERAND (t
, 0));
302 dump_child ("op 1", TREE_OPERAND (t
, 1));
308 /* These nodes are handled explicitly below. */
317 /* All declarations have names. */
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
));
324 queue_and_dump_type (di
, t
);
325 dump_child ("scpe", DECL_CONTEXT (t
));
326 /* And a source position. */
327 if (DECL_SOURCE_FILE (t
))
329 const char *filename
= strrchr (DECL_SOURCE_FILE (t
), '/');
331 filename
= DECL_SOURCE_FILE (t
);
333 /* Skip the slash. */
336 dump_maybe_newline (di
);
337 fprintf (di
->stream
, "srcp: %s:%-6d ", filename
,
338 DECL_SOURCE_LINE (t
));
339 di
->column
+= 6 + strlen (filename
) + 8;
341 /* And any declaration can be compiler-generated. */
342 if (DECL_ARTIFICIAL (t
))
343 dump_string (di
, "artificial");
344 if (TREE_CHAIN (t
) && !dump_flag (di
, TDF_SLIM
, NULL
))
345 dump_child ("chan", TREE_CHAIN (t
));
347 else if (code_class
== 't')
349 /* All types have qualifiers. */
350 int quals
= lang_hooks
.tree_dump
.type_quals (t
);
352 if (quals
!= TYPE_UNQUALIFIED
)
354 fprintf (di
->stream
, "qual: %c%c%c ",
355 (quals
& TYPE_QUAL_CONST
) ? 'c' : ' ',
356 (quals
& TYPE_QUAL_VOLATILE
) ? 'v' : ' ',
357 (quals
& TYPE_QUAL_RESTRICT
) ? 'r' : ' ');
361 /* All types have associated declarations. */
362 dump_child ("name", TYPE_NAME (t
));
364 /* All types have a main variant. */
365 if (TYPE_MAIN_VARIANT (t
) != t
)
366 dump_child ("unql", TYPE_MAIN_VARIANT (t
));
369 dump_child ("size", TYPE_SIZE (t
));
371 /* All types have alignments. */
372 dump_int (di
, "algn", TYPE_ALIGN (t
));
374 else if (code_class
== 'c')
375 /* All constants can have types. */
376 queue_and_dump_type (di
, t
);
378 /* Give the language-specific code a chance to print something. If
379 it's completely taken care of things, don't bother printing
380 anything more ourselves. */
381 if (lang_hooks
.tree_dump
.dump_tree (di
, t
))
384 /* Now handle the various kinds of nodes. */
389 case IDENTIFIER_NODE
:
390 dump_string_field (di
, "strg", IDENTIFIER_POINTER (t
));
391 dump_int (di
, "lngt", IDENTIFIER_LENGTH (t
));
395 dump_child ("purp", TREE_PURPOSE (t
));
396 dump_child ("valu", TREE_VALUE (t
));
397 dump_child ("chan", TREE_CHAIN (t
));
401 dump_int (di
, "lngt", TREE_VEC_LENGTH (t
));
402 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
405 sprintf (buffer
, "%u", i
);
406 dump_child (buffer
, TREE_VEC_ELT (t
, i
));
412 dump_int (di
, "prec", TYPE_PRECISION (t
));
413 if (TYPE_UNSIGNED (t
))
414 dump_string (di
, "unsigned");
415 dump_child ("min", TYPE_MIN_VALUE (t
));
416 dump_child ("max", TYPE_MAX_VALUE (t
));
418 if (code
== ENUMERAL_TYPE
)
419 dump_child ("csts", TYPE_VALUES (t
));
423 dump_int (di
, "prec", TYPE_PRECISION (t
));
427 dump_child ("ptd", TREE_TYPE (t
));
431 dump_child ("refd", TREE_TYPE (t
));
435 dump_child ("clas", TYPE_METHOD_BASETYPE (t
));
439 dump_child ("retn", TREE_TYPE (t
));
440 dump_child ("prms", TYPE_ARG_TYPES (t
));
444 dump_child ("elts", TREE_TYPE (t
));
445 dump_child ("domn", TYPE_DOMAIN (t
));
450 if (TREE_CODE (t
) == RECORD_TYPE
)
451 dump_string (di
, "struct");
453 dump_string (di
, "union");
455 dump_child ("flds", TYPE_FIELDS (t
));
456 dump_child ("fncs", TYPE_METHODS (t
));
457 queue_and_dump_index (di
, "binf", TYPE_BINFO (t
),
462 dump_child ("cnst", DECL_INITIAL (t
));
469 if (TREE_CODE (t
) == PARM_DECL
)
470 dump_child ("argt", DECL_ARG_TYPE (t
));
472 dump_child ("init", DECL_INITIAL (t
));
473 dump_child ("size", DECL_SIZE (t
));
474 dump_int (di
, "algn", DECL_ALIGN (t
));
476 if (TREE_CODE (t
) == FIELD_DECL
)
478 if (DECL_FIELD_OFFSET (t
))
479 dump_child ("bpos", bit_position (t
));
481 else if (TREE_CODE (t
) == VAR_DECL
482 || TREE_CODE (t
) == PARM_DECL
)
484 dump_int (di
, "used", TREE_USED (t
));
485 if (DECL_REGISTER (t
))
486 dump_string (di
, "register");
491 dump_child ("args", DECL_ARGUMENTS (t
));
492 if (DECL_EXTERNAL (t
))
493 dump_string (di
, "undefined");
495 dump_string (di
, "extern");
497 dump_string (di
, "static");
498 if (DECL_LANG_SPECIFIC (t
) && !dump_flag (di
, TDF_SLIM
, t
))
499 dump_child ("body", DECL_SAVED_TREE (t
));
503 if (TREE_INT_CST_HIGH (t
))
504 dump_int (di
, "high", TREE_INT_CST_HIGH (t
));
505 dump_int (di
, "low", TREE_INT_CST_LOW (t
));
509 fprintf (di
->stream
, "strg: %-7s ", TREE_STRING_POINTER (t
));
510 dump_int (di
, "lngt", TREE_STRING_LENGTH (t
));
516 case CLEANUP_POINT_EXPR
:
518 /* These nodes are unary, but do not have code class `1'. */
519 dump_child ("op 0", TREE_OPERAND (t
, 0));
522 case TRUTH_ANDIF_EXPR
:
523 case TRUTH_ORIF_EXPR
:
529 case PREDECREMENT_EXPR
:
530 case PREINCREMENT_EXPR
:
531 case POSTDECREMENT_EXPR
:
532 case POSTINCREMENT_EXPR
:
533 /* These nodes are binary, but do not have code class `2'. */
534 dump_child ("op 0", TREE_OPERAND (t
, 0));
535 dump_child ("op 1", TREE_OPERAND (t
, 1));
539 dump_child ("op 0", TREE_OPERAND (t
, 0));
540 dump_child ("op 1", TREE_OPERAND (t
, 1));
541 dump_child ("op 2", TREE_OPERAND (t
, 2));
545 dump_child ("fn", TREE_OPERAND (t
, 0));
546 dump_child ("args", TREE_OPERAND (t
, 1));
550 dump_child ("elts", CONSTRUCTOR_ELTS (t
));
554 dump_child ("vars", TREE_OPERAND (t
, 0));
555 dump_child ("body", TREE_OPERAND (t
, 1));
559 dump_child ("body", TREE_OPERAND (t
, 0));
563 dump_child ("cond", TREE_OPERAND (t
, 0));
567 dump_child ("decl", TREE_OPERAND (t
, 0));
568 dump_child ("init", TREE_OPERAND (t
, 1));
569 dump_child ("clnp", TREE_OPERAND (t
, 2));
570 /* There really are two possible places the initializer can be.
571 After RTL expansion, the second operand is moved to the
572 position of the fourth operand, and the second operand
574 dump_child ("init", TREE_OPERAND (t
, 3));
578 /* There are no additional fields to print. */
583 if (dump_flag (di
, TDF_ADDRESS
, NULL
))
584 dump_pointer (di
, "addr", (void *)t
);
586 /* Terminate the line. */
587 fprintf (di
->stream
, "\n");
590 /* Return nonzero if FLAG has been specified for the dump, and NODE
591 is not the root node of the dump. */
593 int dump_flag (dump_info_p di
, int flag
, tree node
)
595 return (di
->flags
& flag
) && (node
!= di
->node
);
598 /* Dump T, and all its children, on STREAM. */
601 dump_node (tree t
, int flags
, FILE *stream
)
605 dump_queue_p next_dq
;
607 /* Initialize the dump-information structure. */
616 di
.nodes
= splay_tree_new (splay_tree_compare_pointers
, 0,
617 (splay_tree_delete_value_fn
) &free
);
619 /* Queue up the first node. */
620 queue (&di
, t
, DUMP_NONE
);
622 /* Until the queue is empty, keep dumping nodes. */
624 dequeue_and_dump (&di
);
627 for (dq
= di
.free_list
; dq
; dq
= next_dq
)
632 splay_tree_delete (di
.nodes
);
635 /* Define a tree dump switch. */
636 struct dump_file_info
638 const char *suffix
; /* suffix to give output file. */
639 const char *swtch
; /* command line switch */
640 int flags
; /* user flags */
641 int state
; /* state of play */
644 /* Table of tree dump switches. This must be consistent with the
645 TREE_DUMP_INDEX enumeration in tree.h */
646 static struct dump_file_info dump_files
[TDI_end
] =
649 {".tu", "translation-unit", 0, 0},
650 {".class", "class-hierarchy", 0, 0},
651 {".original", "tree-original", 0, 0},
652 {".generic", "tree-generic", 0, 0},
653 {".nested", "tree-nested", 0, 0},
654 {".inlined", "tree-inlined", 0, 0},
655 {".vcg", "tree-vcg", 0, 0},
656 {".xml", "call-graph", 0, 0},
657 {NULL
, "tree-all", 0, 0},
660 /* Dynamically registered tree dump files and switches. */
661 static struct dump_file_info
*extra_dump_files
;
662 static size_t extra_dump_files_in_use
;
663 static size_t extra_dump_files_alloced
;
665 /* Define a name->number mapping for a dump flag value. */
666 struct dump_option_value_info
668 const char *const name
; /* the name of the value */
669 const int value
; /* the value of the name */
672 /* Table of dump options. This must be consistent with the TDF_* flags
674 static const struct dump_option_value_info dump_options
[] =
676 {"address", TDF_ADDRESS
},
679 {"details", TDF_DETAILS
},
680 {"stats", TDF_STATS
},
681 {"blocks", TDF_BLOCKS
},
683 {"lineno", TDF_LINENO
},
685 {"all", ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
)},
690 dump_register (const char *suffix
, const char *swtch
)
692 size_t this = extra_dump_files_in_use
++;
694 if (this >= extra_dump_files_alloced
)
696 if (extra_dump_files_alloced
== 0)
697 extra_dump_files_alloced
= 32;
699 extra_dump_files_alloced
*= 2;
700 extra_dump_files
= xrealloc (extra_dump_files
,
701 sizeof (struct dump_file_info
)
702 * extra_dump_files_alloced
);
705 memset (&extra_dump_files
[this], 0, sizeof (struct dump_file_info
));
706 extra_dump_files
[this].suffix
= suffix
;
707 extra_dump_files
[this].swtch
= swtch
;
709 return this + TDI_end
;
712 /* Return the dump_file_info for the given phase. */
714 static struct dump_file_info
*
715 get_dump_file_info (enum tree_dump_index phase
)
718 return &dump_files
[phase
];
719 else if (phase
- TDI_end
>= extra_dump_files_in_use
)
722 return extra_dump_files
+ (phase
- TDI_end
);
726 /* Begin a tree dump for PHASE. Stores any user supplied flag in
727 *FLAG_PTR and returns a stream to write to. If the dump is not
728 enabled, returns NULL.
729 Multiple calls will reopen and append to the dump file. */
732 dump_begin (enum tree_dump_index phase
, int *flag_ptr
)
737 struct dump_file_info
*dfi
;
739 if (phase
== TDI_none
)
742 dfi
= get_dump_file_info (phase
);
746 if (snprintf (dump_id
, sizeof (dump_id
), ".t%02d", phase
) < 0)
749 name
= concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
750 stream
= fopen (name
, dfi
->state
< 0 ? "w" : "a");
752 error ("could not open dump file `%s': %s", name
, strerror (errno
));
758 *flag_ptr
= dfi
->flags
;
763 /* Returns nonzero if tree dump PHASE is enabled. */
766 dump_enabled_p (enum tree_dump_index phase
)
768 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
772 /* Returns the switch name of PHASE. */
775 dump_flag_name (enum tree_dump_index phase
)
777 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
781 /* Finish a tree dump for PHASE. STREAM is the stream created by
785 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED
, FILE *stream
)
790 /* Enable all tree dumps. */
793 dump_enable_all (int flags
)
797 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
799 dump_files
[i
].state
= -1;
800 dump_files
[i
].flags
= flags
;
803 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
805 extra_dump_files
[i
].state
= -1;
806 extra_dump_files
[i
].flags
= flags
;
809 /* FIXME -fdump-call-graph is broken. */
810 dump_files
[TDI_xml
].state
= 0;
811 dump_files
[TDI_xml
].flags
= 0;
814 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
815 relevant details in the dump_files array. */
818 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
)
820 const char *option_value
;
824 option_value
= skip_leading_substring (arg
, dfi
->swtch
);
833 const struct dump_option_value_info
*option_ptr
;
839 end_ptr
= strchr (ptr
, '-');
841 end_ptr
= ptr
+ strlen (ptr
);
842 length
= end_ptr
- ptr
;
844 for (option_ptr
= dump_options
; option_ptr
->name
; option_ptr
++)
845 if (strlen (option_ptr
->name
) == length
846 && !memcmp (option_ptr
->name
, ptr
, length
))
848 flags
|= option_ptr
->value
;
851 warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
852 length
, ptr
, dfi
->swtch
);
860 /* Process -fdump-tree-all by enabling all the known dumps. */
861 if (dfi
->suffix
== NULL
)
862 dump_enable_all (flags
);
868 dump_switch_p (const char *arg
)
873 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
874 any
|= dump_switch_p_1 (arg
, &dump_files
[i
]);
876 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
877 any
|= dump_switch_p_1 (arg
, &extra_dump_files
[i
]);
882 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
885 dump_function (enum tree_dump_index phase
, tree fn
)
890 stream
= dump_begin (phase
, &flags
);
893 dump_function_to_file (fn
, stream
, flags
);
894 dump_end (phase
, stream
);