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. */
27 /* Flags used with queue functions. */
31 /* Information about a node to be dumped. */
33 typedef struct dump_node_info
35 /* The index for the node. */
37 /* Nonzero if the node is a binfo. */
38 unsigned int binfo_p : 1;
41 /* A dump_queue is a link in the queue of things to be dumped. */
43 typedef struct dump_queue
45 /* The queued tree node. */
47 /* The next node in the queue. */
48 struct dump_queue *next;
51 /* A dump_info gives information about how we should perform the dump
52 and about the current state of the dump. */
54 typedef struct dump_info
56 /* The stream on which to dump the information. */
58 /* The next unused node index. */
60 /* The next column. */
62 /* The first node in the queue of nodes to be written out. */
64 /* The last node in the queue. */
65 dump_queue_p queue_end;
66 /* Free queue nodes. */
67 dump_queue_p free_list;
68 /* The tree nodes which we have already written out. The
69 keys are the addresses of the nodes; the values are the integer
70 indices we assigned them. */
74 static unsigned int queue PARAMS ((dump_info_p, tree, int));
75 static void dump_index PARAMS ((dump_info_p, unsigned int));
76 static void queue_and_dump_index PARAMS ((dump_info_p, const char *, tree, int));
77 static void queue_and_dump_type PARAMS ((dump_info_p, tree));
78 static void dequeue_and_dump PARAMS ((dump_info_p));
79 static void dump_new_line PARAMS ((dump_info_p));
80 static void dump_maybe_newline PARAMS ((dump_info_p));
81 static void dump_int PARAMS ((dump_info_p, const char *, int));
82 static void dump_string PARAMS ((dump_info_p, const char *));
83 static void dump_string_field PARAMS ((dump_info_p, const char *, const char *));
84 static void dump_node PARAMS ((tree, FILE *));
85 static void dump_stmt PARAMS ((dump_info_p, tree));
86 static void dump_next_stmt PARAMS ((dump_info_p, tree));
88 /* Add T to the end of the queue of nodes to dump. Returns the index
101 /* Assign the next available index to T. */
104 /* Obtain a new queue node. */
108 di->free_list = dq->next;
111 dq = (dump_queue_p) xmalloc (sizeof (struct dump_queue));
113 /* Create a new entry in the splay-tree. */
114 dni = (dump_node_info_p) xmalloc (sizeof (struct dump_node_info));
116 dni->binfo_p = ((flags & DUMP_BINFO) != 0);
117 dq->node = splay_tree_insert (di->nodes, (splay_tree_key) t,
118 (splay_tree_value) dni);
120 /* Add it to the end of the queue. */
125 di->queue_end->next = dq;
128 /* Return the index. */
133 dump_index (di, index)
137 fprintf (di->stream, "@%-6u ", index);
141 /* If T has not already been output, queue it for subsequent output.
142 FIELD is a string to print before printing the index. Then, the
143 index of T is printed. */
146 queue_and_dump_index (di, field, t, flags)
155 /* If there's no node, just return. This makes for fewer checks in
160 /* See if we've already queued or dumped this node. */
161 n = splay_tree_lookup (di->nodes, (splay_tree_key) t);
163 index = ((dump_node_info_p) n->value)->index;
165 /* If we haven't, add it to the queue. */
166 index = queue (di, t, flags);
168 /* Print the index of the node. */
169 dump_maybe_newline (di);
170 fprintf (di->stream, "%-4s: ", field);
172 dump_index (di, index);
175 /* Dump the type of T. */
178 queue_and_dump_type (di, t)
182 queue_and_dump_index (di, "type", TREE_TYPE (t), DUMP_NONE);
185 /* Insert a new line in the dump output, and indent to an appropriate
186 place to start printing more fields. */
192 fprintf (di->stream, "\n%25s", "");
196 /* If necessary, insert a new line. */
199 dump_maybe_newline (di)
202 /* See if we need a new line. */
205 /* See if we need any padding. */
206 else if ((di->column - 25) % 14 != 0)
208 fprintf (di->stream, "%*s", 14 - ((di->column - 25) % 14), "");
209 di->column += 14 - (di->column - 25) % 14;
213 /* Dump I using FIELD to identity it. */
216 dump_int (di, field, i)
221 dump_maybe_newline (di);
222 fprintf (di->stream, "%-4s: %-7d ", field, i);
226 /* Dump the string S. */
229 dump_string (di, string)
233 dump_maybe_newline (di);
234 fprintf (di->stream, "%-13s ", string);
235 if (strlen (string) > 13)
236 di->column += strlen (string) + 1;
241 /* Dump the string field S. */
244 dump_string_field (di, field, string)
249 dump_maybe_newline (di);
250 fprintf (di->stream, "%-4s: %-7s ", field, string);
251 if (strlen (string) > 7)
252 di->column += 6 + strlen (string) + 1;
257 /* Dump information common to statements from STMT. */
264 dump_int (di, "line", STMT_LINENO (t));
267 /* Dump the CHILD and its children. */
268 #define dump_child(field, child) \
269 queue_and_dump_index (di, field, child, DUMP_NONE)
271 /* Dump the next statement after STMT. */
274 dump_next_stmt (di, t)
278 dump_child ("next", TREE_CHAIN (t));
281 /* Dump the next node in the queue. */
284 dequeue_and_dump (di)
289 dump_node_info_p dni;
294 const char* code_name;
296 /* Get the next node from the queue. */
300 dni = (dump_node_info_p) stn->value;
303 /* Remove the node from the queue, and put it on the free list. */
304 di->queue = dq->next;
307 dq->next = di->free_list;
310 /* Print the node index. */
311 dump_index (di, index);
312 /* And the type of node this is. */
316 code_name = tree_code_name[(int) TREE_CODE (t)];
317 fprintf (di->stream, "%-16s ", code_name);
320 /* Figure out what kind of node this is. */
321 code = TREE_CODE (t);
322 code_class = TREE_CODE_CLASS (code);
324 /* Although BINFOs are TREE_VECs, we dump them specially so as to be
328 if (TREE_VIA_PUBLIC (t))
329 dump_string (di, "pub");
330 else if (TREE_VIA_PROTECTED (t))
331 dump_string (di, "prot");
332 else if (TREE_VIA_PRIVATE (t))
333 dump_string (di, "priv");
334 if (TREE_VIA_VIRTUAL (t))
335 dump_string (di, "virt");
337 dump_child ("type", BINFO_TYPE (t));
338 dump_child ("base", BINFO_BASETYPES (t));
343 /* We can knock off a bunch of expression nodes in exactly the same
345 if (IS_EXPR_CODE_CLASS (code_class))
347 /* If we're dumping children, dump them now. */
348 queue_and_dump_type (di, t);
353 dump_child ("op 0", TREE_OPERAND (t, 0));
358 dump_child ("op 0", TREE_OPERAND (t, 0));
359 dump_child ("op 1", TREE_OPERAND (t, 1));
363 /* These nodes are handled explicitly below. */
367 my_friendly_abort (19990726);
370 else if (code_class == 'd')
372 /* All declarations have names. */
374 dump_child ("name", DECL_NAME (t));
376 queue_and_dump_type (di, t);
377 dump_child ("scpe", DECL_CONTEXT (t));
378 /* And a source position. */
379 if (DECL_SOURCE_FILE (t))
381 const char *filename = rindex (DECL_SOURCE_FILE (t), '/');
383 filename = DECL_SOURCE_FILE (t);
385 /* Skip the slash. */
388 dump_maybe_newline (di);
389 fprintf (di->stream, "srcp: %s:%-6d ", filename,
390 DECL_SOURCE_LINE (t));
391 di->column += 6 + strlen (filename) + 8;
393 /* And any declaration can be compiler-generated. */
394 if (DECL_ARTIFICIAL (t))
395 dump_string (di, "artificial");
397 dump_child ("chan", TREE_CHAIN (t));
399 else if (code_class == 't')
401 /* All types have qualifiers. */
402 int quals = CP_TYPE_QUALS (t);
403 if (quals != TYPE_UNQUALIFIED)
405 fprintf (di->stream, "qual: %c%c%c ",
406 (quals & TYPE_QUAL_CONST) ? 'c' : ' ',
407 (quals & TYPE_QUAL_VOLATILE) ? 'v' : ' ',
408 (quals & TYPE_QUAL_RESTRICT) ? 'r' : ' ');
412 /* All types have associated declarations. */
413 dump_child ("name", TYPE_NAME (t));
415 /* All types have a main variant. */
416 if (TYPE_MAIN_VARIANT (t) != t)
417 dump_child ("unql", TYPE_MAIN_VARIANT (t));
420 dump_child ("size", TYPE_SIZE (t));
422 /* All types have alignments. */
423 dump_int (di, "algn", TYPE_ALIGN (t));
425 else if (code_class == 'c')
426 /* All constants can have types. */
427 queue_and_dump_type (di, t);
429 /* Now handle the various kinds of nodes. */
434 case IDENTIFIER_NODE:
435 if (IDENTIFIER_OPNAME_P (t))
436 dump_string (di, "operator");
437 else if (IDENTIFIER_TYPENAME_P (t))
438 dump_child ("tynm", TREE_TYPE (t));
439 else if (t == anonymous_namespace_name)
440 dump_string (di, "unnamed");
443 dump_string_field (di, "strg", IDENTIFIER_POINTER (t));
444 dump_int (di, "lngt", IDENTIFIER_LENGTH (t));
449 dump_child ("purp", TREE_PURPOSE (t));
450 dump_child ("valu", TREE_VALUE (t));
451 dump_child ("chan", TREE_CHAIN (t));
455 dump_int (di, "lngt", IDENTIFIER_LENGTH (t));
456 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
459 sprintf (buffer, "%u", i);
460 dump_child (buffer, TREE_VEC_ELT (t, i));
466 dump_int (di, "prec", TYPE_PRECISION (t));
467 if (TREE_UNSIGNED (t))
468 dump_string (di, "unsigned");
469 dump_child ("min", TYPE_MIN_VALUE (t));
470 dump_child ("max", TYPE_MAX_VALUE (t));
472 if (code == ENUMERAL_TYPE)
473 dump_child ("csts", TYPE_VALUES (t));
477 dump_int (di, "prec", TYPE_PRECISION (t));
481 if (TYPE_PTRMEM_P (t))
483 dump_string (di, "ptrmem");
484 dump_child ("ptd", TYPE_PTRMEM_POINTED_TO_TYPE (t));
485 dump_child ("cls", TYPE_PTRMEM_CLASS_TYPE (t));
488 dump_child ("ptd", TREE_TYPE (t));
492 dump_child ("refd", TREE_TYPE (t));
496 dump_child ("clas", TYPE_METHOD_BASETYPE (t));
500 dump_child ("retn", TREE_TYPE (t));
501 dump_child ("prms", TYPE_ARG_TYPES (t));
505 dump_child ("elts", TREE_TYPE (t));
506 dump_child ("domn", TYPE_DOMAIN (t));
511 if (TYPE_PTRMEMFUNC_P (t))
513 dump_string (di, "ptrmem");
514 dump_child ("ptd", TYPE_PTRMEM_POINTED_TO_TYPE (t));
515 dump_child ("cls", TYPE_PTRMEM_CLASS_TYPE (t));
519 if (CLASSTYPE_DECLARED_CLASS (t))
520 dump_string (di, "class");
521 else if (TREE_CODE (t) == RECORD_TYPE)
522 dump_string (di, "struct");
524 dump_string (di, "union");
526 dump_child ("flds", TYPE_FIELDS (t));
527 dump_child ("fncs", TYPE_METHODS (t));
528 dump_child ("vfld", TYPE_VFIELD (t));
529 queue_and_dump_index (di, "binf", TYPE_BINFO (t),
535 dump_child ("cnst", DECL_INITIAL (t));
542 if (TREE_CODE (t) == PARM_DECL)
543 dump_child ("argt", DECL_ARG_TYPE (t));
545 dump_child ("init", DECL_INITIAL (t));
546 dump_child ("size", DECL_SIZE (t));
547 dump_int (di, "algn", DECL_ALIGN (t));
549 if (TREE_CODE (t) == FIELD_DECL)
551 if (DECL_C_BIT_FIELD (t))
552 dump_string (di, "bitfield");
553 dump_child ("bpos", DECL_FIELD_BITPOS (t));
559 dump_child ("scpe", CP_DECL_CONTEXT (t));
560 dump_child ("mngl", DECL_ASSEMBLER_NAME (t));
561 dump_child ("args", DECL_ARGUMENTS (t));
562 if (DECL_EXTERNAL (t))
563 dump_string (di, "undefined");
565 dump_string(di, "extern");
567 dump_string (di, "static");
568 if (TREE_CODE (t) == FUNCTION_DECL)
570 if (DECL_FUNCTION_MEMBER_P (t))
571 dump_string (di, "member");
572 if (DECL_CONSTRUCTOR_P (t))
573 dump_string (di, "constructor");
574 if (DECL_DESTRUCTOR_P (t))
575 dump_string (di, "destructor");
576 if (DECL_OVERLOADED_OPERATOR_P (t))
577 dump_string (di, "operator");
578 if (DECL_CONV_FN_P (t))
579 dump_string (di, "conversion");
580 if (DECL_GLOBAL_CTOR_P (t) || DECL_GLOBAL_DTOR_P (t))
582 if (DECL_GLOBAL_CTOR_P (t))
583 dump_string (di, "global init");
584 if (DECL_GLOBAL_DTOR_P (t))
585 dump_string (di, "global fini");
586 dump_int (di, "prio", GLOBAL_INIT_PRIORITY (t));
588 if (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (t))
589 dump_string (di, "pseudo tmpl");
591 dump_child ("body", DECL_SAVED_TREE (t));
595 dump_int (di, "dlta", THUNK_DELTA (t));
596 dump_child ("init", DECL_INITIAL (t));
601 /* The fake `::std' namespace does not have DECL_LANG_SPECIFIC,
602 and therefore many other macros do not work on it. */
605 if (DECL_NAMESPACE_ALIAS (t))
606 dump_child ("alis", DECL_NAMESPACE_ALIAS (t));
608 dump_child ("dcls", cp_namespace_decls (t));
612 dump_child ("spcs", DECL_TEMPLATE_SPECIALIZATIONS (t));
616 dump_child ("crnt", OVL_CURRENT (t));
617 dump_child ("chan", OVL_CHAIN (t));
622 if (ASM_VOLATILE_P (t))
623 dump_string (di, "volatile");
624 dump_child ("strg", ASM_STRING (t));
625 dump_child ("outs", ASM_OUTPUTS (t));
626 dump_child ("ins", ASM_INPUTS (t));
627 dump_child ("clbr", ASM_CLOBBERS (t));
628 dump_next_stmt (di, t);
634 dump_next_stmt (di, t);
638 /* Note that a case label is not like other statments; there is
639 no way to get the line-number of a case label. */
640 dump_child ("low", CASE_LOW (t));
641 dump_child ("high", CASE_HIGH (t));
642 dump_next_stmt (di, t);
647 dump_child ("body", COMPOUND_BODY (t));
648 dump_next_stmt (di, t);
653 if (CTOR_BEGIN_P (t))
654 dump_string (di, "begn");
656 dump_string (di, "end");
657 dump_next_stmt (di, t);
662 dump_child ("decl", DECL_STMT_DECL (t));
663 dump_next_stmt (di, t);
668 dump_child ("body", DO_BODY (t));
669 dump_child ("cond", DO_COND (t));
670 dump_next_stmt (di, t);
675 dump_child ("expr", EXPR_STMT_EXPR (t));
676 dump_next_stmt (di, t);
681 dump_child ("init", FOR_INIT_STMT (t));
682 dump_child ("cond", FOR_COND (t));
683 dump_child ("expr", FOR_EXPR (t));
684 dump_child ("body", FOR_BODY (t));
685 dump_next_stmt (di, t);
690 dump_child ("dest", GOTO_DESTINATION (t));
691 dump_next_stmt (di, t);
696 dump_child ("body", HANDLER_BODY (t));
697 dump_next_stmt (di, t);
702 dump_child ("cond", IF_COND (t));
703 dump_child ("then", THEN_CLAUSE (t));
704 dump_child ("else", ELSE_CLAUSE (t));
705 dump_next_stmt (di, t);
710 dump_child ("labl", LABEL_STMT_LABEL (t));
711 dump_next_stmt (di, t);
716 dump_child ("expr", RETURN_EXPR (t));
717 dump_next_stmt (di, t);
722 dump_child ("cond", SWITCH_COND (t));
723 dump_child ("body", SWITCH_BODY (t));
724 dump_next_stmt (di, t);
730 dump_string (di, "cleanup");
731 dump_child ("body", TRY_STMTS (t));
732 dump_child ("hdlr", TRY_HANDLERS (t));
733 dump_next_stmt (di, t);
738 dump_child ("cond", WHILE_COND (t));
739 dump_child ("body", WHILE_BODY (t));
740 dump_next_stmt (di, t);
745 dump_child ("clnp", TREE_OPERAND (t, 0));
746 dump_next_stmt (di, t);
749 case START_CATCH_STMT:
751 queue_and_dump_type (di, t);
752 dump_next_stmt (di, t);
757 dump_child ("decl", CLEANUP_DECL (t));
758 dump_child ("expr", CLEANUP_EXPR (t));
759 dump_next_stmt (di, t);
764 if (SCOPE_BEGIN_P (t))
765 dump_string (di, "begn");
767 dump_string (di, "end");
768 if (SCOPE_NULLIFIED_P (t))
769 dump_string (di, "null");
770 dump_next_stmt (di, t);
774 if (TREE_INT_CST_HIGH (t))
775 dump_int (di, "high", TREE_INT_CST_HIGH (t));
776 dump_int (di, "low", TREE_INT_CST_LOW (t));
780 fprintf (di->stream, "strg: %-7s ", TREE_STRING_POINTER (t));
781 dump_int (di, "lngt", TREE_STRING_LENGTH (t));
785 dump_child ("clas", PTRMEM_CST_CLASS (t));
786 dump_child ("mbr", PTRMEM_CST_MEMBER (t));
793 case CLEANUP_POINT_EXPR:
795 /* These nodes are unary, but do not have code class `1'. */
796 dump_child ("op 0", TREE_OPERAND (t, 0));
799 case TRUTH_ANDIF_EXPR:
800 case TRUTH_ORIF_EXPR:
806 /* These nodes are binary, but do not have code class `2'. */
807 dump_child ("op 0", TREE_OPERAND (t, 0));
808 dump_child ("op 1", TREE_OPERAND (t, 1));
812 dump_child ("op 0", TREE_OPERAND (t, 0));
813 dump_child ("op 1", TREE_OPERAND (t, 1));
814 dump_child ("op 2", TREE_OPERAND (t, 2));
818 dump_child ("fn", TREE_OPERAND (t, 0));
819 dump_child ("args", TREE_OPERAND (t, 1));
823 dump_child ("elts", TREE_OPERAND (t, 1));
827 dump_child ("stmt", STMT_EXPR_STMT (t));
831 dump_child ("vars", TREE_OPERAND (t, 0));
832 dump_child ("body", TREE_OPERAND (t, 1));
836 dump_child ("body", TREE_OPERAND (t, 0));
840 dump_child ("cond", TREE_OPERAND (t, 0));
844 dump_child ("decl", TREE_OPERAND (t, 0));
845 dump_child ("init", TREE_OPERAND (t, 1));
846 dump_child ("clnp", TREE_OPERAND (t, 2));
847 /* There really are two possible places the initializer can be.
848 After RTL expansion, the second operand is moved to the
849 position of the fourth operand, and the second operand
851 dump_child ("init", TREE_OPERAND (t, 3));
855 dump_int (di, "ctor", AGGR_INIT_VIA_CTOR_P (t));
856 dump_child ("fn", TREE_OPERAND (t, 0));
857 dump_child ("args", TREE_OPERAND (t, 1));
858 dump_child ("decl", TREE_OPERAND (t, 2));
862 /* There are no additional fields to print. */
867 /* Terminate the line. */
868 fprintf (di->stream, "\n");
871 /* Dump T, and all its children, on STREAM. */
874 dump_node (t, stream)
880 dump_queue_p next_dq;
882 /* Initialize the dump-information structure. */
889 di.nodes = splay_tree_new (splay_tree_compare_pointers, 0,
890 (splay_tree_delete_value_fn) &free);
892 /* Queue up the first node. */
893 queue (&di, t, DUMP_NONE);
895 /* Until the queue is empty, keep dumping nodes. */
897 dequeue_and_dump (&di);
900 for (dq = di.free_list; dq; dq = next_dq)
905 splay_tree_delete (di.nodes);
908 /* Dump T, and all its children, to FILE. */
911 dump_node_to_file (t, file)
917 f = fopen (file, "w");
919 cp_error ("could not open `%s'", file);