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 "diagnostic.h"
29 #include "tree-pretty-print.h"
33 #define TB_OUT_FILE stdout
34 #define TB_IN_FILE stdin
35 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
36 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
39 /* Structures for handling Tree Browser's commands. */
40 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
42 #include "tree-browser.def"
46 typedef enum TB_Comm_code TB_CODE;
50 const char *comm_text;
55 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
56 static const struct tb_command tb_commands[] =
58 #include "tree-browser.def"
62 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
63 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
64 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
65 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
68 /* Next structure is for parsing TREE_CODEs. */
71 const char *code_string;
72 size_t code_string_len;
75 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
76 #define END_OF_BASE_TREE_CODES \
77 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
78 static const struct tb_tree_code tb_tree_codes[] =
80 #include "all-tree.def"
83 #undef END_OF_BASE_TREE_CODES
85 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
86 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
87 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
90 /* Function declarations. */
92 static long TB_getline (char **, long *, FILE *);
93 static TB_CODE TB_get_command (char *);
94 static enum tree_code TB_get_tree_code (char *);
95 static tree find_node_with_code (tree *, int *, void *);
96 static tree store_child_info (tree *, int *, void *);
97 static void TB_update_up (tree);
98 static tree TB_current_chain_node (tree);
99 static tree TB_prev_expr (tree);
100 static tree TB_next_expr (tree);
101 static tree TB_up_expr (tree);
102 static tree TB_first_in_bind (tree);
103 static tree TB_last_in_bind (tree);
104 static int TB_parent_eq (const void *, const void *);
105 static tree TB_history_prev (void);
107 /* FIXME: To be declared in a .h file. */
108 void browse_tree (tree);
110 /* Static variables. */
111 static htab_t TB_up_ht;
112 static tree TB_history_stack = NULL_TREE;
113 static int TB_verbose = 1;
116 /* Entry point in the Tree Browser. */
119 browse_tree (tree begin)
122 TB_CODE tbc = TB_UNUSED_COMMAND;
127 fprintf (TB_OUT_FILE, "\nTree Browser\n");
129 #define TB_SET_HEAD(N) do { \
130 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
135 print_generic_expr (TB_OUT_FILE, head, 0); \
136 fprintf (TB_OUT_FILE, "\n"); \
142 /* Store in a hashtable information about previous and upper statements. */
144 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
150 fprintf (TB_OUT_FILE, "TB> ");
151 rd = TB_getline (&input, &input_size, TB_IN_FILE);
158 /* Get a new command. Otherwise the user just pressed enter, and thus
159 she expects the last command to be reexecuted. */
160 tbc = TB_get_command (input);
169 if (head && (INTEGRAL_TYPE_P (head)
170 || TREE_CODE (head) == REAL_TYPE
171 || TREE_CODE (head) == FIXED_POINT_TYPE))
172 TB_SET_HEAD (TYPE_MAX_VALUE (head));
178 if (head && (INTEGRAL_TYPE_P (head)
179 || TREE_CODE (head) == REAL_TYPE
180 || TREE_CODE (head) == FIXED_POINT_TYPE))
181 TB_SET_HEAD (TYPE_MIN_VALUE (head));
187 if (head && TREE_CODE (head) == TREE_VEC)
189 /* This command takes another argument: the element number:
190 for example "elt 1". */
193 else if (head && TREE_CODE (head) == VECTOR_CST)
195 /* This command takes another argument: the element number:
196 for example "elt 1". */
204 if (head && TREE_CODE (head) == TREE_LIST)
205 TB_SET_HEAD (TREE_VALUE (head));
211 if (head && TREE_CODE (head) == TREE_LIST)
212 TB_SET_HEAD (TREE_PURPOSE (head));
218 if (head && TREE_CODE (head) == COMPLEX_CST)
219 TB_SET_HEAD (TREE_IMAGPART (head));
225 if (head && TREE_CODE (head) == COMPLEX_CST)
226 TB_SET_HEAD (TREE_REALPART (head));
232 if (head && TREE_CODE (head) == BIND_EXPR)
233 TB_SET_HEAD (TREE_OPERAND (head, 2));
239 if (head && TREE_CODE (head) == BLOCK)
240 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
245 case TB_SUPERCONTEXT:
246 if (head && TREE_CODE (head) == BLOCK)
247 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
253 if (head && TREE_CODE (head) == BLOCK)
254 TB_SET_HEAD (BLOCK_VARS (head));
255 else if (head && TREE_CODE (head) == BIND_EXPR)
256 TB_SET_HEAD (TREE_OPERAND (head, 0));
261 case TB_REFERENCE_TO_THIS:
262 if (head && TYPE_P (head))
263 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
268 case TB_POINTER_TO_THIS:
269 if (head && TYPE_P (head))
270 TB_SET_HEAD (TYPE_POINTER_TO (head));
276 if (head && TREE_CODE (head) == OFFSET_TYPE)
277 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
283 if (head && (TREE_CODE (head) == FUNCTION_TYPE
284 || TREE_CODE (head) == METHOD_TYPE))
285 TB_SET_HEAD (TYPE_ARG_TYPES (head));
290 case TB_METHOD_BASE_TYPE:
291 if (head && (TREE_CODE (head) == FUNCTION_TYPE
292 || TREE_CODE (head) == METHOD_TYPE)
293 && TYPE_METHOD_BASETYPE (head))
294 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
300 if (head && (TREE_CODE (head) == RECORD_TYPE
301 || TREE_CODE (head) == UNION_TYPE
302 || TREE_CODE (head) == QUAL_UNION_TYPE))
303 TB_SET_HEAD (TYPE_FIELDS (head));
309 if (head && TREE_CODE (head) == ARRAY_TYPE)
310 TB_SET_HEAD (TYPE_DOMAIN (head));
316 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
317 TB_SET_HEAD (TYPE_VALUES (head));
323 if (head && TREE_CODE (head) == PARM_DECL)
324 TB_SET_HEAD (DECL_ARG_TYPE (head));
330 if (head && DECL_P (head))
331 TB_SET_HEAD (DECL_INITIAL (head));
337 if (head && DECL_P (head))
338 TB_SET_HEAD (DECL_RESULT_FLD (head));
344 if (head && DECL_P (head))
345 TB_SET_HEAD (DECL_ARGUMENTS (head));
350 case TB_ABSTRACT_ORIGIN:
351 if (head && DECL_P (head))
352 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
353 else if (head && TREE_CODE (head) == BLOCK)
354 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
360 if (head && DECL_P (head))
361 TB_SET_HEAD (DECL_ATTRIBUTES (head));
362 else if (head && TYPE_P (head))
363 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
369 if (head && DECL_P (head))
370 TB_SET_HEAD (DECL_CONTEXT (head));
371 else if (head && TYPE_P (head)
372 && TYPE_CONTEXT (head))
373 TB_SET_HEAD (TYPE_CONTEXT (head));
379 if (head && TREE_CODE (head) == FIELD_DECL)
380 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
386 if (head && TREE_CODE (head) == FIELD_DECL)
387 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
393 if (head && DECL_P (head))
394 TB_SET_HEAD (DECL_SIZE_UNIT (head));
395 else if (head && TYPE_P (head))
396 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
402 if (head && DECL_P (head))
403 TB_SET_HEAD (DECL_SIZE (head));
404 else if (head && TYPE_P (head))
405 TB_SET_HEAD (TYPE_SIZE (head));
411 if (head && TREE_TYPE (head))
412 TB_SET_HEAD (TREE_TYPE (head));
417 case TB_DECL_SAVED_TREE:
418 if (head && TREE_CODE (head) == FUNCTION_DECL
419 && DECL_SAVED_TREE (head))
420 TB_SET_HEAD (DECL_SAVED_TREE (head));
426 if (head && TREE_CODE (head) == BIND_EXPR)
427 TB_SET_HEAD (TREE_OPERAND (head, 1));
433 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
434 TB_SET_HEAD (TREE_OPERAND (head, 0));
440 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
441 TB_SET_HEAD (TREE_OPERAND (head, 1));
447 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
448 TB_SET_HEAD (TREE_OPERAND (head, 2));
454 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
455 TB_SET_HEAD (TREE_OPERAND (head, 3));
467 case TB_PRETTY_PRINT:
470 print_generic_stmt (TB_OUT_FILE, head, 0);
471 fprintf (TB_OUT_FILE, "\n");
486 arg_text = strchr (input, ' ');
487 if (arg_text == NULL)
489 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
492 code = TB_get_tree_code (arg_text + 1);
494 /* Search in the subtree a node with the given code. */
498 res = walk_tree (&head, find_node_with_code, &code, NULL);
499 if (res == NULL_TREE)
501 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
505 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
512 #define TB_MOVE_HEAD(FCT) do { \
527 TB_MOVE_HEAD (TB_first_in_bind);
531 TB_MOVE_HEAD (TB_last_in_bind);
535 TB_MOVE_HEAD (TB_up_expr);
539 TB_MOVE_HEAD (TB_prev_expr);
543 TB_MOVE_HEAD (TB_next_expr);
547 /* This command is a little bit special, since it deals with history
548 stack. For this reason it should keep the "head = ..." statement
549 and not use TB_MOVE_HEAD. */
553 t = TB_history_prev ();
559 print_generic_expr (TB_OUT_FILE, head, 0);
560 fprintf (TB_OUT_FILE, "\n");
571 /* Don't go further if it's the last node in this chain. */
572 if (head && TREE_CODE (head) == BLOCK)
573 TB_SET_HEAD (BLOCK_CHAIN (head));
574 else if (head && TREE_CHAIN (head))
575 TB_SET_HEAD (TREE_CHAIN (head));
581 /* Go up to the current function declaration. */
582 TB_SET_HEAD (current_function_decl);
583 fprintf (TB_OUT_FILE, "Current function declaration.\n");
587 /* Display a help message. */
590 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
591 for (i = 0; i < TB_UNUSED_COMMAND; i++)
593 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
602 fprintf (TB_OUT_FILE, "Verbose on.\n");
607 fprintf (TB_OUT_FILE, "Verbose off.\n");
613 /* Just exit from this function. */
622 htab_delete (TB_up_ht);
627 /* Search the first node in this BIND_EXPR. */
630 TB_first_in_bind (tree node)
634 if (node == NULL_TREE)
637 while ((t = TB_prev_expr (node)))
643 /* Search the last node in this BIND_EXPR. */
646 TB_last_in_bind (tree node)
650 if (node == NULL_TREE)
653 while ((t = TB_next_expr (node)))
659 /* Search the parent expression for this node. */
662 TB_up_expr (tree node)
665 if (node == NULL_TREE)
668 res = (tree) htab_find (TB_up_ht, node);
672 /* Search the previous expression in this BIND_EXPR. */
675 TB_prev_expr (tree node)
677 node = TB_current_chain_node (node);
679 if (node == NULL_TREE)
682 node = TB_up_expr (node);
683 if (node && TREE_CODE (node) == COMPOUND_EXPR)
689 /* Search the next expression in this BIND_EXPR. */
692 TB_next_expr (tree node)
694 node = TB_current_chain_node (node);
696 if (node == NULL_TREE)
699 node = TREE_OPERAND (node, 1);
704 TB_current_chain_node (tree node)
706 if (node == NULL_TREE)
709 if (TREE_CODE (node) == COMPOUND_EXPR)
712 node = TB_up_expr (node);
715 if (TREE_CODE (node) == COMPOUND_EXPR)
718 node = TB_up_expr (node);
719 if (TREE_CODE (node) == COMPOUND_EXPR)
726 /* For each node store in its children nodes that the current node is their
727 parent. This function is used by walk_tree. */
730 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
731 void *data ATTRIBUTE_UNUSED)
738 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
741 int n = TREE_OPERAND_LENGTH (node);
743 for (i = 0; i < n; i++)
745 tree op = TREE_OPERAND (node, i);
746 slot = htab_find_slot (TB_up_ht, op, INSERT);
747 *slot = (void *) node;
751 /* Never stop walk_tree. */
755 /* Function used in TB_up_ht. */
758 TB_parent_eq (const void *p1, const void *p2)
760 const_tree const node = (const_tree)p2;
761 const_tree const parent = (const_tree) p1;
763 if (p1 == NULL || p2 == NULL)
768 int n = TREE_OPERAND_LENGTH (parent);
770 for (i = 0; i < n; i++)
771 if (node == TREE_OPERAND (parent, i))
777 /* Update information about upper expressions in the hash table. */
780 TB_update_up (tree node)
784 walk_tree (&node, store_child_info, NULL, NULL);
786 /* Walk function's body. */
787 if (TREE_CODE (node) == FUNCTION_DECL)
788 if (DECL_SAVED_TREE (node))
789 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
791 /* Walk rest of the chain. */
792 node = TREE_CHAIN (node);
794 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
797 /* Parse the input string for determining the command the user asked for. */
800 TB_get_command (char *input)
802 unsigned int mn, size_tok;
806 space = strchr (input, ' ');
808 size_tok = strlen (input) - strlen (space);
810 size_tok = strlen (input) - 1;
812 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
814 if (size_tok != TB_COMMAND_LEN (mn))
817 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
819 /* Here we just determined the command. If this command takes
820 an argument, then the argument is determined later. */
821 return TB_COMMAND_CODE (mn);
824 /* Not a valid command. */
825 return TB_UNUSED_COMMAND;
828 /* Parse the input string for determining the tree code. */
830 static enum tree_code
831 TB_get_tree_code (char *input)
833 unsigned int mn, size_tok;
837 space = strchr (input, ' ');
839 size_tok = strlen (input) - strlen (space);
841 size_tok = strlen (input) - 1;
843 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
845 if (size_tok != TB_TREE_CODE_LEN (mn))
848 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
851 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
852 return TB_TREE_CODE (mn);
856 /* This isn't a valid code. */
857 return LAST_AND_UNUSED_TREE_CODE;
860 /* Find a node with a given code. This function is used as an argument to
864 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
867 enum tree_code *code;
868 code = (enum tree_code *) data;
869 if (*code == TREE_CODE (*tp))
875 /* Returns a pointer to the last visited node. */
878 TB_history_prev (void)
880 if (TB_history_stack)
882 TB_history_stack = TREE_CHAIN (TB_history_stack);
883 if (TB_history_stack)
884 return TREE_VALUE (TB_history_stack);
889 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
890 (and null-terminate it). *LINEPTR is a pointer returned from malloc
891 (or NULL), pointing to *N characters of space. It is realloc'd as
892 necessary. Returns the number of characters read (not including the
893 null terminator), or -1 on error or EOF.
894 This function comes from sed (and is supposed to be a portable version
898 TB_getline (char **lineptr, long *n, FILE *stream)
903 if (lineptr == NULL || n == NULL)
912 /* Make sure we have a line buffer to start with. */
913 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
916 #define MAX_CANON 256
918 line = (char *) xrealloc (*lineptr, MAX_CANON);
937 register int c = getc (stream);
940 else if ((*p++ = c) == '\n')
944 /* Need to enlarge the line buffer. */
947 line = (char *) xrealloc (line, size);
960 /* Return a partial line since we got an error in the middle. */
962 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
963 if (p - 2 >= *lineptr && p[-2] == '\r')