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
: pointer_hash
<tree_node
>
107 static inline bool equal (const value_type
&, const compare_type
&);
111 tree_upper_hasher::equal (const value_type
&parent
, const compare_type
&node
)
113 if (parent
== NULL
|| node
== NULL
)
118 int n
= TREE_OPERAND_LENGTH (parent
);
120 for (i
= 0; i
< n
; i
++)
121 if (node
== TREE_OPERAND (parent
, i
))
127 /* Static variables. */
128 static hash_table
<tree_upper_hasher
> *TB_up_ht
;
129 static vec
<tree
, va_gc
> *TB_history_stack
;
130 static int TB_verbose
= 1;
133 /* Entry point in the Tree Browser. */
136 browse_tree (tree begin
)
139 TB_CODE tbc
= TB_UNUSED_COMMAND
;
144 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
146 #define TB_SET_HEAD(N) do { \
147 vec_safe_push (TB_history_stack, N); \
152 print_generic_expr (TB_OUT_FILE, head, 0); \
153 fprintf (TB_OUT_FILE, "\n"); \
159 /* Store in a hashtable information about previous and upper statements. */
161 TB_up_ht
= new hash_table
<tree_upper_hasher
> (1023);
167 fprintf (TB_OUT_FILE
, "TB> ");
168 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
175 /* Get a new command. Otherwise the user just pressed enter, and thus
176 she expects the last command to be reexecuted. */
177 tbc
= TB_get_command (input
);
186 if (head
&& (INTEGRAL_TYPE_P (head
)
187 || TREE_CODE (head
) == REAL_TYPE
188 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
189 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
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_MIN_VALUE (head
));
204 if (head
&& TREE_CODE (head
) == TREE_VEC
)
206 /* This command takes another argument: the element number:
207 for example "elt 1". */
210 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
212 /* This command takes another argument: the element number:
213 for example "elt 1". */
221 if (head
&& TREE_CODE (head
) == TREE_LIST
)
222 TB_SET_HEAD (TREE_VALUE (head
));
228 if (head
&& TREE_CODE (head
) == TREE_LIST
)
229 TB_SET_HEAD (TREE_PURPOSE (head
));
235 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
236 TB_SET_HEAD (TREE_IMAGPART (head
));
242 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
243 TB_SET_HEAD (TREE_REALPART (head
));
249 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
250 TB_SET_HEAD (TREE_OPERAND (head
, 2));
256 if (head
&& TREE_CODE (head
) == BLOCK
)
257 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
262 case TB_SUPERCONTEXT
:
263 if (head
&& TREE_CODE (head
) == BLOCK
)
264 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
270 if (head
&& TREE_CODE (head
) == BLOCK
)
271 TB_SET_HEAD (BLOCK_VARS (head
));
272 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
273 TB_SET_HEAD (TREE_OPERAND (head
, 0));
278 case TB_REFERENCE_TO_THIS
:
279 if (head
&& TYPE_P (head
))
280 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
285 case TB_POINTER_TO_THIS
:
286 if (head
&& TYPE_P (head
))
287 TB_SET_HEAD (TYPE_POINTER_TO (head
));
293 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
294 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
300 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
301 || TREE_CODE (head
) == METHOD_TYPE
))
302 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
307 case TB_METHOD_BASE_TYPE
:
308 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
309 || TREE_CODE (head
) == METHOD_TYPE
)
310 && TYPE_METHOD_BASETYPE (head
))
311 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
317 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
318 || TREE_CODE (head
) == UNION_TYPE
319 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
320 TB_SET_HEAD (TYPE_FIELDS (head
));
326 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
327 TB_SET_HEAD (TYPE_DOMAIN (head
));
333 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
334 TB_SET_HEAD (TYPE_VALUES (head
));
340 if (head
&& TREE_CODE (head
) == PARM_DECL
)
341 TB_SET_HEAD (DECL_ARG_TYPE (head
));
347 if (head
&& DECL_P (head
))
348 TB_SET_HEAD (DECL_INITIAL (head
));
354 if (head
&& DECL_P (head
))
355 TB_SET_HEAD (DECL_RESULT_FLD (head
));
361 if (head
&& DECL_P (head
))
362 TB_SET_HEAD (DECL_ARGUMENTS (head
));
367 case TB_ABSTRACT_ORIGIN
:
368 if (head
&& DECL_P (head
))
369 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
370 else if (head
&& TREE_CODE (head
) == BLOCK
)
371 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
377 if (head
&& DECL_P (head
))
378 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
379 else if (head
&& TYPE_P (head
))
380 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
386 if (head
&& DECL_P (head
))
387 TB_SET_HEAD (DECL_CONTEXT (head
));
388 else if (head
&& TYPE_P (head
)
389 && TYPE_CONTEXT (head
))
390 TB_SET_HEAD (TYPE_CONTEXT (head
));
396 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
397 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
403 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
404 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
410 if (head
&& DECL_P (head
))
411 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
412 else if (head
&& TYPE_P (head
))
413 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
419 if (head
&& DECL_P (head
))
420 TB_SET_HEAD (DECL_SIZE (head
));
421 else if (head
&& TYPE_P (head
))
422 TB_SET_HEAD (TYPE_SIZE (head
));
428 if (head
&& TREE_TYPE (head
))
429 TB_SET_HEAD (TREE_TYPE (head
));
434 case TB_DECL_SAVED_TREE
:
435 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
436 && DECL_SAVED_TREE (head
))
437 TB_SET_HEAD (DECL_SAVED_TREE (head
));
443 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
444 TB_SET_HEAD (TREE_OPERAND (head
, 1));
450 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
451 TB_SET_HEAD (TREE_OPERAND (head
, 0));
457 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
458 TB_SET_HEAD (TREE_OPERAND (head
, 1));
464 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
465 TB_SET_HEAD (TREE_OPERAND (head
, 2));
471 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
472 TB_SET_HEAD (TREE_OPERAND (head
, 3));
484 case TB_PRETTY_PRINT
:
487 print_generic_stmt (TB_OUT_FILE
, head
, 0);
488 fprintf (TB_OUT_FILE
, "\n");
503 arg_text
= strchr (input
, ' ');
504 if (arg_text
== NULL
)
506 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
509 code
= TB_get_tree_code (arg_text
+ 1);
511 /* Search in the subtree a node with the given code. */
515 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
516 if (res
== NULL_TREE
)
518 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
522 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
529 #define TB_MOVE_HEAD(FCT) do { \
544 TB_MOVE_HEAD (TB_first_in_bind
);
548 TB_MOVE_HEAD (TB_last_in_bind
);
552 TB_MOVE_HEAD (TB_up_expr
);
556 TB_MOVE_HEAD (TB_prev_expr
);
560 TB_MOVE_HEAD (TB_next_expr
);
564 /* This command is a little bit special, since it deals with history
565 stack. For this reason it should keep the "head = ..." statement
566 and not use TB_MOVE_HEAD. */
570 t
= TB_history_prev ();
576 print_generic_expr (TB_OUT_FILE
, head
, 0);
577 fprintf (TB_OUT_FILE
, "\n");
588 /* Don't go further if it's the last node in this chain. */
589 if (head
&& TREE_CODE (head
) == BLOCK
)
590 TB_SET_HEAD (BLOCK_CHAIN (head
));
591 else if (head
&& TREE_CHAIN (head
))
592 TB_SET_HEAD (TREE_CHAIN (head
));
598 /* Go up to the current function declaration. */
599 TB_SET_HEAD (current_function_decl
);
600 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
604 /* Display a help message. */
607 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
608 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
610 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
619 fprintf (TB_OUT_FILE
, "Verbose on.\n");
624 fprintf (TB_OUT_FILE
, "Verbose off.\n");
630 /* Just exit from this function. */
645 /* Search the first node in this BIND_EXPR. */
648 TB_first_in_bind (tree node
)
652 if (node
== NULL_TREE
)
655 while ((t
= TB_prev_expr (node
)))
661 /* Search the last node in this BIND_EXPR. */
664 TB_last_in_bind (tree node
)
668 if (node
== NULL_TREE
)
671 while ((t
= TB_next_expr (node
)))
677 /* Search the parent expression for this node. */
680 TB_up_expr (tree node
)
683 if (node
== NULL_TREE
)
686 res
= TB_up_ht
->find (node
);
690 /* Search the previous expression in this BIND_EXPR. */
693 TB_prev_expr (tree node
)
695 node
= TB_current_chain_node (node
);
697 if (node
== NULL_TREE
)
700 node
= TB_up_expr (node
);
701 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
707 /* Search the next expression in this BIND_EXPR. */
710 TB_next_expr (tree node
)
712 node
= TB_current_chain_node (node
);
714 if (node
== NULL_TREE
)
717 node
= TREE_OPERAND (node
, 1);
722 TB_current_chain_node (tree node
)
724 if (node
== NULL_TREE
)
727 if (TREE_CODE (node
) == COMPOUND_EXPR
)
730 node
= TB_up_expr (node
);
733 if (TREE_CODE (node
) == COMPOUND_EXPR
)
736 node
= TB_up_expr (node
);
737 if (TREE_CODE (node
) == COMPOUND_EXPR
)
744 /* For each node store in its children nodes that the current node is their
745 parent. This function is used by walk_tree. */
748 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
749 void *data ATTRIBUTE_UNUSED
)
756 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
759 int n
= TREE_OPERAND_LENGTH (node
);
761 for (i
= 0; i
< n
; i
++)
763 tree op
= TREE_OPERAND (node
, i
);
764 slot
= TB_up_ht
->find_slot (op
, INSERT
);
769 /* Never stop walk_tree. */
773 /* Update information about upper expressions in the hash table. */
776 TB_update_up (tree node
)
780 walk_tree (&node
, store_child_info
, NULL
, NULL
);
782 /* Walk function's body. */
783 if (TREE_CODE (node
) == FUNCTION_DECL
)
784 if (DECL_SAVED_TREE (node
))
785 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
787 /* Walk rest of the chain. */
788 node
= TREE_CHAIN (node
);
790 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
793 /* Parse the input string for determining the command the user asked for. */
796 TB_get_command (char *input
)
798 unsigned int mn
, size_tok
;
802 space
= strchr (input
, ' ');
804 size_tok
= strlen (input
) - strlen (space
);
806 size_tok
= strlen (input
) - 1;
808 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
810 if (size_tok
!= TB_COMMAND_LEN (mn
))
813 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
815 /* Here we just determined the command. If this command takes
816 an argument, then the argument is determined later. */
817 return TB_COMMAND_CODE (mn
);
820 /* Not a valid command. */
821 return TB_UNUSED_COMMAND
;
824 /* Parse the input string for determining the tree code. */
826 static enum tree_code
827 TB_get_tree_code (char *input
)
829 unsigned int mn
, size_tok
;
833 space
= strchr (input
, ' ');
835 size_tok
= strlen (input
) - strlen (space
);
837 size_tok
= strlen (input
) - 1;
839 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
841 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
844 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
847 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
848 return TB_TREE_CODE (mn
);
852 /* This isn't a valid code. */
853 return LAST_AND_UNUSED_TREE_CODE
;
856 /* Find a node with a given code. This function is used as an argument to
860 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
863 enum tree_code
*code
;
864 code
= (enum tree_code
*) data
;
865 if (*code
== TREE_CODE (*tp
))
871 /* Returns a pointer to the last visited node. */
874 TB_history_prev (void)
876 if (!vec_safe_is_empty (TB_history_stack
))
878 tree last
= TB_history_stack
->last ();
879 TB_history_stack
->pop ();
885 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
886 (and null-terminate it). *LINEPTR is a pointer returned from malloc
887 (or NULL), pointing to *N characters of space. It is realloc'd as
888 necessary. Returns the number of characters read (not including the
889 null terminator), or -1 on error or EOF.
890 This function comes from sed (and is supposed to be a portable version
894 TB_getline (char **lineptr
, long *n
, FILE *stream
)
899 if (lineptr
== NULL
|| n
== NULL
)
908 /* Make sure we have a line buffer to start with. */
909 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
912 #define MAX_CANON 256
914 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
933 register int c
= getc (stream
);
936 else if ((*p
++ = c
) == '\n')
940 /* Need to enlarge the line buffer. */
943 line
= (char *) xrealloc (line
, size
);
956 /* Return a partial line since we got an error in the middle. */
958 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
959 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')