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"
25 #include "tree-pretty-print.h"
27 #define TB_OUT_FILE stdout
28 #define TB_IN_FILE stdin
29 #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n")
30 #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n")
32 /* Structures for handling Tree Browser's commands. */
33 #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND,
35 #include "tree-browser.def"
39 typedef enum TB_Comm_code TB_CODE;
43 const char *comm_text;
48 #define DEFTBCODE(code, str, help) { help, str, sizeof(str) - 1, code },
49 static const struct tb_command tb_commands[] =
51 #include "tree-browser.def"
55 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
56 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
57 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
58 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
61 /* Next structure is for parsing TREE_CODEs. */
64 const char *code_string;
65 size_t code_string_len;
68 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
69 #define END_OF_BASE_TREE_CODES \
70 { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 },
71 static const struct tb_tree_code tb_tree_codes[] =
73 #include "all-tree.def"
76 #undef END_OF_BASE_TREE_CODES
78 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
79 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
80 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
83 /* Function declarations. */
85 static long TB_getline (char **, long *, FILE *);
86 static TB_CODE TB_get_command (char *);
87 static enum tree_code TB_get_tree_code (char *);
88 static tree find_node_with_code (tree *, int *, void *);
89 static tree store_child_info (tree *, int *, void *);
90 static void TB_update_up (tree);
91 static tree TB_current_chain_node (tree);
92 static tree TB_prev_expr (tree);
93 static tree TB_next_expr (tree);
94 static tree TB_up_expr (tree);
95 static tree TB_first_in_bind (tree);
96 static tree TB_last_in_bind (tree);
97 static int TB_parent_eq (const void *, const void *);
98 static tree TB_history_prev (void);
100 /* FIXME: To be declared in a .h file. */
101 void browse_tree (tree);
103 /* Static variables. */
104 static htab_t TB_up_ht;
105 static vec<tree, va_gc> *TB_history_stack;
106 static int TB_verbose = 1;
109 /* Entry point in the Tree Browser. */
112 browse_tree (tree begin)
115 TB_CODE tbc = TB_UNUSED_COMMAND;
120 fprintf (TB_OUT_FILE, "\nTree Browser\n");
122 #define TB_SET_HEAD(N) do { \
123 vec_safe_push (TB_history_stack, N); \
128 print_generic_expr (TB_OUT_FILE, head, 0); \
129 fprintf (TB_OUT_FILE, "\n"); \
135 /* Store in a hashtable information about previous and upper statements. */
137 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
143 fprintf (TB_OUT_FILE, "TB> ");
144 rd = TB_getline (&input, &input_size, TB_IN_FILE);
151 /* Get a new command. Otherwise the user just pressed enter, and thus
152 she expects the last command to be reexecuted. */
153 tbc = TB_get_command (input);
162 if (head && (INTEGRAL_TYPE_P (head)
163 || TREE_CODE (head) == REAL_TYPE
164 || TREE_CODE (head) == FIXED_POINT_TYPE))
165 TB_SET_HEAD (TYPE_MAX_VALUE (head));
171 if (head && (INTEGRAL_TYPE_P (head)
172 || TREE_CODE (head) == REAL_TYPE
173 || TREE_CODE (head) == FIXED_POINT_TYPE))
174 TB_SET_HEAD (TYPE_MIN_VALUE (head));
180 if (head && TREE_CODE (head) == TREE_VEC)
182 /* This command takes another argument: the element number:
183 for example "elt 1". */
186 else if (head && TREE_CODE (head) == VECTOR_CST)
188 /* This command takes another argument: the element number:
189 for example "elt 1". */
197 if (head && TREE_CODE (head) == TREE_LIST)
198 TB_SET_HEAD (TREE_VALUE (head));
204 if (head && TREE_CODE (head) == TREE_LIST)
205 TB_SET_HEAD (TREE_PURPOSE (head));
211 if (head && TREE_CODE (head) == COMPLEX_CST)
212 TB_SET_HEAD (TREE_IMAGPART (head));
218 if (head && TREE_CODE (head) == COMPLEX_CST)
219 TB_SET_HEAD (TREE_REALPART (head));
225 if (head && TREE_CODE (head) == BIND_EXPR)
226 TB_SET_HEAD (TREE_OPERAND (head, 2));
232 if (head && TREE_CODE (head) == BLOCK)
233 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
238 case TB_SUPERCONTEXT:
239 if (head && TREE_CODE (head) == BLOCK)
240 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
246 if (head && TREE_CODE (head) == BLOCK)
247 TB_SET_HEAD (BLOCK_VARS (head));
248 else if (head && TREE_CODE (head) == BIND_EXPR)
249 TB_SET_HEAD (TREE_OPERAND (head, 0));
254 case TB_REFERENCE_TO_THIS:
255 if (head && TYPE_P (head))
256 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
261 case TB_POINTER_TO_THIS:
262 if (head && TYPE_P (head))
263 TB_SET_HEAD (TYPE_POINTER_TO (head));
269 if (head && TREE_CODE (head) == OFFSET_TYPE)
270 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
276 if (head && (TREE_CODE (head) == FUNCTION_TYPE
277 || TREE_CODE (head) == METHOD_TYPE))
278 TB_SET_HEAD (TYPE_ARG_TYPES (head));
283 case TB_METHOD_BASE_TYPE:
284 if (head && (TREE_CODE (head) == FUNCTION_TYPE
285 || TREE_CODE (head) == METHOD_TYPE)
286 && TYPE_METHOD_BASETYPE (head))
287 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
293 if (head && (TREE_CODE (head) == RECORD_TYPE
294 || TREE_CODE (head) == UNION_TYPE
295 || TREE_CODE (head) == QUAL_UNION_TYPE))
296 TB_SET_HEAD (TYPE_FIELDS (head));
302 if (head && TREE_CODE (head) == ARRAY_TYPE)
303 TB_SET_HEAD (TYPE_DOMAIN (head));
309 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
310 TB_SET_HEAD (TYPE_VALUES (head));
316 if (head && TREE_CODE (head) == PARM_DECL)
317 TB_SET_HEAD (DECL_ARG_TYPE (head));
323 if (head && DECL_P (head))
324 TB_SET_HEAD (DECL_INITIAL (head));
330 if (head && DECL_P (head))
331 TB_SET_HEAD (DECL_RESULT_FLD (head));
337 if (head && DECL_P (head))
338 TB_SET_HEAD (DECL_ARGUMENTS (head));
343 case TB_ABSTRACT_ORIGIN:
344 if (head && DECL_P (head))
345 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
346 else if (head && TREE_CODE (head) == BLOCK)
347 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
353 if (head && DECL_P (head))
354 TB_SET_HEAD (DECL_ATTRIBUTES (head));
355 else if (head && TYPE_P (head))
356 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
362 if (head && DECL_P (head))
363 TB_SET_HEAD (DECL_CONTEXT (head));
364 else if (head && TYPE_P (head)
365 && TYPE_CONTEXT (head))
366 TB_SET_HEAD (TYPE_CONTEXT (head));
372 if (head && TREE_CODE (head) == FIELD_DECL)
373 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
379 if (head && TREE_CODE (head) == FIELD_DECL)
380 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
386 if (head && DECL_P (head))
387 TB_SET_HEAD (DECL_SIZE_UNIT (head));
388 else if (head && TYPE_P (head))
389 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
395 if (head && DECL_P (head))
396 TB_SET_HEAD (DECL_SIZE (head));
397 else if (head && TYPE_P (head))
398 TB_SET_HEAD (TYPE_SIZE (head));
404 if (head && TREE_TYPE (head))
405 TB_SET_HEAD (TREE_TYPE (head));
410 case TB_DECL_SAVED_TREE:
411 if (head && TREE_CODE (head) == FUNCTION_DECL
412 && DECL_SAVED_TREE (head))
413 TB_SET_HEAD (DECL_SAVED_TREE (head));
419 if (head && TREE_CODE (head) == BIND_EXPR)
420 TB_SET_HEAD (TREE_OPERAND (head, 1));
426 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
427 TB_SET_HEAD (TREE_OPERAND (head, 0));
433 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
434 TB_SET_HEAD (TREE_OPERAND (head, 1));
440 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
441 TB_SET_HEAD (TREE_OPERAND (head, 2));
447 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
448 TB_SET_HEAD (TREE_OPERAND (head, 3));
460 case TB_PRETTY_PRINT:
463 print_generic_stmt (TB_OUT_FILE, head, 0);
464 fprintf (TB_OUT_FILE, "\n");
479 arg_text = strchr (input, ' ');
480 if (arg_text == NULL)
482 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
485 code = TB_get_tree_code (arg_text + 1);
487 /* Search in the subtree a node with the given code. */
491 res = walk_tree (&head, find_node_with_code, &code, NULL);
492 if (res == NULL_TREE)
494 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
498 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
505 #define TB_MOVE_HEAD(FCT) do { \
520 TB_MOVE_HEAD (TB_first_in_bind);
524 TB_MOVE_HEAD (TB_last_in_bind);
528 TB_MOVE_HEAD (TB_up_expr);
532 TB_MOVE_HEAD (TB_prev_expr);
536 TB_MOVE_HEAD (TB_next_expr);
540 /* This command is a little bit special, since it deals with history
541 stack. For this reason it should keep the "head = ..." statement
542 and not use TB_MOVE_HEAD. */
546 t = TB_history_prev ();
552 print_generic_expr (TB_OUT_FILE, head, 0);
553 fprintf (TB_OUT_FILE, "\n");
564 /* Don't go further if it's the last node in this chain. */
565 if (head && TREE_CODE (head) == BLOCK)
566 TB_SET_HEAD (BLOCK_CHAIN (head));
567 else if (head && TREE_CHAIN (head))
568 TB_SET_HEAD (TREE_CHAIN (head));
574 /* Go up to the current function declaration. */
575 TB_SET_HEAD (current_function_decl);
576 fprintf (TB_OUT_FILE, "Current function declaration.\n");
580 /* Display a help message. */
583 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
584 for (i = 0; i < TB_UNUSED_COMMAND; i++)
586 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
595 fprintf (TB_OUT_FILE, "Verbose on.\n");
600 fprintf (TB_OUT_FILE, "Verbose off.\n");
606 /* Just exit from this function. */
615 htab_delete (TB_up_ht);
620 /* Search the first node in this BIND_EXPR. */
623 TB_first_in_bind (tree node)
627 if (node == NULL_TREE)
630 while ((t = TB_prev_expr (node)))
636 /* Search the last node in this BIND_EXPR. */
639 TB_last_in_bind (tree node)
643 if (node == NULL_TREE)
646 while ((t = TB_next_expr (node)))
652 /* Search the parent expression for this node. */
655 TB_up_expr (tree node)
658 if (node == NULL_TREE)
661 res = (tree) htab_find (TB_up_ht, node);
665 /* Search the previous expression in this BIND_EXPR. */
668 TB_prev_expr (tree node)
670 node = TB_current_chain_node (node);
672 if (node == NULL_TREE)
675 node = TB_up_expr (node);
676 if (node && TREE_CODE (node) == COMPOUND_EXPR)
682 /* Search the next expression in this BIND_EXPR. */
685 TB_next_expr (tree node)
687 node = TB_current_chain_node (node);
689 if (node == NULL_TREE)
692 node = TREE_OPERAND (node, 1);
697 TB_current_chain_node (tree node)
699 if (node == NULL_TREE)
702 if (TREE_CODE (node) == COMPOUND_EXPR)
705 node = TB_up_expr (node);
708 if (TREE_CODE (node) == COMPOUND_EXPR)
711 node = TB_up_expr (node);
712 if (TREE_CODE (node) == COMPOUND_EXPR)
719 /* For each node store in its children nodes that the current node is their
720 parent. This function is used by walk_tree. */
723 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
724 void *data ATTRIBUTE_UNUSED)
731 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
734 int n = TREE_OPERAND_LENGTH (node);
736 for (i = 0; i < n; i++)
738 tree op = TREE_OPERAND (node, i);
739 slot = htab_find_slot (TB_up_ht, op, INSERT);
740 *slot = (void *) node;
744 /* Never stop walk_tree. */
748 /* Function used in TB_up_ht. */
751 TB_parent_eq (const void *p1, const void *p2)
753 const_tree const node = (const_tree)p2;
754 const_tree const parent = (const_tree) p1;
756 if (p1 == NULL || p2 == NULL)
761 int n = TREE_OPERAND_LENGTH (parent);
763 for (i = 0; i < n; i++)
764 if (node == TREE_OPERAND (parent, i))
770 /* Update information about upper expressions in the hash table. */
773 TB_update_up (tree node)
777 walk_tree (&node, store_child_info, NULL, NULL);
779 /* Walk function's body. */
780 if (TREE_CODE (node) == FUNCTION_DECL)
781 if (DECL_SAVED_TREE (node))
782 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
784 /* Walk rest of the chain. */
785 node = TREE_CHAIN (node);
787 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
790 /* Parse the input string for determining the command the user asked for. */
793 TB_get_command (char *input)
795 unsigned int mn, size_tok;
799 space = strchr (input, ' ');
801 size_tok = strlen (input) - strlen (space);
803 size_tok = strlen (input) - 1;
805 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
807 if (size_tok != TB_COMMAND_LEN (mn))
810 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
812 /* Here we just determined the command. If this command takes
813 an argument, then the argument is determined later. */
814 return TB_COMMAND_CODE (mn);
817 /* Not a valid command. */
818 return TB_UNUSED_COMMAND;
821 /* Parse the input string for determining the tree code. */
823 static enum tree_code
824 TB_get_tree_code (char *input)
826 unsigned int mn, size_tok;
830 space = strchr (input, ' ');
832 size_tok = strlen (input) - strlen (space);
834 size_tok = strlen (input) - 1;
836 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
838 if (size_tok != TB_TREE_CODE_LEN (mn))
841 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
844 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
845 return TB_TREE_CODE (mn);
849 /* This isn't a valid code. */
850 return LAST_AND_UNUSED_TREE_CODE;
853 /* Find a node with a given code. This function is used as an argument to
857 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
860 enum tree_code *code;
861 code = (enum tree_code *) data;
862 if (*code == TREE_CODE (*tp))
868 /* Returns a pointer to the last visited node. */
871 TB_history_prev (void)
873 if (!vec_safe_is_empty (TB_history_stack))
875 tree last = TB_history_stack->last ();
876 TB_history_stack->pop ();
882 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
883 (and null-terminate it). *LINEPTR is a pointer returned from malloc
884 (or NULL), pointing to *N characters of space. It is realloc'd as
885 necessary. Returns the number of characters read (not including the
886 null terminator), or -1 on error or EOF.
887 This function comes from sed (and is supposed to be a portable version
891 TB_getline (char **lineptr, long *n, FILE *stream)
896 if (lineptr == NULL || n == NULL)
905 /* Make sure we have a line buffer to start with. */
906 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
909 #define MAX_CANON 256
911 line = (char *) xrealloc (*lineptr, MAX_CANON);
930 register int c = getc (stream);
933 else if ((*p++ = c) == '\n')
937 /* Need to enlarge the line buffer. */
940 line = (char *) xrealloc (line, size);
953 /* Return a partial line since we got an error in the middle. */
955 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
956 if (p - 2 >= *lineptr && p[-2] == '\r')