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"
24 #include "hash-table.h"
28 #include "double-int.h"
36 #include "tree-pretty-print.h"
37 #include "print-tree.h"
39 #define TB_OUT_FILE stdout
40 #define TB_IN_FILE stdin
41 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
42 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
44 /* Structures for handling Tree Browser's commands. */
45 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
47 #include "tree-browser.def"
51 typedef enum TB_Comm_code TB_CODE
;
55 const char *comm_text
;
60 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
61 static const struct tb_command tb_commands
[] =
63 #include "tree-browser.def"
67 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
68 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
69 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
70 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
73 /* Next structure is for parsing TREE_CODEs. */
76 const char *code_string
;
77 size_t code_string_len
;
80 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
81 #define END_OF_BASE_TREE_CODES \
82 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
83 static const struct tb_tree_code tb_tree_codes
[] =
85 #include "all-tree.def"
88 #undef END_OF_BASE_TREE_CODES
90 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
91 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
92 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
95 /* Function declarations. */
97 static long TB_getline (char **, long *, FILE *);
98 static TB_CODE
TB_get_command (char *);
99 static enum tree_code
TB_get_tree_code (char *);
100 static tree
find_node_with_code (tree
*, int *, void *);
101 static tree
store_child_info (tree
*, int *, void *);
102 static void TB_update_up (tree
);
103 static tree
TB_current_chain_node (tree
);
104 static tree
TB_prev_expr (tree
);
105 static tree
TB_next_expr (tree
);
106 static tree
TB_up_expr (tree
);
107 static tree
TB_first_in_bind (tree
);
108 static tree
TB_last_in_bind (tree
);
109 static tree
TB_history_prev (void);
111 /* FIXME: To be declared in a .h file. */
112 void browse_tree (tree
);
114 /* Hashtable helpers. */
115 struct tree_upper_hasher
: pointer_hash
<tree_node
>
117 static inline bool equal (const value_type
&, const compare_type
&);
121 tree_upper_hasher::equal (const value_type
&parent
, const compare_type
&node
)
123 if (parent
== NULL
|| node
== NULL
)
128 int n
= TREE_OPERAND_LENGTH (parent
);
130 for (i
= 0; i
< n
; i
++)
131 if (node
== TREE_OPERAND (parent
, i
))
137 /* Static variables. */
138 static hash_table
<tree_upper_hasher
> *TB_up_ht
;
139 static vec
<tree
, va_gc
> *TB_history_stack
;
140 static int TB_verbose
= 1;
143 /* Entry point in the Tree Browser. */
146 browse_tree (tree begin
)
149 TB_CODE tbc
= TB_UNUSED_COMMAND
;
154 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
156 #define TB_SET_HEAD(N) do { \
157 vec_safe_push (TB_history_stack, N); \
162 print_generic_expr (TB_OUT_FILE, head, 0); \
163 fprintf (TB_OUT_FILE, "\n"); \
169 /* Store in a hashtable information about previous and upper statements. */
171 TB_up_ht
= new hash_table
<tree_upper_hasher
> (1023);
177 fprintf (TB_OUT_FILE
, "TB> ");
178 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
185 /* Get a new command. Otherwise the user just pressed enter, and thus
186 she expects the last command to be reexecuted. */
187 tbc
= TB_get_command (input
);
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_MAX_VALUE (head
));
205 if (head
&& (INTEGRAL_TYPE_P (head
)
206 || TREE_CODE (head
) == REAL_TYPE
207 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
208 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
214 if (head
&& TREE_CODE (head
) == TREE_VEC
)
216 /* This command takes another argument: the element number:
217 for example "elt 1". */
220 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
222 /* This command takes another argument: the element number:
223 for example "elt 1". */
231 if (head
&& TREE_CODE (head
) == TREE_LIST
)
232 TB_SET_HEAD (TREE_VALUE (head
));
238 if (head
&& TREE_CODE (head
) == TREE_LIST
)
239 TB_SET_HEAD (TREE_PURPOSE (head
));
245 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
246 TB_SET_HEAD (TREE_IMAGPART (head
));
252 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
253 TB_SET_HEAD (TREE_REALPART (head
));
259 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
260 TB_SET_HEAD (TREE_OPERAND (head
, 2));
266 if (head
&& TREE_CODE (head
) == BLOCK
)
267 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
272 case TB_SUPERCONTEXT
:
273 if (head
&& TREE_CODE (head
) == BLOCK
)
274 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
280 if (head
&& TREE_CODE (head
) == BLOCK
)
281 TB_SET_HEAD (BLOCK_VARS (head
));
282 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
283 TB_SET_HEAD (TREE_OPERAND (head
, 0));
288 case TB_REFERENCE_TO_THIS
:
289 if (head
&& TYPE_P (head
))
290 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
295 case TB_POINTER_TO_THIS
:
296 if (head
&& TYPE_P (head
))
297 TB_SET_HEAD (TYPE_POINTER_TO (head
));
303 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
304 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
310 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
311 || TREE_CODE (head
) == METHOD_TYPE
))
312 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
317 case TB_METHOD_BASE_TYPE
:
318 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
319 || TREE_CODE (head
) == METHOD_TYPE
)
320 && TYPE_METHOD_BASETYPE (head
))
321 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
327 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
328 || TREE_CODE (head
) == UNION_TYPE
329 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
330 TB_SET_HEAD (TYPE_FIELDS (head
));
336 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
337 TB_SET_HEAD (TYPE_DOMAIN (head
));
343 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
344 TB_SET_HEAD (TYPE_VALUES (head
));
350 if (head
&& TREE_CODE (head
) == PARM_DECL
)
351 TB_SET_HEAD (DECL_ARG_TYPE (head
));
357 if (head
&& DECL_P (head
))
358 TB_SET_HEAD (DECL_INITIAL (head
));
364 if (head
&& DECL_P (head
))
365 TB_SET_HEAD (DECL_RESULT_FLD (head
));
371 if (head
&& DECL_P (head
))
372 TB_SET_HEAD (DECL_ARGUMENTS (head
));
377 case TB_ABSTRACT_ORIGIN
:
378 if (head
&& DECL_P (head
))
379 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
380 else if (head
&& TREE_CODE (head
) == BLOCK
)
381 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
387 if (head
&& DECL_P (head
))
388 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
389 else if (head
&& TYPE_P (head
))
390 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
396 if (head
&& DECL_P (head
))
397 TB_SET_HEAD (DECL_CONTEXT (head
));
398 else if (head
&& TYPE_P (head
)
399 && TYPE_CONTEXT (head
))
400 TB_SET_HEAD (TYPE_CONTEXT (head
));
406 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
407 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
413 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
414 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
420 if (head
&& DECL_P (head
))
421 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
422 else if (head
&& TYPE_P (head
))
423 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
429 if (head
&& DECL_P (head
))
430 TB_SET_HEAD (DECL_SIZE (head
));
431 else if (head
&& TYPE_P (head
))
432 TB_SET_HEAD (TYPE_SIZE (head
));
438 if (head
&& TREE_TYPE (head
))
439 TB_SET_HEAD (TREE_TYPE (head
));
444 case TB_DECL_SAVED_TREE
:
445 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
446 && DECL_SAVED_TREE (head
))
447 TB_SET_HEAD (DECL_SAVED_TREE (head
));
453 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
454 TB_SET_HEAD (TREE_OPERAND (head
, 1));
460 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
461 TB_SET_HEAD (TREE_OPERAND (head
, 0));
467 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
468 TB_SET_HEAD (TREE_OPERAND (head
, 1));
474 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
475 TB_SET_HEAD (TREE_OPERAND (head
, 2));
481 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
482 TB_SET_HEAD (TREE_OPERAND (head
, 3));
494 case TB_PRETTY_PRINT
:
497 print_generic_stmt (TB_OUT_FILE
, head
, 0);
498 fprintf (TB_OUT_FILE
, "\n");
513 arg_text
= strchr (input
, ' ');
514 if (arg_text
== NULL
)
516 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
519 code
= TB_get_tree_code (arg_text
+ 1);
521 /* Search in the subtree a node with the given code. */
525 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
526 if (res
== NULL_TREE
)
528 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
532 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
539 #define TB_MOVE_HEAD(FCT) do { \
554 TB_MOVE_HEAD (TB_first_in_bind
);
558 TB_MOVE_HEAD (TB_last_in_bind
);
562 TB_MOVE_HEAD (TB_up_expr
);
566 TB_MOVE_HEAD (TB_prev_expr
);
570 TB_MOVE_HEAD (TB_next_expr
);
574 /* This command is a little bit special, since it deals with history
575 stack. For this reason it should keep the "head = ..." statement
576 and not use TB_MOVE_HEAD. */
580 t
= TB_history_prev ();
586 print_generic_expr (TB_OUT_FILE
, head
, 0);
587 fprintf (TB_OUT_FILE
, "\n");
598 /* Don't go further if it's the last node in this chain. */
599 if (head
&& TREE_CODE (head
) == BLOCK
)
600 TB_SET_HEAD (BLOCK_CHAIN (head
));
601 else if (head
&& TREE_CHAIN (head
))
602 TB_SET_HEAD (TREE_CHAIN (head
));
608 /* Go up to the current function declaration. */
609 TB_SET_HEAD (current_function_decl
);
610 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
614 /* Display a help message. */
617 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
618 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
620 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
629 fprintf (TB_OUT_FILE
, "Verbose on.\n");
634 fprintf (TB_OUT_FILE
, "Verbose off.\n");
640 /* Just exit from this function. */
655 /* Search the first node in this BIND_EXPR. */
658 TB_first_in_bind (tree node
)
662 if (node
== NULL_TREE
)
665 while ((t
= TB_prev_expr (node
)))
671 /* Search the last node in this BIND_EXPR. */
674 TB_last_in_bind (tree node
)
678 if (node
== NULL_TREE
)
681 while ((t
= TB_next_expr (node
)))
687 /* Search the parent expression for this node. */
690 TB_up_expr (tree node
)
693 if (node
== NULL_TREE
)
696 res
= TB_up_ht
->find (node
);
700 /* Search the previous expression in this BIND_EXPR. */
703 TB_prev_expr (tree node
)
705 node
= TB_current_chain_node (node
);
707 if (node
== NULL_TREE
)
710 node
= TB_up_expr (node
);
711 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
717 /* Search the next expression in this BIND_EXPR. */
720 TB_next_expr (tree node
)
722 node
= TB_current_chain_node (node
);
724 if (node
== NULL_TREE
)
727 node
= TREE_OPERAND (node
, 1);
732 TB_current_chain_node (tree node
)
734 if (node
== NULL_TREE
)
737 if (TREE_CODE (node
) == COMPOUND_EXPR
)
740 node
= TB_up_expr (node
);
743 if (TREE_CODE (node
) == COMPOUND_EXPR
)
746 node
= TB_up_expr (node
);
747 if (TREE_CODE (node
) == COMPOUND_EXPR
)
754 /* For each node store in its children nodes that the current node is their
755 parent. This function is used by walk_tree. */
758 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
759 void *data ATTRIBUTE_UNUSED
)
766 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
769 int n
= TREE_OPERAND_LENGTH (node
);
771 for (i
= 0; i
< n
; i
++)
773 tree op
= TREE_OPERAND (node
, i
);
774 slot
= TB_up_ht
->find_slot (op
, INSERT
);
779 /* Never stop walk_tree. */
783 /* Update information about upper expressions in the hash table. */
786 TB_update_up (tree node
)
790 walk_tree (&node
, store_child_info
, NULL
, NULL
);
792 /* Walk function's body. */
793 if (TREE_CODE (node
) == FUNCTION_DECL
)
794 if (DECL_SAVED_TREE (node
))
795 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
797 /* Walk rest of the chain. */
798 node
= TREE_CHAIN (node
);
800 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
803 /* Parse the input string for determining the command the user asked for. */
806 TB_get_command (char *input
)
808 unsigned int mn
, size_tok
;
812 space
= strchr (input
, ' ');
814 size_tok
= strlen (input
) - strlen (space
);
816 size_tok
= strlen (input
) - 1;
818 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
820 if (size_tok
!= TB_COMMAND_LEN (mn
))
823 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
825 /* Here we just determined the command. If this command takes
826 an argument, then the argument is determined later. */
827 return TB_COMMAND_CODE (mn
);
830 /* Not a valid command. */
831 return TB_UNUSED_COMMAND
;
834 /* Parse the input string for determining the tree code. */
836 static enum tree_code
837 TB_get_tree_code (char *input
)
839 unsigned int mn
, size_tok
;
843 space
= strchr (input
, ' ');
845 size_tok
= strlen (input
) - strlen (space
);
847 size_tok
= strlen (input
) - 1;
849 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
851 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
854 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
857 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
858 return TB_TREE_CODE (mn
);
862 /* This isn't a valid code. */
863 return LAST_AND_UNUSED_TREE_CODE
;
866 /* Find a node with a given code. This function is used as an argument to
870 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
873 enum tree_code
*code
;
874 code
= (enum tree_code
*) data
;
875 if (*code
== TREE_CODE (*tp
))
881 /* Returns a pointer to the last visited node. */
884 TB_history_prev (void)
886 if (!vec_safe_is_empty (TB_history_stack
))
888 tree last
= TB_history_stack
->last ();
889 TB_history_stack
->pop ();
895 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
896 (and null-terminate it). *LINEPTR is a pointer returned from malloc
897 (or NULL), pointing to *N characters of space. It is realloc'd as
898 necessary. Returns the number of characters read (not including the
899 null terminator), or -1 on error or EOF.
900 This function comes from sed (and is supposed to be a portable version
904 TB_getline (char **lineptr
, long *n
, FILE *stream
)
909 if (lineptr
== NULL
|| n
== NULL
)
918 /* Make sure we have a line buffer to start with. */
919 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
922 #define MAX_CANON 256
924 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
943 register int c
= getc (stream
);
946 else if ((*p
++ = c
) == '\n')
950 /* Need to enlarge the line buffer. */
953 line
= (char *) xrealloc (line
, size
);
966 /* Return a partial line since we got an error in the middle. */
968 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
969 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')