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, 2001 Free Software Foundation, Inc.
5 Written by 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 /* If non-NULL, the address of a language-specific function for
44 expanding a DECL_STMT. After the language-independent cases are
45 handled, this function will be called. If this function is not
46 defined, it is assumed that declarations other than those for
47 variables and labels do not require any RTL generation. */
48 void (*lang_expand_decl_stmt
) PARAMS ((tree
));
50 static tree prune_unused_decls
PARAMS ((tree
*, int *, void *));
52 /* Create an empty statement tree rooted at T. */
58 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
59 what follows. We remove the extraneous statement in
61 *t
= build_nt (EXPR_STMT
, void_zero_node
);
63 last_expr_type
= NULL_TREE
;
66 /* T is a statement. Add it to the statement-tree. */
72 /* Add T to the statement-tree. */
73 TREE_CHAIN (last_tree
) = t
;
75 /* When we expand a statement-tree, we must know whether or not the
76 statements are full-expresions. We record that fact here. */
77 STMT_IS_FULL_EXPR_P (last_tree
) = stmts_are_full_exprs_p ();
79 /* Keep track of the number of statements in this function. */
80 if (current_function_decl
)
81 ++DECL_NUM_STMTS (current_function_decl
);
86 /* Create a declaration statement for the declaration given by the
95 /* We need the type to last until instantiation time. */
96 decl_stmt
= build_stmt (DECL_STMT
, decl
);
100 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
101 whether this statements opens or closes a scope. PARTIAL_P is true
102 for a partial scope, i.e, the scope that begins after a label when
103 an object that needs a cleanup is created. If BEGIN_P is nonzero,
104 returns a new TREE_LIST representing the top of the SCOPE_STMT
105 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
106 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
107 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
108 SCOPE_BEGIN_P set. */
111 add_scope_stmt (begin_p
, partial_p
)
118 /* Build the statement. */
119 ss
= build_stmt (SCOPE_STMT
, NULL_TREE
);
120 SCOPE_BEGIN_P (ss
) = begin_p
;
121 SCOPE_PARTIAL_P (ss
) = partial_p
;
123 /* Keep the scope stack up to date. */
126 *current_scope_stmt_stack ()
127 = tree_cons (ss
, NULL_TREE
, *current_scope_stmt_stack ());
128 top
= *current_scope_stmt_stack ();
132 top
= *current_scope_stmt_stack ();
133 TREE_VALUE (top
) = ss
;
134 *current_scope_stmt_stack () = TREE_CHAIN (top
);
137 /* Add the new statement to the statement-tree. */
143 /* Remove declarations of internal variables that are not used from a
144 stmt tree. To qualify, the variable must have a name and must have
145 a zero DECL_SOURCE_LINE. We tried to remove all variables for
146 which TREE_USED was false, but it turns out that there's tons of
147 variables for which TREE_USED is false but that are still in fact
151 prune_unused_decls (tp
, walk_subtrees
, data
)
153 int *walk_subtrees ATTRIBUTE_UNUSED
;
154 void *data ATTRIBUTE_UNUSED
;
164 if (TREE_CODE (t
) == DECL_STMT
)
166 tree d
= DECL_STMT_DECL (t
);
167 if (!TREE_USED (d
) && DECL_NAME (d
) && DECL_SOURCE_LINE (d
) == 0)
169 *tp
= TREE_CHAIN (t
);
170 /* Recurse on the new value of tp, otherwise we will skip
171 the next statement. */
172 return prune_unused_decls (tp
, walk_subtrees
, data
);
175 else if (TREE_CODE (t
) == SCOPE_STMT
)
177 /* Remove all unused decls from the BLOCK of this SCOPE_STMT. */
178 tree block
= SCOPE_STMT_BLOCK (t
);
184 for (vp
= &BLOCK_VARS (block
); *vp
; )
187 if (! TREE_USED (v
) && DECL_NAME (v
) && DECL_SOURCE_LINE (v
) == 0)
188 *vp
= TREE_CHAIN (v
); /* drop */
190 vp
= &TREE_CHAIN (v
); /* advance */
192 /* If there are now no variables, the entire BLOCK can be dropped.
193 (This causes SCOPE_NULLIFIED_P (t) to be true.) */
194 if (BLOCK_VARS (block
) == NULL_TREE
)
195 SCOPE_STMT_BLOCK (t
) = NULL_TREE
;
201 /* Finish the statement tree rooted at T. */
209 /* Remove the fake extra statement added in begin_stmt_tree. */
210 stmt
= TREE_CHAIN (*t
);
212 last_tree
= NULL_TREE
;
214 /* Remove unused decls from the stmt tree. */
215 walk_stmt_tree (t
, prune_unused_decls
, NULL
);
219 /* The line-number recorded in the outermost statement in a function
220 is the line number of the end of the function. */
221 STMT_LINENO (stmt
) = lineno
;
222 STMT_LINENO_FOR_FN_P (stmt
) = 1;
226 /* Build a generic statement based on the given type of node and
227 arguments. Similar to `build_nt', except that we set
228 STMT_LINENO to be the current line number. */
229 /* ??? This should be obsolete with the lineno_stmt productions
233 build_stmt
VPARAMS ((enum tree_code code
, ...))
235 #ifndef ANSI_PROTOTYPES
245 #ifndef ANSI_PROTOTYPES
246 code
= va_arg (p
, enum tree_code
);
249 t
= make_node (code
);
250 length
= TREE_CODE_LENGTH (code
);
251 STMT_LINENO (t
) = lineno
;
253 for (i
= 0; i
< length
; i
++)
254 TREE_OPERAND (t
, i
) = va_arg (p
, tree
);
260 /* Some statements, like for-statements or if-statements, require a
261 condition. This condition can be a declaration. If T is such a
262 declaration it is processed, and an expression appropriate to use
263 as the condition is returned. Otherwise, T itself is returned. */
269 if (t
&& TREE_CODE (t
) == TREE_LIST
)
271 expand_stmt (TREE_PURPOSE (t
));
272 return TREE_VALUE (t
);
278 /* Create RTL for the local static variable DECL. */
281 make_rtl_for_local_static (decl
)
284 const char *asmspec
= NULL
;
286 /* If we inlined this variable, we could see it's declaration
288 if (TREE_ASM_WRITTEN (decl
))
291 /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
292 either we already created RTL for this DECL (and since it was a
293 local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
294 clashes with other local statics with the same name by a previous
295 call to make_decl_rtl), or the user explicitly requested a
296 particular assembly name for this variable, using the GNU
297 extension for this purpose:
301 There's no way to know which case we're in, here. But, it turns
302 out we're safe. If there's already RTL, then
303 rest_of_decl_compilation ignores the ASMSPEC parameter, so we
304 may as well not pass it in. If there isn't RTL, then we didn't
305 already create RTL, which means that the modification to
306 DECL_ASSEMBLER_NAME came only via the explicit extension. */
307 if (DECL_ASSEMBLER_NAME (decl
) != DECL_NAME (decl
)
309 asmspec
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
311 rest_of_decl_compilation (decl
, asmspec
, /*top_level=*/0, /*at_end=*/0);
314 /* Let the back-end know about DECL. */
317 emit_local_var (decl
)
320 /* Create RTL for this variable. */
321 if (!DECL_RTL_SET_P (decl
))
323 if (DECL_C_HARD_REGISTER (decl
))
324 /* The user specified an assembler name for this variable.
326 rest_of_decl_compilation
327 (decl
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)),
328 /*top_level=*/0, /*at_end=*/0);
333 /* Actually do the initialization. */
334 if (stmts_are_full_exprs_p ())
335 expand_start_target_temps ();
337 expand_decl_init (decl
);
339 if (stmts_are_full_exprs_p ())
340 expand_end_target_temps ();
343 /* Helper for generating the RTL at the beginning of a scope. */
346 genrtl_do_pushlevel ()
348 emit_line_note (input_filename
, lineno
);
352 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
355 genrtl_goto_stmt (destination
)
358 if (TREE_CODE (destination
) == IDENTIFIER_NODE
)
361 /* We warn about unused labels with -Wunused. That means we have to
362 mark the used labels as used. */
363 if (TREE_CODE (destination
) == LABEL_DECL
)
364 TREE_USED (destination
) = 1;
366 emit_line_note (input_filename
, lineno
);
368 if (TREE_CODE (destination
) == LABEL_DECL
)
370 label_rtx (destination
);
371 expand_goto (destination
);
374 expand_computed_goto (destination
);
377 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
380 genrtl_expr_stmt (expr
)
383 if (expr
!= NULL_TREE
)
385 emit_line_note (input_filename
, lineno
);
387 if (stmts_are_full_exprs_p ())
388 expand_start_target_temps ();
390 if (expr
!= error_mark_node
)
391 expand_expr_stmt (expr
);
393 if (stmts_are_full_exprs_p ())
394 expand_end_target_temps ();
398 /* Generate the RTL for T, which is a DECL_STMT. */
405 emit_line_note (input_filename
, lineno
);
406 decl
= DECL_STMT_DECL (t
);
407 /* If this is a declaration for an automatic local
408 variable, initialize it. Note that we might also see a
409 declaration for a namespace-scope object (declared with
410 `extern'). We don't have to handle the initialization
411 of those objects here; they can only be declarations,
412 rather than definitions. */
413 if (TREE_CODE (decl
) == VAR_DECL
414 && !TREE_STATIC (decl
)
415 && !DECL_EXTERNAL (decl
))
417 /* Let the back-end know about this variable. */
418 if (!anon_aggr_type_p (TREE_TYPE (decl
)))
419 emit_local_var (decl
);
421 expand_anon_union_decl (decl
, NULL_TREE
,
422 DECL_ANON_UNION_ELEMS (decl
));
424 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_STATIC (decl
))
426 if (DECL_ARTIFICIAL (decl
) && ! TREE_USED (decl
))
427 /* Do not emit unused decls. This is not just an
428 optimization. We really do not want to emit
429 __PRETTY_FUNCTION__ etc, if they're never used. */
430 DECL_IGNORED_P (decl
) = 1;
432 make_rtl_for_local_static (decl
);
434 else if (TREE_CODE (decl
) == LABEL_DECL
435 && C_DECLARED_LABEL_FLAG (decl
))
436 declare_nonlocal_label (decl
);
437 else if (lang_expand_decl_stmt
)
438 (*lang_expand_decl_stmt
) (t
);
441 /* Generate the RTL for T, which is an IF_STMT. */
448 genrtl_do_pushlevel ();
449 cond
= expand_cond (IF_COND (t
));
450 emit_line_note (input_filename
, lineno
);
451 expand_start_cond (cond
, 0);
453 expand_stmt (THEN_CLAUSE (t
));
456 expand_start_else ();
457 expand_stmt (ELSE_CLAUSE (t
));
462 /* Generate the RTL for T, which is a WHILE_STMT. */
465 genrtl_while_stmt (t
)
470 emit_line_note (input_filename
, lineno
);
471 expand_start_loop (1);
472 genrtl_do_pushlevel ();
474 cond
= expand_cond (WHILE_COND (t
));
475 emit_line_note (input_filename
, lineno
);
476 expand_exit_loop_if_false (0, cond
);
477 genrtl_do_pushlevel ();
479 expand_stmt (WHILE_BODY (t
));
484 /* Generate the RTL for T, which is a DO_STMT. */
490 tree cond
= DO_COND (t
);
492 /* Recognize the common special-case of do { ... } while (0) and do
493 not emit the loop widgetry in this case. In particular this
494 avoids cluttering the rtl with dummy loop notes, which can affect
495 alignment of adjacent labels. */
496 if (integer_zerop (cond
))
498 expand_start_null_loop ();
499 expand_stmt (DO_BODY (t
));
500 expand_end_null_loop ();
505 emit_line_note (input_filename
, lineno
);
506 expand_start_loop_continue_elsewhere (1);
508 expand_stmt (DO_BODY (t
));
510 expand_loop_continue_here ();
511 cond
= expand_cond (cond
);
512 emit_line_note (input_filename
, lineno
);
513 expand_exit_loop_if_false (0, cond
);
518 /* Build the node for a return statement and return it. */
521 build_return_stmt (expr
)
524 return (build_stmt (RETURN_STMT
, expr
));
527 /* Generate the RTL for STMT, which is a RETURN_STMT. */
530 genrtl_return_stmt (stmt
)
533 tree expr
= RETURN_EXPR (stmt
);
535 emit_line_note (input_filename
, lineno
);
537 expand_null_return ();
540 expand_start_target_temps ();
541 expand_return (expr
);
542 expand_end_target_temps ();
546 /* Generate the RTL for T, which is a FOR_STMT. */
553 const char *saved_filename
;
556 if (NEW_FOR_SCOPE_P (t
))
557 genrtl_do_pushlevel ();
559 expand_stmt (FOR_INIT_STMT (t
));
561 /* Expand the initialization. */
563 emit_line_note (input_filename
, lineno
);
564 expand_start_loop_continue_elsewhere (1);
565 genrtl_do_pushlevel ();
566 cond
= expand_cond (FOR_COND (t
));
568 /* Save the filename and line number so that we expand the FOR_EXPR
569 we can reset them back to the saved values. */
570 saved_filename
= input_filename
;
571 saved_lineno
= lineno
;
573 /* Expand the condition. */
574 emit_line_note (input_filename
, lineno
);
576 expand_exit_loop_if_false (0, cond
);
578 /* Expand the body. */
579 genrtl_do_pushlevel ();
580 expand_stmt (FOR_BODY (t
));
582 /* Expand the increment expression. */
583 input_filename
= saved_filename
;
584 lineno
= saved_lineno
;
585 emit_line_note (input_filename
, lineno
);
586 expand_loop_continue_here ();
588 genrtl_expr_stmt (FOR_EXPR (t
));
592 /* Build a break statement node and return it. */
597 return (build_stmt (BREAK_STMT
));
600 /* Generate the RTL for a BREAK_STMT. */
605 emit_line_note (input_filename
, lineno
);
606 if ( ! expand_exit_something ())
607 error ("break statement not within loop or switch");
610 /* Build a continue statement node and return it. */
613 build_continue_stmt ()
615 return (build_stmt (CONTINUE_STMT
));
618 /* Generate the RTL for a CONTINUE_STMT. */
621 genrtl_continue_stmt ()
623 emit_line_note (input_filename
, lineno
);
624 if (! expand_continue_loop (0))
625 error ("continue statement not within a loop");
628 /* Generate the RTL for T, which is a SCOPE_STMT. */
631 genrtl_scope_stmt (t
)
634 tree block
= SCOPE_STMT_BLOCK (t
);
636 if (!SCOPE_NO_CLEANUPS_P (t
))
638 if (SCOPE_BEGIN_P (t
))
639 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t
), block
);
640 else if (SCOPE_END_P (t
))
641 expand_end_bindings (NULL_TREE
, !SCOPE_NULLIFIED_P (t
), 0);
643 else if (!SCOPE_NULLIFIED_P (t
))
645 rtx note
= emit_note (NULL
,
647 ? NOTE_INSN_BLOCK_BEG
648 : NOTE_INSN_BLOCK_END
));
649 NOTE_BLOCK (note
) = block
;
652 /* If we're at the end of a scope that contains inlined nested
653 functions, we have to decide whether or not to write them out. */
654 if (block
&& SCOPE_END_P (t
))
658 for (fn
= BLOCK_VARS (block
); fn
; fn
= TREE_CHAIN (fn
))
660 if (TREE_CODE (fn
) == FUNCTION_DECL
661 && DECL_CONTEXT (fn
) == current_function_decl
662 && !TREE_ASM_WRITTEN (fn
)
663 && TREE_ADDRESSABLE (fn
))
665 push_function_context ();
666 output_inline_function (fn
);
667 pop_function_context ();
673 /* Generate the RTL for T, which is a SWITCH_STMT. */
676 genrtl_switch_stmt (t
)
680 genrtl_do_pushlevel ();
682 cond
= expand_cond (SWITCH_COND (t
));
683 if (cond
== error_mark_node
)
684 /* The code is in error, but we don't want expand_end_case to
686 cond
= boolean_false_node
;
688 emit_line_note (input_filename
, lineno
);
689 expand_start_case (1, cond
, TREE_TYPE (cond
), "switch statement");
690 expand_stmt (SWITCH_BODY (t
));
691 expand_end_case (cond
);
694 /* Create a CASE_LABEL tree node and return it. */
697 build_case_label (low_value
, high_value
, label_decl
)
702 return build_stmt (CASE_LABEL
, low_value
, high_value
, label_decl
);
706 /* Generate the RTL for a CASE_LABEL. */
709 genrtl_case_label (case_label
)
715 cleanup
= last_cleanup_this_contour ();
718 static int explained
= 0;
719 warning_with_decl (TREE_PURPOSE (cleanup
),
720 "destructor needed for `%#D'");
721 warning ("where case label appears here");
724 warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
729 add_case_node (CASE_LOW (case_label
), CASE_HIGH (case_label
),
730 CASE_LABEL_DECL (case_label
), &duplicate
);
733 /* Generate the RTL for T, which is a COMPOUND_STMT. */
736 genrtl_compound_stmt (t
)
739 expand_stmt (COMPOUND_BODY (t
));
742 /* Generate the RTL for an ASM_STMT. */
745 genrtl_asm_stmt (cv_qualifier
, string
, output_operands
,
746 input_operands
, clobbers
, asm_input_p
)
749 tree output_operands
;
754 if (cv_qualifier
!= NULL_TREE
755 && cv_qualifier
!= ridpointers
[(int) RID_VOLATILE
])
757 warning ("%s qualifier ignored on asm",
758 IDENTIFIER_POINTER (cv_qualifier
));
759 cv_qualifier
= NULL_TREE
;
762 emit_line_note (input_filename
, lineno
);
766 c_expand_asm_operands (string
, output_operands
, input_operands
,
767 clobbers
, cv_qualifier
!= NULL_TREE
,
768 input_filename
, lineno
);
771 /* Generate the RTL for a DECL_CLEANUP. */
774 genrtl_decl_cleanup (decl
, cleanup
)
778 if (!decl
|| (DECL_SIZE (decl
) && TREE_TYPE (decl
) != error_mark_node
))
779 expand_decl_cleanup (decl
, cleanup
);
782 /* We're about to expand T, a statement. Set up appropriate context
783 for the substitution. */
789 if (!STMT_LINENO_FOR_FN_P (t
))
790 lineno
= STMT_LINENO (t
);
791 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
794 /* Generate the RTL for the statement T, its substatements, and any
795 other statements at its nesting level. */
801 while (t
&& t
!= error_mark_node
)
803 int saved_stmts_are_full_exprs_p
;
805 /* Set up context appropriately for handling this statement. */
806 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
809 switch (TREE_CODE (t
))
812 genrtl_return_stmt (t
);
816 genrtl_expr_stmt (EXPR_STMT_EXPR (t
));
820 genrtl_decl_stmt (t
);
828 genrtl_while_stmt (t
);
840 genrtl_compound_stmt (t
);
844 genrtl_break_stmt ();
848 genrtl_continue_stmt ();
852 genrtl_switch_stmt (t
);
856 genrtl_case_label (t
);
860 expand_label (LABEL_STMT_LABEL (t
));
864 genrtl_goto_stmt (GOTO_DESTINATION (t
));
868 genrtl_asm_stmt (ASM_CV_QUAL (t
), ASM_STRING (t
),
869 ASM_OUTPUTS (t
), ASM_INPUTS (t
),
870 ASM_CLOBBERS (t
), ASM_INPUT_P (t
));
874 genrtl_scope_stmt (t
);
878 if (lang_expand_stmt
)
879 (*lang_expand_stmt
) (t
);
885 /* Restore saved state. */
886 current_stmt_tree ()->stmts_are_full_exprs_p
=
887 saved_stmts_are_full_exprs_p
;
889 /* Go on to the next statement in this scope. */