]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Tree browser. |
23a5b65a | 2 | Copyright (C) 2002-2014 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Sebastian Pop <s.pop@laposte.net> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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 | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
6de9cd9a DN |
10 | version. |
11 | ||
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 | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
4a8fb1a1 | 24 | #include "hash-table.h" |
6de9cd9a | 25 | #include "tree.h" |
cf835838 | 26 | #include "tree-pretty-print.h" |
d8a2d370 | 27 | #include "print-tree.h" |
6de9cd9a DN |
28 | |
29 | #define TB_OUT_FILE stdout | |
30 | #define TB_IN_FILE stdin | |
31 | #define TB_NIY fprintf (TB_OUT_FILE, "Sorry this command is not yet implemented.\n") | |
32 | #define TB_WF fprintf (TB_OUT_FILE, "Warning, this command failed.\n") | |
33 | ||
6de9cd9a DN |
34 | /* Structures for handling Tree Browser's commands. */ |
35 | #define DEFTBCODE(COMMAND, STRING, HELP) COMMAND, | |
36 | enum TB_Comm_code { | |
37 | #include "tree-browser.def" | |
38 | TB_UNUSED_COMMAND | |
39 | }; | |
40 | #undef DEFTBCODE | |
41 | typedef enum TB_Comm_code TB_CODE; | |
42 | ||
43 | struct tb_command { | |
44 | const char *help_msg; | |
45 | const char *comm_text; | |
46 | size_t comm_len; | |
47 | TB_CODE comm_code; | |
48 | }; | |
49 | ||
c3284718 | 50 | #define DEFTBCODE(code, str, help) { help, str, sizeof (str) - 1, code }, |
6de9cd9a | 51 | static const struct tb_command tb_commands[] = |
6de9cd9a DN |
52 | { |
53 | #include "tree-browser.def" | |
54 | }; | |
55 | #undef DEFTBCODE | |
56 | ||
57 | #define TB_COMMAND_LEN(N) (tb_commands[N].comm_len) | |
58 | #define TB_COMMAND_TEXT(N) (tb_commands[N].comm_text) | |
59 | #define TB_COMMAND_CODE(N) (tb_commands[N].comm_code) | |
60 | #define TB_COMMAND_HELP(N) (tb_commands[N].help_msg) | |
61 | ||
62 | ||
63 | /* Next structure is for parsing TREE_CODEs. */ | |
64 | struct tb_tree_code { | |
65 | enum tree_code code; | |
66 | const char *code_string; | |
67 | size_t code_string_len; | |
68 | }; | |
69 | ||
70 | #define DEFTREECODE(SYM, STRING, TYPE, NARGS) { SYM, STRING, sizeof (STRING) - 1 }, | |
c0ed0531 ILT |
71 | #define END_OF_BASE_TREE_CODES \ |
72 | { LAST_AND_UNUSED_TREE_CODE, "@dummy", sizeof ("@dummy") - 1 }, | |
6de9cd9a | 73 | static const struct tb_tree_code tb_tree_codes[] = |
6de9cd9a | 74 | { |
c0ed0531 | 75 | #include "all-tree.def" |
6de9cd9a DN |
76 | }; |
77 | #undef DEFTREECODE | |
c0ed0531 | 78 | #undef END_OF_BASE_TREE_CODES |
6de9cd9a DN |
79 | |
80 | #define TB_TREE_CODE(N) (tb_tree_codes[N].code) | |
81 | #define TB_TREE_CODE_TEXT(N) (tb_tree_codes[N].code_string) | |
82 | #define TB_TREE_CODE_LEN(N) (tb_tree_codes[N].code_string_len) | |
83 | ||
84 | ||
85 | /* Function declarations. */ | |
86 | ||
87 | static long TB_getline (char **, long *, FILE *); | |
88 | static TB_CODE TB_get_command (char *); | |
89 | static enum tree_code TB_get_tree_code (char *); | |
90 | static tree find_node_with_code (tree *, int *, void *); | |
91 | static tree store_child_info (tree *, int *, void *); | |
92 | static void TB_update_up (tree); | |
93 | static tree TB_current_chain_node (tree); | |
94 | static tree TB_prev_expr (tree); | |
95 | static tree TB_next_expr (tree); | |
96 | static tree TB_up_expr (tree); | |
97 | static tree TB_first_in_bind (tree); | |
98 | static tree TB_last_in_bind (tree); | |
6de9cd9a DN |
99 | static tree TB_history_prev (void); |
100 | ||
101 | /* FIXME: To be declared in a .h file. */ | |
102 | void browse_tree (tree); | |
103 | ||
4a8fb1a1 LC |
104 | /* Hashtable helpers. */ |
105 | struct tree_upper_hasher : typed_noop_remove <tree_node> | |
106 | { | |
107 | typedef tree_node value_type; | |
108 | typedef tree_node compare_type; | |
109 | static inline hashval_t hash (const value_type *); | |
110 | static inline bool equal (const value_type *, const compare_type *); | |
111 | }; | |
112 | ||
113 | inline hashval_t | |
114 | tree_upper_hasher::hash (const value_type *v) | |
115 | { | |
116 | return pointer_hash <value_type>::hash (v); | |
117 | } | |
118 | ||
119 | inline bool | |
120 | tree_upper_hasher::equal (const value_type *parent, const compare_type *node) | |
121 | { | |
122 | if (parent == NULL || node == NULL) | |
123 | return 0; | |
124 | ||
125 | if (EXPR_P (parent)) | |
126 | { | |
127 | int n = TREE_OPERAND_LENGTH (parent); | |
128 | int i; | |
129 | for (i = 0; i < n; i++) | |
130 | if (node == TREE_OPERAND (parent, i)) | |
131 | return true; | |
132 | } | |
133 | return false; | |
134 | } | |
135 | ||
6de9cd9a | 136 | /* Static variables. */ |
c203e8a7 | 137 | static hash_table<tree_upper_hasher> *TB_up_ht; |
9771b263 | 138 | static vec<tree, va_gc> *TB_history_stack; |
6de9cd9a DN |
139 | static int TB_verbose = 1; |
140 | ||
141 | ||
142 | /* Entry point in the Tree Browser. */ | |
143 | ||
144 | void | |
145 | browse_tree (tree begin) | |
146 | { | |
147 | tree head; | |
148 | TB_CODE tbc = TB_UNUSED_COMMAND; | |
149 | ssize_t rd; | |
150 | char *input = NULL; | |
151 | long input_size = 0; | |
152 | ||
153 | fprintf (TB_OUT_FILE, "\nTree Browser\n"); | |
154 | ||
155 | #define TB_SET_HEAD(N) do { \ | |
9771b263 | 156 | vec_safe_push (TB_history_stack, N); \ |
6de9cd9a DN |
157 | head = N; \ |
158 | if (TB_verbose) \ | |
159 | if (head) \ | |
160 | { \ | |
161 | print_generic_expr (TB_OUT_FILE, head, 0); \ | |
162 | fprintf (TB_OUT_FILE, "\n"); \ | |
163 | } \ | |
164 | } while (0) | |
165 | ||
166 | TB_SET_HEAD (begin); | |
167 | ||
168 | /* Store in a hashtable information about previous and upper statements. */ | |
169 | { | |
c203e8a7 | 170 | TB_up_ht = new hash_table<tree_upper_hasher> (1023); |
6de9cd9a DN |
171 | TB_update_up (head); |
172 | } | |
173 | ||
174 | while (24) | |
175 | { | |
176 | fprintf (TB_OUT_FILE, "TB> "); | |
177 | rd = TB_getline (&input, &input_size, TB_IN_FILE); | |
178 | ||
179 | if (rd == -1) | |
180 | /* EOF. */ | |
181 | goto ret; | |
182 | ||
183 | if (rd != 1) | |
184 | /* Get a new command. Otherwise the user just pressed enter, and thus | |
185 | she expects the last command to be reexecuted. */ | |
186 | tbc = TB_get_command (input); | |
187 | ||
188 | switch (tbc) | |
189 | { | |
190 | case TB_UPDATE_UP: | |
191 | TB_update_up (head); | |
192 | break; | |
193 | ||
194 | case TB_MAX: | |
195 | if (head && (INTEGRAL_TYPE_P (head) | |
325217ed CF |
196 | || TREE_CODE (head) == REAL_TYPE |
197 | || TREE_CODE (head) == FIXED_POINT_TYPE)) | |
6de9cd9a DN |
198 | TB_SET_HEAD (TYPE_MAX_VALUE (head)); |
199 | else | |
200 | TB_WF; | |
201 | break; | |
202 | ||
203 | case TB_MIN: | |
204 | if (head && (INTEGRAL_TYPE_P (head) | |
325217ed CF |
205 | || TREE_CODE (head) == REAL_TYPE |
206 | || TREE_CODE (head) == FIXED_POINT_TYPE)) | |
6de9cd9a DN |
207 | TB_SET_HEAD (TYPE_MIN_VALUE (head)); |
208 | else | |
209 | TB_WF; | |
210 | break; | |
211 | ||
212 | case TB_ELT: | |
213 | if (head && TREE_CODE (head) == TREE_VEC) | |
214 | { | |
215 | /* This command takes another argument: the element number: | |
216 | for example "elt 1". */ | |
217 | TB_NIY; | |
218 | } | |
219 | else if (head && TREE_CODE (head) == VECTOR_CST) | |
220 | { | |
221 | /* This command takes another argument: the element number: | |
222 | for example "elt 1". */ | |
223 | TB_NIY; | |
224 | } | |
225 | else | |
226 | TB_WF; | |
227 | break; | |
228 | ||
229 | case TB_VALUE: | |
230 | if (head && TREE_CODE (head) == TREE_LIST) | |
231 | TB_SET_HEAD (TREE_VALUE (head)); | |
232 | else | |
233 | TB_WF; | |
234 | break; | |
235 | ||
236 | case TB_PURPOSE: | |
237 | if (head && TREE_CODE (head) == TREE_LIST) | |
238 | TB_SET_HEAD (TREE_PURPOSE (head)); | |
239 | else | |
240 | TB_WF; | |
241 | break; | |
242 | ||
243 | case TB_IMAG: | |
244 | if (head && TREE_CODE (head) == COMPLEX_CST) | |
245 | TB_SET_HEAD (TREE_IMAGPART (head)); | |
246 | else | |
247 | TB_WF; | |
248 | break; | |
249 | ||
250 | case TB_REAL: | |
251 | if (head && TREE_CODE (head) == COMPLEX_CST) | |
252 | TB_SET_HEAD (TREE_REALPART (head)); | |
253 | else | |
254 | TB_WF; | |
255 | break; | |
256 | ||
257 | case TB_BLOCK: | |
258 | if (head && TREE_CODE (head) == BIND_EXPR) | |
259 | TB_SET_HEAD (TREE_OPERAND (head, 2)); | |
260 | else | |
261 | TB_WF; | |
262 | break; | |
263 | ||
264 | case TB_SUBBLOCKS: | |
90afe2c9 | 265 | if (head && TREE_CODE (head) == BLOCK) |
6de9cd9a DN |
266 | TB_SET_HEAD (BLOCK_SUBBLOCKS (head)); |
267 | else | |
268 | TB_WF; | |
269 | break; | |
270 | ||
271 | case TB_SUPERCONTEXT: | |
90afe2c9 | 272 | if (head && TREE_CODE (head) == BLOCK) |
6de9cd9a DN |
273 | TB_SET_HEAD (BLOCK_SUPERCONTEXT (head)); |
274 | else | |
275 | TB_WF; | |
276 | break; | |
277 | ||
278 | case TB_VARS: | |
90afe2c9 | 279 | if (head && TREE_CODE (head) == BLOCK) |
6de9cd9a DN |
280 | TB_SET_HEAD (BLOCK_VARS (head)); |
281 | else if (head && TREE_CODE (head) == BIND_EXPR) | |
282 | TB_SET_HEAD (TREE_OPERAND (head, 0)); | |
283 | else | |
284 | TB_WF; | |
285 | break; | |
286 | ||
287 | case TB_REFERENCE_TO_THIS: | |
6615c446 | 288 | if (head && TYPE_P (head)) |
6de9cd9a DN |
289 | TB_SET_HEAD (TYPE_REFERENCE_TO (head)); |
290 | else | |
291 | TB_WF; | |
292 | break; | |
293 | ||
294 | case TB_POINTER_TO_THIS: | |
6615c446 | 295 | if (head && TYPE_P (head)) |
6de9cd9a DN |
296 | TB_SET_HEAD (TYPE_POINTER_TO (head)); |
297 | else | |
298 | TB_WF; | |
299 | break; | |
300 | ||
301 | case TB_BASETYPE: | |
302 | if (head && TREE_CODE (head) == OFFSET_TYPE) | |
303 | TB_SET_HEAD (TYPE_OFFSET_BASETYPE (head)); | |
304 | else | |
305 | TB_WF; | |
306 | break; | |
307 | ||
308 | case TB_ARG_TYPES: | |
309 | if (head && (TREE_CODE (head) == FUNCTION_TYPE | |
310 | || TREE_CODE (head) == METHOD_TYPE)) | |
311 | TB_SET_HEAD (TYPE_ARG_TYPES (head)); | |
312 | else | |
313 | TB_WF; | |
314 | break; | |
315 | ||
316 | case TB_METHOD_BASE_TYPE: | |
317 | if (head && (TREE_CODE (head) == FUNCTION_TYPE | |
318 | || TREE_CODE (head) == METHOD_TYPE) | |
319 | && TYPE_METHOD_BASETYPE (head)) | |
320 | TB_SET_HEAD (TYPE_METHOD_BASETYPE (head)); | |
321 | else | |
322 | TB_WF; | |
323 | break; | |
324 | ||
325 | case TB_FIELDS: | |
326 | if (head && (TREE_CODE (head) == RECORD_TYPE | |
327 | || TREE_CODE (head) == UNION_TYPE | |
328 | || TREE_CODE (head) == QUAL_UNION_TYPE)) | |
329 | TB_SET_HEAD (TYPE_FIELDS (head)); | |
330 | else | |
331 | TB_WF; | |
332 | break; | |
333 | ||
334 | case TB_DOMAIN: | |
08f2586c | 335 | if (head && TREE_CODE (head) == ARRAY_TYPE) |
6de9cd9a DN |
336 | TB_SET_HEAD (TYPE_DOMAIN (head)); |
337 | else | |
338 | TB_WF; | |
339 | break; | |
340 | ||
341 | case TB_VALUES: | |
342 | if (head && TREE_CODE (head) == ENUMERAL_TYPE) | |
343 | TB_SET_HEAD (TYPE_VALUES (head)); | |
344 | else | |
345 | TB_WF; | |
346 | break; | |
347 | ||
6de9cd9a DN |
348 | case TB_ARG_TYPE: |
349 | if (head && TREE_CODE (head) == PARM_DECL) | |
350 | TB_SET_HEAD (DECL_ARG_TYPE (head)); | |
351 | else | |
352 | TB_WF; | |
353 | break; | |
354 | ||
355 | case TB_INITIAL: | |
6615c446 | 356 | if (head && DECL_P (head)) |
6de9cd9a DN |
357 | TB_SET_HEAD (DECL_INITIAL (head)); |
358 | else | |
359 | TB_WF; | |
360 | break; | |
361 | ||
362 | case TB_RESULT: | |
6615c446 | 363 | if (head && DECL_P (head)) |
6de9cd9a DN |
364 | TB_SET_HEAD (DECL_RESULT_FLD (head)); |
365 | else | |
366 | TB_WF; | |
367 | break; | |
368 | ||
369 | case TB_ARGUMENTS: | |
6615c446 | 370 | if (head && DECL_P (head)) |
6de9cd9a DN |
371 | TB_SET_HEAD (DECL_ARGUMENTS (head)); |
372 | else | |
373 | TB_WF; | |
374 | break; | |
375 | ||
376 | case TB_ABSTRACT_ORIGIN: | |
6615c446 | 377 | if (head && DECL_P (head)) |
6de9cd9a | 378 | TB_SET_HEAD (DECL_ABSTRACT_ORIGIN (head)); |
90afe2c9 | 379 | else if (head && TREE_CODE (head) == BLOCK) |
6de9cd9a DN |
380 | TB_SET_HEAD (BLOCK_ABSTRACT_ORIGIN (head)); |
381 | else | |
382 | TB_WF; | |
383 | break; | |
384 | ||
385 | case TB_ATTRIBUTES: | |
6615c446 | 386 | if (head && DECL_P (head)) |
6de9cd9a | 387 | TB_SET_HEAD (DECL_ATTRIBUTES (head)); |
6615c446 | 388 | else if (head && TYPE_P (head)) |
6de9cd9a DN |
389 | TB_SET_HEAD (TYPE_ATTRIBUTES (head)); |
390 | else | |
391 | TB_WF; | |
392 | break; | |
393 | ||
394 | case TB_CONTEXT: | |
6615c446 | 395 | if (head && DECL_P (head)) |
6de9cd9a | 396 | TB_SET_HEAD (DECL_CONTEXT (head)); |
6615c446 | 397 | else if (head && TYPE_P (head) |
6de9cd9a DN |
398 | && TYPE_CONTEXT (head)) |
399 | TB_SET_HEAD (TYPE_CONTEXT (head)); | |
400 | else | |
401 | TB_WF; | |
402 | break; | |
403 | ||
404 | case TB_OFFSET: | |
405 | if (head && TREE_CODE (head) == FIELD_DECL) | |
406 | TB_SET_HEAD (DECL_FIELD_OFFSET (head)); | |
407 | else | |
408 | TB_WF; | |
409 | break; | |
410 | ||
411 | case TB_BIT_OFFSET: | |
412 | if (head && TREE_CODE (head) == FIELD_DECL) | |
413 | TB_SET_HEAD (DECL_FIELD_BIT_OFFSET (head)); | |
414 | else | |
415 | TB_WF; | |
416 | break; | |
417 | ||
418 | case TB_UNIT_SIZE: | |
6615c446 | 419 | if (head && DECL_P (head)) |
6de9cd9a | 420 | TB_SET_HEAD (DECL_SIZE_UNIT (head)); |
6615c446 | 421 | else if (head && TYPE_P (head)) |
6de9cd9a DN |
422 | TB_SET_HEAD (TYPE_SIZE_UNIT (head)); |
423 | else | |
424 | TB_WF; | |
425 | break; | |
426 | ||
427 | case TB_SIZE: | |
6615c446 | 428 | if (head && DECL_P (head)) |
6de9cd9a | 429 | TB_SET_HEAD (DECL_SIZE (head)); |
6615c446 | 430 | else if (head && TYPE_P (head)) |
6de9cd9a DN |
431 | TB_SET_HEAD (TYPE_SIZE (head)); |
432 | else | |
433 | TB_WF; | |
434 | break; | |
435 | ||
436 | case TB_TYPE: | |
437 | if (head && TREE_TYPE (head)) | |
438 | TB_SET_HEAD (TREE_TYPE (head)); | |
439 | else | |
440 | TB_WF; | |
441 | break; | |
442 | ||
443 | case TB_DECL_SAVED_TREE: | |
444 | if (head && TREE_CODE (head) == FUNCTION_DECL | |
445 | && DECL_SAVED_TREE (head)) | |
446 | TB_SET_HEAD (DECL_SAVED_TREE (head)); | |
447 | else | |
448 | TB_WF; | |
449 | break; | |
450 | ||
451 | case TB_BODY: | |
452 | if (head && TREE_CODE (head) == BIND_EXPR) | |
453 | TB_SET_HEAD (TREE_OPERAND (head, 1)); | |
454 | else | |
455 | TB_WF; | |
456 | break; | |
457 | ||
458 | case TB_CHILD_0: | |
6615c446 | 459 | if (head && EXPR_P (head) && TREE_OPERAND (head, 0)) |
6de9cd9a DN |
460 | TB_SET_HEAD (TREE_OPERAND (head, 0)); |
461 | else | |
462 | TB_WF; | |
463 | break; | |
464 | ||
465 | case TB_CHILD_1: | |
6615c446 | 466 | if (head && EXPR_P (head) && TREE_OPERAND (head, 1)) |
6de9cd9a DN |
467 | TB_SET_HEAD (TREE_OPERAND (head, 1)); |
468 | else | |
469 | TB_WF; | |
470 | break; | |
471 | ||
472 | case TB_CHILD_2: | |
6615c446 | 473 | if (head && EXPR_P (head) && TREE_OPERAND (head, 2)) |
6de9cd9a DN |
474 | TB_SET_HEAD (TREE_OPERAND (head, 2)); |
475 | else | |
476 | TB_WF; | |
477 | break; | |
478 | ||
479 | case TB_CHILD_3: | |
6615c446 | 480 | if (head && EXPR_P (head) && TREE_OPERAND (head, 3)) |
6de9cd9a DN |
481 | TB_SET_HEAD (TREE_OPERAND (head, 3)); |
482 | else | |
483 | TB_WF; | |
484 | break; | |
485 | ||
486 | case TB_PRINT: | |
487 | if (head) | |
488 | debug_tree (head); | |
489 | else | |
490 | TB_WF; | |
491 | break; | |
492 | ||
493 | case TB_PRETTY_PRINT: | |
494 | if (head) | |
495 | { | |
496 | print_generic_stmt (TB_OUT_FILE, head, 0); | |
497 | fprintf (TB_OUT_FILE, "\n"); | |
498 | } | |
499 | else | |
500 | TB_WF; | |
501 | break; | |
502 | ||
503 | case TB_SEARCH_NAME: | |
504 | ||
505 | break; | |
506 | ||
507 | case TB_SEARCH_CODE: | |
508 | { | |
509 | enum tree_code code; | |
510 | char *arg_text; | |
511 | ||
512 | arg_text = strchr (input, ' '); | |
513 | if (arg_text == NULL) | |
514 | { | |
515 | fprintf (TB_OUT_FILE, "First argument is missing. This isn't a valid search command. \n"); | |
516 | break; | |
517 | } | |
518 | code = TB_get_tree_code (arg_text + 1); | |
519 | ||
520 | /* Search in the subtree a node with the given code. */ | |
521 | { | |
522 | tree res; | |
523 | ||
524 | res = walk_tree (&head, find_node_with_code, &code, NULL); | |
525 | if (res == NULL_TREE) | |
526 | { | |
527 | fprintf (TB_OUT_FILE, "There's no node with this code (reachable via the walk_tree function from this node).\n"); | |
528 | } | |
529 | else | |
530 | { | |
531 | fprintf (TB_OUT_FILE, "Achoo! I got this node in the tree.\n"); | |
532 | TB_SET_HEAD (res); | |
533 | } | |
534 | } | |
535 | break; | |
536 | } | |
537 | ||
538 | #define TB_MOVE_HEAD(FCT) do { \ | |
539 | if (head) \ | |
540 | { \ | |
541 | tree t; \ | |
542 | t = FCT (head); \ | |
543 | if (t) \ | |
544 | TB_SET_HEAD (t); \ | |
545 | else \ | |
546 | TB_WF; \ | |
547 | } \ | |
548 | else \ | |
549 | TB_WF; \ | |
550 | } while (0) | |
551 | ||
552 | case TB_FIRST: | |
553 | TB_MOVE_HEAD (TB_first_in_bind); | |
554 | break; | |
555 | ||
556 | case TB_LAST: | |
557 | TB_MOVE_HEAD (TB_last_in_bind); | |
558 | break; | |
559 | ||
560 | case TB_UP: | |
561 | TB_MOVE_HEAD (TB_up_expr); | |
562 | break; | |
563 | ||
564 | case TB_PREV: | |
565 | TB_MOVE_HEAD (TB_prev_expr); | |
566 | break; | |
567 | ||
568 | case TB_NEXT: | |
569 | TB_MOVE_HEAD (TB_next_expr); | |
570 | break; | |
571 | ||
572 | case TB_HPREV: | |
573 | /* This command is a little bit special, since it deals with history | |
574 | stack. For this reason it should keep the "head = ..." statement | |
575 | and not use TB_MOVE_HEAD. */ | |
576 | if (head) | |
577 | { | |
578 | tree t; | |
579 | t = TB_history_prev (); | |
580 | if (t) | |
581 | { | |
582 | head = t; | |
583 | if (TB_verbose) | |
584 | { | |
585 | print_generic_expr (TB_OUT_FILE, head, 0); | |
586 | fprintf (TB_OUT_FILE, "\n"); | |
587 | } | |
588 | } | |
589 | else | |
590 | TB_WF; | |
591 | } | |
592 | else | |
593 | TB_WF; | |
594 | break; | |
595 | ||
596 | case TB_CHAIN: | |
597 | /* Don't go further if it's the last node in this chain. */ | |
90afe2c9 | 598 | if (head && TREE_CODE (head) == BLOCK) |
6de9cd9a DN |
599 | TB_SET_HEAD (BLOCK_CHAIN (head)); |
600 | else if (head && TREE_CHAIN (head)) | |
601 | TB_SET_HEAD (TREE_CHAIN (head)); | |
602 | else | |
603 | TB_WF; | |
604 | break; | |
605 | ||
606 | case TB_FUN: | |
607 | /* Go up to the current function declaration. */ | |
608 | TB_SET_HEAD (current_function_decl); | |
609 | fprintf (TB_OUT_FILE, "Current function declaration.\n"); | |
610 | break; | |
611 | ||
612 | case TB_HELP: | |
613 | /* Display a help message. */ | |
614 | { | |
615 | int i; | |
616 | fprintf (TB_OUT_FILE, "Possible commands are:\n\n"); | |
617 | for (i = 0; i < TB_UNUSED_COMMAND; i++) | |
618 | { | |
619 | fprintf (TB_OUT_FILE, "%20s - %s\n", TB_COMMAND_TEXT (i), TB_COMMAND_HELP (i)); | |
620 | } | |
621 | } | |
622 | break; | |
623 | ||
624 | case TB_VERBOSE: | |
625 | if (TB_verbose == 0) | |
626 | { | |
627 | TB_verbose = 1; | |
628 | fprintf (TB_OUT_FILE, "Verbose on.\n"); | |
629 | } | |
630 | else | |
631 | { | |
632 | TB_verbose = 0; | |
633 | fprintf (TB_OUT_FILE, "Verbose off.\n"); | |
634 | } | |
635 | break; | |
636 | ||
637 | case TB_EXIT: | |
638 | case TB_QUIT: | |
639 | /* Just exit from this function. */ | |
640 | goto ret; | |
641 | ||
642 | default: | |
643 | TB_NIY; | |
644 | } | |
645 | } | |
646 | ||
647 | ret:; | |
c203e8a7 TS |
648 | delete TB_up_ht; |
649 | TB_up_ht = NULL; | |
6de9cd9a DN |
650 | return; |
651 | } | |
652 | ||
653 | ||
654 | /* Search the first node in this BIND_EXPR. */ | |
655 | ||
656 | static tree | |
657 | TB_first_in_bind (tree node) | |
658 | { | |
659 | tree t; | |
660 | ||
661 | if (node == NULL_TREE) | |
662 | return NULL_TREE; | |
663 | ||
664 | while ((t = TB_prev_expr (node))) | |
665 | node = t; | |
666 | ||
667 | return node; | |
668 | } | |
669 | ||
670 | /* Search the last node in this BIND_EXPR. */ | |
671 | ||
672 | static tree | |
673 | TB_last_in_bind (tree node) | |
674 | { | |
675 | tree t; | |
676 | ||
677 | if (node == NULL_TREE) | |
678 | return NULL_TREE; | |
679 | ||
680 | while ((t = TB_next_expr (node))) | |
681 | node = t; | |
682 | ||
683 | return node; | |
684 | } | |
685 | ||
686 | /* Search the parent expression for this node. */ | |
687 | ||
688 | static tree | |
689 | TB_up_expr (tree node) | |
690 | { | |
691 | tree res; | |
692 | if (node == NULL_TREE) | |
693 | return NULL_TREE; | |
694 | ||
c203e8a7 | 695 | res = TB_up_ht->find (node); |
6de9cd9a DN |
696 | return res; |
697 | } | |
698 | ||
699 | /* Search the previous expression in this BIND_EXPR. */ | |
700 | ||
701 | static tree | |
702 | TB_prev_expr (tree node) | |
703 | { | |
704 | node = TB_current_chain_node (node); | |
705 | ||
706 | if (node == NULL_TREE) | |
707 | return NULL_TREE; | |
708 | ||
709 | node = TB_up_expr (node); | |
710 | if (node && TREE_CODE (node) == COMPOUND_EXPR) | |
711 | return node; | |
712 | else | |
713 | return NULL_TREE; | |
714 | } | |
715 | ||
716 | /* Search the next expression in this BIND_EXPR. */ | |
717 | ||
718 | static tree | |
719 | TB_next_expr (tree node) | |
720 | { | |
721 | node = TB_current_chain_node (node); | |
722 | ||
723 | if (node == NULL_TREE) | |
724 | return NULL_TREE; | |
725 | ||
726 | node = TREE_OPERAND (node, 1); | |
727 | return node; | |
728 | } | |
729 | ||
730 | static tree | |
731 | TB_current_chain_node (tree node) | |
732 | { | |
733 | if (node == NULL_TREE) | |
734 | return NULL_TREE; | |
735 | ||
736 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
737 | return node; | |
738 | ||
739 | node = TB_up_expr (node); | |
740 | if (node) | |
741 | { | |
742 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
743 | return node; | |
744 | ||
745 | node = TB_up_expr (node); | |
746 | if (TREE_CODE (node) == COMPOUND_EXPR) | |
747 | return node; | |
748 | } | |
749 | ||
750 | return NULL_TREE; | |
751 | } | |
752 | ||
753 | /* For each node store in its children nodes that the current node is their | |
754 | parent. This function is used by walk_tree. */ | |
755 | ||
756 | static tree | |
757 | store_child_info (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, | |
758 | void *data ATTRIBUTE_UNUSED) | |
759 | { | |
760 | tree node; | |
4a8fb1a1 | 761 | tree_node **slot; |
6de9cd9a DN |
762 | |
763 | node = *tp; | |
764 | ||
765 | /* 'node' is the parent of 'TREE_OPERAND (node, *)'. */ | |
5039610b | 766 | if (EXPR_P (node)) |
6de9cd9a | 767 | { |
5039610b SL |
768 | int n = TREE_OPERAND_LENGTH (node); |
769 | int i; | |
770 | for (i = 0; i < n; i++) | |
6de9cd9a | 771 | { |
5039610b | 772 | tree op = TREE_OPERAND (node, i); |
c203e8a7 | 773 | slot = TB_up_ht->find_slot (op, INSERT); |
4a8fb1a1 | 774 | *slot = node; |
6de9cd9a | 775 | } |
6de9cd9a DN |
776 | } |
777 | ||
778 | /* Never stop walk_tree. */ | |
779 | return NULL_TREE; | |
780 | } | |
781 | ||
6de9cd9a DN |
782 | /* Update information about upper expressions in the hash table. */ |
783 | ||
784 | static void | |
785 | TB_update_up (tree node) | |
786 | { | |
787 | while (node) | |
788 | { | |
789 | walk_tree (&node, store_child_info, NULL, NULL); | |
790 | ||
791 | /* Walk function's body. */ | |
792 | if (TREE_CODE (node) == FUNCTION_DECL) | |
793 | if (DECL_SAVED_TREE (node)) | |
794 | walk_tree (&DECL_SAVED_TREE (node), store_child_info, NULL, NULL); | |
795 | ||
796 | /* Walk rest of the chain. */ | |
797 | node = TREE_CHAIN (node); | |
798 | } | |
799 | fprintf (TB_OUT_FILE, "Up/prev expressions updated.\n"); | |
800 | } | |
801 | ||
802 | /* Parse the input string for determining the command the user asked for. */ | |
803 | ||
804 | static TB_CODE | |
805 | TB_get_command (char *input) | |
806 | { | |
807 | unsigned int mn, size_tok; | |
808 | int comp; | |
809 | char *space; | |
810 | ||
811 | space = strchr (input, ' '); | |
812 | if (space != NULL) | |
813 | size_tok = strlen (input) - strlen (space); | |
814 | else | |
815 | size_tok = strlen (input) - 1; | |
816 | ||
817 | for (mn = 0; mn < TB_UNUSED_COMMAND; mn++) | |
818 | { | |
819 | if (size_tok != TB_COMMAND_LEN (mn)) | |
820 | continue; | |
821 | ||
822 | comp = memcmp (input, TB_COMMAND_TEXT (mn), TB_COMMAND_LEN (mn)); | |
823 | if (comp == 0) | |
824 | /* Here we just determined the command. If this command takes | |
825 | an argument, then the argument is determined later. */ | |
826 | return TB_COMMAND_CODE (mn); | |
827 | } | |
828 | ||
829 | /* Not a valid command. */ | |
830 | return TB_UNUSED_COMMAND; | |
831 | } | |
832 | ||
833 | /* Parse the input string for determining the tree code. */ | |
834 | ||
835 | static enum tree_code | |
836 | TB_get_tree_code (char *input) | |
837 | { | |
838 | unsigned int mn, size_tok; | |
839 | int comp; | |
840 | char *space; | |
841 | ||
842 | space = strchr (input, ' '); | |
843 | if (space != NULL) | |
844 | size_tok = strlen (input) - strlen (space); | |
845 | else | |
846 | size_tok = strlen (input) - 1; | |
847 | ||
848 | for (mn = 0; mn < LAST_AND_UNUSED_TREE_CODE; mn++) | |
849 | { | |
850 | if (size_tok != TB_TREE_CODE_LEN (mn)) | |
851 | continue; | |
852 | ||
853 | comp = memcmp (input, TB_TREE_CODE_TEXT (mn), TB_TREE_CODE_LEN (mn)); | |
854 | if (comp == 0) | |
855 | { | |
856 | fprintf (TB_OUT_FILE, "%s\n", TB_TREE_CODE_TEXT (mn)); | |
857 | return TB_TREE_CODE (mn); | |
858 | } | |
859 | } | |
860 | ||
861 | /* This isn't a valid code. */ | |
862 | return LAST_AND_UNUSED_TREE_CODE; | |
863 | } | |
864 | ||
865 | /* Find a node with a given code. This function is used as an argument to | |
866 | walk_tree. */ | |
867 | ||
868 | static tree | |
869 | find_node_with_code (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, | |
870 | void *data) | |
871 | { | |
872 | enum tree_code *code; | |
873 | code = (enum tree_code *) data; | |
874 | if (*code == TREE_CODE (*tp)) | |
875 | return *tp; | |
876 | ||
877 | return NULL_TREE; | |
878 | } | |
879 | ||
880 | /* Returns a pointer to the last visited node. */ | |
881 | ||
882 | static tree | |
883 | TB_history_prev (void) | |
884 | { | |
9771b263 | 885 | if (!vec_safe_is_empty (TB_history_stack)) |
6de9cd9a | 886 | { |
9771b263 DN |
887 | tree last = TB_history_stack->last (); |
888 | TB_history_stack->pop (); | |
40523581 | 889 | return last; |
6de9cd9a DN |
890 | } |
891 | return NULL_TREE; | |
892 | } | |
893 | ||
894 | /* Read up to (and including) a '\n' from STREAM into *LINEPTR | |
895 | (and null-terminate it). *LINEPTR is a pointer returned from malloc | |
896 | (or NULL), pointing to *N characters of space. It is realloc'd as | |
897 | necessary. Returns the number of characters read (not including the | |
898 | null terminator), or -1 on error or EOF. | |
899 | This function comes from sed (and is supposed to be a portable version | |
900 | of getline). */ | |
901 | ||
902 | static long | |
903 | TB_getline (char **lineptr, long *n, FILE *stream) | |
904 | { | |
905 | char *line, *p; | |
906 | long size, copy; | |
907 | ||
908 | if (lineptr == NULL || n == NULL) | |
909 | { | |
910 | errno = EINVAL; | |
911 | return -1; | |
912 | } | |
913 | ||
914 | if (ferror (stream)) | |
915 | return -1; | |
916 | ||
917 | /* Make sure we have a line buffer to start with. */ | |
918 | if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */ | |
919 | { | |
920 | #ifndef MAX_CANON | |
921 | #define MAX_CANON 256 | |
922 | #endif | |
923 | line = (char *) xrealloc (*lineptr, MAX_CANON); | |
924 | if (line == NULL) | |
925 | return -1; | |
926 | *lineptr = line; | |
927 | *n = MAX_CANON; | |
928 | } | |
929 | ||
930 | line = *lineptr; | |
931 | size = *n; | |
932 | ||
933 | copy = size; | |
934 | p = line; | |
935 | ||
936 | while (1) | |
937 | { | |
938 | long len; | |
939 | ||
940 | while (--copy > 0) | |
941 | { | |
942 | register int c = getc (stream); | |
943 | if (c == EOF) | |
944 | goto lose; | |
945 | else if ((*p++ = c) == '\n') | |
946 | goto win; | |
947 | } | |
948 | ||
949 | /* Need to enlarge the line buffer. */ | |
950 | len = p - line; | |
951 | size *= 2; | |
952 | line = (char *) xrealloc (line, size); | |
953 | if (line == NULL) | |
954 | goto lose; | |
955 | *lineptr = line; | |
956 | *n = size; | |
957 | p = line + len; | |
958 | copy = size - len; | |
959 | } | |
960 | ||
961 | lose: | |
962 | if (p == *lineptr) | |
963 | return -1; | |
964 | ||
965 | /* Return a partial line since we got an error in the middle. */ | |
966 | win: | |
967 | #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) | |
968 | if (p - 2 >= *lineptr && p[-2] == '\r') | |
969 | p[-2] = p[-1], --p; | |
970 | #endif | |
971 | *p = '\0'; | |
972 | return p - *lineptr; | |
973 | } |