2 Copyright (C) 2002-2014 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"
24 #include "hash-table.h"
26 #include "tree-pretty-print.h"
27 #include "print-tree.h"
29 #define TB_OUT_FILE stdout
30 #define TB_IN_FILE stdin
31 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
32 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
34 /* Structures for handling Tree Browser's commands. */
35 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
37 #include "tree-browser.def"
41 typedef enum TB_Comm_code TB_CODE
;
45 const char *comm_text
;
50 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
51 static const struct tb_command tb_commands
[] =
53 #include "tree-browser.def"
57 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
58 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
59 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
60 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
63 /* Next structure is for parsing TREE_CODEs. */
66 const char *code_string
;
67 size_t code_string_len
;
70 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
71 #define END_OF_BASE_TREE_CODES \
72 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
73 static const struct tb_tree_code tb_tree_codes
[] =
75 #include "all-tree.def"
78 #undef END_OF_BASE_TREE_CODES
80 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
81 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
82 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
85 /* Function declarations. */
87 static long TB_getline (char **, long *, FILE *);
88 static TB_CODE
TB_get_command (char *);
89 static enum tree_code
TB_get_tree_code (char *);
90 static tree
find_node_with_code (tree
*, int *, void *);
91 static tree
store_child_info (tree
*, int *, void *);
92 static void TB_update_up (tree
);
93 static tree
TB_current_chain_node (tree
);
94 static tree
TB_prev_expr (tree
);
95 static tree
TB_next_expr (tree
);
96 static tree
TB_up_expr (tree
);
97 static tree
TB_first_in_bind (tree
);
98 static tree
TB_last_in_bind (tree
);
99 static tree
TB_history_prev (void);
101 /* FIXME: To be declared in a .h file. */
102 void browse_tree (tree
);
104 /* Hashtable helpers. */
105 struct tree_upper_hasher
: typed_noop_remove
<tree_node
>
107 typedef tree_node value_type
;
108 typedef tree_node compare_type
;
109 static inline hashval_t
hash (const value_type
*);
110 static inline bool equal (const value_type
*, const compare_type
*);
114 tree_upper_hasher::hash (const value_type
*v
)
116 return pointer_hash
<value_type
>::hash (v
);
120 tree_upper_hasher::equal (const value_type
*parent
, const compare_type
*node
)
122 if (parent
== NULL
|| node
== NULL
)
127 int n
= TREE_OPERAND_LENGTH (parent
);
129 for (i
= 0; i
< n
; i
++)
130 if (node
== TREE_OPERAND (parent
, i
))
136 /* Static variables. */
137 static hash_table
<tree_upper_hasher
> TB_up_ht
;
138 static vec
<tree
, va_gc
> *TB_history_stack
;
139 static int TB_verbose
= 1;
142 /* Entry point in the Tree Browser. */
145 browse_tree (tree begin
)
148 TB_CODE tbc
= TB_UNUSED_COMMAND
;
153 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
155 #define TB_SET_HEAD(N) do { \
156 vec_safe_push (TB_history_stack, N); \
161 print_generic_expr (TB_OUT_FILE, head, 0); \
162 fprintf (TB_OUT_FILE, "\n"); \
168 /* Store in a hashtable information about previous and upper statements. */
170 TB_up_ht
.create (1023);
176 fprintf (TB_OUT_FILE
, "TB> ");
177 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
184 /* Get a new command. Otherwise the user just pressed enter, and thus
185 she expects the last command to be reexecuted. */
186 tbc
= TB_get_command (input
);
195 if (head
&& (INTEGRAL_TYPE_P (head
)
196 || TREE_CODE (head
) == REAL_TYPE
197 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
198 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
204 if (head
&& (INTEGRAL_TYPE_P (head
)
205 || TREE_CODE (head
) == REAL_TYPE
206 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
207 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
213 if (head
&& TREE_CODE (head
) == TREE_VEC
)
215 /* This command takes another argument: the element number:
216 for example "elt 1". */
219 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
221 /* This command takes another argument: the element number:
222 for example "elt 1". */
230 if (head
&& TREE_CODE (head
) == TREE_LIST
)
231 TB_SET_HEAD (TREE_VALUE (head
));
237 if (head
&& TREE_CODE (head
) == TREE_LIST
)
238 TB_SET_HEAD (TREE_PURPOSE (head
));
244 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
245 TB_SET_HEAD (TREE_IMAGPART (head
));
251 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
252 TB_SET_HEAD (TREE_REALPART (head
));
258 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
259 TB_SET_HEAD (TREE_OPERAND (head
, 2));
265 if (head
&& TREE_CODE (head
) == BLOCK
)
266 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
271 case TB_SUPERCONTEXT
:
272 if (head
&& TREE_CODE (head
) == BLOCK
)
273 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
279 if (head
&& TREE_CODE (head
) == BLOCK
)
280 TB_SET_HEAD (BLOCK_VARS (head
));
281 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
282 TB_SET_HEAD (TREE_OPERAND (head
, 0));
287 case TB_REFERENCE_TO_THIS
:
288 if (head
&& TYPE_P (head
))
289 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
294 case TB_POINTER_TO_THIS
:
295 if (head
&& TYPE_P (head
))
296 TB_SET_HEAD (TYPE_POINTER_TO (head
));
302 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
303 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
309 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
310 || TREE_CODE (head
) == METHOD_TYPE
))
311 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
316 case TB_METHOD_BASE_TYPE
:
317 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
318 || TREE_CODE (head
) == METHOD_TYPE
)
319 && TYPE_METHOD_BASETYPE (head
))
320 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
326 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
327 || TREE_CODE (head
) == UNION_TYPE
328 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
329 TB_SET_HEAD (TYPE_FIELDS (head
));
335 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
336 TB_SET_HEAD (TYPE_DOMAIN (head
));
342 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
343 TB_SET_HEAD (TYPE_VALUES (head
));
349 if (head
&& TREE_CODE (head
) == PARM_DECL
)
350 TB_SET_HEAD (DECL_ARG_TYPE (head
));
356 if (head
&& DECL_P (head
))
357 TB_SET_HEAD (DECL_INITIAL (head
));
363 if (head
&& DECL_P (head
))
364 TB_SET_HEAD (DECL_RESULT_FLD (head
));
370 if (head
&& DECL_P (head
))
371 TB_SET_HEAD (DECL_ARGUMENTS (head
));
376 case TB_ABSTRACT_ORIGIN
:
377 if (head
&& DECL_P (head
))
378 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
379 else if (head
&& TREE_CODE (head
) == BLOCK
)
380 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
386 if (head
&& DECL_P (head
))
387 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
388 else if (head
&& TYPE_P (head
))
389 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
395 if (head
&& DECL_P (head
))
396 TB_SET_HEAD (DECL_CONTEXT (head
));
397 else if (head
&& TYPE_P (head
)
398 && TYPE_CONTEXT (head
))
399 TB_SET_HEAD (TYPE_CONTEXT (head
));
405 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
406 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
412 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
413 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
419 if (head
&& DECL_P (head
))
420 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
421 else if (head
&& TYPE_P (head
))
422 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
428 if (head
&& DECL_P (head
))
429 TB_SET_HEAD (DECL_SIZE (head
));
430 else if (head
&& TYPE_P (head
))
431 TB_SET_HEAD (TYPE_SIZE (head
));
437 if (head
&& TREE_TYPE (head
))
438 TB_SET_HEAD (TREE_TYPE (head
));
443 case TB_DECL_SAVED_TREE
:
444 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
445 && DECL_SAVED_TREE (head
))
446 TB_SET_HEAD (DECL_SAVED_TREE (head
));
452 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
453 TB_SET_HEAD (TREE_OPERAND (head
, 1));
459 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
460 TB_SET_HEAD (TREE_OPERAND (head
, 0));
466 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
467 TB_SET_HEAD (TREE_OPERAND (head
, 1));
473 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
474 TB_SET_HEAD (TREE_OPERAND (head
, 2));
480 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
481 TB_SET_HEAD (TREE_OPERAND (head
, 3));
493 case TB_PRETTY_PRINT
:
496 print_generic_stmt (TB_OUT_FILE
, head
, 0);
497 fprintf (TB_OUT_FILE
, "\n");
512 arg_text
= strchr (input
, ' ');
513 if (arg_text
== NULL
)
515 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
518 code
= TB_get_tree_code (arg_text
+ 1);
520 /* Search in the subtree a node with the given code. */
524 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
525 if (res
== NULL_TREE
)
527 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
531 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
538 #define TB_MOVE_HEAD(FCT) do { \
553 TB_MOVE_HEAD (TB_first_in_bind
);
557 TB_MOVE_HEAD (TB_last_in_bind
);
561 TB_MOVE_HEAD (TB_up_expr
);
565 TB_MOVE_HEAD (TB_prev_expr
);
569 TB_MOVE_HEAD (TB_next_expr
);
573 /* This command is a little bit special, since it deals with history
574 stack. For this reason it should keep the "head = ..." statement
575 and not use TB_MOVE_HEAD. */
579 t
= TB_history_prev ();
585 print_generic_expr (TB_OUT_FILE
, head
, 0);
586 fprintf (TB_OUT_FILE
, "\n");
597 /* Don't go further if it's the last node in this chain. */
598 if (head
&& TREE_CODE (head
) == BLOCK
)
599 TB_SET_HEAD (BLOCK_CHAIN (head
));
600 else if (head
&& TREE_CHAIN (head
))
601 TB_SET_HEAD (TREE_CHAIN (head
));
607 /* Go up to the current function declaration. */
608 TB_SET_HEAD (current_function_decl
);
609 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
613 /* Display a help message. */
616 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
617 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
619 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
628 fprintf (TB_OUT_FILE
, "Verbose on.\n");
633 fprintf (TB_OUT_FILE
, "Verbose off.\n");
639 /* Just exit from this function. */
653 /* Search the first node in this BIND_EXPR. */
656 TB_first_in_bind (tree node
)
660 if (node
== NULL_TREE
)
663 while ((t
= TB_prev_expr (node
)))
669 /* Search the last node in this BIND_EXPR. */
672 TB_last_in_bind (tree node
)
676 if (node
== NULL_TREE
)
679 while ((t
= TB_next_expr (node
)))
685 /* Search the parent expression for this node. */
688 TB_up_expr (tree node
)
691 if (node
== NULL_TREE
)
694 res
= TB_up_ht
.find (node
);
698 /* Search the previous expression in this BIND_EXPR. */
701 TB_prev_expr (tree node
)
703 node
= TB_current_chain_node (node
);
705 if (node
== NULL_TREE
)
708 node
= TB_up_expr (node
);
709 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
715 /* Search the next expression in this BIND_EXPR. */
718 TB_next_expr (tree node
)
720 node
= TB_current_chain_node (node
);
722 if (node
== NULL_TREE
)
725 node
= TREE_OPERAND (node
, 1);
730 TB_current_chain_node (tree node
)
732 if (node
== NULL_TREE
)
735 if (TREE_CODE (node
) == COMPOUND_EXPR
)
738 node
= TB_up_expr (node
);
741 if (TREE_CODE (node
) == COMPOUND_EXPR
)
744 node
= TB_up_expr (node
);
745 if (TREE_CODE (node
) == COMPOUND_EXPR
)
752 /* For each node store in its children nodes that the current node is their
753 parent. This function is used by walk_tree. */
756 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
757 void *data ATTRIBUTE_UNUSED
)
764 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
767 int n
= TREE_OPERAND_LENGTH (node
);
769 for (i
= 0; i
< n
; i
++)
771 tree op
= TREE_OPERAND (node
, i
);
772 slot
= TB_up_ht
.find_slot (op
, INSERT
);
777 /* Never stop walk_tree. */
781 /* Update information about upper expressions in the hash table. */
784 TB_update_up (tree node
)
788 walk_tree (&node
, store_child_info
, NULL
, NULL
);
790 /* Walk function's body. */
791 if (TREE_CODE (node
) == FUNCTION_DECL
)
792 if (DECL_SAVED_TREE (node
))
793 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
795 /* Walk rest of the chain. */
796 node
= TREE_CHAIN (node
);
798 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
801 /* Parse the input string for determining the command the user asked for. */
804 TB_get_command (char *input
)
806 unsigned int mn
, size_tok
;
810 space
= strchr (input
, ' ');
812 size_tok
= strlen (input
) - strlen (space
);
814 size_tok
= strlen (input
) - 1;
816 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
818 if (size_tok
!= TB_COMMAND_LEN (mn
))
821 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
823 /* Here we just determined the command. If this command takes
824 an argument, then the argument is determined later. */
825 return TB_COMMAND_CODE (mn
);
828 /* Not a valid command. */
829 return TB_UNUSED_COMMAND
;
832 /* Parse the input string for determining the tree code. */
834 static enum tree_code
835 TB_get_tree_code (char *input
)
837 unsigned int mn
, size_tok
;
841 space
= strchr (input
, ' ');
843 size_tok
= strlen (input
) - strlen (space
);
845 size_tok
= strlen (input
) - 1;
847 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
849 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
852 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
855 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
856 return TB_TREE_CODE (mn
);
860 /* This isn't a valid code. */
861 return LAST_AND_UNUSED_TREE_CODE
;
864 /* Find a node with a given code. This function is used as an argument to
868 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
871 enum tree_code
*code
;
872 code
= (enum tree_code
*) data
;
873 if (*code
== TREE_CODE (*tp
))
879 /* Returns a pointer to the last visited node. */
882 TB_history_prev (void)
884 if (!vec_safe_is_empty (TB_history_stack
))
886 tree last
= TB_history_stack
->last ();
887 TB_history_stack
->pop ();
893 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
894 (and null-terminate it). *LINEPTR is a pointer returned from malloc
895 (or NULL), pointing to *N characters of space. It is realloc'd as
896 necessary. Returns the number of characters read (not including the
897 null terminator), or -1 on error or EOF.
898 This function comes from sed (and is supposed to be a portable version
902 TB_getline (char **lineptr
, long *n
, FILE *stream
)
907 if (lineptr
== NULL
|| n
== NULL
)
916 /* Make sure we have a line buffer to start with. */
917 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
920 #define MAX_CANON 256
922 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
941 register int c
= getc (stream
);
944 else if ((*p
++ = c
) == '\n')
948 /* Need to enlarge the line buffer. */
951 line
= (char *) xrealloc (line
, size
);
964 /* Return a partial line since we got an error in the middle. */
966 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
967 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')