1 /* Tree-dumping functionality for intermediate representation.
2 Copyright (C) 1999, 2000, 2002 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
PARAMS ((dump_info_p
, tree
, int));
34 static void dump_index
PARAMS ((dump_info_p
, unsigned int));
35 static void dequeue_and_dump
PARAMS ((dump_info_p
));
36 static void dump_new_line
PARAMS ((dump_info_p
));
37 static void dump_maybe_newline
PARAMS ((dump_info_p
));
38 static void dump_string_field
PARAMS ((dump_info_p
, const char *, const char *));
40 /* Add T to the end of the queue of nodes to dump. Returns the index
53 /* Assign the next available index to T. */
56 /* Obtain a new queue node. */
60 di
->free_list
= dq
->next
;
63 dq
= (dump_queue_p
) xmalloc (sizeof (struct dump_queue
));
65 /* Create a new entry in the splay-tree. */
66 dni
= (dump_node_info_p
) xmalloc (sizeof (struct dump_node_info
));
68 dni
->binfo_p
= ((flags
& DUMP_BINFO
) != 0);
69 dq
->node
= splay_tree_insert (di
->nodes
, (splay_tree_key
) t
,
70 (splay_tree_value
) dni
);
72 /* Add it to the end of the queue. */
77 di
->queue_end
->next
= dq
;
80 /* Return the index. */
85 dump_index (di
, index
)
89 fprintf (di
->stream
, "@%-6u ", index
);
93 /* If T has not already been output, queue it for subsequent output.
94 FIELD is a string to print before printing the index. Then, the
95 index of T is printed. */
98 queue_and_dump_index (di
, field
, t
, flags
)
107 /* If there's no node, just return. This makes for fewer checks in
112 /* See if we've already queued or dumped this node. */
113 n
= splay_tree_lookup (di
->nodes
, (splay_tree_key
) t
);
115 index
= ((dump_node_info_p
) n
->value
)->index
;
117 /* If we haven't, add it to the queue. */
118 index
= queue (di
, t
, flags
);
120 /* Print the index of the node. */
121 dump_maybe_newline (di
);
122 fprintf (di
->stream
, "%-4s: ", field
);
124 dump_index (di
, index
);
127 /* Dump the type of T. */
130 queue_and_dump_type (di
, t
)
134 queue_and_dump_index (di
, "type", TREE_TYPE (t
), DUMP_NONE
);
137 /* Dump column control */
138 #define SOL_COLUMN 25 /* Start of line column. */
139 #define EOL_COLUMN 55 /* End of line column. */
140 #define COLUMN_ALIGNMENT 15 /* Alignment. */
142 /* Insert a new line in the dump output, and indent to an appropriate
143 place to start printing more fields. */
149 fprintf (di
->stream
, "\n%*s", SOL_COLUMN
, "");
150 di
->column
= SOL_COLUMN
;
153 /* If necessary, insert a new line. */
156 dump_maybe_newline (di
)
161 /* See if we need a new line. */
162 if (di
->column
> EOL_COLUMN
)
164 /* See if we need any padding. */
165 else if ((extra
= (di
->column
- SOL_COLUMN
) % COLUMN_ALIGNMENT
) != 0)
167 fprintf (di
->stream
, "%*s", COLUMN_ALIGNMENT
- extra
, "");
168 di
->column
+= COLUMN_ALIGNMENT
- extra
;
172 /* Dump pointer PTR using FIELD to identify it. */
175 dump_pointer (di
, field
, ptr
)
180 dump_maybe_newline (di
);
181 fprintf (di
->stream
, "%-4s: %-8lx ", field
, (long) ptr
);
185 /* Dump integer I using FIELD to identify it. */
188 dump_int (di
, field
, i
)
193 dump_maybe_newline (di
);
194 fprintf (di
->stream
, "%-4s: %-7d ", field
, i
);
198 /* Dump the string S. */
201 dump_string (di
, string
)
205 dump_maybe_newline (di
);
206 fprintf (di
->stream
, "%-13s ", string
);
207 if (strlen (string
) > 13)
208 di
->column
+= strlen (string
) + 1;
213 /* Dump the string field S. */
216 dump_string_field (di
, field
, string
)
221 dump_maybe_newline (di
);
222 fprintf (di
->stream
, "%-4s: %-7s ", field
, string
);
223 if (strlen (string
) > 7)
224 di
->column
+= 6 + strlen (string
) + 1;
229 /* Dump the next node in the queue. */
232 dequeue_and_dump (di
)
237 dump_node_info_p dni
;
242 const char* code_name
;
244 /* Get the next node from the queue. */
248 dni
= (dump_node_info_p
) stn
->value
;
251 /* Remove the node from the queue, and put it on the free list. */
252 di
->queue
= dq
->next
;
255 dq
->next
= di
->free_list
;
258 /* Print the node index. */
259 dump_index (di
, index
);
260 /* And the type of node this is. */
264 code_name
= tree_code_name
[(int) TREE_CODE (t
)];
265 fprintf (di
->stream
, "%-16s ", code_name
);
268 /* Figure out what kind of node this is. */
269 code
= TREE_CODE (t
);
270 code_class
= TREE_CODE_CLASS (code
);
272 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
276 if (TREE_VIA_PUBLIC (t
))
277 dump_string (di
, "pub");
278 else if (TREE_VIA_PROTECTED (t
))
279 dump_string (di
, "prot");
280 else if (TREE_VIA_PRIVATE (t
))
281 dump_string (di
, "priv");
282 if (TREE_VIA_VIRTUAL (t
))
283 dump_string (di
, "virt");
285 dump_child ("type", BINFO_TYPE (t
));
286 dump_child ("base", BINFO_BASETYPES (t
));
291 /* We can knock off a bunch of expression nodes in exactly the same
293 if (IS_EXPR_CODE_CLASS (code_class
))
295 /* If we're dumping children, dump them now. */
296 queue_and_dump_type (di
, t
);
301 dump_child ("op 0", TREE_OPERAND (t
, 0));
306 dump_child ("op 0", TREE_OPERAND (t
, 0));
307 dump_child ("op 1", TREE_OPERAND (t
, 1));
311 /* These nodes are handled explicitly below. */
320 /* All declarations have names. */
322 dump_child ("name", DECL_NAME (t
));
323 if (DECL_ASSEMBLER_NAME_SET_P (t
)
324 && DECL_ASSEMBLER_NAME (t
) != DECL_NAME (t
))
325 dump_child ("mngl", DECL_ASSEMBLER_NAME (t
));
327 queue_and_dump_type (di
, t
);
328 dump_child ("scpe", DECL_CONTEXT (t
));
329 /* And a source position. */
330 if (DECL_SOURCE_FILE (t
))
332 const char *filename
= strrchr (DECL_SOURCE_FILE (t
), '/');
334 filename
= DECL_SOURCE_FILE (t
);
336 /* Skip the slash. */
339 dump_maybe_newline (di
);
340 fprintf (di
->stream
, "srcp: %s:%-6d ", filename
,
341 DECL_SOURCE_LINE (t
));
342 di
->column
+= 6 + strlen (filename
) + 8;
344 /* And any declaration can be compiler-generated. */
345 if (DECL_ARTIFICIAL (t
))
346 dump_string (di
, "artificial");
347 if (TREE_CHAIN (t
) && !dump_flag (di
, TDF_SLIM
, NULL
))
348 dump_child ("chan", TREE_CHAIN (t
));
350 else if (code_class
== 't')
352 /* All types have qualifiers. */
353 int quals
= (*lang_hooks
.tree_dump
.type_quals
) (t
);
355 if (quals
!= TYPE_UNQUALIFIED
)
357 fprintf (di
->stream
, "qual: %c%c%c ",
358 (quals
& TYPE_QUAL_CONST
) ? 'c' : ' ',
359 (quals
& TYPE_QUAL_VOLATILE
) ? 'v' : ' ',
360 (quals
& TYPE_QUAL_RESTRICT
) ? 'r' : ' ');
364 /* All types have associated declarations. */
365 dump_child ("name", TYPE_NAME (t
));
367 /* All types have a main variant. */
368 if (TYPE_MAIN_VARIANT (t
) != t
)
369 dump_child ("unql", TYPE_MAIN_VARIANT (t
));
372 dump_child ("size", TYPE_SIZE (t
));
374 /* All types have alignments. */
375 dump_int (di
, "algn", TYPE_ALIGN (t
));
377 else if (code_class
== 'c')
378 /* All constants can have types. */
379 queue_and_dump_type (di
, t
);
381 /* Give the language-specific code a chance to print something. If
382 it's completely taken care of things, don't bother printing
383 anything more ourselves. */
384 if ((*lang_hooks
.tree_dump
.dump_tree
) (di
, t
))
387 /* Now handle the various kinds of nodes. */
392 case IDENTIFIER_NODE
:
393 dump_string_field (di
, "strg", IDENTIFIER_POINTER (t
));
394 dump_int (di
, "lngt", IDENTIFIER_LENGTH (t
));
398 dump_child ("purp", TREE_PURPOSE (t
));
399 dump_child ("valu", TREE_VALUE (t
));
400 dump_child ("chan", TREE_CHAIN (t
));
404 dump_int (di
, "lngt", TREE_VEC_LENGTH (t
));
405 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
408 sprintf (buffer
, "%u", i
);
409 dump_child (buffer
, TREE_VEC_ELT (t
, i
));
415 dump_int (di
, "prec", TYPE_PRECISION (t
));
416 if (TREE_UNSIGNED (t
))
417 dump_string (di
, "unsigned");
418 dump_child ("min", TYPE_MIN_VALUE (t
));
419 dump_child ("max", TYPE_MAX_VALUE (t
));
421 if (code
== ENUMERAL_TYPE
)
422 dump_child ("csts", TYPE_VALUES (t
));
426 dump_int (di
, "prec", TYPE_PRECISION (t
));
430 dump_child ("ptd", TREE_TYPE (t
));
434 dump_child ("refd", TREE_TYPE (t
));
438 dump_child ("clas", TYPE_METHOD_BASETYPE (t
));
442 dump_child ("retn", TREE_TYPE (t
));
443 dump_child ("prms", TYPE_ARG_TYPES (t
));
447 dump_child ("elts", TREE_TYPE (t
));
448 dump_child ("domn", TYPE_DOMAIN (t
));
453 if (TREE_CODE (t
) == RECORD_TYPE
)
454 dump_string (di
, "struct");
456 dump_string (di
, "union");
458 dump_child ("flds", TYPE_FIELDS (t
));
459 dump_child ("fncs", TYPE_METHODS (t
));
460 queue_and_dump_index (di
, "binf", TYPE_BINFO (t
),
465 dump_child ("cnst", DECL_INITIAL (t
));
472 if (TREE_CODE (t
) == PARM_DECL
)
473 dump_child ("argt", DECL_ARG_TYPE (t
));
475 dump_child ("init", DECL_INITIAL (t
));
476 dump_child ("size", DECL_SIZE (t
));
477 dump_int (di
, "algn", DECL_ALIGN (t
));
479 if (TREE_CODE (t
) == FIELD_DECL
)
481 if (DECL_FIELD_OFFSET (t
))
482 dump_child ("bpos", bit_position (t
));
484 else if (TREE_CODE (t
) == VAR_DECL
485 || TREE_CODE (t
) == PARM_DECL
)
487 dump_int (di
, "used", TREE_USED (t
));
488 if (DECL_REGISTER (t
))
489 dump_string (di
, "register");
494 dump_child ("args", DECL_ARGUMENTS (t
));
495 if (DECL_EXTERNAL (t
))
496 dump_string (di
, "undefined");
498 dump_string (di
, "extern");
500 dump_string (di
, "static");
501 if (DECL_LANG_SPECIFIC (t
) && !dump_flag (di
, TDF_SLIM
, t
))
502 dump_child ("body", DECL_SAVED_TREE (t
));
506 if (TREE_INT_CST_HIGH (t
))
507 dump_int (di
, "high", TREE_INT_CST_HIGH (t
));
508 dump_int (di
, "low", TREE_INT_CST_LOW (t
));
512 fprintf (di
->stream
, "strg: %-7s ", TREE_STRING_POINTER (t
));
513 dump_int (di
, "lngt", TREE_STRING_LENGTH (t
));
519 case CLEANUP_POINT_EXPR
:
521 /* These nodes are unary, but do not have code class `1'. */
522 dump_child ("op 0", TREE_OPERAND (t
, 0));
525 case TRUTH_ANDIF_EXPR
:
526 case TRUTH_ORIF_EXPR
:
532 case PREDECREMENT_EXPR
:
533 case PREINCREMENT_EXPR
:
534 case POSTDECREMENT_EXPR
:
535 case POSTINCREMENT_EXPR
:
536 /* These nodes are binary, but do not have code class `2'. */
537 dump_child ("op 0", TREE_OPERAND (t
, 0));
538 dump_child ("op 1", TREE_OPERAND (t
, 1));
542 dump_child ("op 0", TREE_OPERAND (t
, 0));
543 dump_child ("op 1", TREE_OPERAND (t
, 1));
544 dump_child ("op 2", TREE_OPERAND (t
, 2));
548 dump_child ("fn", TREE_OPERAND (t
, 0));
549 dump_child ("args", TREE_OPERAND (t
, 1));
553 dump_child ("elts", TREE_OPERAND (t
, 1));
557 dump_child ("vars", TREE_OPERAND (t
, 0));
558 dump_child ("body", TREE_OPERAND (t
, 1));
562 dump_child ("body", TREE_OPERAND (t
, 0));
566 dump_child ("cond", TREE_OPERAND (t
, 0));
570 dump_child ("decl", TREE_OPERAND (t
, 0));
571 dump_child ("init", TREE_OPERAND (t
, 1));
572 dump_child ("clnp", TREE_OPERAND (t
, 2));
573 /* There really are two possible places the initializer can be.
574 After RTL expansion, the second operand is moved to the
575 position of the fourth operand, and the second operand
577 dump_child ("init", TREE_OPERAND (t
, 3));
580 case EXPR_WITH_FILE_LOCATION
:
581 dump_child ("expr", EXPR_WFL_NODE (t
));
585 /* There are no additional fields to print. */
590 if (dump_flag (di
, TDF_ADDRESS
, NULL
))
591 dump_pointer (di
, "addr", (void *)t
);
593 /* Terminate the line. */
594 fprintf (di
->stream
, "\n");
597 /* Return nonzero if FLAG has been specified for the dump, and NODE
598 is not the root node of the dump. */
600 int dump_flag (di
, flag
, node
)
605 return (di
->flags
& flag
) && (node
!= di
->node
);
608 /* Dump T, and all its children, on STREAM. */
611 dump_node (t
, flags
, 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 *const suffix
; /* suffix to give output file. */
652 const char *const 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
] =
661 {".tu", "dump-translation-unit", 0, 0},
662 {".class", "dump-class-hierarchy", 0, 0},
663 {".original", "dump-tree-original", 0, 0},
664 {".optimized", "dump-tree-optimized", 0, 0},
665 {".inlined", "dump-tree-inlined", 0, 0},
668 /* Define a name->number mapping for a dump flag value. */
669 struct dump_option_value_info
671 const char *const name
; /* the name of the value */
672 const int value
; /* the value of the name */
675 /* Table of dump options. This must be consistent with the TDF_* flags
677 static const struct dump_option_value_info dump_options
[] =
679 {"address", TDF_ADDRESS
},
685 /* Begin a tree dump for PHASE. Stores any user supplied flag in
686 *FLAG_PTR and returns a stream to write to. If the dump is not
687 enabled, returns NULL.
688 Multiple calls will reopen and append to the dump file. */
691 dump_begin (phase
, flag_ptr
)
692 enum tree_dump_index phase
;
698 if (!dump_files
[phase
].state
)
701 name
= concat (dump_base_name
, dump_files
[phase
].suffix
, NULL
);
702 stream
= fopen (name
, dump_files
[phase
].state
< 0 ? "w" : "a");
704 error ("could not open dump file `%s'", name
);
706 dump_files
[phase
].state
= 1;
709 *flag_ptr
= dump_files
[phase
].flags
;
714 /* Returns nonzero if tree dump PHASE is enabled. */
717 dump_enabled_p (phase
)
718 enum tree_dump_index phase
;
720 return dump_files
[phase
].state
;
723 /* Returns the switch name of PHASE. */
726 dump_flag_name (phase
)
727 enum tree_dump_index phase
;
729 return dump_files
[phase
].swtch
;
732 /* Finish a tree dump for PHASE. STREAM is the stream created by
736 dump_end (phase
, stream
)
737 enum tree_dump_index phase ATTRIBUTE_UNUSED
;
743 /* Parse ARG as a dump switch. Return nonzero if it is, and store the
744 relevant details in the dump_files array. */
751 const char *option_value
;
753 for (ix
= 0; ix
!= TDI_end
; ix
++)
754 if ((option_value
= skip_leading_substring (arg
, dump_files
[ix
].swtch
)))
756 const char *ptr
= option_value
;
761 const struct dump_option_value_info
*option_ptr
;
767 end_ptr
= strchr (ptr
, '-');
769 end_ptr
= ptr
+ strlen (ptr
);
770 length
= end_ptr
- ptr
;
772 for (option_ptr
= dump_options
; option_ptr
->name
;
774 if (strlen (option_ptr
->name
) == length
775 && !memcmp (option_ptr
->name
, ptr
, length
))
777 flags
|= option_ptr
->value
;
780 warning ("ignoring unknown option `%.*s' in `-f%s'",
781 length
, ptr
, dump_files
[ix
].swtch
);
786 dump_files
[ix
].state
= -1;
787 dump_files
[ix
].flags
= flags
;