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, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
28 #include "tree-inline.h"
29 #include "diagnostic.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 },
57 static struct tb_command tb_commands[] =
59 static const struct tb_command tb_commands[] =
62 #include "tree-browser.def"
66 #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len)
67 #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text)
68 #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code)
69 #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg)
72 /* Next structure is for parsing TREE_CODEs. */
75 const char *code_string;
76 size_t code_string_len;
79 #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 },
81 static struct tb_tree_code tb_tree_codes[] =
83 static const struct tb_tree_code tb_tree_codes[] =
90 #define TB_TREE_CODE(N) (tb_tree_codes[N].code)
91 #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string)
92 #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len)
95 /* Function declarations. */
97 static long TB_getline (char **, long *, FILE *);
98 static TB_CODE TB_get_command (char *);
99 static enum tree_code TB_get_tree_code (char *);
100 static tree find_node_with_code (tree *, int *, void *);
101 static tree store_child_info (tree *, int *, void *);
102 static void TB_update_up (tree);
103 static tree TB_current_chain_node (tree);
104 static tree TB_prev_expr (tree);
105 static tree TB_next_expr (tree);
106 static tree TB_up_expr (tree);
107 static tree TB_first_in_bind (tree);
108 static tree TB_last_in_bind (tree);
109 static int TB_parent_eq (const void *, const void *);
110 static tree TB_history_prev (void);
112 /* FIXME: To be declared in a .h file. */
113 void browse_tree (tree);
115 /* Static variables. */
116 static htab_t TB_up_ht;
117 static tree TB_history_stack = NULL_TREE;
118 static int TB_verbose = 1;
121 /* Entry point in the Tree Browser. */
124 browse_tree (tree begin)
127 TB_CODE tbc = TB_UNUSED_COMMAND;
132 fprintf (TB_OUT_FILE, "\nTree Browser\n");
134 #define TB_SET_HEAD(N) do { \
135 TB_history_stack = tree_cons (NULL_TREE, (N), TB_history_stack); \
140 print_generic_expr (TB_OUT_FILE, head, 0); \
141 fprintf (TB_OUT_FILE, "\n"); \
147 /* Store in a hashtable information about previous and upper statements. */
149 TB_up_ht = htab_create (1023, htab_hash_pointer, &TB_parent_eq, NULL);
155 fprintf (TB_OUT_FILE, "TB> ");
156 rd = TB_getline (&input, &input_size, TB_IN_FILE);
163 /* Get a new command. Otherwise the user just pressed enter, and thus
164 she expects the last command to be reexecuted. */
165 tbc = TB_get_command (input);
174 if (head && (INTEGRAL_TYPE_P (head)
175 || TREE_CODE (head) == REAL_TYPE))
176 TB_SET_HEAD (TYPE_MAX_VALUE (head));
182 if (head && (INTEGRAL_TYPE_P (head)
183 || TREE_CODE (head) == REAL_TYPE))
184 TB_SET_HEAD (TYPE_MIN_VALUE (head));
190 if (head && TREE_CODE (head) == TREE_VEC)
192 /* This command takes another argument: the element number:
193 for example "elt 1". */
196 else if (head && TREE_CODE (head) == VECTOR_CST)
198 /* This command takes another argument: the element number:
199 for example "elt 1". */
207 if (head && TREE_CODE (head) == TREE_LIST)
208 TB_SET_HEAD (TREE_VALUE (head));
214 if (head && TREE_CODE (head) == TREE_LIST)
215 TB_SET_HEAD (TREE_PURPOSE (head));
221 if (head && TREE_CODE (head) == COMPLEX_CST)
222 TB_SET_HEAD (TREE_IMAGPART (head));
228 if (head && TREE_CODE (head) == COMPLEX_CST)
229 TB_SET_HEAD (TREE_REALPART (head));
235 if (head && TREE_CODE (head) == BIND_EXPR)
236 TB_SET_HEAD (TREE_OPERAND (head, 2));
242 if (head && TREE_CODE (head) == BLOCK)
243 TB_SET_HEAD (BLOCK_SUBBLOCKS (head));
248 case TB_SUPERCONTEXT:
249 if (head && TREE_CODE (head) == BLOCK)
250 TB_SET_HEAD (BLOCK_SUPERCONTEXT (head));
256 if (head && TREE_CODE (head) == BLOCK)
257 TB_SET_HEAD (BLOCK_VARS (head));
258 else if (head && TREE_CODE (head) == BIND_EXPR)
259 TB_SET_HEAD (TREE_OPERAND (head, 0));
264 case TB_REFERENCE_TO_THIS:
265 if (head && TYPE_P (head))
266 TB_SET_HEAD (TYPE_REFERENCE_TO (head));
271 case TB_POINTER_TO_THIS:
272 if (head && TYPE_P (head))
273 TB_SET_HEAD (TYPE_POINTER_TO (head));
279 if (head && TREE_CODE (head) == OFFSET_TYPE)
280 TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head));
286 if (head && (TREE_CODE (head) == FUNCTION_TYPE
287 || TREE_CODE (head) == METHOD_TYPE))
288 TB_SET_HEAD (TYPE_ARG_TYPES (head));
293 case TB_METHOD_BASE_TYPE:
294 if (head && (TREE_CODE (head) == FUNCTION_TYPE
295 || TREE_CODE (head) == METHOD_TYPE)
296 && TYPE_METHOD_BASETYPE (head))
297 TB_SET_HEAD (TYPE_METHOD_BASETYPE (head));
303 if (head && (TREE_CODE (head) == RECORD_TYPE
304 || TREE_CODE (head) == UNION_TYPE
305 || TREE_CODE (head) == QUAL_UNION_TYPE))
306 TB_SET_HEAD (TYPE_FIELDS (head));
312 if (head && TREE_CODE (head) == ARRAY_TYPE)
313 TB_SET_HEAD (TYPE_DOMAIN (head));
319 if (head && TREE_CODE (head) == ENUMERAL_TYPE)
320 TB_SET_HEAD (TYPE_VALUES (head));
325 case TB_ARG_TYPE_AS_WRITTEN:
326 if (head && TREE_CODE (head) == PARM_DECL)
327 TB_SET_HEAD (DECL_ARG_TYPE_AS_WRITTEN (head));
333 if (head && TREE_CODE (head) == PARM_DECL)
334 TB_SET_HEAD (DECL_ARG_TYPE (head));
340 if (head && DECL_P (head))
341 TB_SET_HEAD (DECL_INITIAL (head));
347 if (head && DECL_P (head))
348 TB_SET_HEAD (DECL_RESULT_FLD (head));
354 if (head && DECL_P (head))
355 TB_SET_HEAD (DECL_ARGUMENTS (head));
360 case TB_ABSTRACT_ORIGIN:
361 if (head && DECL_P (head))
362 TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head));
363 else if (head && TREE_CODE (head) == BLOCK)
364 TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head));
370 if (head && DECL_P (head))
371 TB_SET_HEAD (DECL_ATTRIBUTES (head));
372 else if (head && TYPE_P (head))
373 TB_SET_HEAD (TYPE_ATTRIBUTES (head));
379 if (head && DECL_P (head))
380 TB_SET_HEAD (DECL_CONTEXT (head));
381 else if (head && TYPE_P (head)
382 && TYPE_CONTEXT (head))
383 TB_SET_HEAD (TYPE_CONTEXT (head));
389 if (head && TREE_CODE (head) == FIELD_DECL)
390 TB_SET_HEAD (DECL_FIELD_OFFSET (head));
396 if (head && TREE_CODE (head) == FIELD_DECL)
397 TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head));
403 if (head && DECL_P (head))
404 TB_SET_HEAD (DECL_SIZE_UNIT (head));
405 else if (head && TYPE_P (head))
406 TB_SET_HEAD (TYPE_SIZE_UNIT (head));
412 if (head && DECL_P (head))
413 TB_SET_HEAD (DECL_SIZE (head));
414 else if (head && TYPE_P (head))
415 TB_SET_HEAD (TYPE_SIZE (head));
421 if (head && TREE_TYPE (head))
422 TB_SET_HEAD (TREE_TYPE (head));
427 case TB_DECL_SAVED_TREE:
428 if (head && TREE_CODE (head) == FUNCTION_DECL
429 && DECL_SAVED_TREE (head))
430 TB_SET_HEAD (DECL_SAVED_TREE (head));
436 if (head && TREE_CODE (head) == BIND_EXPR)
437 TB_SET_HEAD (TREE_OPERAND (head, 1));
443 if (head && EXPR_P (head) && TREE_OPERAND (head, 0))
444 TB_SET_HEAD (TREE_OPERAND (head, 0));
450 if (head && EXPR_P (head) && TREE_OPERAND (head, 1))
451 TB_SET_HEAD (TREE_OPERAND (head, 1));
457 if (head && EXPR_P (head) && TREE_OPERAND (head, 2))
458 TB_SET_HEAD (TREE_OPERAND (head, 2));
464 if (head && EXPR_P (head) && TREE_OPERAND (head, 3))
465 TB_SET_HEAD (TREE_OPERAND (head, 3));
477 case TB_PRETTY_PRINT:
480 print_generic_stmt (TB_OUT_FILE, head, 0);
481 fprintf (TB_OUT_FILE, "\n");
496 arg_text = strchr (input, ' ');
497 if (arg_text == NULL)
499 fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n");
502 code = TB_get_tree_code (arg_text + 1);
504 /* Search in the subtree a node with the given code. */
508 res = walk_tree (&head, find_node_with_code, &code, NULL);
509 if (res == NULL_TREE)
511 fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n");
515 fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n");
522 #define TB_MOVE_HEAD(FCT) do { \
537 TB_MOVE_HEAD (TB_first_in_bind);
541 TB_MOVE_HEAD (TB_last_in_bind);
545 TB_MOVE_HEAD (TB_up_expr);
549 TB_MOVE_HEAD (TB_prev_expr);
553 TB_MOVE_HEAD (TB_next_expr);
557 /* This command is a little bit special, since it deals with history
558 stack. For this reason it should keep the "head = ..." statement
559 and not use TB_MOVE_HEAD. */
563 t = TB_history_prev ();
569 print_generic_expr (TB_OUT_FILE, head, 0);
570 fprintf (TB_OUT_FILE, "\n");
581 /* Don't go further if it's the last node in this chain. */
582 if (head && TREE_CODE (head) == BLOCK)
583 TB_SET_HEAD (BLOCK_CHAIN (head));
584 else if (head && TREE_CHAIN (head))
585 TB_SET_HEAD (TREE_CHAIN (head));
591 /* Go up to the current function declaration. */
592 TB_SET_HEAD (current_function_decl);
593 fprintf (TB_OUT_FILE, "Current function declaration.\n");
597 /* Display a help message. */
600 fprintf (TB_OUT_FILE, "Possible commands are:\n\n");
601 for (i = 0; i < TB_UNUSED_COMMAND; i++)
603 fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i));
612 fprintf (TB_OUT_FILE, "Verbose on.\n");
617 fprintf (TB_OUT_FILE, "Verbose off.\n");
623 /* Just exit from this function. */
632 htab_delete (TB_up_ht);
637 /* Search the first node in this BIND_EXPR. */
640 TB_first_in_bind (tree node)
644 if (node == NULL_TREE)
647 while ((t = TB_prev_expr (node)))
653 /* Search the last node in this BIND_EXPR. */
656 TB_last_in_bind (tree node)
660 if (node == NULL_TREE)
663 while ((t = TB_next_expr (node)))
669 /* Search the parent expression for this node. */
672 TB_up_expr (tree node)
675 if (node == NULL_TREE)
678 res = (tree) htab_find (TB_up_ht, node);
682 /* Search the previous expression in this BIND_EXPR. */
685 TB_prev_expr (tree node)
687 node = TB_current_chain_node (node);
689 if (node == NULL_TREE)
692 node = TB_up_expr (node);
693 if (node && TREE_CODE (node) == COMPOUND_EXPR)
699 /* Search the next expression in this BIND_EXPR. */
702 TB_next_expr (tree node)
704 node = TB_current_chain_node (node);
706 if (node == NULL_TREE)
709 node = TREE_OPERAND (node, 1);
714 TB_current_chain_node (tree node)
716 if (node == NULL_TREE)
719 if (TREE_CODE (node) == COMPOUND_EXPR)
722 node = TB_up_expr (node);
725 if (TREE_CODE (node) == COMPOUND_EXPR)
728 node = TB_up_expr (node);
729 if (TREE_CODE (node) == COMPOUND_EXPR)
736 /* For each node store in its children nodes that the current node is their
737 parent. This function is used by walk_tree. */
740 store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
741 void *data ATTRIBUTE_UNUSED)
748 /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */
749 if (EXPRESSION_CLASS_P (node))
752 #define STORE_CHILD(N) do { \
753 tree op = TREE_OPERAND (node, N); \
754 slot = htab_find_slot (TB_up_ht, op, INSERT); \
755 *slot = (void *) node; \
758 switch (TREE_CODE_LENGTH (TREE_CODE (node)))
784 /* No children: nothing to do. */
790 /* Never stop walk_tree. */
794 /* Function used in TB_up_ht. */
797 TB_parent_eq (const void *p1, const void *p2)
803 if (p1 == NULL || p2 == NULL)
806 if (EXPRESSION_CLASS_P (parent))
809 #define TEST_CHILD(N) do { \
810 if (node == TREE_OPERAND (parent, N)) \
814 switch (TREE_CODE_LENGTH (TREE_CODE (parent)))
840 /* No children: nothing to do. */
849 /* Update information about upper expressions in the hash table. */
852 TB_update_up (tree node)
856 walk_tree (&node, store_child_info, NULL, NULL);
858 /* Walk function's body. */
859 if (TREE_CODE (node) == FUNCTION_DECL)
860 if (DECL_SAVED_TREE (node))
861 walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL);
863 /* Walk rest of the chain. */
864 node = TREE_CHAIN (node);
866 fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n");
869 /* Parse the input string for determining the command the user asked for. */
872 TB_get_command (char *input)
874 unsigned int mn, size_tok;
878 space = strchr (input, ' ');
880 size_tok = strlen (input) - strlen (space);
882 size_tok = strlen (input) - 1;
884 for (mn = 0; mn < TB_UNUSED_COMMAND; mn++)
886 if (size_tok != TB_COMMAND_LEN (mn))
889 comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn));
891 /* Here we just determined the command. If this command takes
892 an argument, then the argument is determined later. */
893 return TB_COMMAND_CODE (mn);
896 /* Not a valid command. */
897 return TB_UNUSED_COMMAND;
900 /* Parse the input string for determining the tree code. */
902 static enum tree_code
903 TB_get_tree_code (char *input)
905 unsigned int mn, size_tok;
909 space = strchr (input, ' ');
911 size_tok = strlen (input) - strlen (space);
913 size_tok = strlen (input) - 1;
915 for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++)
917 if (size_tok != TB_TREE_CODE_LEN (mn))
920 comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn));
923 fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn));
924 return TB_TREE_CODE (mn);
928 /* This isn't a valid code. */
929 return LAST_AND_UNUSED_TREE_CODE;
932 /* Find a node with a given code. This function is used as an argument to
936 find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
939 enum tree_code *code;
940 code = (enum tree_code *) data;
941 if (*code == TREE_CODE (*tp))
947 /* Returns a pointer to the last visited node. */
950 TB_history_prev (void)
952 if (TB_history_stack)
954 TB_history_stack = TREE_CHAIN (TB_history_stack);
955 if (TB_history_stack)
956 return TREE_VALUE (TB_history_stack);
961 /* Read up to (and including) a '\n' from STREAM into *LINEPTR
962 (and null-terminate it). *LINEPTR is a pointer returned from malloc
963 (or NULL), pointing to *N characters of space. It is realloc'd as
964 necessary. Returns the number of characters read (not including the
965 null terminator), or -1 on error or EOF.
966 This function comes from sed (and is supposed to be a portable version
970 TB_getline (char **lineptr, long *n, FILE *stream)
975 if (lineptr == NULL || n == NULL)
984 /* Make sure we have a line buffer to start with. */
985 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
988 #define MAX_CANON 256
990 line = (char *) xrealloc (*lineptr, MAX_CANON);
1009 register int c = getc (stream);
1012 else if ((*p++ = c) == '\n')
1016 /* Need to enlarge the line buffer. */
1019 line = (char *) xrealloc (line, size);
1032 /* Return a partial line since we got an error in the middle. */
1034 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS)
1035 if (p - 2 >= *lineptr && p[-2] == '\r')
1039 return p - *lineptr;