1 /* Tree-dumping functionality for intermediate representation.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "splay-tree.h"
27 #include "diagnostic.h"
31 /* A callback function used dump language-specific parts of tree
32 nodes. Returns non-zero if it does not want the usual dumping of
33 the second argument. */
35 dump_tree_fn lang_dump_tree;
37 static unsigned int queue PARAMS ((dump_info_p, tree, int));
38 static void dump_index PARAMS ((dump_info_p, unsigned int));
39 static void dequeue_and_dump PARAMS ((dump_info_p));
40 static void dump_new_line PARAMS ((dump_info_p));
41 static void dump_maybe_newline PARAMS ((dump_info_p));
42 static void dump_string_field PARAMS ((dump_info_p, const char *, const char *));
43 static void dump_node PARAMS ((tree, FILE *));
45 /* Add T to the end of the queue of nodes to dump. Returns the index
58 /* Assign the next available index to T. */
61 /* Obtain a new queue node. */
65 di->free_list = dq->next;
68 dq = (dump_queue_p) xmalloc (sizeof (struct dump_queue));
70 /* Create a new entry in the splay-tree. */
71 dni = (dump_node_info_p) xmalloc (sizeof (struct dump_node_info));
73 dni->binfo_p = ((flags & DUMP_BINFO) != 0);
74 dq->node = splay_tree_insert (di->nodes, (splay_tree_key) t,
75 (splay_tree_value) dni);
77 /* Add it to the end of the queue. */
82 di->queue_end->next = dq;
85 /* Return the index. */
90 dump_index (di, index)
94 fprintf (di->stream, "@%-6u ", index);
98 /* If T has not already been output, queue it for subsequent output.
99 FIELD is a string to print before printing the index. Then, the
100 index of T is printed. */
103 queue_and_dump_index (di, field, t, flags)
112 /* If there's no node, just return. This makes for fewer checks in
117 /* See if we've already queued or dumped this node. */
118 n = splay_tree_lookup (di->nodes, (splay_tree_key) t);
120 index = ((dump_node_info_p) n->value)->index;
122 /* If we haven't, add it to the queue. */
123 index = queue (di, t, flags);
125 /* Print the index of the node. */
126 dump_maybe_newline (di);
127 fprintf (di->stream, "%-4s: ", field);
129 dump_index (di, index);
132 /* Dump the type of T. */
135 queue_and_dump_type (di, t)
139 queue_and_dump_index (di, "type", TREE_TYPE (t), DUMP_NONE);
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%25s", "");
153 /* If necessary, insert a new line. */
156 dump_maybe_newline (di)
159 /* See if we need a new line. */
162 /* See if we need any padding. */
163 else if ((di->column - 25) % 14 != 0)
165 fprintf (di->stream, "%*s", 14 - ((di->column - 25) % 14), "");
166 di->column += 14 - (di->column - 25) % 14;
170 /* Dump I using FIELD to identity it. */
173 dump_int (di, field, i)
178 dump_maybe_newline (di);
179 fprintf (di->stream, "%-4s: %-7d ", field, i);
183 /* Dump the string S. */
186 dump_string (di, string)
190 dump_maybe_newline (di);
191 fprintf (di->stream, "%-13s ", string);
192 if (strlen (string) > 13)
193 di->column += strlen (string) + 1;
198 /* Dump the string field S. */
201 dump_string_field (di, field, string)
206 dump_maybe_newline (di);
207 fprintf (di->stream, "%-4s: %-7s ", field, string);
208 if (strlen (string) > 7)
209 di->column += 6 + strlen (string) + 1;
214 /* Dump information common to statements from STMT. */
221 dump_int (di, "line", STMT_LINENO (t));
224 /* Dump the next statement after STMT. */
227 dump_next_stmt (di, t)
231 dump_child ("next", TREE_CHAIN (t));
234 /* Dump the next node in the queue. */
237 dequeue_and_dump (di)
242 dump_node_info_p dni;
247 const char* code_name;
249 /* Get the next node from the queue. */
253 dni = (dump_node_info_p) stn->value;
256 /* Remove the node from the queue, and put it on the free list. */
257 di->queue = dq->next;
260 dq->next = di->free_list;
263 /* Print the node index. */
264 dump_index (di, index);
265 /* And the type of node this is. */
269 code_name = tree_code_name[(int) TREE_CODE (t)];
270 fprintf (di->stream, "%-16s ", code_name);
273 /* Figure out what kind of node this is. */
274 code = TREE_CODE (t);
275 code_class = TREE_CODE_CLASS (code);
277 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
281 if (TREE_VIA_PUBLIC (t))
282 dump_string (di, "pub");
283 else if (TREE_VIA_PROTECTED (t))
284 dump_string (di, "prot");
285 else if (TREE_VIA_PRIVATE (t))
286 dump_string (di, "priv");
287 if (TREE_VIA_VIRTUAL (t))
288 dump_string (di, "virt");
290 dump_child ("type", BINFO_TYPE (t));
291 dump_child ("base", BINFO_BASETYPES (t));
296 /* We can knock off a bunch of expression nodes in exactly the same
298 if (IS_EXPR_CODE_CLASS (code_class))
300 /* If we're dumping children, dump them now. */
301 queue_and_dump_type (di, t);
306 dump_child ("op 0", TREE_OPERAND (t, 0));
311 dump_child ("op 0", TREE_OPERAND (t, 0));
312 dump_child ("op 1", TREE_OPERAND (t, 1));
316 /* These nodes are handled explicitly below. */
325 /* All declarations have names. */
327 dump_child ("name", DECL_NAME (t));
328 if (DECL_ASSEMBLER_NAME (t)
329 && DECL_ASSEMBLER_NAME (t) != DECL_NAME (t))
330 dump_child ("mngl", DECL_ASSEMBLER_NAME (t));
332 queue_and_dump_type (di, t);
333 dump_child ("scpe", DECL_CONTEXT (t));
334 /* And a source position. */
335 if (DECL_SOURCE_FILE (t))
337 const char *filename = strrchr (DECL_SOURCE_FILE (t), '/');
339 filename = DECL_SOURCE_FILE (t);
341 /* Skip the slash. */
344 dump_maybe_newline (di);
345 fprintf (di->stream, "srcp: %s:%-6d ", filename,
346 DECL_SOURCE_LINE (t));
347 di->column += 6 + strlen (filename) + 8;
349 /* And any declaration can be compiler-generated. */
350 if (DECL_ARTIFICIAL (t))
351 dump_string (di, "artificial");
353 dump_child ("chan", TREE_CHAIN (t));
355 else if (code_class == 't')
357 /* All types have qualifiers. */
358 int quals = C_TYPE_QUALS (t);
359 if (quals != TYPE_UNQUALIFIED)
361 fprintf (di->stream, "qual: %c%c%c ",
362 (quals & TYPE_QUAL_CONST) ? 'c' : ' ',
363 (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ',
364 (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' ');
368 /* All types have associated declarations. */
369 dump_child ("name", TYPE_NAME (t));
371 /* All types have a main variant. */
372 if (TYPE_MAIN_VARIANT (t) != t)
373 dump_child ("unql", TYPE_MAIN_VARIANT (t));
376 dump_child ("size", TYPE_SIZE (t));
378 /* All types have alignments. */
379 dump_int (di, "algn", TYPE_ALIGN (t));
381 else if (code_class == 'c')
382 /* All constants can have types. */
383 queue_and_dump_type (di, t);
385 /* Give the language-specific code a chance to print something. If
386 it's completely taken care of things, don't bother printing
387 anything more ourselves. */
388 if (lang_dump_tree && (*lang_dump_tree) (di, t))
391 /* Now handle the various kinds of nodes. */
396 case IDENTIFIER_NODE:
397 dump_string_field (di, "strg", IDENTIFIER_POINTER (t));
398 dump_int (di, "lngt", IDENTIFIER_LENGTH (t));
402 dump_child ("purp", TREE_PURPOSE (t));
403 dump_child ("valu", TREE_VALUE (t));
404 dump_child ("chan", TREE_CHAIN (t));
408 dump_int (di, "lngt", TREE_VEC_LENGTH (t));
409 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
412 sprintf (buffer, "%u", i);
413 dump_child (buffer, TREE_VEC_ELT (t, i));
419 dump_int (di, "prec", TYPE_PRECISION (t));
420 if (TREE_UNSIGNED (t))
421 dump_string (di, "unsigned");
422 dump_child ("min", TYPE_MIN_VALUE (t));
423 dump_child ("max", TYPE_MAX_VALUE (t));
425 if (code == ENUMERAL_TYPE)
426 dump_child ("csts", TYPE_VALUES (t));
430 dump_int (di, "prec", TYPE_PRECISION (t));
434 dump_child ("ptd", TREE_TYPE (t));
438 dump_child ("refd", TREE_TYPE (t));
442 dump_child ("clas", TYPE_METHOD_BASETYPE (t));
446 dump_child ("retn", TREE_TYPE (t));
447 dump_child ("prms", TYPE_ARG_TYPES (t));
451 dump_child ("elts", TREE_TYPE (t));
452 dump_child ("domn", TYPE_DOMAIN (t));
457 if (TREE_CODE (t) == RECORD_TYPE)
458 dump_string (di, "struct");
460 dump_string (di, "union");
462 dump_child ("flds", TYPE_FIELDS (t));
463 dump_child ("fncs", TYPE_METHODS (t));
464 queue_and_dump_index (di, "binf", TYPE_BINFO (t),
469 dump_child ("cnst", DECL_INITIAL (t));
476 if (TREE_CODE (t) == PARM_DECL)
477 dump_child ("argt", DECL_ARG_TYPE (t));
479 dump_child ("init", DECL_INITIAL (t));
480 dump_child ("size", DECL_SIZE (t));
481 dump_int (di, "algn", DECL_ALIGN (t));
483 if (TREE_CODE (t) == FIELD_DECL)
485 if (DECL_C_BIT_FIELD (t))
486 dump_string (di, "bitfield");
487 if (DECL_FIELD_OFFSET (t))
488 dump_child ("bpos", bit_position (t));
490 else if (TREE_CODE (t) == VAR_DECL
491 || TREE_CODE (t) == PARM_DECL)
493 dump_int (di, "used", TREE_USED (t));
494 if (DECL_REGISTER (t))
495 dump_string (di, "register");
500 dump_child ("args", DECL_ARGUMENTS (t));
501 if (DECL_EXTERNAL (t))
502 dump_string (di, "undefined");
504 dump_string (di, "extern");
506 dump_string (di, "static");
511 if (ASM_VOLATILE_P (t))
512 dump_string (di, "volatile");
513 dump_child ("strg", ASM_STRING (t));
514 dump_child ("outs", ASM_OUTPUTS (t));
515 dump_child ("ins", ASM_INPUTS (t));
516 dump_child ("clbr", ASM_CLOBBERS (t));
517 dump_next_stmt (di, t);
523 dump_next_stmt (di, t);
527 /* Note that a case label is not like other statments; there is
528 no way to get the line-number of a case label. */
529 dump_child ("low", CASE_LOW (t));
530 dump_child ("high", CASE_HIGH (t));
531 dump_next_stmt (di, t);
536 dump_child ("body", COMPOUND_BODY (t));
537 dump_next_stmt (di, t);
542 dump_child ("decl", DECL_STMT_DECL (t));
543 dump_next_stmt (di, t);
548 dump_child ("body", DO_BODY (t));
549 dump_child ("cond", DO_COND (t));
550 dump_next_stmt (di, t);
555 dump_child ("expr", EXPR_STMT_EXPR (t));
556 dump_next_stmt (di, t);
561 dump_child ("init", FOR_INIT_STMT (t));
562 dump_child ("cond", FOR_COND (t));
563 dump_child ("expr", FOR_EXPR (t));
564 dump_child ("body", FOR_BODY (t));
565 dump_next_stmt (di, t);
570 dump_child ("dest", GOTO_DESTINATION (t));
571 dump_next_stmt (di, t);
576 dump_child ("cond", IF_COND (t));
577 dump_child ("then", THEN_CLAUSE (t));
578 dump_child ("else", ELSE_CLAUSE (t));
579 dump_next_stmt (di, t);
584 dump_child ("labl", LABEL_STMT_LABEL (t));
585 dump_next_stmt (di, t);
590 dump_child ("expr", RETURN_EXPR (t));
591 dump_next_stmt (di, t);
596 dump_child ("cond", SWITCH_COND (t));
597 dump_child ("body", SWITCH_BODY (t));
598 dump_next_stmt (di, t);
603 dump_child ("cond", WHILE_COND (t));
604 dump_child ("body", WHILE_BODY (t));
605 dump_next_stmt (di, t);
610 if (SCOPE_BEGIN_P (t))
611 dump_string (di, "begn");
613 dump_string (di, "end");
614 if (SCOPE_NULLIFIED_P (t))
615 dump_string (di, "null");
616 if (!SCOPE_NO_CLEANUPS_P (t))
617 dump_string (di, "clnp");
618 dump_next_stmt (di, t);
622 if (TREE_INT_CST_HIGH (t))
623 dump_int (di, "high", TREE_INT_CST_HIGH (t));
624 dump_int (di, "low", TREE_INT_CST_LOW (t));
628 fprintf (di->stream, "strg: %-7s ", TREE_STRING_POINTER (t));
629 dump_int (di, "lngt", TREE_STRING_LENGTH (t));
635 case CLEANUP_POINT_EXPR:
637 /* These nodes are unary, but do not have code class `1'. */
638 dump_child ("op 0", TREE_OPERAND (t, 0));
641 case TRUTH_ANDIF_EXPR:
642 case TRUTH_ORIF_EXPR:
648 case PREDECREMENT_EXPR:
649 case PREINCREMENT_EXPR:
650 case POSTDECREMENT_EXPR:
651 case POSTINCREMENT_EXPR:
652 /* These nodes are binary, but do not have code class `2'. */
653 dump_child ("op 0", TREE_OPERAND (t, 0));
654 dump_child ("op 1", TREE_OPERAND (t, 1));
658 dump_child ("op 0", TREE_OPERAND (t, 0));
659 dump_child ("op 1", TREE_OPERAND (t, 1));
660 dump_child ("op 2", TREE_OPERAND (t, 2));
664 dump_child ("fn", TREE_OPERAND (t, 0));
665 dump_child ("args", TREE_OPERAND (t, 1));
669 dump_child ("elts", TREE_OPERAND (t, 1));
673 dump_child ("stmt", STMT_EXPR_STMT (t));
677 dump_child ("vars", TREE_OPERAND (t, 0));
678 dump_child ("body", TREE_OPERAND (t, 1));
682 dump_child ("body", TREE_OPERAND (t, 0));
686 dump_child ("cond", TREE_OPERAND (t, 0));
690 dump_child ("decl", TREE_OPERAND (t, 0));
691 dump_child ("init", TREE_OPERAND (t, 1));
692 dump_child ("clnp", TREE_OPERAND (t, 2));
693 /* There really are two possible places the initializer can be.
694 After RTL expansion, the second operand is moved to the
695 position of the fourth operand, and the second operand
697 dump_child ("init", TREE_OPERAND (t, 3));
700 case EXPR_WITH_FILE_LOCATION:
701 dump_child ("expr", EXPR_WFL_NODE (t));
705 /* There are no additional fields to print. */
710 /* Terminate the line. */
711 fprintf (di->stream, "\n");
714 /* Dump T, and all its children, on STREAM. */
717 dump_node (t, stream)
723 dump_queue_p next_dq;
725 /* Initialize the dump-information structure. */
732 di.nodes = splay_tree_new (splay_tree_compare_pointers, 0,
733 (splay_tree_delete_value_fn) &free);
735 /* Queue up the first node. */
736 queue (&di, t, DUMP_NONE);
738 /* Until the queue is empty, keep dumping nodes. */
740 dequeue_and_dump (&di);
743 for (dq = di.free_list; dq; dq = next_dq)
748 splay_tree_delete (di.nodes);
751 /* Dump T, and all its children, to FILE. */
754 dump_node_to_file (t, file)
760 f = fopen (file, "w");
762 error ("could not open dump file `%s'", file);