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
255 VEC (tree
) *accesses
= BINFO_BASE_ACCESSES (t
);
257 dump_child ("type", BINFO_TYPE (t
));
259 if (BINFO_VIRTUAL_P (t
))
260 dump_string (di
, "virt");
262 dump_int (di
, "bases", BINFO_N_BASE_BINFOS (t
));
263 for (ix
= 0; BINFO_BASE_ITERATE (t
, ix
, base
); ix
++)
265 tree access
= (accesses
? VEC_index (tree
, accesses
, ix
)
266 : access_public_node
);
267 const char *string
= NULL
;
269 if (access
== access_public_node
)
271 else if (access
== access_protected_node
)
273 else if (access
== access_private_node
)
278 dump_string (di
, string
);
279 queue_and_dump_index (di
, "binf", base
, DUMP_BINFO
);
285 /* We can knock off a bunch of expression nodes in exactly the same
287 if (IS_EXPR_CODE_CLASS (code_class
))
289 /* If we're dumping children, dump them now. */
290 queue_and_dump_type (di
, t
);
295 dump_child ("op 0", TREE_OPERAND (t
, 0));
300 dump_child ("op 0", TREE_OPERAND (t
, 0));
301 dump_child ("op 1", TREE_OPERAND (t
, 1));
307 /* These nodes are handled explicitly below. */
316 expanded_location xloc
;
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 xloc
= expand_location (DECL_SOURCE_LOCATION (t
));
330 const char *filename
= strrchr (xloc
.file
, '/');
332 filename
= xloc
.file
;
334 /* Skip the slash. */
337 dump_maybe_newline (di
);
338 fprintf (di
->stream
, "srcp: %s:%-6d ", filename
,
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
:
533 /* These nodes are unary, but do not have code class `1'. */
534 dump_child ("op 0", TREE_OPERAND (t
, 0));
537 case TRUTH_ANDIF_EXPR
:
538 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 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));
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));
572 dump_child ("fn", TREE_OPERAND (t
, 0));
573 dump_child ("args", TREE_OPERAND (t
, 1));
577 dump_child ("elts", CONSTRUCTOR_ELTS (t
));
581 dump_child ("vars", TREE_OPERAND (t
, 0));
582 dump_child ("body", TREE_OPERAND (t
, 1));
586 dump_child ("body", TREE_OPERAND (t
, 0));
590 dump_child ("cond", TREE_OPERAND (t
, 0));
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
601 dump_child ("init", TREE_OPERAND (t
, 3));
605 /* There are no additional fields to print. */
610 if (dump_flag (di
, TDF_ADDRESS
, NULL
))
611 dump_pointer (di
, "addr", (void *)t
);
613 /* Terminate the line. */
614 fprintf (di
->stream
, "\n");
617 /* Return nonzero if FLAG has been specified for the dump, and NODE
618 is not the root node of the dump. */
620 int dump_flag (dump_info_p di
, int flag
, tree node
)
622 return (di
->flags
& flag
) && (node
!= di
->node
);
625 /* Dump T, and all its children, on STREAM. */
628 dump_node (tree t
, int flags
, FILE *stream
)
632 dump_queue_p next_dq
;
634 /* Initialize the dump-information structure. */
643 di
.nodes
= splay_tree_new (splay_tree_compare_pointers
, 0,
644 (splay_tree_delete_value_fn
) &free
);
646 /* Queue up the first node. */
647 queue (&di
, t
, DUMP_NONE
);
649 /* Until the queue is empty, keep dumping nodes. */
651 dequeue_and_dump (&di
);
654 for (dq
= di
.free_list
; dq
; dq
= next_dq
)
659 splay_tree_delete (di
.nodes
);
662 /* Define a tree dump switch. */
663 struct dump_file_info
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 */
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
] =
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},
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
;
692 /* Define a name->number mapping for a dump flag value. */
693 struct dump_option_value_info
695 const char *const name
; /* the name of the value */
696 const int value
; /* the value of the name */
699 /* Table of dump options. This must be consistent with the TDF_* flags
701 static const struct dump_option_value_info dump_options
[] =
703 {"address", TDF_ADDRESS
},
706 {"details", TDF_DETAILS
},
707 {"stats", TDF_STATS
},
708 {"blocks", TDF_BLOCKS
},
710 {"lineno", TDF_LINENO
},
712 {"all", ~(TDF_RAW
| TDF_SLIM
| TDF_LINENO
)},
717 dump_register (const char *suffix
, const char *swtch
)
719 size_t this = extra_dump_files_in_use
++;
721 if (this >= extra_dump_files_alloced
)
723 if (extra_dump_files_alloced
== 0)
724 extra_dump_files_alloced
= 32;
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
);
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
;
736 return this + TDI_end
;
739 /* Return the dump_file_info for the given phase. */
741 static struct dump_file_info
*
742 get_dump_file_info (enum tree_dump_index phase
)
745 return &dump_files
[phase
];
746 else if (phase
- TDI_end
>= extra_dump_files_in_use
)
749 return extra_dump_files
+ (phase
- TDI_end
);
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. */
759 dump_begin (enum tree_dump_index phase
, int *flag_ptr
)
764 struct dump_file_info
*dfi
;
766 if (phase
== TDI_none
)
769 dfi
= get_dump_file_info (phase
);
773 if (snprintf (dump_id
, sizeof (dump_id
), ".t%02d", phase
) < 0)
776 name
= concat (dump_base_name
, dump_id
, dfi
->suffix
, NULL
);
777 stream
= fopen (name
, dfi
->state
< 0 ? "w" : "a");
779 error ("could not open dump file `%s': %s", name
, strerror (errno
));
785 *flag_ptr
= dfi
->flags
;
790 /* Returns nonzero if tree dump PHASE is enabled. */
793 dump_enabled_p (enum tree_dump_index phase
)
795 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
799 /* Returns the switch name of PHASE. */
802 dump_flag_name (enum tree_dump_index phase
)
804 struct dump_file_info
*dfi
= get_dump_file_info (phase
);
808 /* Finish a tree dump for PHASE. STREAM is the stream created by
812 dump_end (enum tree_dump_index phase ATTRIBUTE_UNUSED
, FILE *stream
)
817 /* Enable all tree dumps. */
820 dump_enable_all (int flags
)
824 for (i
= TDI_none
+ 1; i
< (size_t) TDI_end
; i
++)
826 dump_files
[i
].state
= -1;
827 dump_files
[i
].flags
= flags
;
830 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
832 extra_dump_files
[i
].state
= -1;
833 extra_dump_files
[i
].flags
= flags
;
836 /* FIXME -fdump-call-graph is broken. */
837 dump_files
[TDI_xml
].state
= 0;
838 dump_files
[TDI_xml
].flags
= 0;
841 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
842 relevant details in the dump_files array. */
845 dump_switch_p_1 (const char *arg
, struct dump_file_info
*dfi
)
847 const char *option_value
;
851 option_value
= skip_leading_substring (arg
, dfi
->swtch
);
860 const struct dump_option_value_info
*option_ptr
;
866 end_ptr
= strchr (ptr
, '-');
868 end_ptr
= ptr
+ strlen (ptr
);
869 length
= end_ptr
- ptr
;
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
))
875 flags
|= option_ptr
->value
;
878 warning ("ignoring unknown option `%.*s' in `-fdump-%s'",
879 length
, ptr
, dfi
->swtch
);
887 /* Process -fdump-tree-all by enabling all the known dumps. */
888 if (dfi
->suffix
== NULL
)
889 dump_enable_all (flags
);
895 dump_switch_p (const char *arg
)
900 for (i
= TDI_none
+ 1; i
!= TDI_end
; i
++)
901 any
|= dump_switch_p_1 (arg
, &dump_files
[i
]);
903 for (i
= 0; i
< extra_dump_files_in_use
; i
++)
904 any
|= dump_switch_p_1 (arg
, &extra_dump_files
[i
]);
909 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
912 dump_function (enum tree_dump_index phase
, tree fn
)
917 stream
= dump_begin (phase
, &flags
);
920 dump_function_to_file (fn
, stream
, flags
);
921 dump_end (phase
, stream
);