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"
28 #include "tree-pretty-print.h"
29 #include "print-tree.h"
31 #define TB_OUT_FILE stdout
32 #define TB_IN_FILE stdin
33 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
34 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
36 /* Structures for handling Tree Browser's commands. */
37 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
39 #include "tree-browser.def"
43 typedef enum TB_Comm_code TB_CODE
;
47 const char *comm_text
;
52 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
53 static const struct tb_command tb_commands
[] =
55 #include "tree-browser.def"
59 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
60 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
61 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
62 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
65 /* Next structure is for parsing TREE_CODEs. */
68 const char *code_string
;
69 size_t code_string_len
;
72 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
73 #define END_OF_BASE_TREE_CODES \
74 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
75 static const struct tb_tree_code tb_tree_codes
[] =
77 #include "all-tree.def"
80 #undef END_OF_BASE_TREE_CODES
82 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
83 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
84 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
87 /* Function declarations. */
89 static long TB_getline (char **, long *, FILE *);
90 static TB_CODE
TB_get_command (char *);
91 static enum tree_code
TB_get_tree_code (char *);
92 static tree
find_node_with_code (tree
*, int *, void *);
93 static tree
store_child_info (tree
*, int *, void *);
94 static void TB_update_up (tree
);
95 static tree
TB_current_chain_node (tree
);
96 static tree
TB_prev_expr (tree
);
97 static tree
TB_next_expr (tree
);
98 static tree
TB_up_expr (tree
);
99 static tree
TB_first_in_bind (tree
);
100 static tree
TB_last_in_bind (tree
);
101 static tree
TB_history_prev (void);
103 /* FIXME: To be declared in a .h file. */
104 void browse_tree (tree
);
106 /* Hashtable helpers. */
107 struct tree_upper_hasher
: nofree_ptr_hash
<tree_node
>
109 static inline bool equal (const value_type
&, const compare_type
&);
113 tree_upper_hasher::equal (const value_type
&parent
, const compare_type
&node
)
115 if (parent
== NULL
|| node
== NULL
)
120 int n
= TREE_OPERAND_LENGTH (parent
);
122 for (i
= 0; i
< n
; i
++)
123 if (node
== TREE_OPERAND (parent
, i
))
129 /* Static variables. */
130 static hash_table
<tree_upper_hasher
> *TB_up_ht
;
131 static vec
<tree
, va_gc
> *TB_history_stack
;
132 static int TB_verbose
= 1;
135 /* Entry point in the Tree Browser. */
138 browse_tree (tree begin
)
141 TB_CODE tbc
= TB_UNUSED_COMMAND
;
146 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
148 #define TB_SET_HEAD(N) do { \
149 vec_safe_push (TB_history_stack, N); \
154 print_generic_expr (TB_OUT_FILE, head, 0); \
155 fprintf (TB_OUT_FILE, "\n"); \
161 /* Store in a hashtable information about previous and upper statements. */
163 TB_up_ht
= new hash_table
<tree_upper_hasher
> (1023);
169 fprintf (TB_OUT_FILE
, "TB> ");
170 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
177 /* Get a new command. Otherwise the user just pressed enter, and thus
178 she expects the last command to be reexecuted. */
179 tbc
= TB_get_command (input
);
188 if (head
&& (INTEGRAL_TYPE_P (head
)
189 || TREE_CODE (head
) == REAL_TYPE
190 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
191 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
197 if (head
&& (INTEGRAL_TYPE_P (head
)
198 || TREE_CODE (head
) == REAL_TYPE
199 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
200 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
206 if (head
&& TREE_CODE (head
) == TREE_VEC
)
208 /* This command takes another argument: the element number:
209 for example "elt 1". */
212 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
214 /* This command takes another argument: the element number:
215 for example "elt 1". */
223 if (head
&& TREE_CODE (head
) == TREE_LIST
)
224 TB_SET_HEAD (TREE_VALUE (head
));
230 if (head
&& TREE_CODE (head
) == TREE_LIST
)
231 TB_SET_HEAD (TREE_PURPOSE (head
));
237 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
238 TB_SET_HEAD (TREE_IMAGPART (head
));
244 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
245 TB_SET_HEAD (TREE_REALPART (head
));
251 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
252 TB_SET_HEAD (TREE_OPERAND (head
, 2));
258 if (head
&& TREE_CODE (head
) == BLOCK
)
259 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
264 case TB_SUPERCONTEXT
:
265 if (head
&& TREE_CODE (head
) == BLOCK
)
266 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
272 if (head
&& TREE_CODE (head
) == BLOCK
)
273 TB_SET_HEAD (BLOCK_VARS (head
));
274 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
275 TB_SET_HEAD (TREE_OPERAND (head
, 0));
280 case TB_REFERENCE_TO_THIS
:
281 if (head
&& TYPE_P (head
))
282 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
287 case TB_POINTER_TO_THIS
:
288 if (head
&& TYPE_P (head
))
289 TB_SET_HEAD (TYPE_POINTER_TO (head
));
295 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
296 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
302 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
303 || TREE_CODE (head
) == METHOD_TYPE
))
304 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
309 case TB_METHOD_BASE_TYPE
:
310 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
311 || TREE_CODE (head
) == METHOD_TYPE
)
312 && TYPE_METHOD_BASETYPE (head
))
313 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
319 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
320 || TREE_CODE (head
) == UNION_TYPE
321 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
322 TB_SET_HEAD (TYPE_FIELDS (head
));
328 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
329 TB_SET_HEAD (TYPE_DOMAIN (head
));
335 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
336 TB_SET_HEAD (TYPE_VALUES (head
));
342 if (head
&& TREE_CODE (head
) == PARM_DECL
)
343 TB_SET_HEAD (DECL_ARG_TYPE (head
));
349 if (head
&& DECL_P (head
))
350 TB_SET_HEAD (DECL_INITIAL (head
));
356 if (head
&& DECL_P (head
))
357 TB_SET_HEAD (DECL_RESULT_FLD (head
));
363 if (head
&& DECL_P (head
))
364 TB_SET_HEAD (DECL_ARGUMENTS (head
));
369 case TB_ABSTRACT_ORIGIN
:
370 if (head
&& DECL_P (head
))
371 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
372 else if (head
&& TREE_CODE (head
) == BLOCK
)
373 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
379 if (head
&& DECL_P (head
))
380 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
381 else if (head
&& TYPE_P (head
))
382 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
388 if (head
&& DECL_P (head
))
389 TB_SET_HEAD (DECL_CONTEXT (head
));
390 else if (head
&& TYPE_P (head
)
391 && TYPE_CONTEXT (head
))
392 TB_SET_HEAD (TYPE_CONTEXT (head
));
398 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
399 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
405 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
406 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
412 if (head
&& DECL_P (head
))
413 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
414 else if (head
&& TYPE_P (head
))
415 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
421 if (head
&& DECL_P (head
))
422 TB_SET_HEAD (DECL_SIZE (head
));
423 else if (head
&& TYPE_P (head
))
424 TB_SET_HEAD (TYPE_SIZE (head
));
430 if (head
&& TREE_TYPE (head
))
431 TB_SET_HEAD (TREE_TYPE (head
));
436 case TB_DECL_SAVED_TREE
:
437 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
438 && DECL_SAVED_TREE (head
))
439 TB_SET_HEAD (DECL_SAVED_TREE (head
));
445 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
446 TB_SET_HEAD (TREE_OPERAND (head
, 1));
452 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
453 TB_SET_HEAD (TREE_OPERAND (head
, 0));
459 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
460 TB_SET_HEAD (TREE_OPERAND (head
, 1));
466 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
467 TB_SET_HEAD (TREE_OPERAND (head
, 2));
473 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
474 TB_SET_HEAD (TREE_OPERAND (head
, 3));
486 case TB_PRETTY_PRINT
:
489 print_generic_stmt (TB_OUT_FILE
, head
, 0);
490 fprintf (TB_OUT_FILE
, "\n");
505 arg_text
= strchr (input
, ' ');
506 if (arg_text
== NULL
)
508 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
511 code
= TB_get_tree_code (arg_text
+ 1);
513 /* Search in the subtree a node with the given code. */
517 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
518 if (res
== NULL_TREE
)
520 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
524 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
531 #define TB_MOVE_HEAD(FCT) do { \
546 TB_MOVE_HEAD (TB_first_in_bind
);
550 TB_MOVE_HEAD (TB_last_in_bind
);
554 TB_MOVE_HEAD (TB_up_expr
);
558 TB_MOVE_HEAD (TB_prev_expr
);
562 TB_MOVE_HEAD (TB_next_expr
);
566 /* This command is a little bit special, since it deals with history
567 stack. For this reason it should keep the "head = ..." statement
568 and not use TB_MOVE_HEAD. */
572 t
= TB_history_prev ();
578 print_generic_expr (TB_OUT_FILE
, head
, 0);
579 fprintf (TB_OUT_FILE
, "\n");
590 /* Don't go further if it's the last node in this chain. */
591 if (head
&& TREE_CODE (head
) == BLOCK
)
592 TB_SET_HEAD (BLOCK_CHAIN (head
));
593 else if (head
&& TREE_CHAIN (head
))
594 TB_SET_HEAD (TREE_CHAIN (head
));
600 /* Go up to the current function declaration. */
601 TB_SET_HEAD (current_function_decl
);
602 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
606 /* Display a help message. */
609 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
610 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
612 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
621 fprintf (TB_OUT_FILE
, "Verbose on.\n");
626 fprintf (TB_OUT_FILE
, "Verbose off.\n");
632 /* Just exit from this function. */
647 /* Search the first node in this BIND_EXPR. */
650 TB_first_in_bind (tree node
)
654 if (node
== NULL_TREE
)
657 while ((t
= TB_prev_expr (node
)))
663 /* Search the last node in this BIND_EXPR. */
666 TB_last_in_bind (tree node
)
670 if (node
== NULL_TREE
)
673 while ((t
= TB_next_expr (node
)))
679 /* Search the parent expression for this node. */
682 TB_up_expr (tree node
)
685 if (node
== NULL_TREE
)
688 res
= TB_up_ht
->find (node
);
692 /* Search the previous expression in this BIND_EXPR. */
695 TB_prev_expr (tree node
)
697 node
= TB_current_chain_node (node
);
699 if (node
== NULL_TREE
)
702 node
= TB_up_expr (node
);
703 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
709 /* Search the next expression in this BIND_EXPR. */
712 TB_next_expr (tree node
)
714 node
= TB_current_chain_node (node
);
716 if (node
== NULL_TREE
)
719 node
= TREE_OPERAND (node
, 1);
724 TB_current_chain_node (tree node
)
726 if (node
== NULL_TREE
)
729 if (TREE_CODE (node
) == COMPOUND_EXPR
)
732 node
= TB_up_expr (node
);
735 if (TREE_CODE (node
) == COMPOUND_EXPR
)
738 node
= TB_up_expr (node
);
739 if (TREE_CODE (node
) == COMPOUND_EXPR
)
746 /* For each node store in its children nodes that the current node is their
747 parent. This function is used by walk_tree. */
750 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
751 void *data ATTRIBUTE_UNUSED
)
758 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
761 int n
= TREE_OPERAND_LENGTH (node
);
763 for (i
= 0; i
< n
; i
++)
765 tree op
= TREE_OPERAND (node
, i
);
766 slot
= TB_up_ht
->find_slot (op
, INSERT
);
771 /* Never stop walk_tree. */
775 /* Update information about upper expressions in the hash table. */
778 TB_update_up (tree node
)
782 walk_tree (&node
, store_child_info
, NULL
, NULL
);
784 /* Walk function's body. */
785 if (TREE_CODE (node
) == FUNCTION_DECL
)
786 if (DECL_SAVED_TREE (node
))
787 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
789 /* Walk rest of the chain. */
790 node
= TREE_CHAIN (node
);
792 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
795 /* Parse the input string for determining the command the user asked for. */
798 TB_get_command (char *input
)
800 unsigned int mn
, size_tok
;
804 space
= strchr (input
, ' ');
806 size_tok
= strlen (input
) - strlen (space
);
808 size_tok
= strlen (input
) - 1;
810 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
812 if (size_tok
!= TB_COMMAND_LEN (mn
))
815 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
817 /* Here we just determined the command. If this command takes
818 an argument, then the argument is determined later. */
819 return TB_COMMAND_CODE (mn
);
822 /* Not a valid command. */
823 return TB_UNUSED_COMMAND
;
826 /* Parse the input string for determining the tree code. */
828 static enum tree_code
829 TB_get_tree_code (char *input
)
831 unsigned int mn
, size_tok
;
835 space
= strchr (input
, ' ');
837 size_tok
= strlen (input
) - strlen (space
);
839 size_tok
= strlen (input
) - 1;
841 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
843 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
846 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
849 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
850 return TB_TREE_CODE (mn
);
854 /* This isn't a valid code. */
855 return LAST_AND_UNUSED_TREE_CODE
;
858 /* Find a node with a given code. This function is used as an argument to
862 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
865 enum tree_code
*code
;
866 code
= (enum tree_code
*) data
;
867 if (*code
== TREE_CODE (*tp
))
873 /* Returns a pointer to the last visited node. */
876 TB_history_prev (void)
878 if (!vec_safe_is_empty (TB_history_stack
))
880 tree last
= TB_history_stack
->last ();
881 TB_history_stack
->pop ();
887 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
888 (and null-terminate it). *LINEPTR is a pointer returned from malloc
889 (or NULL), pointing to *N characters of space. It is realloc'd as
890 necessary. Returns the number of characters read (not including the
891 null terminator), or -1 on error or EOF.
892 This function comes from sed (and is supposed to be a portable version
896 TB_getline (char **lineptr
, long *n
, FILE *stream
)
901 if (lineptr
== NULL
|| n
== NULL
)
910 /* Make sure we have a line buffer to start with. */
911 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
914 #define MAX_CANON 256
916 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
935 register int c
= getc (stream
);
938 else if ((*p
++ = c
) == '\n')
942 /* Need to enlarge the line buffer. */
945 line
= (char *) xrealloc (line
, size
);
958 /* Return a partial line since we got an error in the middle. */
960 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
961 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')