2 Copyright (C) 2002, 2003, 2004, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Sebastian Pop <s.pop@laposte.net>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "tree-inline.h"
28 #include "tree-pretty-print.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")
38 /* Structures for handling Tree Browser's commands. */
39 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
41 #include "tree-browser.def"
45 typedef enum TB_Comm_code TB_CODE
;
49 const char *comm_text
;
54 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
55 static const struct tb_command tb_commands
[] =
57 #include "tree-browser.def"
61 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
62 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
63 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
64 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
67 /* Next structure is for parsing TREE_CODEs. */
70 const char *code_string
;
71 size_t code_string_len
;
74 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
75 #define END_OF_BASE_TREE_CODES \
76 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
77 static const struct tb_tree_code tb_tree_codes
[] =
79 #include "all-tree.def"
82 #undef END_OF_BASE_TREE_CODES
84 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
85 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
86 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
89 /* Function declarations. */
91 static long TB_getline (char **, long *, FILE *);
92 static TB_CODE
TB_get_command (char *);
93 static enum tree_code
TB_get_tree_code (char *);
94 static tree
find_node_with_code (tree
*, int *, void *);
95 static tree
store_child_info (tree
*, int *, void *);
96 static void TB_update_up (tree
);
97 static tree
TB_current_chain_node (tree
);
98 static tree
TB_prev_expr (tree
);
99 static tree
TB_next_expr (tree
);
100 static tree
TB_up_expr (tree
);
101 static tree
TB_first_in_bind (tree
);
102 static tree
TB_last_in_bind (tree
);
103 static int TB_parent_eq (const void *, const void *);
104 static tree
TB_history_prev (void);
106 /* FIXME: To be declared in a .h file. */
107 void browse_tree (tree
);
109 /* Static variables. */
110 static htab_t TB_up_ht
;
111 static VEC(tree
,gc
) *TB_history_stack
;
112 static int TB_verbose
= 1;
115 /* Entry point in the Tree Browser. */
118 browse_tree (tree begin
)
121 TB_CODE tbc
= TB_UNUSED_COMMAND
;
126 fprintf (TB_OUT_FILE
, "\nTree Browser\n");
128 #define TB_SET_HEAD(N) do { \
129 VEC_safe_push (tree, gc, TB_history_stack, N); \
134 print_generic_expr (TB_OUT_FILE, head, 0); \
135 fprintf (TB_OUT_FILE, "\n"); \
141 /* Store in a hashtable information about previous and upper statements. */
143 TB_up_ht
= htab_create (1023, htab_hash_pointer
, &TB_parent_eq
, NULL
);
149 fprintf (TB_OUT_FILE
, "TB> ");
150 rd
= TB_getline (&input
, &input_size
, TB_IN_FILE
);
157 /* Get a new command. Otherwise the user just pressed enter, and thus
158 she expects the last command to be reexecuted. */
159 tbc
= TB_get_command (input
);
168 if (head
&& (INTEGRAL_TYPE_P (head
)
169 || TREE_CODE (head
) == REAL_TYPE
170 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
171 TB_SET_HEAD (TYPE_MAX_VALUE (head
));
177 if (head
&& (INTEGRAL_TYPE_P (head
)
178 || TREE_CODE (head
) == REAL_TYPE
179 || TREE_CODE (head
) == FIXED_POINT_TYPE
))
180 TB_SET_HEAD (TYPE_MIN_VALUE (head
));
186 if (head
&& TREE_CODE (head
) == TREE_VEC
)
188 /* This command takes another argument: the element number:
189 for example "elt 1". */
192 else if (head
&& TREE_CODE (head
) == VECTOR_CST
)
194 /* This command takes another argument: the element number:
195 for example "elt 1". */
203 if (head
&& TREE_CODE (head
) == TREE_LIST
)
204 TB_SET_HEAD (TREE_VALUE (head
));
210 if (head
&& TREE_CODE (head
) == TREE_LIST
)
211 TB_SET_HEAD (TREE_PURPOSE (head
));
217 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
218 TB_SET_HEAD (TREE_IMAGPART (head
));
224 if (head
&& TREE_CODE (head
) == COMPLEX_CST
)
225 TB_SET_HEAD (TREE_REALPART (head
));
231 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
232 TB_SET_HEAD (TREE_OPERAND (head
, 2));
238 if (head
&& TREE_CODE (head
) == BLOCK
)
239 TB_SET_HEAD (BLOCK_SUBBLOCKS (head
));
244 case TB_SUPERCONTEXT
:
245 if (head
&& TREE_CODE (head
) == BLOCK
)
246 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head
));
252 if (head
&& TREE_CODE (head
) == BLOCK
)
253 TB_SET_HEAD (BLOCK_VARS (head
));
254 else if (head
&& TREE_CODE (head
) == BIND_EXPR
)
255 TB_SET_HEAD (TREE_OPERAND (head
, 0));
260 case TB_REFERENCE_TO_THIS
:
261 if (head
&& TYPE_P (head
))
262 TB_SET_HEAD (TYPE_REFERENCE_TO (head
));
267 case TB_POINTER_TO_THIS
:
268 if (head
&& TYPE_P (head
))
269 TB_SET_HEAD (TYPE_POINTER_TO (head
));
275 if (head
&& TREE_CODE (head
) == OFFSET_TYPE
)
276 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head
));
282 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
283 || TREE_CODE (head
) == METHOD_TYPE
))
284 TB_SET_HEAD (TYPE_ARG_TYPES (head
));
289 case TB_METHOD_BASE_TYPE
:
290 if (head
&& (TREE_CODE (head
) == FUNCTION_TYPE
291 || TREE_CODE (head
) == METHOD_TYPE
)
292 && TYPE_METHOD_BASETYPE (head
))
293 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head
));
299 if (head
&& (TREE_CODE (head
) == RECORD_TYPE
300 || TREE_CODE (head
) == UNION_TYPE
301 || TREE_CODE (head
) == QUAL_UNION_TYPE
))
302 TB_SET_HEAD (TYPE_FIELDS (head
));
308 if (head
&& TREE_CODE (head
) == ARRAY_TYPE
)
309 TB_SET_HEAD (TYPE_DOMAIN (head
));
315 if (head
&& TREE_CODE (head
) == ENUMERAL_TYPE
)
316 TB_SET_HEAD (TYPE_VALUES (head
));
322 if (head
&& TREE_CODE (head
) == PARM_DECL
)
323 TB_SET_HEAD (DECL_ARG_TYPE (head
));
329 if (head
&& DECL_P (head
))
330 TB_SET_HEAD (DECL_INITIAL (head
));
336 if (head
&& DECL_P (head
))
337 TB_SET_HEAD (DECL_RESULT_FLD (head
));
343 if (head
&& DECL_P (head
))
344 TB_SET_HEAD (DECL_ARGUMENTS (head
));
349 case TB_ABSTRACT_ORIGIN
:
350 if (head
&& DECL_P (head
))
351 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head
));
352 else if (head
&& TREE_CODE (head
) == BLOCK
)
353 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head
));
359 if (head
&& DECL_P (head
))
360 TB_SET_HEAD (DECL_ATTRIBUTES (head
));
361 else if (head
&& TYPE_P (head
))
362 TB_SET_HEAD (TYPE_ATTRIBUTES (head
));
368 if (head
&& DECL_P (head
))
369 TB_SET_HEAD (DECL_CONTEXT (head
));
370 else if (head
&& TYPE_P (head
)
371 && TYPE_CONTEXT (head
))
372 TB_SET_HEAD (TYPE_CONTEXT (head
));
378 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
379 TB_SET_HEAD (DECL_FIELD_OFFSET (head
));
385 if (head
&& TREE_CODE (head
) == FIELD_DECL
)
386 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head
));
392 if (head
&& DECL_P (head
))
393 TB_SET_HEAD (DECL_SIZE_UNIT (head
));
394 else if (head
&& TYPE_P (head
))
395 TB_SET_HEAD (TYPE_SIZE_UNIT (head
));
401 if (head
&& DECL_P (head
))
402 TB_SET_HEAD (DECL_SIZE (head
));
403 else if (head
&& TYPE_P (head
))
404 TB_SET_HEAD (TYPE_SIZE (head
));
410 if (head
&& TREE_TYPE (head
))
411 TB_SET_HEAD (TREE_TYPE (head
));
416 case TB_DECL_SAVED_TREE
:
417 if (head
&& TREE_CODE (head
) == FUNCTION_DECL
418 && DECL_SAVED_TREE (head
))
419 TB_SET_HEAD (DECL_SAVED_TREE (head
));
425 if (head
&& TREE_CODE (head
) == BIND_EXPR
)
426 TB_SET_HEAD (TREE_OPERAND (head
, 1));
432 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 0))
433 TB_SET_HEAD (TREE_OPERAND (head
, 0));
439 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 1))
440 TB_SET_HEAD (TREE_OPERAND (head
, 1));
446 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 2))
447 TB_SET_HEAD (TREE_OPERAND (head
, 2));
453 if (head
&& EXPR_P (head
) && TREE_OPERAND (head
, 3))
454 TB_SET_HEAD (TREE_OPERAND (head
, 3));
466 case TB_PRETTY_PRINT
:
469 print_generic_stmt (TB_OUT_FILE
, head
, 0);
470 fprintf (TB_OUT_FILE
, "\n");
485 arg_text
= strchr (input
, ' ');
486 if (arg_text
== NULL
)
488 fprintf (TB_OUT_FILE
, "First argument is missing. This isn't a valid search command. \n");
491 code
= TB_get_tree_code (arg_text
+ 1);
493 /* Search in the subtree a node with the given code. */
497 res
= walk_tree (&head
, find_node_with_code
, &code
, NULL
);
498 if (res
== NULL_TREE
)
500 fprintf (TB_OUT_FILE
, "There's no node with this code (reachable via the walk_tree function from this node).\n");
504 fprintf (TB_OUT_FILE
, "Achoo! I got this node in the tree.\n");
511 #define TB_MOVE_HEAD(FCT) do { \
526 TB_MOVE_HEAD (TB_first_in_bind
);
530 TB_MOVE_HEAD (TB_last_in_bind
);
534 TB_MOVE_HEAD (TB_up_expr
);
538 TB_MOVE_HEAD (TB_prev_expr
);
542 TB_MOVE_HEAD (TB_next_expr
);
546 /* This command is a little bit special, since it deals with history
547 stack. For this reason it should keep the "head = ..." statement
548 and not use TB_MOVE_HEAD. */
552 t
= TB_history_prev ();
558 print_generic_expr (TB_OUT_FILE
, head
, 0);
559 fprintf (TB_OUT_FILE
, "\n");
570 /* Don't go further if it's the last node in this chain. */
571 if (head
&& TREE_CODE (head
) == BLOCK
)
572 TB_SET_HEAD (BLOCK_CHAIN (head
));
573 else if (head
&& TREE_CHAIN (head
))
574 TB_SET_HEAD (TREE_CHAIN (head
));
580 /* Go up to the current function declaration. */
581 TB_SET_HEAD (current_function_decl
);
582 fprintf (TB_OUT_FILE
, "Current function declaration.\n");
586 /* Display a help message. */
589 fprintf (TB_OUT_FILE
, "Possible commands are:\n\n");
590 for (i
= 0; i
< TB_UNUSED_COMMAND
; i
++)
592 fprintf (TB_OUT_FILE
, "%20s - %s\n", TB_COMMAND_TEXT (i
), TB_COMMAND_HELP (i
));
601 fprintf (TB_OUT_FILE
, "Verbose on.\n");
606 fprintf (TB_OUT_FILE
, "Verbose off.\n");
612 /* Just exit from this function. */
621 htab_delete (TB_up_ht
);
626 /* Search the first node in this BIND_EXPR. */
629 TB_first_in_bind (tree node
)
633 if (node
== NULL_TREE
)
636 while ((t
= TB_prev_expr (node
)))
642 /* Search the last node in this BIND_EXPR. */
645 TB_last_in_bind (tree node
)
649 if (node
== NULL_TREE
)
652 while ((t
= TB_next_expr (node
)))
658 /* Search the parent expression for this node. */
661 TB_up_expr (tree node
)
664 if (node
== NULL_TREE
)
667 res
= (tree
) htab_find (TB_up_ht
, node
);
671 /* Search the previous expression in this BIND_EXPR. */
674 TB_prev_expr (tree node
)
676 node
= TB_current_chain_node (node
);
678 if (node
== NULL_TREE
)
681 node
= TB_up_expr (node
);
682 if (node
&& TREE_CODE (node
) == COMPOUND_EXPR
)
688 /* Search the next expression in this BIND_EXPR. */
691 TB_next_expr (tree node
)
693 node
= TB_current_chain_node (node
);
695 if (node
== NULL_TREE
)
698 node
= TREE_OPERAND (node
, 1);
703 TB_current_chain_node (tree node
)
705 if (node
== NULL_TREE
)
708 if (TREE_CODE (node
) == COMPOUND_EXPR
)
711 node
= TB_up_expr (node
);
714 if (TREE_CODE (node
) == COMPOUND_EXPR
)
717 node
= TB_up_expr (node
);
718 if (TREE_CODE (node
) == COMPOUND_EXPR
)
725 /* For each node store in its children nodes that the current node is their
726 parent. This function is used by walk_tree. */
729 store_child_info (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
730 void *data ATTRIBUTE_UNUSED
)
737 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
740 int n
= TREE_OPERAND_LENGTH (node
);
742 for (i
= 0; i
< n
; i
++)
744 tree op
= TREE_OPERAND (node
, i
);
745 slot
= htab_find_slot (TB_up_ht
, op
, INSERT
);
746 *slot
= (void *) node
;
750 /* Never stop walk_tree. */
754 /* Function used in TB_up_ht. */
757 TB_parent_eq (const void *p1
, const void *p2
)
759 const_tree
const node
= (const_tree
)p2
;
760 const_tree
const parent
= (const_tree
) p1
;
762 if (p1
== NULL
|| p2
== NULL
)
767 int n
= TREE_OPERAND_LENGTH (parent
);
769 for (i
= 0; i
< n
; i
++)
770 if (node
== TREE_OPERAND (parent
, i
))
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_empty (tree
, TB_history_stack
))
881 tree last
= VEC_last (tree
, TB_history_stack
);
882 VEC_pop (tree
, TB_history_stack
);
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')