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"
29 #include "tree-pretty-print.h"
30 #include "print-tree.h"
32 #define TB_OUT_FILE stdout
33 #define TB_IN_FILE stdin
34 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
35 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
37 /* Structures for handling Tree Browser's commands. */
38 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
40 #include "tree-browser.def"
44 typedef enum TB_Comm_code TB_CODE
;
48 const char *comm_text
;
53 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
54 static const struct tb_command tb_commands
[] =
56 #include "tree-browser.def"
60 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
61 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
62 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
63 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
66 /* Next structure is for parsing TREE_CODEs. */
69 const char *code_string
;
70 size_t code_string_len
;
73 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
74 #define END_OF_BASE_TREE_CODES \
75 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
76 static const struct tb_tree_code tb_tree_codes
[] =
78 #include "all-tree.def"
81 #undef END_OF_BASE_TREE_CODES
83 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
84 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
85 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
88 /* Function declarations. */
90 static long TB_getline (char **, long *, FILE *);
91 static TB_CODE
TB_get_command (char *);
92 static enum tree_code
TB_get_tree_code (char *);
93 static tree
find_node_with_code (tree
*, int *, void *);
94 static tree
store_child_info (tree
*, int *, void *);
95 static void TB_update_up (tree
);
96 static tree
TB_current_chain_node (tree
);
97 static tree
TB_prev_expr (tree
);
98 static tree
TB_next_expr (tree
);
99 static tree
TB_up_expr (tree
);
100 static tree
TB_first_in_bind (tree
);
101 static tree
TB_last_in_bind (tree
);
102 static tree
TB_history_prev (void);
104 /* FIXME: To be declared in a .h file. */
105 void browse_tree (tree
);
107 /* Hashtable helpers. */
108 struct tree_upper_hasher
: pointer_hash
<tree_node
>
110 static inline bool equal (const value_type
&, const compare_type
&);
114 tree_upper_hasher::equal (const value_type
&parent
, const compare_type
&node
)
116 if (parent
== NULL
|| node
== NULL
)
121 int n
= TREE_OPERAND_LENGTH (parent
);
123 for (i
= 0; i
< n
; i
++)
124 if (node
== TREE_OPERAND (parent
, i
))
130 /* Static variables. */
131 static hash_table
<tree_upper_hasher
> *TB_up_ht
;
132 static vec
<tree
, va_gc
> *TB_history_stack
;
133 static int TB_verbose
= 1;
136 /* Entry point in the Tree Browser. */
139 browse_tree (tree begin
)
142 TB_CODE tbc
= TB_UNUSED_COMMAND
;
147 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
149 #define TB_SET_HEAD(N) do { \
150 vec_safe_push (TB_history_stack, N); \
155 print_generic_expr (TB_OUT_FILE, head, 0); \
156 fprintf (TB_OUT_FILE, "\n"); \
162 /* Store in a hashtable information about previous and upper statements. */
164 TB_up_ht
= new hash_table
<tree_upper_hasher
> (1023);
170 fprintf (TB_OUT_FILE
, "TB> ");
171 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
178 /* Get a new command. Otherwise the user just pressed enter, and thus
179 she expects the last command to be reexecuted. */
180 tbc
= TB_get_command (input
);
189 if (head
&& (INTEGRAL_TYPE_P (head
)
190 || TREE_CODE (head
) == REAL_TYPE
191 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
192 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
198 if (head
&& (INTEGRAL_TYPE_P (head
)
199 || TREE_CODE (head
) == REAL_TYPE
200 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
201 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
207 if (head
&& TREE_CODE (head
) == TREE_VEC
)
209 /* This command takes another argument: the element number:
210 for example "elt 1". */
213 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
215 /* This command takes another argument: the element number:
216 for example "elt 1". */
224 if (head
&& TREE_CODE (head
) == TREE_LIST
)
225 TB_SET_HEAD (TREE_VALUE (head
));
231 if (head
&& TREE_CODE (head
) == TREE_LIST
)
232 TB_SET_HEAD (TREE_PURPOSE (head
));
238 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
239 TB_SET_HEAD (TREE_IMAGPART (head
));
245 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
246 TB_SET_HEAD (TREE_REALPART (head
));
252 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
253 TB_SET_HEAD (TREE_OPERAND (head
, 2));
259 if (head
&& TREE_CODE (head
) == BLOCK
)
260 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
265 case TB_SUPERCONTEXT
:
266 if (head
&& TREE_CODE (head
) == BLOCK
)
267 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
273 if (head
&& TREE_CODE (head
) == BLOCK
)
274 TB_SET_HEAD (BLOCK_VARS (head
));
275 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
276 TB_SET_HEAD (TREE_OPERAND (head
, 0));
281 case TB_REFERENCE_TO_THIS
:
282 if (head
&& TYPE_P (head
))
283 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
288 case TB_POINTER_TO_THIS
:
289 if (head
&& TYPE_P (head
))
290 TB_SET_HEAD (TYPE_POINTER_TO (head
));
296 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
297 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
303 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
304 || TREE_CODE (head
) == METHOD_TYPE
))
305 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
310 case TB_METHOD_BASE_TYPE
:
311 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
312 || TREE_CODE (head
) == METHOD_TYPE
)
313 && TYPE_METHOD_BASETYPE (head
))
314 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
320 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
321 || TREE_CODE (head
) == UNION_TYPE
322 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
323 TB_SET_HEAD (TYPE_FIELDS (head
));
329 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
330 TB_SET_HEAD (TYPE_DOMAIN (head
));
336 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
337 TB_SET_HEAD (TYPE_VALUES (head
));
343 if (head
&& TREE_CODE (head
) == PARM_DECL
)
344 TB_SET_HEAD (DECL_ARG_TYPE (head
));
350 if (head
&& DECL_P (head
))
351 TB_SET_HEAD (DECL_INITIAL (head
));
357 if (head
&& DECL_P (head
))
358 TB_SET_HEAD (DECL_RESULT_FLD (head
));
364 if (head
&& DECL_P (head
))
365 TB_SET_HEAD (DECL_ARGUMENTS (head
));
370 case TB_ABSTRACT_ORIGIN
:
371 if (head
&& DECL_P (head
))
372 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
373 else if (head
&& TREE_CODE (head
) == BLOCK
)
374 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
380 if (head
&& DECL_P (head
))
381 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
382 else if (head
&& TYPE_P (head
))
383 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
389 if (head
&& DECL_P (head
))
390 TB_SET_HEAD (DECL_CONTEXT (head
));
391 else if (head
&& TYPE_P (head
)
392 && TYPE_CONTEXT (head
))
393 TB_SET_HEAD (TYPE_CONTEXT (head
));
399 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
400 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
406 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
407 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
413 if (head
&& DECL_P (head
))
414 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
415 else if (head
&& TYPE_P (head
))
416 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
422 if (head
&& DECL_P (head
))
423 TB_SET_HEAD (DECL_SIZE (head
));
424 else if (head
&& TYPE_P (head
))
425 TB_SET_HEAD (TYPE_SIZE (head
));
431 if (head
&& TREE_TYPE (head
))
432 TB_SET_HEAD (TREE_TYPE (head
));
437 case TB_DECL_SAVED_TREE
:
438 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
439 && DECL_SAVED_TREE (head
))
440 TB_SET_HEAD (DECL_SAVED_TREE (head
));
446 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
447 TB_SET_HEAD (TREE_OPERAND (head
, 1));
453 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
454 TB_SET_HEAD (TREE_OPERAND (head
, 0));
460 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
461 TB_SET_HEAD (TREE_OPERAND (head
, 1));
467 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
468 TB_SET_HEAD (TREE_OPERAND (head
, 2));
474 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
475 TB_SET_HEAD (TREE_OPERAND (head
, 3));
487 case TB_PRETTY_PRINT
:
490 print_generic_stmt (TB_OUT_FILE
, head
, 0);
491 fprintf (TB_OUT_FILE
, "\n");
506 arg_text
= strchr (input
, ' ');
507 if (arg_text
== NULL
)
509 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
512 code
= TB_get_tree_code (arg_text
+ 1);
514 /* Search in the subtree a node with the given code. */
518 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
519 if (res
== NULL_TREE
)
521 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
525 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
532 #define TB_MOVE_HEAD(FCT) do { \
547 TB_MOVE_HEAD (TB_first_in_bind
);
551 TB_MOVE_HEAD (TB_last_in_bind
);
555 TB_MOVE_HEAD (TB_up_expr
);
559 TB_MOVE_HEAD (TB_prev_expr
);
563 TB_MOVE_HEAD (TB_next_expr
);
567 /* This command is a little bit special, since it deals with history
568 stack. For this reason it should keep the "head = ..." statement
569 and not use TB_MOVE_HEAD. */
573 t
= TB_history_prev ();
579 print_generic_expr (TB_OUT_FILE
, head
, 0);
580 fprintf (TB_OUT_FILE
, "\n");
591 /* Don't go further if it's the last node in this chain. */
592 if (head
&& TREE_CODE (head
) == BLOCK
)
593 TB_SET_HEAD (BLOCK_CHAIN (head
));
594 else if (head
&& TREE_CHAIN (head
))
595 TB_SET_HEAD (TREE_CHAIN (head
));
601 /* Go up to the current function declaration. */
602 TB_SET_HEAD (current_function_decl
);
603 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
607 /* Display a help message. */
610 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
611 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
613 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
622 fprintf (TB_OUT_FILE
, "Verbose on.\n");
627 fprintf (TB_OUT_FILE
, "Verbose off.\n");
633 /* Just exit from this function. */
648 /* Search the first node in this BIND_EXPR. */
651 TB_first_in_bind (tree node
)
655 if (node
== NULL_TREE
)
658 while ((t
= TB_prev_expr (node
)))
664 /* Search the last node in this BIND_EXPR. */
667 TB_last_in_bind (tree node
)
671 if (node
== NULL_TREE
)
674 while ((t
= TB_next_expr (node
)))
680 /* Search the parent expression for this node. */
683 TB_up_expr (tree node
)
686 if (node
== NULL_TREE
)
689 res
= TB_up_ht
->find (node
);
693 /* Search the previous expression in this BIND_EXPR. */
696 TB_prev_expr (tree node
)
698 node
= TB_current_chain_node (node
);
700 if (node
== NULL_TREE
)
703 node
= TB_up_expr (node
);
704 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
710 /* Search the next expression in this BIND_EXPR. */
713 TB_next_expr (tree node
)
715 node
= TB_current_chain_node (node
);
717 if (node
== NULL_TREE
)
720 node
= TREE_OPERAND (node
, 1);
725 TB_current_chain_node (tree node
)
727 if (node
== NULL_TREE
)
730 if (TREE_CODE (node
) == COMPOUND_EXPR
)
733 node
= TB_up_expr (node
);
736 if (TREE_CODE (node
) == COMPOUND_EXPR
)
739 node
= TB_up_expr (node
);
740 if (TREE_CODE (node
) == COMPOUND_EXPR
)
747 /* For each node store in its children nodes that the current node is their
748 parent. This function is used by walk_tree. */
751 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
752 void *data ATTRIBUTE_UNUSED
)
759 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
762 int n
= TREE_OPERAND_LENGTH (node
);
764 for (i
= 0; i
< n
; i
++)
766 tree op
= TREE_OPERAND (node
, i
);
767 slot
= TB_up_ht
->find_slot (op
, INSERT
);
772 /* Never stop walk_tree. */
776 /* Update information about upper expressions in the hash table. */
779 TB_update_up (tree node
)
783 walk_tree (&node
, store_child_info
, NULL
, NULL
);
785 /* Walk function's body. */
786 if (TREE_CODE (node
) == FUNCTION_DECL
)
787 if (DECL_SAVED_TREE (node
))
788 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
790 /* Walk rest of the chain. */
791 node
= TREE_CHAIN (node
);
793 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
796 /* Parse the input string for determining the command the user asked for. */
799 TB_get_command (char *input
)
801 unsigned int mn
, size_tok
;
805 space
= strchr (input
, ' ');
807 size_tok
= strlen (input
) - strlen (space
);
809 size_tok
= strlen (input
) - 1;
811 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
813 if (size_tok
!= TB_COMMAND_LEN (mn
))
816 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
818 /* Here we just determined the command. If this command takes
819 an argument, then the argument is determined later. */
820 return TB_COMMAND_CODE (mn
);
823 /* Not a valid command. */
824 return TB_UNUSED_COMMAND
;
827 /* Parse the input string for determining the tree code. */
829 static enum tree_code
830 TB_get_tree_code (char *input
)
832 unsigned int mn
, size_tok
;
836 space
= strchr (input
, ' ');
838 size_tok
= strlen (input
) - strlen (space
);
840 size_tok
= strlen (input
) - 1;
842 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
844 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
847 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
850 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
851 return TB_TREE_CODE (mn
);
855 /* This isn't a valid code. */
856 return LAST_AND_UNUSED_TREE_CODE
;
859 /* Find a node with a given code. This function is used as an argument to
863 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
866 enum tree_code
*code
;
867 code
= (enum tree_code
*) data
;
868 if (*code
== TREE_CODE (*tp
))
874 /* Returns a pointer to the last visited node. */
877 TB_history_prev (void)
879 if (!vec_safe_is_empty (TB_history_stack
))
881 tree last
= TB_history_stack
->last ();
882 TB_history_stack
->pop ();
888 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
889 (and null-terminate it). *LINEPTR is a pointer returned from malloc
890 (or NULL), pointing to *N characters of space. It is realloc'd as
891 necessary. Returns the number of characters read (not including the
892 null terminator), or -1 on error or EOF.
893 This function comes from sed (and is supposed to be a portable version
897 TB_getline (char **lineptr
, long *n
, FILE *stream
)
902 if (lineptr
== NULL
|| n
== NULL
)
911 /* Make sure we have a line buffer to start with. */
912 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
915 #define MAX_CANON 256
917 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
936 register int c
= getc (stream
);
939 else if ((*p
++ = c
) == '\n')
943 /* Need to enlarge the line buffer. */
946 line
= (char *) xrealloc (line
, size
);
959 /* Return a partial line since we got an error in the middle. */
961 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
962 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')