1 /* This file contains the definitions and documentation for the common
2 tree codes used in the GNU C and C++ compilers (see c-common.def
3 for the standard codes).
4 Copyright (C) 2000 Free Software Foundation, Inc. Written by
5 Benjamin Chelf (chelf@codesourcery.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
28 #include "splay-tree.h"
39 /* If non-NULL, the address of a language-specific function for
40 expanding statements. */
41 void (*lang_expand_stmt) PARAMS ((tree));
43 static tree prune_unused_decls PARAMS ((tree *, int *, void *));
45 /* Create an empty statement tree rooted at T. */
51 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
52 what follows. We remove the extraneous statement in
54 *t = build_nt (EXPR_STMT, void_zero_node);
56 last_expr_type = NULL_TREE;
59 /* T is a statement. Add it to the statement-tree. */
65 /* Add T to the statement-tree. */
66 TREE_CHAIN (last_tree) = t;
68 /* When we expand a statement-tree, we must know whether or not the
69 statements are full-expresions. We record that fact here. */
70 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
73 /* Remove declarations of internal variables that are not used from a
74 stmt tree. To qualify, the variable must have a name and must have
75 a zero DECL_SOURCE_LINE. We tried to remove all variables for
76 which TREE_USED was false, but it turns out that there's tons of
77 variables for which TREE_USED is false but that are still in fact
81 prune_unused_decls (tp, walk_subtrees, data)
83 int *walk_subtrees ATTRIBUTE_UNUSED;
84 void *data ATTRIBUTE_UNUSED;
94 if (TREE_CODE (t) == DECL_STMT)
96 tree d = DECL_STMT_DECL (t);
97 if (!TREE_USED (d) && DECL_NAME (d) && DECL_SOURCE_LINE (d) == 0)
100 /* Recurse on the new value of tp, otherwise we will skip
101 the next statement. */
102 return prune_unused_decls (tp, walk_subtrees, data);
105 else if (TREE_CODE (t) == SCOPE_STMT)
107 /* Remove all unused decls from the BLOCK of this SCOPE_STMT. */
108 tree block = SCOPE_STMT_BLOCK (t);
114 for (vp = &BLOCK_VARS (block); *vp; )
117 if (! TREE_USED (v) && DECL_NAME (v) && DECL_SOURCE_LINE (v) == 0)
118 *vp = TREE_CHAIN (v); /* drop */
120 vp = &TREE_CHAIN (v); /* advance */
122 /* If there are now no variables, the entire BLOCK can be dropped.
123 (This causes SCOPE_NULLIFIED_P (t) to be true.) */
124 if (BLOCK_VARS (block) == NULL_TREE)
125 SCOPE_STMT_BLOCK (t) = NULL_TREE;
131 /* Finish the statement tree rooted at T. */
139 /* Remove the fake extra statement added in begin_stmt_tree. */
140 stmt = TREE_CHAIN (*t);
142 last_tree = NULL_TREE;
144 /* Remove unused decls from the stmt tree. */
145 walk_stmt_tree (t, prune_unused_decls, NULL);
149 /* The line-number recorded in the outermost statement in a function
150 is the line number of the end of the function. */
151 STMT_LINENO (stmt) = lineno;
152 STMT_LINENO_FOR_FN_P (stmt) = 1;
156 /* Build a generic statement based on the given type of node and
157 arguments. Similar to `build_nt', except that we set
158 TREE_COMPLEXITY to be the current line number. */
161 build_stmt VPARAMS ((enum tree_code code, ...))
163 #ifndef ANSI_PROTOTYPES
173 #ifndef ANSI_PROTOTYPES
174 code = va_arg (p, enum tree_code);
177 t = make_node (code);
178 length = TREE_CODE_LENGTH (code);
179 TREE_COMPLEXITY (t) = lineno;
181 for (i = 0; i < length; i++)
182 TREE_OPERAND (t, i) = va_arg (p, tree);
188 /* Some statements, like for-statements or if-statements, require a
189 condition. This condition can be a declaration. If T is such a
190 declaration it is processed, and an expression appropriate to use
191 as the condition is returned. Otherwise, T itself is returned. */
197 if (t && TREE_CODE (t) == TREE_LIST)
199 expand_stmt (TREE_PURPOSE (t));
200 return TREE_VALUE (t);
206 /* Create RTL for the local static variable DECL. */
209 make_rtl_for_local_static (decl)
212 const char *asmspec = NULL;
214 /* If we inlined this variable, we could see it's declaration
216 if (TREE_ASM_WRITTEN (decl))
219 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
221 /* The only way this situaton can occur is if the
222 user specified a name for this DECL using the
223 `attribute' syntax. */
224 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
225 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
228 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
231 /* Let the back-end know about DECL. */
234 emit_local_var (decl)
237 /* Create RTL for this variable. */
238 if (!DECL_RTL (decl))
240 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
241 /* The user must have specified an assembler name for this
242 variable. Set that up now. */
243 rest_of_decl_compilation
244 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
245 /*top_level=*/0, /*at_end=*/0);
250 /* Actually do the initialization. */
251 if (stmts_are_full_exprs_p ())
252 expand_start_target_temps ();
254 expand_decl_init (decl);
256 if (stmts_are_full_exprs_p ())
257 expand_end_target_temps ();
260 /* Helper for generating the RTL at the beginning of a scope. */
263 genrtl_do_pushlevel ()
265 emit_line_note (input_filename, lineno);
269 /* Helper for generating the RTL. */
272 genrtl_clear_out_block ()
274 /* If COND wasn't a declaration, clear out the
275 block we made for it and start a new one here so the
276 optimization in expand_end_loop will work. */
277 if (getdecls () == NULL_TREE)
278 genrtl_do_pushlevel ();
281 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
284 genrtl_goto_stmt (destination)
287 if (TREE_CODE (destination) == IDENTIFIER_NODE)
290 /* We warn about unused labels with -Wunused. That means we have to
291 mark the used labels as used. */
292 if (TREE_CODE (destination) == LABEL_DECL)
293 TREE_USED (destination) = 1;
295 emit_line_note (input_filename, lineno);
297 if (TREE_CODE (destination) == LABEL_DECL)
299 label_rtx (destination);
300 expand_goto (destination);
303 expand_computed_goto (destination);
306 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
309 genrtl_expr_stmt (expr)
312 if (expr != NULL_TREE)
314 emit_line_note (input_filename, lineno);
316 if (stmts_are_full_exprs_p ())
317 expand_start_target_temps ();
319 lang_expand_expr_stmt (expr);
321 if (stmts_are_full_exprs_p ())
322 expand_end_target_temps ();
326 /* Generate the RTL for T, which is a DECL_STMT. */
333 emit_line_note (input_filename, lineno);
334 decl = DECL_STMT_DECL (t);
335 /* If this is a declaration for an automatic local
336 variable, initialize it. Note that we might also see a
337 declaration for a namespace-scope object (declared with
338 `extern'). We don't have to handle the initialization
339 of those objects here; they can only be declarations,
340 rather than definitions. */
341 if (TREE_CODE (decl) == VAR_DECL
342 && !TREE_STATIC (decl)
343 && !DECL_EXTERNAL (decl))
345 /* Let the back-end know about this variable. */
346 if (!anon_aggr_type_p (TREE_TYPE (decl)))
347 emit_local_var (decl);
349 expand_anon_union_decl (decl, NULL_TREE,
350 DECL_ANON_UNION_ELEMS (decl));
352 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
354 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
355 /* Do not emit unused decls. This is not just an
356 optimization. We really do not want to emit
357 __PRETTY_FUNCTION__ etc, if they're never used. */
358 DECL_IGNORED_P (decl) = 1;
360 make_rtl_for_local_static (decl);
364 /* Generate the RTL for T, which is an IF_STMT. */
371 genrtl_do_pushlevel ();
372 cond = expand_cond (IF_COND (t));
373 emit_line_note (input_filename, lineno);
374 expand_start_cond (cond, 0);
376 expand_stmt (THEN_CLAUSE (t));
379 expand_start_else ();
380 expand_stmt (ELSE_CLAUSE (t));
385 /* Generate the RTL for T, which is a WHILE_STMT. */
388 genrtl_while_stmt (t)
393 emit_line_note (input_filename, lineno);
394 expand_start_loop (1);
395 genrtl_do_pushlevel ();
397 cond = expand_cond (WHILE_COND (t));
398 emit_line_note (input_filename, lineno);
399 expand_exit_loop_if_false (0, cond);
400 genrtl_clear_out_block ();
402 expand_stmt (WHILE_BODY (t));
407 /* Generate the RTL for T, which is a DO_STMT. */
415 emit_line_note (input_filename, lineno);
416 expand_start_loop_continue_elsewhere (1);
418 expand_stmt (DO_BODY (t));
420 expand_loop_continue_here ();
422 cond = expand_cond (DO_COND (t));
423 emit_line_note (input_filename, lineno);
424 expand_exit_loop_if_false (0, cond);
428 /* Build the node for a return statement and return it. */
431 build_return_stmt (expr)
434 return (build_stmt (RETURN_STMT, expr));
437 /* Generate the RTL for EXPR, which is a RETURN_STMT. */
440 genrtl_return_stmt (expr)
443 emit_line_note (input_filename, lineno);
444 c_expand_return (expr);
447 /* Generate the RTL for T, which is a FOR_STMT. */
455 if (NEW_FOR_SCOPE_P (t))
456 genrtl_do_pushlevel ();
458 expand_stmt (FOR_INIT_STMT (t));
461 emit_line_note (input_filename, lineno);
462 expand_start_loop_continue_elsewhere (1);
463 genrtl_do_pushlevel ();
464 cond = expand_cond (FOR_COND (t));
465 emit_line_note (input_filename, lineno);
467 expand_exit_loop_if_false (0, cond);
468 genrtl_clear_out_block ();
471 expand_stmt (FOR_BODY (t));
473 emit_line_note (input_filename, lineno);
474 expand_loop_continue_here ();
476 genrtl_expr_stmt (tmp);
480 /* Build a break statement node and return it. */
485 return (build_stmt (BREAK_STMT));
488 /* Generate the RTL for a BREAK_STMT. */
493 emit_line_note (input_filename, lineno);
494 if ( ! expand_exit_something ())
495 error ("break statement not within loop or switch");
498 /* Build a continue statement node and return it. */
501 build_continue_stmt ()
503 return (build_stmt (CONTINUE_STMT));
506 /* Generate the RTL for a CONTINUE_STMT. */
509 genrtl_continue_stmt ()
511 emit_line_note (input_filename, lineno);
512 if (! expand_continue_loop (0))
513 error ("continue statement not within a loop");
516 /* Generate the RTL for T, which is a SCOPE_STMT. */
519 genrtl_scope_stmt (t)
522 if (!SCOPE_NO_CLEANUPS_P (t))
524 if (SCOPE_BEGIN_P (t))
525 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
526 SCOPE_STMT_BLOCK (t));
527 else if (SCOPE_END_P (t))
528 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
530 else if (!SCOPE_NULLIFIED_P (t))
532 rtx note = emit_note (NULL,
534 ? NOTE_INSN_BLOCK_BEG
535 : NOTE_INSN_BLOCK_END));
536 NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
540 /* Generate the RTL for T, which is a SWITCH_STMT. */
543 genrtl_switch_stmt (t)
547 genrtl_do_pushlevel ();
549 cond = expand_cond (SWITCH_COND (t));
550 if (cond != error_mark_node)
552 emit_line_note (input_filename, lineno);
553 c_expand_start_case (cond);
556 /* The code is in error, but we don't want expand_end_case to
558 c_expand_start_case (boolean_false_node);
560 expand_stmt (SWITCH_BODY (t));
562 expand_end_case (cond);
565 /* Create a CASE_LABEL tree node and return it. */
568 build_case_label (low_value, high_value)
572 return build_stmt (CASE_LABEL, low_value, high_value);
576 /* Generate the RTL for a CASE_LABEL. */
579 genrtl_case_label (low_value, high_value)
583 do_case (low_value, high_value);
586 /* Generate the RTL for T, which is a COMPOUND_STMT. */
589 genrtl_compound_stmt (t)
592 /* If this is the outermost block of the function, declare the
593 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
595 && !current_function_name_declared ()
596 && !COMPOUND_STMT_NO_SCOPE (t))
598 set_current_function_name_declared (1);
599 declare_function_name ();
602 expand_stmt (COMPOUND_BODY (t));
605 /* Generate the RTL for an ASM_STMT. */
608 genrtl_asm_stmt (cv_qualifier, string, output_operands,
609 input_operands, clobbers)
612 tree output_operands;
616 if (TREE_CHAIN (string))
617 string = combine_strings (string);
619 if (cv_qualifier != NULL_TREE
620 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
622 warning ("%s qualifier ignored on asm",
623 IDENTIFIER_POINTER (cv_qualifier));
624 cv_qualifier = NULL_TREE;
627 emit_line_note (input_filename, lineno);
628 if (output_operands != NULL_TREE || input_operands != NULL_TREE
629 || clobbers != NULL_TREE)
630 c_expand_asm_operands (string, output_operands,
633 cv_qualifier != NULL_TREE,
634 input_filename, lineno);
639 /* Generate the RTL for a DECL_CLEANUP. */
642 genrtl_decl_cleanup (decl, cleanup)
646 if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
647 expand_decl_cleanup (decl, cleanup);
650 /* We're about to expand T, a statement. Set up appropriate context
651 for the substitution. */
657 if (!STMT_LINENO_FOR_FN_P (t))
658 lineno = STMT_LINENO (t);
659 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
662 /* Generate the RTL for the statement T, its substatements, and any
663 other statements at its nesting level. */
669 while (t && t != error_mark_node)
671 int saved_stmts_are_full_exprs_p;
673 /* Set up context appropriately for handling this statement. */
674 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
677 switch (TREE_CODE (t))
680 genrtl_return_stmt (RETURN_EXPR (t));
684 genrtl_expr_stmt (EXPR_STMT_EXPR (t));
688 genrtl_decl_stmt (t);
696 genrtl_while_stmt (t);
708 genrtl_compound_stmt (t);
712 genrtl_break_stmt ();
716 genrtl_continue_stmt ();
720 genrtl_switch_stmt (t);
724 genrtl_case_label (CASE_LOW (t), CASE_HIGH (t));
728 expand_label (LABEL_STMT_LABEL (t));
732 genrtl_goto_stmt (GOTO_DESTINATION (t));
736 genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
737 ASM_OUTPUTS (t), ASM_INPUTS (t), ASM_CLOBBERS (t));
741 if (lang_expand_stmt)
742 (*lang_expand_stmt) (t);
748 /* Restore saved state. */
749 current_stmt_tree ()->stmts_are_full_exprs_p =
750 saved_stmts_are_full_exprs_p;
752 /* Go on to the next statement in this scope. */