2 Copyright (C) 2002-2013 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"
28 #define TB_OUT_FILE stdout
29 #define TB_IN_FILE stdin
30 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
31 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
33 /* Structures for handling Tree Browser's commands. */
34 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
36 #include "tree-browser.def"
40 typedef enum TB_Comm_code TB_CODE
;
44 const char *comm_text
;
49 #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code },
50 static const struct tb_command tb_commands
[] =
52 #include "tree-browser.def"
56 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
57 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
58 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
59 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
62 /* Next structure is for parsing TREE_CODEs. */
65 const char *code_string
;
66 size_t code_string_len
;
69 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
70 #define END_OF_BASE_TREE_CODES \
71 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
72 static const struct tb_tree_code tb_tree_codes
[] =
74 #include "all-tree.def"
77 #undef END_OF_BASE_TREE_CODES
79 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
80 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
81 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
84 /* Function declarations. */
86 static long TB_getline (char **, long *, FILE *);
87 static TB_CODE
TB_get_command (char *);
88 static enum tree_code
TB_get_tree_code (char *);
89 static tree
find_node_with_code (tree
*, int *, void *);
90 static tree
store_child_info (tree
*, int *, void *);
91 static void TB_update_up (tree
);
92 static tree
TB_current_chain_node (tree
);
93 static tree
TB_prev_expr (tree
);
94 static tree
TB_next_expr (tree
);
95 static tree
TB_up_expr (tree
);
96 static tree
TB_first_in_bind (tree
);
97 static tree
TB_last_in_bind (tree
);
98 static tree
TB_history_prev (void);
100 /* FIXME: To be declared in a .h file. */
101 void browse_tree (tree
);
103 /* Hashtable helpers. */
104 struct tree_upper_hasher
: typed_noop_remove
<tree_node
>
106 typedef tree_node value_type
;
107 typedef tree_node compare_type
;
108 static inline hashval_t
hash (const value_type
*);
109 static inline bool equal (const value_type
*, const compare_type
*);
113 tree_upper_hasher::hash (const value_type
*v
)
115 return pointer_hash
<value_type
>::hash (v
);
119 tree_upper_hasher::equal (const value_type
*parent
, const compare_type
*node
)
121 if (parent
== NULL
|| node
== NULL
)
126 int n
= TREE_OPERAND_LENGTH (parent
);
128 for (i
= 0; i
< n
; i
++)
129 if (node
== TREE_OPERAND (parent
, i
))
135 /* Static variables. */
136 static hash_table
<tree_upper_hasher
> TB_up_ht
;
137 static vec
<tree
, va_gc
> *TB_history_stack
;
138 static int TB_verbose
= 1;
141 /* Entry point in the Tree Browser. */
144 browse_tree (tree begin
)
147 TB_CODE tbc
= TB_UNUSED_COMMAND
;
152 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
154 #define TB_SET_HEAD(N) do { \
155 vec_safe_push (TB_history_stack, N); \
160 print_generic_expr (TB_OUT_FILE, head, 0); \
161 fprintf (TB_OUT_FILE, "\n"); \
167 /* Store in a hashtable information about previous and upper statements. */
169 TB_up_ht
.create (1023);
175 fprintf (TB_OUT_FILE
, "TB> ");
176 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
183 /* Get a new command. Otherwise the user just pressed enter, and thus
184 she expects the last command to be reexecuted. */
185 tbc
= TB_get_command (input
);
194 if (head
&& (INTEGRAL_TYPE_P (head
)
195 || TREE_CODE (head
) == REAL_TYPE
196 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
197 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
203 if (head
&& (INTEGRAL_TYPE_P (head
)
204 || TREE_CODE (head
) == REAL_TYPE
205 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
206 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
212 if (head
&& TREE_CODE (head
) == TREE_VEC
)
214 /* This command takes another argument: the element number:
215 for example "elt 1". */
218 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
220 /* This command takes another argument: the element number:
221 for example "elt 1". */
229 if (head
&& TREE_CODE (head
) == TREE_LIST
)
230 TB_SET_HEAD (TREE_VALUE (head
));
236 if (head
&& TREE_CODE (head
) == TREE_LIST
)
237 TB_SET_HEAD (TREE_PURPOSE (head
));
243 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
244 TB_SET_HEAD (TREE_IMAGPART (head
));
250 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
251 TB_SET_HEAD (TREE_REALPART (head
));
257 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
258 TB_SET_HEAD (TREE_OPERAND (head
, 2));
264 if (head
&& TREE_CODE (head
) == BLOCK
)
265 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
270 case TB_SUPERCONTEXT
:
271 if (head
&& TREE_CODE (head
) == BLOCK
)
272 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
278 if (head
&& TREE_CODE (head
) == BLOCK
)
279 TB_SET_HEAD (BLOCK_VARS (head
));
280 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
281 TB_SET_HEAD (TREE_OPERAND (head
, 0));
286 case TB_REFERENCE_TO_THIS
:
287 if (head
&& TYPE_P (head
))
288 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
293 case TB_POINTER_TO_THIS
:
294 if (head
&& TYPE_P (head
))
295 TB_SET_HEAD (TYPE_POINTER_TO (head
));
301 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
302 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
308 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
309 || TREE_CODE (head
) == METHOD_TYPE
))
310 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
315 case TB_METHOD_BASE_TYPE
:
316 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
317 || TREE_CODE (head
) == METHOD_TYPE
)
318 && TYPE_METHOD_BASETYPE (head
))
319 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
325 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
326 || TREE_CODE (head
) == UNION_TYPE
327 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
328 TB_SET_HEAD (TYPE_FIELDS (head
));
334 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
335 TB_SET_HEAD (TYPE_DOMAIN (head
));
341 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
342 TB_SET_HEAD (TYPE_VALUES (head
));
348 if (head
&& TREE_CODE (head
) == PARM_DECL
)
349 TB_SET_HEAD (DECL_ARG_TYPE (head
));
355 if (head
&& DECL_P (head
))
356 TB_SET_HEAD (DECL_INITIAL (head
));
362 if (head
&& DECL_P (head
))
363 TB_SET_HEAD (DECL_RESULT_FLD (head
));
369 if (head
&& DECL_P (head
))
370 TB_SET_HEAD (DECL_ARGUMENTS (head
));
375 case TB_ABSTRACT_ORIGIN
:
376 if (head
&& DECL_P (head
))
377 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
378 else if (head
&& TREE_CODE (head
) == BLOCK
)
379 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
385 if (head
&& DECL_P (head
))
386 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
387 else if (head
&& TYPE_P (head
))
388 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
394 if (head
&& DECL_P (head
))
395 TB_SET_HEAD (DECL_CONTEXT (head
));
396 else if (head
&& TYPE_P (head
)
397 && TYPE_CONTEXT (head
))
398 TB_SET_HEAD (TYPE_CONTEXT (head
));
404 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
405 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
411 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
412 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
418 if (head
&& DECL_P (head
))
419 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
420 else if (head
&& TYPE_P (head
))
421 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
427 if (head
&& DECL_P (head
))
428 TB_SET_HEAD (DECL_SIZE (head
));
429 else if (head
&& TYPE_P (head
))
430 TB_SET_HEAD (TYPE_SIZE (head
));
436 if (head
&& TREE_TYPE (head
))
437 TB_SET_HEAD (TREE_TYPE (head
));
442 case TB_DECL_SAVED_TREE
:
443 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
444 && DECL_SAVED_TREE (head
))
445 TB_SET_HEAD (DECL_SAVED_TREE (head
));
451 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
452 TB_SET_HEAD (TREE_OPERAND (head
, 1));
458 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
459 TB_SET_HEAD (TREE_OPERAND (head
, 0));
465 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
466 TB_SET_HEAD (TREE_OPERAND (head
, 1));
472 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
473 TB_SET_HEAD (TREE_OPERAND (head
, 2));
479 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
480 TB_SET_HEAD (TREE_OPERAND (head
, 3));
492 case TB_PRETTY_PRINT
:
495 print_generic_stmt (TB_OUT_FILE
, head
, 0);
496 fprintf (TB_OUT_FILE
, "\n");
511 arg_text
= strchr (input
, ' ');
512 if (arg_text
== NULL
)
514 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
517 code
= TB_get_tree_code (arg_text
+ 1);
519 /* Search in the subtree a node with the given code. */
523 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
524 if (res
== NULL_TREE
)
526 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
530 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
537 #define TB_MOVE_HEAD(FCT) do { \
552 TB_MOVE_HEAD (TB_first_in_bind
);
556 TB_MOVE_HEAD (TB_last_in_bind
);
560 TB_MOVE_HEAD (TB_up_expr
);
564 TB_MOVE_HEAD (TB_prev_expr
);
568 TB_MOVE_HEAD (TB_next_expr
);
572 /* This command is a little bit special, since it deals with history
573 stack. For this reason it should keep the "head = ..." statement
574 and not use TB_MOVE_HEAD. */
578 t
= TB_history_prev ();
584 print_generic_expr (TB_OUT_FILE
, head
, 0);
585 fprintf (TB_OUT_FILE
, "\n");
596 /* Don't go further if it's the last node in this chain. */
597 if (head
&& TREE_CODE (head
) == BLOCK
)
598 TB_SET_HEAD (BLOCK_CHAIN (head
));
599 else if (head
&& TREE_CHAIN (head
))
600 TB_SET_HEAD (TREE_CHAIN (head
));
606 /* Go up to the current function declaration. */
607 TB_SET_HEAD (current_function_decl
);
608 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
612 /* Display a help message. */
615 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
616 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
618 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
627 fprintf (TB_OUT_FILE
, "Verbose on.\n");
632 fprintf (TB_OUT_FILE
, "Verbose off.\n");
638 /* Just exit from this function. */
652 /* Search the first node in this BIND_EXPR. */
655 TB_first_in_bind (tree node
)
659 if (node
== NULL_TREE
)
662 while ((t
= TB_prev_expr (node
)))
668 /* Search the last node in this BIND_EXPR. */
671 TB_last_in_bind (tree node
)
675 if (node
== NULL_TREE
)
678 while ((t
= TB_next_expr (node
)))
684 /* Search the parent expression for this node. */
687 TB_up_expr (tree node
)
690 if (node
== NULL_TREE
)
693 res
= TB_up_ht
.find (node
);
697 /* Search the previous expression in this BIND_EXPR. */
700 TB_prev_expr (tree node
)
702 node
= TB_current_chain_node (node
);
704 if (node
== NULL_TREE
)
707 node
= TB_up_expr (node
);
708 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
714 /* Search the next expression in this BIND_EXPR. */
717 TB_next_expr (tree node
)
719 node
= TB_current_chain_node (node
);
721 if (node
== NULL_TREE
)
724 node
= TREE_OPERAND (node
, 1);
729 TB_current_chain_node (tree node
)
731 if (node
== NULL_TREE
)
734 if (TREE_CODE (node
) == COMPOUND_EXPR
)
737 node
= TB_up_expr (node
);
740 if (TREE_CODE (node
) == COMPOUND_EXPR
)
743 node
= TB_up_expr (node
);
744 if (TREE_CODE (node
) == COMPOUND_EXPR
)
751 /* For each node store in its children nodes that the current node is their
752 parent. This function is used by walk_tree. */
755 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
756 void *data ATTRIBUTE_UNUSED
)
763 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
766 int n
= TREE_OPERAND_LENGTH (node
);
768 for (i
= 0; i
< n
; i
++)
770 tree op
= TREE_OPERAND (node
, i
);
771 slot
= TB_up_ht
.find_slot (op
, INSERT
);
776 /* Never stop walk_tree. */
780 /* Update information about upper expressions in the hash table. */
783 TB_update_up (tree node
)
787 walk_tree (&node
, store_child_info
, NULL
, NULL
);
789 /* Walk function's body. */
790 if (TREE_CODE (node
) == FUNCTION_DECL
)
791 if (DECL_SAVED_TREE (node
))
792 walk_tree (&DECL_SAVED_TREE (node
), store_child_info
, NULL
, NULL
);
794 /* Walk rest of the chain. */
795 node
= TREE_CHAIN (node
);
797 fprintf (TB_OUT_FILE
, "Up/prev expressions updated.\n");
800 /* Parse the input string for determining the command the user asked for. */
803 TB_get_command (char *input
)
805 unsigned int mn
, size_tok
;
809 space
= strchr (input
, ' ');
811 size_tok
= strlen (input
) - strlen (space
);
813 size_tok
= strlen (input
) - 1;
815 for (mn
= 0; mn
< TB_UNUSED_COMMAND
; mn
++)
817 if (size_tok
!= TB_COMMAND_LEN (mn
))
820 comp
= memcmp (input
, TB_COMMAND_TEXT (mn
), TB_COMMAND_LEN (mn
));
822 /* Here we just determined the command. If this command takes
823 an argument, then the argument is determined later. */
824 return TB_COMMAND_CODE (mn
);
827 /* Not a valid command. */
828 return TB_UNUSED_COMMAND
;
831 /* Parse the input string for determining the tree code. */
833 static enum tree_code
834 TB_get_tree_code (char *input
)
836 unsigned int mn
, size_tok
;
840 space
= strchr (input
, ' ');
842 size_tok
= strlen (input
) - strlen (space
);
844 size_tok
= strlen (input
) - 1;
846 for (mn
= 0; mn
< LAST_AND_UNUSED_TREE_CODE
; mn
++)
848 if (size_tok
!= TB_TREE_CODE_LEN (mn
))
851 comp
= memcmp (input
, TB_TREE_CODE_TEXT (mn
), TB_TREE_CODE_LEN (mn
));
854 fprintf (TB_OUT_FILE
, "%s\n", TB_TREE_CODE_TEXT (mn
));
855 return TB_TREE_CODE (mn
);
859 /* This isn't a valid code. */
860 return LAST_AND_UNUSED_TREE_CODE
;
863 /* Find a node with a given code. This function is used as an argument to
867 find_node_with_code (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
870 enum tree_code
*code
;
871 code
= (enum tree_code
*) data
;
872 if (*code
== TREE_CODE (*tp
))
878 /* Returns a pointer to the last visited node. */
881 TB_history_prev (void)
883 if (!vec_safe_is_empty (TB_history_stack
))
885 tree last
= TB_history_stack
->last ();
886 TB_history_stack
->pop ();
892 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
893 (and null-terminate it). *LINEPTR is a pointer returned from malloc
894 (or NULL), pointing to *N characters of space. It is realloc'd as
895 necessary. Returns the number of characters read (not including the
896 null terminator), or -1 on error or EOF.
897 This function comes from sed (and is supposed to be a portable version
901 TB_getline (char **lineptr
, long *n
, FILE *stream
)
906 if (lineptr
== NULL
|| n
== NULL
)
915 /* Make sure we have a line buffer to start with. */
916 if (*lineptr
== NULL
|| *n
< 2) /* !seen and no buf yet need 2 chars. */
919 #define MAX_CANON 256
921 line
= (char *) xrealloc (*lineptr
, MAX_CANON
);
940 register int c
= getc (stream
);
943 else if ((*p
++ = c
) == '\n')
947 /* Need to enlarge the line buffer. */
950 line
= (char *) xrealloc (line
, size
);
963 /* Return a partial line since we got an error in the middle. */
965 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
966 if (p
- 2 >= *lineptr
&& p
[-2] == '\r')