1 /* Tree-dumping functionality for intermediate representation.
2 Copyright (C) 1999-2025 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "tree-pretty-print.h"
26 #include "tree-dump.h"
27 #include "langhooks.h"
28 #include "tree-iterator.h"
30 static unsigned int queue (dump_info_p
, const_tree
, int);
31 static void dump_index (dump_info_p
, unsigned int);
32 static void dequeue_and_dump (dump_info_p
);
33 static void dump_new_line (dump_info_p
);
34 static void dump_maybe_newline (dump_info_p
);
36 /* Add T to the end of the queue of nodes to dump. Returns the index
40 queue (dump_info_p di
, const_tree t
, int flags
)
46 /* Assign the next available index to T. */
49 /* Obtain a new queue node. */
53 di
->free_list
= dq
->next
;
56 dq
= XNEW (struct dump_queue
);
58 /* Create a new entry in the splay-tree. */
59 dni
= XNEW (struct dump_node_info
);
61 dni
->binfo_p
= ((flags
& DUMP_BINFO
) != 0);
62 dq
->node
= splay_tree_insert (di
->nodes
, (splay_tree_key
) t
,
63 (splay_tree_value
) dni
);
65 /* Add it to the end of the queue. */
70 di
->queue_end
->next
= dq
;
73 /* Return the index. */
78 dump_index (dump_info_p di
, unsigned int index
)
80 fprintf (di
->stream
, "@%-6u ", index
);
84 /* If T has not already been output, queue it for subsequent output.
85 FIELD is a string to print before printing the index. Then, the
86 index of T is printed. */
89 queue_and_dump_index (dump_info_p di
, const char *field
, const_tree t
, int flags
)
94 /* If there's no node, just return. This makes for fewer checks in
99 /* See if we've already queued or dumped this node. */
100 n
= splay_tree_lookup (di
->nodes
, (splay_tree_key
) t
);
102 index
= ((dump_node_info_p
) n
->value
)->index
;
104 /* If we haven't, add it to the queue. */
105 index
= queue (di
, t
, flags
);
107 /* Print the index of the node. */
108 dump_maybe_newline (di
);
109 fprintf (di
->stream
, "%-4s: ", field
);
111 dump_index (di
, index
);
114 /* Dump the type of T. */
117 queue_and_dump_type (dump_info_p di
, const_tree t
)
119 queue_and_dump_index (di
, "type", TREE_TYPE (t
), DUMP_NONE
);
122 /* Dump column control */
123 #define SOL_COLUMN 25 /* Start of line column. */
124 #define EOL_COLUMN 55 /* End of line column. */
125 #define COLUMN_ALIGNMENT 15 /* Alignment. */
127 /* Insert a new line in the dump output, and indent to an appropriate
128 place to start printing more fields. */
131 dump_new_line (dump_info_p di
)
133 fprintf (di
->stream
, "\n%*s", SOL_COLUMN
, "");
134 di
->column
= SOL_COLUMN
;
137 /* If necessary, insert a new line. */
140 dump_maybe_newline (dump_info_p di
)
144 /* See if we need a new line. */
145 if (di
->column
> EOL_COLUMN
)
147 /* See if we need any padding. */
148 else if ((extra
= (di
->column
- SOL_COLUMN
) % COLUMN_ALIGNMENT
) != 0)
150 fprintf (di
->stream
, "%*s", COLUMN_ALIGNMENT
- extra
, "");
151 di
->column
+= COLUMN_ALIGNMENT
- extra
;
155 /* Dump pointer PTR using FIELD to identify it. */
158 dump_pointer (dump_info_p di
, const char *field
, void *ptr
)
160 dump_maybe_newline (di
);
161 fprintf (di
->stream
, "%-4s: %-8" HOST_WIDE_INT_PRINT
"x ", field
,
162 (unsigned HOST_WIDE_INT
) (uintptr_t) ptr
);
166 /* Dump integer I using FIELD to identify it. */
169 dump_int (dump_info_p di
, const char *field
, int i
)
171 dump_maybe_newline (di
);
172 fprintf (di
->stream
, "%-4s: %-7d ", field
, i
);
176 /* Dump the floating point value R, using FIELD to identify it. */
179 dump_real (dump_info_p di
, const char *field
, const REAL_VALUE_TYPE
*r
)
182 real_to_decimal (buf
, r
, sizeof (buf
), 0, true);
183 dump_maybe_newline (di
);
184 fprintf (di
->stream
, "%-4s: %s ", field
, buf
);
185 di
->column
+= strlen (buf
) + 7;
188 /* Dump the fixed-point value F, using FIELD to identify it. */
191 dump_fixed (dump_info_p di
, const char *field
, const FIXED_VALUE_TYPE
*f
)
194 fixed_to_decimal (buf
, f
, sizeof (buf
));
195 dump_maybe_newline (di
);
196 fprintf (di
->stream
, "%-4s: %s ", field
, buf
);
197 di
->column
+= strlen (buf
) + 7;
201 /* Dump the string S. */
204 dump_string (dump_info_p di
, const char *string
)
206 dump_maybe_newline (di
);
207 fprintf (di
->stream
, "%-13s ", string
);
208 if (strlen (string
) > 13)
209 di
->column
+= strlen (string
) + 1;
214 /* Dump the string field S. */
217 dump_string_field (dump_info_p di
, const char *field
, const char *string
)
219 dump_maybe_newline (di
);
220 fprintf (di
->stream
, "%-4s: %-7s ", field
, string
);
221 if (strlen (string
) > 7)
222 di
->column
+= 6 + strlen (string
) + 1;
227 /* Dump the next node in the queue. */
230 dequeue_and_dump (dump_info_p di
)
234 dump_node_info_p dni
;
238 enum tree_code_class code_class
;
239 const char* code_name
;
241 /* Get the next node from the queue. */
245 dni
= (dump_node_info_p
) stn
->value
;
248 /* Remove the node from the queue, and put it on the free list. */
249 di
->queue
= dq
->next
;
252 dq
->next
= di
->free_list
;
255 /* Print the node index. */
256 dump_index (di
, index
);
257 /* And the type of node this is. */
261 code_name
= get_tree_code_name (TREE_CODE (t
));
262 fprintf (di
->stream
, "%-16s ", code_name
);
265 /* Figure out what kind of node this is. */
266 code
= TREE_CODE (t
);
267 code_class
= TREE_CODE_CLASS (code
);
269 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
275 vec
<tree
, va_gc
> *accesses
= BINFO_BASE_ACCESSES (t
);
277 dump_child ("type", BINFO_TYPE (t
));
279 if (BINFO_VIRTUAL_P (t
))
280 dump_string_field (di
, "spec", "virt");
282 dump_int (di
, "bases", BINFO_N_BASE_BINFOS (t
));
283 for (ix
= 0; BINFO_BASE_ITERATE (t
, ix
, base
); ix
++)
285 tree access
= (accesses
? (*accesses
)[ix
] : access_public_node
);
286 const char *string
= NULL
;
288 if (access
== access_public_node
)
290 else if (access
== access_protected_node
)
292 else if (access
== access_private_node
)
297 dump_string_field (di
, "accs", string
);
298 queue_and_dump_index (di
, "binf", base
, DUMP_BINFO
);
304 /* We can knock off a bunch of expression nodes in exactly the same
306 if (IS_EXPR_CODE_CLASS (code_class
))
308 /* If we're dumping children, dump them now. */
309 queue_and_dump_type (di
, t
);
314 dump_child ("op 0", TREE_OPERAND (t
, 0));
319 dump_child ("op 0", TREE_OPERAND (t
, 0));
320 dump_child ("op 1", TREE_OPERAND (t
, 1));
327 /* These nodes are handled explicitly below. */
336 expanded_location xloc
;
337 /* All declarations have names. */
339 dump_child ("name", DECL_NAME (t
));
340 if (HAS_DECL_ASSEMBLER_NAME_P (t
)
341 && DECL_ASSEMBLER_NAME_SET_P (t
)
342 && DECL_ASSEMBLER_NAME (t
) != DECL_NAME (t
))
343 dump_child ("mngl", DECL_ASSEMBLER_NAME (t
));
344 if (DECL_ABSTRACT_ORIGIN (t
))
345 dump_child ("orig", DECL_ABSTRACT_ORIGIN (t
));
347 queue_and_dump_type (di
, t
);
348 dump_child ("scpe", DECL_CONTEXT (t
));
349 /* And a source position. */
350 xloc
= expand_location (DECL_SOURCE_LOCATION (t
));
353 const char *filename
= lbasename (xloc
.file
);
355 dump_maybe_newline (di
);
356 fprintf (di
->stream
, "srcp: %s:%-6d ", filename
,
358 di
->column
+= 6 + strlen (filename
) + 8;
360 /* And any declaration can be compiler-generated. */
361 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_DECL_COMMON
)
362 && DECL_ARTIFICIAL (t
))
363 dump_string_field (di
, "note", "artificial");
364 if (DECL_CHAIN (t
) && !dump_flag (di
, TDF_SLIM
, NULL
))
365 dump_child ("chain", DECL_CHAIN (t
));
367 else if (code_class
== tcc_type
)
369 /* All types have qualifiers. */
370 int quals
= lang_hooks
.tree_dump
.type_quals (t
);
372 if (quals
!= TYPE_UNQUALIFIED
)
374 fprintf (di
->stream
, "qual: %c%c%c ",
375 (quals
& TYPE_QUAL_CONST
) ? 'c' : ' ',
376 (quals
& TYPE_QUAL_VOLATILE
) ? 'v' : ' ',
377 (quals
& TYPE_QUAL_RESTRICT
) ? 'r' : ' ');
381 /* All types have associated declarations. */
382 dump_child ("name", TYPE_NAME (t
));
384 /* All types have a main variant. */
385 if (TYPE_MAIN_VARIANT (t
) != t
)
386 dump_child ("unql", TYPE_MAIN_VARIANT (t
));
389 dump_child ("size", TYPE_SIZE (t
));
391 /* All types have alignments. */
392 dump_int (di
, "algn", TYPE_ALIGN (t
));
394 else if (code_class
== tcc_constant
)
395 /* All constants can have types. */
396 queue_and_dump_type (di
, t
);
398 /* Give the language-specific code a chance to print something. If
399 it's completely taken care of things, don't bother printing
400 anything more ourselves. */
401 if (lang_hooks
.tree_dump
.dump_tree (di
, t
))
404 /* Now handle the various kinds of nodes. */
409 case IDENTIFIER_NODE
:
410 dump_string_field (di
, "strg", IDENTIFIER_POINTER (t
));
411 dump_int (di
, "lngt", IDENTIFIER_LENGTH (t
));
415 dump_child ("purp", TREE_PURPOSE (t
));
416 dump_child ("valu", TREE_VALUE (t
));
417 dump_child ("chan", TREE_CHAIN (t
));
422 tree_stmt_iterator it
;
423 for (i
= 0, it
= tsi_start (t
); !tsi_end_p (it
); tsi_next (&it
), i
++)
426 sprintf (buffer
, "%u", i
);
427 dump_child (buffer
, tsi_stmt (it
));
433 dump_int (di
, "lngt", TREE_VEC_LENGTH (t
));
434 for (i
= 0; i
< TREE_VEC_LENGTH (t
); ++i
)
437 sprintf (buffer
, "%u", i
);
438 dump_child (buffer
, TREE_VEC_ELT (t
, i
));
444 dump_int (di
, "prec", TYPE_PRECISION (t
));
445 dump_string_field (di
, "sign",
446 TYPE_UNSIGNED (t
) ? "unsigned" : "signed");
447 dump_child ("min", TYPE_MIN_VALUE (t
));
448 dump_child ("max", TYPE_MAX_VALUE (t
));
450 if (code
== ENUMERAL_TYPE
)
451 dump_child ("csts", TYPE_VALUES (t
));
455 dump_int (di
, "prec", TYPE_PRECISION (t
));
458 case FIXED_POINT_TYPE
:
459 dump_int (di
, "prec", TYPE_PRECISION (t
));
460 dump_string_field (di
, "sign",
461 TYPE_UNSIGNED (t
) ? "unsigned" : "signed");
462 dump_string_field (di
, "saturating",
464 ? "saturating" : "non-saturating");
468 dump_child ("ptd", TREE_TYPE (t
));
472 dump_child ("refd", TREE_TYPE (t
));
476 dump_child ("clas", TYPE_METHOD_BASETYPE (t
));
480 dump_child ("retn", TREE_TYPE (t
));
481 dump_child ("prms", TYPE_ARG_TYPES (t
));
485 dump_child ("elts", TREE_TYPE (t
));
486 dump_child ("domn", TYPE_DOMAIN (t
));
491 if (TREE_CODE (t
) == RECORD_TYPE
)
492 dump_string_field (di
, "tag", "struct");
494 dump_string_field (di
, "tag", "union");
496 dump_child ("flds", TYPE_FIELDS (t
));
497 queue_and_dump_index (di
, "binf", TYPE_BINFO (t
),
502 dump_child ("cnst", DECL_INITIAL (t
));
505 case DEBUG_EXPR_DECL
:
506 dump_int (di
, "-uid", DEBUG_TEMP_UID (t
));
513 if (TREE_CODE (t
) == PARM_DECL
)
514 dump_child ("argt", DECL_ARG_TYPE (t
));
516 dump_child ("init", DECL_INITIAL (t
));
517 dump_child ("size", DECL_SIZE (t
));
518 dump_int (di
, "algn", DECL_ALIGN (t
));
520 if (TREE_CODE (t
) == FIELD_DECL
)
522 if (DECL_FIELD_OFFSET (t
))
523 dump_child ("bpos", bit_position (t
));
525 else if (VAR_P (t
) || TREE_CODE (t
) == PARM_DECL
)
527 dump_int (di
, "used", TREE_USED (t
));
528 if (DECL_REGISTER (t
))
529 dump_string_field (di
, "spec", "register");
534 dump_child ("args", DECL_ARGUMENTS (t
));
535 if (DECL_EXTERNAL (t
))
536 dump_string_field (di
, "body", "undefined");
538 dump_string_field (di
, "link", "extern");
540 dump_string_field (di
, "link", "static");
541 if (DECL_SAVED_TREE (t
) && !dump_flag (di
, TDF_SLIM
, t
))
542 dump_child ("body", DECL_SAVED_TREE (t
));
546 fprintf (di
->stream
, "int: ");
547 print_decs (wi::to_wide (t
), di
->stream
);
551 fprintf (di
->stream
, "strg: %-7s ", TREE_STRING_POINTER (t
));
552 dump_int (di
, "lngt", TREE_STRING_LENGTH (t
));
556 dump_real (di
, "valu", TREE_REAL_CST_PTR (t
));
560 dump_fixed (di
, "valu", TREE_FIXED_CST_PTR (t
));
566 case CLEANUP_POINT_EXPR
:
567 case VIEW_CONVERT_EXPR
:
571 /* These nodes are unary, but do not have code class `1'. */
572 dump_child ("op 0", TREE_OPERAND (t
, 0));
575 case TRUTH_ANDIF_EXPR
:
576 case TRUTH_ORIF_EXPR
:
580 case PREDECREMENT_EXPR
:
581 case PREINCREMENT_EXPR
:
582 case POSTDECREMENT_EXPR
:
583 case POSTINCREMENT_EXPR
:
584 /* These nodes are binary, but do not have code class `2'. */
585 dump_child ("op 0", TREE_OPERAND (t
, 0));
586 dump_child ("op 1", TREE_OPERAND (t
, 1));
591 dump_child ("op 0", TREE_OPERAND (t
, 0));
592 dump_child ("op 1", TREE_OPERAND (t
, 1));
593 dump_child ("op 2", TREE_OPERAND (t
, 2));
597 case ARRAY_RANGE_REF
:
598 dump_child ("op 0", TREE_OPERAND (t
, 0));
599 dump_child ("op 1", TREE_OPERAND (t
, 1));
600 dump_child ("op 2", TREE_OPERAND (t
, 2));
601 dump_child ("op 3", TREE_OPERAND (t
, 3));
605 dump_child ("op 0", TREE_OPERAND (t
, 0));
606 dump_child ("op 1", TREE_OPERAND (t
, 1));
607 dump_child ("op 2", TREE_OPERAND (t
, 2));
610 case TRY_FINALLY_EXPR
:
612 dump_child ("op 0", TREE_OPERAND (t
, 0));
613 dump_child ("op 1", TREE_OPERAND (t
, 1));
620 call_expr_arg_iterator iter
;
621 dump_child ("fn", CALL_EXPR_FN (t
));
622 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, t
)
625 sprintf (buffer
, "%u", i
);
626 dump_child (buffer
, arg
);
634 unsigned HOST_WIDE_INT cnt
;
636 dump_int (di
, "lngt", CONSTRUCTOR_NELTS (t
));
637 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), cnt
, index
, value
)
639 dump_child ("idx", index
);
640 dump_child ("val", value
);
646 dump_child ("vars", TREE_OPERAND (t
, 0));
647 dump_child ("body", TREE_OPERAND (t
, 1));
651 dump_child ("body", TREE_OPERAND (t
, 0));
655 dump_child ("cond", TREE_OPERAND (t
, 0));
659 dump_child ("expr", TREE_OPERAND (t
, 0));
663 dump_child ("decl", TREE_OPERAND (t
, 0));
664 dump_child ("init", TREE_OPERAND (t
, 1));
665 dump_child ("clnp", TREE_OPERAND (t
, 2));
666 /* There really are two possible places the initializer can be.
667 After RTL expansion, the second operand is moved to the
668 position of the fourth operand, and the second operand
670 dump_child ("init", TREE_OPERAND (t
, 3));
673 case CASE_LABEL_EXPR
:
674 dump_child ("name", CASE_LABEL (t
));
677 dump_child ("low ", CASE_LOW (t
));
679 dump_child ("high", CASE_HIGH (t
));
683 dump_child ("name", TREE_OPERAND (t
,0));
686 dump_child ("labl", TREE_OPERAND (t
, 0));
689 dump_child ("cond", TREE_OPERAND (t
, 0));
690 dump_child ("body", TREE_OPERAND (t
, 1));
695 fprintf (di
->stream
, "%s\n", omp_clause_code_name
[OMP_CLAUSE_CODE (t
)]);
696 for (i
= 0; i
< omp_clause_num_ops
[OMP_CLAUSE_CODE (t
)]; i
++)
697 dump_child ("op: ", OMP_CLAUSE_OPERAND (t
, i
));
702 dump_child ("expr", OBJ_TYPE_REF_EXPR (t
));
703 dump_child ("obj", OBJ_TYPE_REF_OBJECT (t
));
704 dump_child ("tok", OBJ_TYPE_REF_TOKEN (t
));
708 /* There are no additional fields to print. */
713 if (dump_flag (di
, TDF_ADDRESS
, NULL
))
714 dump_pointer (di
, "addr", (void *)t
);
716 /* Terminate the line. */
717 fprintf (di
->stream
, "\n");
720 /* Return nonzero if FLAG has been specified for the dump, and NODE
721 is not the root node of the dump. */
723 int dump_flag (dump_info_p di
, dump_flags_t flag
, const_tree node
)
725 return (di
->flags
& flag
) && (node
!= di
->node
);
728 /* Dump T, and all its children, on STREAM. */
731 dump_node (const_tree t
, dump_flags_t flags
, FILE *stream
)
735 dump_queue_p next_dq
;
737 /* Initialize the dump-information structure. */
746 di
.nodes
= splay_tree_new (splay_tree_compare_pointers
, 0,
747 splay_tree_delete_pointers
);
749 /* Queue up the first node. */
750 queue (&di
, t
, DUMP_NONE
);
752 /* Until the queue is empty, keep dumping nodes. */
754 dequeue_and_dump (&di
);
757 for (dq
= di
.free_list
; dq
; dq
= next_dq
)
762 splay_tree_delete (di
.nodes
);