2 Copyright (C) 2002, 2003, 2004 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "coretypes.h"
27 #include "tree-inline.h"
28 #include "diagnostic.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 static const struct tb_tree_code tb_tree_codes[] =
81 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
82 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
83 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
86 /* Function declarations. */
88 static long TB_getline (char **, long *, FILE *);
89 static TB_CODE TB_get_command (char *);
90 static enum tree_code TB_get_tree_code (char *);
91 static tree find_node_with_code (tree *, int *, void *);
92 static tree store_child_info (tree *, int *, void *);
93 static void TB_update_up (tree);
94 static tree TB_current_chain_node (tree);
95 static tree TB_prev_expr (tree);
96 static tree TB_next_expr (tree);
97 static tree TB_up_expr (tree);
98 static tree TB_first_in_bind (tree);
99 static tree TB_last_in_bind (tree);
100 static int TB_parent_eq (const void *, const void *);
101 static tree TB_history_prev (void);
103 /* FIXME: To be declared in a .h file. */
104 void browse_tree (tree);
106 /* Static variables. */
107 static htab_t TB_up_ht;
108 static tree TB_history_stack = NULL_TREE;
109 static int TB_verbose = 1;
112 /* Entry point in the Tree Browser. */
115 browse_tree (tree begin)
118 TB_CODE tbc = TB_UNUSED_COMMAND;
123 fprintf (TB_OUT_FILE, "\nTree Browser\n");
125 #define TB_SET_HEAD(N) do { \
126 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
131 print_generic_expr (TB_OUT_FILE, head, 0); \
132 fprintf (TB_OUT_FILE, "\n"); \
138 /* Store in a hashtable information about previous and upper statements. */
140 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
146 fprintf (TB_OUT_FILE, "TB> ");
147 rd = TB_getline (&input, &input_size, TB_IN_FILE);
154 /* Get a new command. Otherwise the user just pressed enter, and thus
155 she expects the last command to be reexecuted. */
156 tbc = TB_get_command (input);
165 if (head && (INTEGRAL_TYPE_P (head)
166 || TREE_CODE (head) == REAL_TYPE))
167 TB_SET_HEAD (TYPE_MAX_VALUE (head));
173 if (head && (INTEGRAL_TYPE_P (head)
174 || TREE_CODE (head) == REAL_TYPE))
175 TB_SET_HEAD (TYPE_MIN_VALUE (head));
181 if (head && TREE_CODE (head) == TREE_VEC)
183 /* This command takes another argument: the element number:
184 for example "elt 1". */
187 else if (head && TREE_CODE (head) == VECTOR_CST)
189 /* This command takes another argument: the element number:
190 for example "elt 1". */
198 if (head && TREE_CODE (head) == TREE_LIST)
199 TB_SET_HEAD (TREE_VALUE (head));
205 if (head && TREE_CODE (head) == TREE_LIST)
206 TB_SET_HEAD (TREE_PURPOSE (head));
212 if (head && TREE_CODE (head) == COMPLEX_CST)
213 TB_SET_HEAD (TREE_IMAGPART (head));
219 if (head && TREE_CODE (head) == COMPLEX_CST)
220 TB_SET_HEAD (TREE_REALPART (head));
226 if (head && TREE_CODE (head) == BIND_EXPR)
227 TB_SET_HEAD (TREE_OPERAND (head, 2));
233 if (head && TREE_CODE (head) == BLOCK)
234 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
239 case TB_SUPERCONTEXT:
240 if (head && TREE_CODE (head) == BLOCK)
241 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
247 if (head && TREE_CODE (head) == BLOCK)
248 TB_SET_HEAD (BLOCK_VARS (head));
249 else if (head && TREE_CODE (head) == BIND_EXPR)
250 TB_SET_HEAD (TREE_OPERAND (head, 0));
255 case TB_REFERENCE_TO_THIS:
256 if (head && TYPE_P (head))
257 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
262 case TB_POINTER_TO_THIS:
263 if (head && TYPE_P (head))
264 TB_SET_HEAD (TYPE_POINTER_TO (head));
270 if (head && TREE_CODE (head) == OFFSET_TYPE)
271 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
277 if (head && (TREE_CODE (head) == FUNCTION_TYPE
278 || TREE_CODE (head) == METHOD_TYPE))
279 TB_SET_HEAD (TYPE_ARG_TYPES (head));
284 case TB_METHOD_BASE_TYPE:
285 if (head && (TREE_CODE (head) == FUNCTION_TYPE
286 || TREE_CODE (head) == METHOD_TYPE)
287 && TYPE_METHOD_BASETYPE (head))
288 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
294 if (head && (TREE_CODE (head) == RECORD_TYPE
295 || TREE_CODE (head) == UNION_TYPE
296 || TREE_CODE (head) == QUAL_UNION_TYPE))
297 TB_SET_HEAD (TYPE_FIELDS (head));
303 if (head && TREE_CODE (head) == ARRAY_TYPE)
304 TB_SET_HEAD (TYPE_DOMAIN (head));
310 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
311 TB_SET_HEAD (TYPE_VALUES (head));
316 case TB_ARG_TYPE_AS_WRITTEN:
317 if (head && TREE_CODE (head) == PARM_DECL)
318 TB_SET_HEAD (DECL_ARG_TYPE_AS_WRITTEN (head));
324 if (head && TREE_CODE (head) == PARM_DECL)
325 TB_SET_HEAD (DECL_ARG_TYPE (head));
331 if (head && DECL_P (head))
332 TB_SET_HEAD (DECL_INITIAL (head));
338 if (head && DECL_P (head))
339 TB_SET_HEAD (DECL_RESULT_FLD (head));
345 if (head && DECL_P (head))
346 TB_SET_HEAD (DECL_ARGUMENTS (head));
351 case TB_ABSTRACT_ORIGIN:
352 if (head && DECL_P (head))
353 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
354 else if (head && TREE_CODE (head) == BLOCK)
355 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
361 if (head && DECL_P (head))
362 TB_SET_HEAD (DECL_ATTRIBUTES (head));
363 else if (head && TYPE_P (head))
364 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
370 if (head && DECL_P (head))
371 TB_SET_HEAD (DECL_CONTEXT (head));
372 else if (head && TYPE_P (head)
373 && TYPE_CONTEXT (head))
374 TB_SET_HEAD (TYPE_CONTEXT (head));
380 if (head && TREE_CODE (head) == FIELD_DECL)
381 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
387 if (head && TREE_CODE (head) == FIELD_DECL)
388 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
394 if (head && DECL_P (head))
395 TB_SET_HEAD (DECL_SIZE_UNIT (head));
396 else if (head && TYPE_P (head))
397 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
403 if (head && DECL_P (head))
404 TB_SET_HEAD (DECL_SIZE (head));
405 else if (head && TYPE_P (head))
406 TB_SET_HEAD (TYPE_SIZE (head));
412 if (head && TREE_TYPE (head))
413 TB_SET_HEAD (TREE_TYPE (head));
418 case TB_DECL_SAVED_TREE:
419 if (head && TREE_CODE (head) == FUNCTION_DECL
420 && DECL_SAVED_TREE (head))
421 TB_SET_HEAD (DECL_SAVED_TREE (head));
427 if (head && TREE_CODE (head) == BIND_EXPR)
428 TB_SET_HEAD (TREE_OPERAND (head, 1));
434 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
435 TB_SET_HEAD (TREE_OPERAND (head, 0));
441 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
442 TB_SET_HEAD (TREE_OPERAND (head, 1));
448 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
449 TB_SET_HEAD (TREE_OPERAND (head, 2));
455 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
456 TB_SET_HEAD (TREE_OPERAND (head, 3));
468 case TB_PRETTY_PRINT:
471 print_generic_stmt (TB_OUT_FILE, head, 0);
472 fprintf (TB_OUT_FILE, "\n");
487 arg_text = strchr (input, ' ');
488 if (arg_text == NULL)
490 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
493 code = TB_get_tree_code (arg_text + 1);
495 /* Search in the subtree a node with the given code. */
499 res = walk_tree (&head, find_node_with_code, &code, NULL);
500 if (res == NULL_TREE)
502 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
506 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
513 #define TB_MOVE_HEAD(FCT) do { \
528 TB_MOVE_HEAD (TB_first_in_bind);
532 TB_MOVE_HEAD (TB_last_in_bind);
536 TB_MOVE_HEAD (TB_up_expr);
540 TB_MOVE_HEAD (TB_prev_expr);
544 TB_MOVE_HEAD (TB_next_expr);
548 /* This command is a little bit special, since it deals with history
549 stack. For this reason it should keep the "head = ..." statement
550 and not use TB_MOVE_HEAD. */
554 t = TB_history_prev ();
560 print_generic_expr (TB_OUT_FILE, head, 0);
561 fprintf (TB_OUT_FILE, "\n");
572 /* Don't go further if it's the last node in this chain. */
573 if (head && TREE_CODE (head) == BLOCK)
574 TB_SET_HEAD (BLOCK_CHAIN (head));
575 else if (head && TREE_CHAIN (head))
576 TB_SET_HEAD (TREE_CHAIN (head));
582 /* Go up to the current function declaration. */
583 TB_SET_HEAD (current_function_decl);
584 fprintf (TB_OUT_FILE, "Current function declaration.\n");
588 /* Display a help message. */
591 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
592 for (i = 0; i < TB_UNUSED_COMMAND; i++)
594 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
603 fprintf (TB_OUT_FILE, "Verbose on.\n");
608 fprintf (TB_OUT_FILE, "Verbose off.\n");
614 /* Just exit from this function. */
623 htab_delete (TB_up_ht);
628 /* Search the first node in this BIND_EXPR. */
631 TB_first_in_bind (tree node)
635 if (node == NULL_TREE)
638 while ((t = TB_prev_expr (node)))
644 /* Search the last node in this BIND_EXPR. */
647 TB_last_in_bind (tree node)
651 if (node == NULL_TREE)
654 while ((t = TB_next_expr (node)))
660 /* Search the parent expression for this node. */
663 TB_up_expr (tree node)
666 if (node == NULL_TREE)
669 res = (tree) htab_find (TB_up_ht, node);
673 /* Search the previous expression in this BIND_EXPR. */
676 TB_prev_expr (tree node)
678 node = TB_current_chain_node (node);
680 if (node == NULL_TREE)
683 node = TB_up_expr (node);
684 if (node && TREE_CODE (node) == COMPOUND_EXPR)
690 /* Search the next expression in this BIND_EXPR. */
693 TB_next_expr (tree node)
695 node = TB_current_chain_node (node);
697 if (node == NULL_TREE)
700 node = TREE_OPERAND (node, 1);
705 TB_current_chain_node (tree node)
707 if (node == NULL_TREE)
710 if (TREE_CODE (node) == COMPOUND_EXPR)
713 node = TB_up_expr (node);
716 if (TREE_CODE (node) == COMPOUND_EXPR)
719 node = TB_up_expr (node);
720 if (TREE_CODE (node) == COMPOUND_EXPR)
727 /* For each node store in its children nodes that the current node is their
728 parent. This function is used by walk_tree. */
731 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
732 void *data ATTRIBUTE_UNUSED)
739 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
740 if (EXPRESSION_CLASS_P (node))
743 #define STORE_CHILD(N) do { \
744 tree op = TREE_OPERAND (node, N); \
745 slot = htab_find_slot (TB_up_ht, op, INSERT); \
746 *slot = (void *) node; \
749 switch (TREE_CODE_LENGTH (TREE_CODE (node)))
775 /* No children: nothing to do. */
781 /* Never stop walk_tree. */
785 /* Function used in TB_up_ht. */
788 TB_parent_eq (const void *p1, const void *p2)
794 if (p1 == NULL || p2 == NULL)
797 if (EXPRESSION_CLASS_P (parent))
800 #define TEST_CHILD(N) do { \
801 if (node == TREE_OPERAND (parent, N)) \
805 switch (TREE_CODE_LENGTH (TREE_CODE (parent)))
831 /* No children: nothing to do. */
840 /* Update information about upper expressions in the hash table. */
843 TB_update_up (tree node)
847 walk_tree (&node, store_child_info, NULL, NULL);
849 /* Walk function's body. */
850 if (TREE_CODE (node) == FUNCTION_DECL)
851 if (DECL_SAVED_TREE (node))
852 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
854 /* Walk rest of the chain. */
855 node = TREE_CHAIN (node);
857 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
860 /* Parse the input string for determining the command the user asked for. */
863 TB_get_command (char *input)
865 unsigned int mn, size_tok;
869 space = strchr (input, ' ');
871 size_tok = strlen (input) - strlen (space);
873 size_tok = strlen (input) - 1;
875 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
877 if (size_tok != TB_COMMAND_LEN (mn))
880 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
882 /* Here we just determined the command. If this command takes
883 an argument, then the argument is determined later. */
884 return TB_COMMAND_CODE (mn);
887 /* Not a valid command. */
888 return TB_UNUSED_COMMAND;
891 /* Parse the input string for determining the tree code. */
893 static enum tree_code
894 TB_get_tree_code (char *input)
896 unsigned int mn, size_tok;
900 space = strchr (input, ' ');
902 size_tok = strlen (input) - strlen (space);
904 size_tok = strlen (input) - 1;
906 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
908 if (size_tok != TB_TREE_CODE_LEN (mn))
911 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
914 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
915 return TB_TREE_CODE (mn);
919 /* This isn't a valid code. */
920 return LAST_AND_UNUSED_TREE_CODE;
923 /* Find a node with a given code. This function is used as an argument to
927 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
930 enum tree_code *code;
931 code = (enum tree_code *) data;
932 if (*code == TREE_CODE (*tp))
938 /* Returns a pointer to the last visited node. */
941 TB_history_prev (void)
943 if (TB_history_stack)
945 TB_history_stack = TREE_CHAIN (TB_history_stack);
946 if (TB_history_stack)
947 return TREE_VALUE (TB_history_stack);
952 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
953 (and null-terminate it). *LINEPTR is a pointer returned from malloc
954 (or NULL), pointing to *N characters of space. It is realloc'd as
955 necessary. Returns the number of characters read (not including the
956 null terminator), or -1 on error or EOF.
957 This function comes from sed (and is supposed to be a portable version
961 TB_getline (char **lineptr, long *n, FILE *stream)
966 if (lineptr == NULL || n == NULL)
975 /* Make sure we have a line buffer to start with. */
976 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
979 #define MAX_CANON 256
981 line = (char *) xrealloc (*lineptr, MAX_CANON);
1000 register int c = getc (stream);
1003 else if ((*p++ = c) == '\n')
1007 /* Need to enlarge the line buffer. */
1010 line = (char *) xrealloc (line, size);
1023 /* Return a partial line since we got an error in the middle. */
1025 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
1026 if (p - 2 >= *lineptr && p[-2] == '\r')
1030 return p - *lineptr;