2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
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"
27 #include "tree-pretty-print.h"
28 #include "print-tree.h"
30 #define TB_OUT_FILE stdout
31 #define TB_IN_FILE stdin
32 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
33 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
35 /* Structures for handling Tree Browser's commands. */
36 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
38 #include "tree-browser.def"
42 typedef enum TB_Comm_code TB_CODE
;
46 const char *comm_text
;
51 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
52 static const struct tb_command tb_commands
[] =
54 #include "tree-browser.def"
58 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
59 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
60 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
61 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
64 /* Next structure is for parsing TREE_CODEs. */
67 const char *code_string
;
68 size_t code_string_len
;
71 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
72 #define END_OF_BASE_TREE_CODES \
73 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
74 static const struct tb_tree_code tb_tree_codes
[] =
76 #include "all-tree.def"
79 #undef END_OF_BASE_TREE_CODES
81 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
82 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
83 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
86 /* Function declarations. */
88 static long TB_getline (char **, long *, FILE *);
89 static TB_CODE
TB_get_command (char *);
90 static enum tree_code
TB_get_tree_code (char *);
91 static tree
find_node_with_code (tree
*, int *, void *);
92 static tree
store_child_info (tree
*, int *, void *);
93 static void TB_update_up (tree
);
94 static tree
TB_current_chain_node (tree
);
95 static tree
TB_prev_expr (tree
);
96 static tree
TB_next_expr (tree
);
97 static tree
TB_up_expr (tree
);
98 static tree
TB_first_in_bind (tree
);
99 static tree
TB_last_in_bind (tree
);
100 static tree
TB_history_prev (void);
102 /* FIXME: To be declared in a .h file. */
103 void browse_tree (tree
);
105 /* Hashtable helpers. */
106 struct tree_upper_hasher
: nofree_ptr_hash
<tree_node
>
108 static inline bool equal (const value_type
&, const compare_type
&);
112 tree_upper_hasher::equal (const value_type
&parent
, const compare_type
&node
)
114 if (parent
== NULL
|| node
== NULL
)
119 int n
= TREE_OPERAND_LENGTH (parent
);
121 for (i
= 0; i
< n
; i
++)
122 if (node
== TREE_OPERAND (parent
, i
))
128 /* Static variables. */
129 static hash_table
<tree_upper_hasher
> *TB_up_ht
;
130 static vec
<tree
, va_gc
> *TB_history_stack
;
131 static int TB_verbose
= 1;
134 /* Entry point in the Tree Browser. */
137 browse_tree (tree begin
)
140 TB_CODE tbc
= TB_UNUSED_COMMAND
;
145 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
147 #define TB_SET_HEAD(N) do { \
148 vec_safe_push (TB_history_stack, N); \
153 print_generic_expr (TB_OUT_FILE, head, 0); \
154 fprintf (TB_OUT_FILE, "\n"); \
160 /* Store in a hashtable information about previous and upper statements. */
162 TB_up_ht
= new hash_table
<tree_upper_hasher
> (1023);
168 fprintf (TB_OUT_FILE
, "TB> ");
169 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
176 /* Get a new command. Otherwise the user just pressed enter, and thus
177 she expects the last command to be reexecuted. */
178 tbc
= TB_get_command (input
);
187 if (head
&& (INTEGRAL_TYPE_P (head
)
188 || TREE_CODE (head
) == REAL_TYPE
189 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
190 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
196 if (head
&& (INTEGRAL_TYPE_P (head
)
197 || TREE_CODE (head
) == REAL_TYPE
198 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
199 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
205 if (head
&& TREE_CODE (head
) == TREE_VEC
)
207 /* This command takes another argument: the element number:
208 for example "elt 1". */
211 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
213 /* This command takes another argument: the element number:
214 for example "elt 1". */
222 if (head
&& TREE_CODE (head
) == TREE_LIST
)
223 TB_SET_HEAD (TREE_VALUE (head
));
229 if (head
&& TREE_CODE (head
) == TREE_LIST
)
230 TB_SET_HEAD (TREE_PURPOSE (head
));
236 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
237 TB_SET_HEAD (TREE_IMAGPART (head
));
243 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
244 TB_SET_HEAD (TREE_REALPART (head
));
250 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
251 TB_SET_HEAD (TREE_OPERAND (head
, 2));
257 if (head
&& TREE_CODE (head
) == BLOCK
)
258 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
263 case TB_SUPERCONTEXT
:
264 if (head
&& TREE_CODE (head
) == BLOCK
)
265 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
271 if (head
&& TREE_CODE (head
) == BLOCK
)
272 TB_SET_HEAD (BLOCK_VARS (head
));
273 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
274 TB_SET_HEAD (TREE_OPERAND (head
, 0));
279 case TB_REFERENCE_TO_THIS
:
280 if (head
&& TYPE_P (head
))
281 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
286 case TB_POINTER_TO_THIS
:
287 if (head
&& TYPE_P (head
))
288 TB_SET_HEAD (TYPE_POINTER_TO (head
));
294 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
295 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
301 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
302 || TREE_CODE (head
) == METHOD_TYPE
))
303 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
308 case TB_METHOD_BASE_TYPE
:
309 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
310 || TREE_CODE (head
) == METHOD_TYPE
)
311 && TYPE_METHOD_BASETYPE (head
))
312 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
318 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
319 || TREE_CODE (head
) == UNION_TYPE
320 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
321 TB_SET_HEAD (TYPE_FIELDS (head
));
327 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
328 TB_SET_HEAD (TYPE_DOMAIN (head
));
334 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
335 TB_SET_HEAD (TYPE_VALUES (head
));
341 if (head
&& TREE_CODE (head
) == PARM_DECL
)
342 TB_SET_HEAD (DECL_ARG_TYPE (head
));
348 if (head
&& DECL_P (head
))
349 TB_SET_HEAD (DECL_INITIAL (head
));
355 if (head
&& DECL_P (head
))
356 TB_SET_HEAD (DECL_RESULT_FLD (head
));
362 if (head
&& DECL_P (head
))
363 TB_SET_HEAD (DECL_ARGUMENTS (head
));
368 case TB_ABSTRACT_ORIGIN
:
369 if (head
&& DECL_P (head
))
370 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
371 else if (head
&& TREE_CODE (head
) == BLOCK
)
372 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
378 if (head
&& DECL_P (head
))
379 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
380 else if (head
&& TYPE_P (head
))
381 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
387 if (head
&& DECL_P (head
))
388 TB_SET_HEAD (DECL_CONTEXT (head
));
389 else if (head
&& TYPE_P (head
)
390 && TYPE_CONTEXT (head
))
391 TB_SET_HEAD (TYPE_CONTEXT (head
));
397 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
398 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
404 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
405 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
411 if (head
&& DECL_P (head
))
412 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
413 else if (head
&& TYPE_P (head
))
414 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
420 if (head
&& DECL_P (head
))
421 TB_SET_HEAD (DECL_SIZE (head
));
422 else if (head
&& TYPE_P (head
))
423 TB_SET_HEAD (TYPE_SIZE (head
));
429 if (head
&& TREE_TYPE (head
))
430 TB_SET_HEAD (TREE_TYPE (head
));
435 case TB_DECL_SAVED_TREE
:
436 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
437 && DECL_SAVED_TREE (head
))
438 TB_SET_HEAD (DECL_SAVED_TREE (head
));
444 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
445 TB_SET_HEAD (TREE_OPERAND (head
, 1));
451 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
452 TB_SET_HEAD (TREE_OPERAND (head
, 0));
458 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
459 TB_SET_HEAD (TREE_OPERAND (head
, 1));
465 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
466 TB_SET_HEAD (TREE_OPERAND (head
, 2));
472 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
473 TB_SET_HEAD (TREE_OPERAND (head
, 3));
485 case TB_PRETTY_PRINT
:
488 print_generic_stmt (TB_OUT_FILE
, head
, 0);
489 fprintf (TB_OUT_FILE
, "\n");
504 arg_text
= strchr (input
, ' ');
505 if (arg_text
== NULL
)
507 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
510 code
= TB_get_tree_code (arg_text
+ 1);
512 /* Search in the subtree a node with the given code. */
516 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
517 if (res
== NULL_TREE
)
519 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
523 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
530 #define TB_MOVE_HEAD(FCT) do { \
545 TB_MOVE_HEAD (TB_first_in_bind
);
549 TB_MOVE_HEAD (TB_last_in_bind
);
553 TB_MOVE_HEAD (TB_up_expr
);
557 TB_MOVE_HEAD (TB_prev_expr
);
561 TB_MOVE_HEAD (TB_next_expr
);
565 /* This command is a little bit special, since it deals with history
566 stack. For this reason it should keep the "head = ..." statement
567 and not use TB_MOVE_HEAD. */
571 t
= TB_history_prev ();
577 print_generic_expr (TB_OUT_FILE
, head
, 0);
578 fprintf (TB_OUT_FILE
, "\n");
589 /* Don't go further if it's the last node in this chain. */
590 if (head
&& TREE_CODE (head
) == BLOCK
)
591 TB_SET_HEAD (BLOCK_CHAIN (head
));
592 else if (head
&& TREE_CHAIN (head
))
593 TB_SET_HEAD (TREE_CHAIN (head
));
599 /* Go up to the current function declaration. */
600 TB_SET_HEAD (current_function_decl
);
601 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
605 /* Display a help message. */
608 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
609 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
611 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
620 fprintf (TB_OUT_FILE
, "Verbose on.\n");
625 fprintf (TB_OUT_FILE
, "Verbose off.\n");
631 /* Just exit from this function. */
646 /* Search the first node in this BIND_EXPR. */
649 TB_first_in_bind (tree node
)
653 if (node
== NULL_TREE
)
656 while ((t
= TB_prev_expr (node
)))
662 /* Search the last node in this BIND_EXPR. */
665 TB_last_in_bind (tree node
)
669 if (node
== NULL_TREE
)
672 while ((t
= TB_next_expr (node
)))
678 /* Search the parent expression for this node. */
681 TB_up_expr (tree node
)
684 if (node
== NULL_TREE
)
687 res
= TB_up_ht
->find (node
);
691 /* Search the previous expression in this BIND_EXPR. */
694 TB_prev_expr (tree node
)
696 node
= TB_current_chain_node (node
);
698 if (node
== NULL_TREE
)
701 node
= TB_up_expr (node
);
702 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
708 /* Search the next expression in this BIND_EXPR. */
711 TB_next_expr (tree node
)
713 node
= TB_current_chain_node (node
);
715 if (node
== NULL_TREE
)
718 node
= TREE_OPERAND (node
, 1);
723 TB_current_chain_node (tree node
)
725 if (node
== NULL_TREE
)
728 if (TREE_CODE (node
) == COMPOUND_EXPR
)
731 node
= TB_up_expr (node
);
734 if (TREE_CODE (node
) == COMPOUND_EXPR
)
737 node
= TB_up_expr (node
);
738 if (TREE_CODE (node
) == COMPOUND_EXPR
)
745 /* For each node store in its children nodes that the current node is their
746 parent. This function is used by walk_tree. */
749 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
750 void *data ATTRIBUTE_UNUSED
)
757 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
760 int n
= TREE_OPERAND_LENGTH (node
);
762 for (i
= 0; i
< n
; i
++)
764 tree op
= TREE_OPERAND (node
, i
);
765 slot
= TB_up_ht
->find_slot (op
, INSERT
);
770 /* Never stop walk_tree. */
774 /* Update information about upper expressions in the hash table. */
777 TB_update_up (tree node
)
781 walk_tree (&node
, store_child_info
, NULL
, NULL
);
783 /* Walk function's body. */
784 if (TREE_CODE (node
) == FUNCTION_DECL
)
785 if (DECL_SAVED_TREE (node
))
786 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
788 /* Walk rest of the chain. */
789 node
= TREE_CHAIN (node
);
791 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
794 /* Parse the input string for determining the command the user asked for. */
797 TB_get_command (char *input
)
799 unsigned int mn
, size_tok
;
803 space
= strchr (input
, ' ');
805 size_tok
= strlen (input
) - strlen (space
);
807 size_tok
= strlen (input
) - 1;
809 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
811 if (size_tok
!= TB_COMMAND_LEN (mn
))
814 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
816 /* Here we just determined the command. If this command takes
817 an argument, then the argument is determined later. */
818 return TB_COMMAND_CODE (mn
);
821 /* Not a valid command. */
822 return TB_UNUSED_COMMAND
;
825 /* Parse the input string for determining the tree code. */
827 static enum tree_code
828 TB_get_tree_code (char *input
)
830 unsigned int mn
, size_tok
;
834 space
= strchr (input
, ' ');
836 size_tok
= strlen (input
) - strlen (space
);
838 size_tok
= strlen (input
) - 1;
840 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
842 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
845 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
848 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
849 return TB_TREE_CODE (mn
);
853 /* This isn't a valid code. */
854 return LAST_AND_UNUSED_TREE_CODE
;
857 /* Find a node with a given code. This function is used as an argument to
861 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
864 enum tree_code
*code
;
865 code
= (enum tree_code
*) data
;
866 if (*code
== TREE_CODE (*tp
))
872 /* Returns a pointer to the last visited node. */
875 TB_history_prev (void)
877 if (!vec_safe_is_empty (TB_history_stack
))
879 tree last
= TB_history_stack
->last ();
880 TB_history_stack
->pop ();
886 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
887 (and null-terminate it). *LINEPTR is a pointer returned from malloc
888 (or NULL), pointing to *N characters of space. It is realloc'd as
889 necessary. Returns the number of characters read (not including the
890 null terminator), or -1 on error or EOF.
891 This function comes from sed (and is supposed to be a portable version
895 TB_getline (char **lineptr
, long *n
, FILE *stream
)
900 if (lineptr
== NULL
|| n
== NULL
)
909 /* Make sure we have a line buffer to start with. */
910 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
913 #define MAX_CANON 256
915 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
934 register int c
= getc (stream
);
937 else if ((*p
++ = c
) == '\n')
941 /* Need to enlarge the line buffer. */
944 line
= (char *) xrealloc (line
, size
);
957 /* Return a partial line since we got an error in the middle. */
959 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
960 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')