2 Copyright (C) 2002, 2003, 2004, 2007, 2008 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"
26 #include "tree-inline.h"
27 #include "diagnostic.h"
31 #define TB_OUT_FILE stdout
32 #define TB_IN_FILE stdin
33 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
34 #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 int TB_parent_eq (const void *, const void *);
103 static tree TB_history_prev (void);
105 /* FIXME: To be declared in a .h file. */
106 void browse_tree (tree);
108 /* Static variables. */
109 static htab_t TB_up_ht;
110 static tree TB_history_stack = NULL_TREE;
111 static int TB_verbose = 1;
114 /* Entry point in the Tree Browser. */
117 browse_tree (tree begin)
120 TB_CODE tbc = TB_UNUSED_COMMAND;
125 fprintf (TB_OUT_FILE, "\nTree Browser\n");
127 #define TB_SET_HEAD(N) do { \
128 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
133 print_generic_expr (TB_OUT_FILE, head, 0); \
134 fprintf (TB_OUT_FILE, "\n"); \
140 /* Store in a hashtable information about previous and upper statements. */
142 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
148 fprintf (TB_OUT_FILE, "TB> ");
149 rd = TB_getline (&input, &input_size, TB_IN_FILE);
156 /* Get a new command. Otherwise the user just pressed enter, and thus
157 she expects the last command to be reexecuted. */
158 tbc = TB_get_command (input);
167 if (head && (INTEGRAL_TYPE_P (head)
168 || TREE_CODE (head) == REAL_TYPE
169 || TREE_CODE (head) == FIXED_POINT_TYPE))
170 TB_SET_HEAD (TYPE_MAX_VALUE (head));
176 if (head && (INTEGRAL_TYPE_P (head)
177 || TREE_CODE (head) == REAL_TYPE
178 || TREE_CODE (head) == FIXED_POINT_TYPE))
179 TB_SET_HEAD (TYPE_MIN_VALUE (head));
185 if (head && TREE_CODE (head) == TREE_VEC)
187 /* This command takes another argument: the element number:
188 for example "elt 1". */
191 else if (head && TREE_CODE (head) == VECTOR_CST)
193 /* This command takes another argument: the element number:
194 for example "elt 1". */
202 if (head && TREE_CODE (head) == TREE_LIST)
203 TB_SET_HEAD (TREE_VALUE (head));
209 if (head && TREE_CODE (head) == TREE_LIST)
210 TB_SET_HEAD (TREE_PURPOSE (head));
216 if (head && TREE_CODE (head) == COMPLEX_CST)
217 TB_SET_HEAD (TREE_IMAGPART (head));
223 if (head && TREE_CODE (head) == COMPLEX_CST)
224 TB_SET_HEAD (TREE_REALPART (head));
230 if (head && TREE_CODE (head) == BIND_EXPR)
231 TB_SET_HEAD (TREE_OPERAND (head, 2));
237 if (head && TREE_CODE (head) == BLOCK)
238 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
243 case TB_SUPERCONTEXT:
244 if (head && TREE_CODE (head) == BLOCK)
245 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
251 if (head && TREE_CODE (head) == BLOCK)
252 TB_SET_HEAD (BLOCK_VARS (head));
253 else if (head && TREE_CODE (head) == BIND_EXPR)
254 TB_SET_HEAD (TREE_OPERAND (head, 0));
259 case TB_REFERENCE_TO_THIS:
260 if (head && TYPE_P (head))
261 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
266 case TB_POINTER_TO_THIS:
267 if (head && TYPE_P (head))
268 TB_SET_HEAD (TYPE_POINTER_TO (head));
274 if (head && TREE_CODE (head) == OFFSET_TYPE)
275 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
281 if (head && (TREE_CODE (head) == FUNCTION_TYPE
282 || TREE_CODE (head) == METHOD_TYPE))
283 TB_SET_HEAD (TYPE_ARG_TYPES (head));
288 case TB_METHOD_BASE_TYPE:
289 if (head && (TREE_CODE (head) == FUNCTION_TYPE
290 || TREE_CODE (head) == METHOD_TYPE)
291 && TYPE_METHOD_BASETYPE (head))
292 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
298 if (head && (TREE_CODE (head) == RECORD_TYPE
299 || TREE_CODE (head) == UNION_TYPE
300 || TREE_CODE (head) == QUAL_UNION_TYPE))
301 TB_SET_HEAD (TYPE_FIELDS (head));
307 if (head && TREE_CODE (head) == ARRAY_TYPE)
308 TB_SET_HEAD (TYPE_DOMAIN (head));
314 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
315 TB_SET_HEAD (TYPE_VALUES (head));
321 if (head && TREE_CODE (head) == PARM_DECL)
322 TB_SET_HEAD (DECL_ARG_TYPE (head));
328 if (head && DECL_P (head))
329 TB_SET_HEAD (DECL_INITIAL (head));
335 if (head && DECL_P (head))
336 TB_SET_HEAD (DECL_RESULT_FLD (head));
342 if (head && DECL_P (head))
343 TB_SET_HEAD (DECL_ARGUMENTS (head));
348 case TB_ABSTRACT_ORIGIN:
349 if (head && DECL_P (head))
350 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
351 else if (head && TREE_CODE (head) == BLOCK)
352 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
358 if (head && DECL_P (head))
359 TB_SET_HEAD (DECL_ATTRIBUTES (head));
360 else if (head && TYPE_P (head))
361 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
367 if (head && DECL_P (head))
368 TB_SET_HEAD (DECL_CONTEXT (head));
369 else if (head && TYPE_P (head)
370 && TYPE_CONTEXT (head))
371 TB_SET_HEAD (TYPE_CONTEXT (head));
377 if (head && TREE_CODE (head) == FIELD_DECL)
378 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
384 if (head && TREE_CODE (head) == FIELD_DECL)
385 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
391 if (head && DECL_P (head))
392 TB_SET_HEAD (DECL_SIZE_UNIT (head));
393 else if (head && TYPE_P (head))
394 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
400 if (head && DECL_P (head))
401 TB_SET_HEAD (DECL_SIZE (head));
402 else if (head && TYPE_P (head))
403 TB_SET_HEAD (TYPE_SIZE (head));
409 if (head && TREE_TYPE (head))
410 TB_SET_HEAD (TREE_TYPE (head));
415 case TB_DECL_SAVED_TREE:
416 if (head && TREE_CODE (head) == FUNCTION_DECL
417 && DECL_SAVED_TREE (head))
418 TB_SET_HEAD (DECL_SAVED_TREE (head));
424 if (head && TREE_CODE (head) == BIND_EXPR)
425 TB_SET_HEAD (TREE_OPERAND (head, 1));
431 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
432 TB_SET_HEAD (TREE_OPERAND (head, 0));
438 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
439 TB_SET_HEAD (TREE_OPERAND (head, 1));
445 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
446 TB_SET_HEAD (TREE_OPERAND (head, 2));
452 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
453 TB_SET_HEAD (TREE_OPERAND (head, 3));
465 case TB_PRETTY_PRINT:
468 print_generic_stmt (TB_OUT_FILE, head, 0);
469 fprintf (TB_OUT_FILE, "\n");
484 arg_text = strchr (input, ' ');
485 if (arg_text == NULL)
487 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
490 code = TB_get_tree_code (arg_text + 1);
492 /* Search in the subtree a node with the given code. */
496 res = walk_tree (&head, find_node_with_code, &code, NULL);
497 if (res == NULL_TREE)
499 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
503 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
510 #define TB_MOVE_HEAD(FCT) do { \
525 TB_MOVE_HEAD (TB_first_in_bind);
529 TB_MOVE_HEAD (TB_last_in_bind);
533 TB_MOVE_HEAD (TB_up_expr);
537 TB_MOVE_HEAD (TB_prev_expr);
541 TB_MOVE_HEAD (TB_next_expr);
545 /* This command is a little bit special, since it deals with history
546 stack. For this reason it should keep the "head = ..." statement
547 and not use TB_MOVE_HEAD. */
551 t = TB_history_prev ();
557 print_generic_expr (TB_OUT_FILE, head, 0);
558 fprintf (TB_OUT_FILE, "\n");
569 /* Don't go further if it's the last node in this chain. */
570 if (head && TREE_CODE (head) == BLOCK)
571 TB_SET_HEAD (BLOCK_CHAIN (head));
572 else if (head && TREE_CHAIN (head))
573 TB_SET_HEAD (TREE_CHAIN (head));
579 /* Go up to the current function declaration. */
580 TB_SET_HEAD (current_function_decl);
581 fprintf (TB_OUT_FILE, "Current function declaration.\n");
585 /* Display a help message. */
588 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
589 for (i = 0; i < TB_UNUSED_COMMAND; i++)
591 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
600 fprintf (TB_OUT_FILE, "Verbose on.\n");
605 fprintf (TB_OUT_FILE, "Verbose off.\n");
611 /* Just exit from this function. */
620 htab_delete (TB_up_ht);
625 /* Search the first node in this BIND_EXPR. */
628 TB_first_in_bind (tree node)
632 if (node == NULL_TREE)
635 while ((t = TB_prev_expr (node)))
641 /* Search the last node in this BIND_EXPR. */
644 TB_last_in_bind (tree node)
648 if (node == NULL_TREE)
651 while ((t = TB_next_expr (node)))
657 /* Search the parent expression for this node. */
660 TB_up_expr (tree node)
663 if (node == NULL_TREE)
666 res = (tree) htab_find (TB_up_ht, node);
670 /* Search the previous expression in this BIND_EXPR. */
673 TB_prev_expr (tree node)
675 node = TB_current_chain_node (node);
677 if (node == NULL_TREE)
680 node = TB_up_expr (node);
681 if (node && TREE_CODE (node) == COMPOUND_EXPR)
687 /* Search the next expression in this BIND_EXPR. */
690 TB_next_expr (tree node)
692 node = TB_current_chain_node (node);
694 if (node == NULL_TREE)
697 node = TREE_OPERAND (node, 1);
702 TB_current_chain_node (tree node)
704 if (node == NULL_TREE)
707 if (TREE_CODE (node) == COMPOUND_EXPR)
710 node = TB_up_expr (node);
713 if (TREE_CODE (node) == COMPOUND_EXPR)
716 node = TB_up_expr (node);
717 if (TREE_CODE (node) == COMPOUND_EXPR)
724 /* For each node store in its children nodes that the current node is their
725 parent. This function is used by walk_tree. */
728 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
729 void *data ATTRIBUTE_UNUSED)
736 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
739 int n = TREE_OPERAND_LENGTH (node);
741 for (i = 0; i < n; i++)
743 tree op = TREE_OPERAND (node, i);
744 slot = htab_find_slot (TB_up_ht, op, INSERT);
745 *slot = (void *) node;
749 /* Never stop walk_tree. */
753 /* Function used in TB_up_ht. */
756 TB_parent_eq (const void *p1, const void *p2)
758 const_tree const node = (const_tree)p2;
759 const_tree const parent = (const_tree) p1;
761 if (p1 == NULL || p2 == NULL)
766 int n = TREE_OPERAND_LENGTH (parent);
768 for (i = 0; i < n; i++)
769 if (node == TREE_OPERAND (parent, i))
775 /* Update information about upper expressions in the hash table. */
778 TB_update_up (tree node)
782 walk_tree (&node, store_child_info, NULL, NULL);
784 /* Walk function's body. */
785 if (TREE_CODE (node) == FUNCTION_DECL)
786 if (DECL_SAVED_TREE (node))
787 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
789 /* Walk rest of the chain. */
790 node = TREE_CHAIN (node);
792 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
795 /* Parse the input string for determining the command the user asked for. */
798 TB_get_command (char *input)
800 unsigned int mn, size_tok;
804 space = strchr (input, ' ');
806 size_tok = strlen (input) - strlen (space);
808 size_tok = strlen (input) - 1;
810 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
812 if (size_tok != TB_COMMAND_LEN (mn))
815 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
817 /* Here we just determined the command. If this command takes
818 an argument, then the argument is determined later. */
819 return TB_COMMAND_CODE (mn);
822 /* Not a valid command. */
823 return TB_UNUSED_COMMAND;
826 /* Parse the input string for determining the tree code. */
828 static enum tree_code
829 TB_get_tree_code (char *input)
831 unsigned int mn, size_tok;
835 space = strchr (input, ' ');
837 size_tok = strlen (input) - strlen (space);
839 size_tok = strlen (input) - 1;
841 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
843 if (size_tok != TB_TREE_CODE_LEN (mn))
846 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
849 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
850 return TB_TREE_CODE (mn);
854 /* This isn't a valid code. */
855 return LAST_AND_UNUSED_TREE_CODE;
858 /* Find a node with a given code. This function is used as an argument to
862 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
865 enum tree_code *code;
866 code = (enum tree_code *) data;
867 if (*code == TREE_CODE (*tp))
873 /* Returns a pointer to the last visited node. */
876 TB_history_prev (void)
878 if (TB_history_stack)
880 TB_history_stack = TREE_CHAIN (TB_history_stack);
881 if (TB_history_stack)
882 return TREE_VALUE (TB_history_stack);
887 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
888 (and null-terminate it). *LINEPTR is a pointer returned from malloc
889 (or NULL), pointing to *N characters of space. It is realloc'd as
890 necessary. Returns the number of characters read (not including the
891 null terminator), or -1 on error or EOF.
892 This function comes from sed (and is supposed to be a portable version
896 TB_getline (char **lineptr, long *n, FILE *stream)
901 if (lineptr == NULL || n == NULL)
910 /* Make sure we have a line buffer to start with. */
911 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
914 #define MAX_CANON 256
916 line = (char *) xrealloc (*lineptr, MAX_CANON);
935 register int c = getc (stream);
938 else if ((*p++ = c) == '\n')
942 /* Need to enlarge the line buffer. */
945 line = (char *) xrealloc (line, size);
958 /* Return a partial line since we got an error in the middle. */
960 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
961 if (p - 2 >= *lineptr && p[-2] == '\r')