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 ();
81 /* Create a declaration statement for the declaration given by the
90 /* We need the type to last until instantiation time. */
91 decl_stmt
= build_stmt (DECL_STMT
, decl
);
95 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
96 whether this statements opens or closes a scope. PARTIAL_P is true
97 for a partial scope, i.e, the scope that begins after a label when
98 an object that needs a cleanup is created. If BEGIN_P is nonzero,
99 returns a new TREE_LIST representing the top of the SCOPE_STMT
100 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
101 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
102 and whose TREE_PURPOSE is the matching SCOPE_STMT iwth
103 SCOPE_BEGIN_P set. */
106 add_scope_stmt (begin_p
, partial_p
)
113 /* Build the statement. */
114 ss
= build_stmt (SCOPE_STMT
, NULL_TREE
);
115 SCOPE_BEGIN_P (ss
) = begin_p
;
116 SCOPE_PARTIAL_P (ss
) = partial_p
;
118 /* Keep the scope stack up to date. */
121 *current_scope_stmt_stack ()
122 = tree_cons (ss
, NULL_TREE
, *current_scope_stmt_stack ());
123 top
= *current_scope_stmt_stack ();
127 top
= *current_scope_stmt_stack ();
128 TREE_VALUE (top
) = ss
;
129 *current_scope_stmt_stack () = TREE_CHAIN (top
);
132 /* Add the new statement to the statement-tree. */
138 /* Remove declarations of internal variables that are not used from a
139 stmt tree. To qualify, the variable must have a name and must have
140 a zero DECL_SOURCE_LINE. We tried to remove all variables for
141 which TREE_USED was false, but it turns out that there's tons of
142 variables for which TREE_USED is false but that are still in fact
146 prune_unused_decls (tp
, walk_subtrees
, data
)
148 int *walk_subtrees ATTRIBUTE_UNUSED
;
149 void *data ATTRIBUTE_UNUSED
;
154 return error_mark_node
;
156 if (TREE_CODE (t
) == DECL_STMT
)
158 tree d
= DECL_STMT_DECL (t
);
159 if (!TREE_USED (d
) && DECL_NAME (d
) && DECL_SOURCE_LINE (d
) == 0)
161 *tp
= TREE_CHAIN (t
);
162 /* Recurse on the new value of tp, otherwise we will skip
163 the next statement. */
164 return prune_unused_decls (tp
, walk_subtrees
, data
);
167 else if (TREE_CODE (t
) == SCOPE_STMT
)
169 /* Remove all unused decls from the BLOCK of this SCOPE_STMT. */
170 tree block
= SCOPE_STMT_BLOCK (t
);
176 for (vp
= &BLOCK_VARS (block
); *vp
; )
179 if (! TREE_USED (v
) && DECL_NAME (v
) && DECL_SOURCE_LINE (v
) == 0)
180 *vp
= TREE_CHAIN (v
); /* drop */
182 vp
= &TREE_CHAIN (v
); /* advance */
184 /* If there are now no variables, the entire BLOCK can be dropped.
185 (This causes SCOPE_NULLIFIED_P (t) to be true.) */
186 if (BLOCK_VARS (block
) == NULL_TREE
)
187 SCOPE_STMT_BLOCK (t
) = NULL_TREE
;
193 /* Finish the statement tree rooted at T. */
201 /* Remove the fake extra statement added in begin_stmt_tree. */
202 stmt
= TREE_CHAIN (*t
);
204 last_tree
= NULL_TREE
;
206 /* Remove unused decls from the stmt tree. */
207 walk_stmt_tree (t
, prune_unused_decls
, NULL
);
211 /* The line-number recorded in the outermost statement in a function
212 is the line number of the end of the function. */
213 STMT_LINENO (stmt
) = lineno
;
214 STMT_LINENO_FOR_FN_P (stmt
) = 1;
218 /* Build a generic statement based on the given type of node and
219 arguments. Similar to `build_nt', except that we set
220 STMT_LINENO to be the current line number. */
221 /* ??? This should be obsolete with the lineno_stmt productions
225 build_stmt
VPARAMS ((enum tree_code code
, ...))
227 #ifndef ANSI_PROTOTYPES
237 #ifndef ANSI_PROTOTYPES
238 code
= va_arg (p
, enum tree_code
);
241 t
= make_node (code
);
242 length
= TREE_CODE_LENGTH (code
);
243 STMT_LINENO (t
) = lineno
;
245 for (i
= 0; i
< length
; i
++)
246 TREE_OPERAND (t
, i
) = va_arg (p
, tree
);
252 /* Some statements, like for-statements or if-statements, require a
253 condition. This condition can be a declaration. If T is such a
254 declaration it is processed, and an expression appropriate to use
255 as the condition is returned. Otherwise, T itself is returned. */
261 if (t
&& TREE_CODE (t
) == TREE_LIST
)
263 expand_stmt (TREE_PURPOSE (t
));
264 return TREE_VALUE (t
);
270 /* Create RTL for the local static variable DECL. */
273 make_rtl_for_local_static (decl
)
276 const char *asmspec
= NULL
;
278 /* If we inlined this variable, we could see it's declaration
280 if (TREE_ASM_WRITTEN (decl
))
283 /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
284 either we already created RTL for this DECL (and since it was a
285 local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
286 clashes with other local statics with the same name by a previous
287 call to make_decl_rtl), or the user explicitly requested a
288 particular assembly name for this variable, using the GNU
289 extension for this purpose:
293 There's no way to know which case we're in, here. But, it turns
294 out we're safe. If there's already RTL, then
295 rest_of_decl_compilation ignores the ASMSPEC parameter, so we
296 may as well not pass it in. If there isn't RTL, then we didn't
297 already create RTL, which means that the modification to
298 DECL_ASSEMBLER_NAME came only via the explicit extension. */
299 if (DECL_ASSEMBLER_NAME (decl
) != DECL_NAME (decl
)
301 asmspec
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
303 rest_of_decl_compilation (decl
, asmspec
, /*top_level=*/0, /*at_end=*/0);
306 /* Let the back-end know about DECL. */
309 emit_local_var (decl
)
312 /* Create RTL for this variable. */
313 if (!DECL_RTL (decl
))
315 if (DECL_C_HARD_REGISTER (decl
))
316 /* The user specified an assembler name for this variable.
318 rest_of_decl_compilation
319 (decl
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)),
320 /*top_level=*/0, /*at_end=*/0);
325 /* Actually do the initialization. */
326 if (stmts_are_full_exprs_p ())
327 expand_start_target_temps ();
329 expand_decl_init (decl
);
331 if (stmts_are_full_exprs_p ())
332 expand_end_target_temps ();
335 /* Helper for generating the RTL at the beginning of a scope. */
338 genrtl_do_pushlevel ()
340 emit_line_note (input_filename
, lineno
);
344 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
347 genrtl_goto_stmt (destination
)
350 if (TREE_CODE (destination
) == IDENTIFIER_NODE
)
353 /* We warn about unused labels with -Wunused. That means we have to
354 mark the used labels as used. */
355 if (TREE_CODE (destination
) == LABEL_DECL
)
356 TREE_USED (destination
) = 1;
358 emit_line_note (input_filename
, lineno
);
360 if (TREE_CODE (destination
) == LABEL_DECL
)
362 label_rtx (destination
);
363 expand_goto (destination
);
366 expand_computed_goto (destination
);
369 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
372 genrtl_expr_stmt (expr
)
375 if (expr
!= NULL_TREE
)
377 emit_line_note (input_filename
, lineno
);
379 if (stmts_are_full_exprs_p ())
380 expand_start_target_temps ();
382 if (expr
!= error_mark_node
)
383 expand_expr_stmt (expr
);
385 if (stmts_are_full_exprs_p ())
386 expand_end_target_temps ();
390 /* Generate the RTL for T, which is a DECL_STMT. */
397 emit_line_note (input_filename
, lineno
);
398 decl
= DECL_STMT_DECL (t
);
399 /* If this is a declaration for an automatic local
400 variable, initialize it. Note that we might also see a
401 declaration for a namespace-scope object (declared with
402 `extern'). We don't have to handle the initialization
403 of those objects here; they can only be declarations,
404 rather than definitions. */
405 if (TREE_CODE (decl
) == VAR_DECL
406 && !TREE_STATIC (decl
)
407 && !DECL_EXTERNAL (decl
))
409 /* Let the back-end know about this variable. */
410 if (!anon_aggr_type_p (TREE_TYPE (decl
)))
411 emit_local_var (decl
);
413 expand_anon_union_decl (decl
, NULL_TREE
,
414 DECL_ANON_UNION_ELEMS (decl
));
416 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_STATIC (decl
))
418 if (DECL_ARTIFICIAL (decl
) && ! TREE_USED (decl
))
419 /* Do not emit unused decls. This is not just an
420 optimization. We really do not want to emit
421 __PRETTY_FUNCTION__ etc, if they're never used. */
422 DECL_IGNORED_P (decl
) = 1;
424 make_rtl_for_local_static (decl
);
426 else if (TREE_CODE (decl
) == LABEL_DECL
427 && C_DECLARED_LABEL_FLAG (decl
))
428 declare_nonlocal_label (decl
);
429 else if (lang_expand_decl_stmt
)
430 (*lang_expand_decl_stmt
) (t
);
433 /* Generate the RTL for T, which is an IF_STMT. */
440 genrtl_do_pushlevel ();
441 cond
= expand_cond (IF_COND (t
));
442 emit_line_note (input_filename
, lineno
);
443 expand_start_cond (cond
, 0);
445 expand_stmt (THEN_CLAUSE (t
));
448 expand_start_else ();
449 expand_stmt (ELSE_CLAUSE (t
));
454 /* Generate the RTL for T, which is a WHILE_STMT. */
457 genrtl_while_stmt (t
)
462 emit_line_note (input_filename
, lineno
);
463 expand_start_loop (1);
464 genrtl_do_pushlevel ();
466 cond
= expand_cond (WHILE_COND (t
));
467 emit_line_note (input_filename
, lineno
);
468 expand_exit_loop_if_false (0, cond
);
469 genrtl_do_pushlevel ();
471 expand_stmt (WHILE_BODY (t
));
476 /* Generate the RTL for T, which is a DO_STMT. */
482 tree cond
= DO_COND (t
);
484 /* Recognize the common special-case of do { ... } while (0) and do
485 not emit the loop widgetry in this case. In particular this
486 avoids cluttering the rtl with dummy loop notes, which can affect
487 alignment of adjacent labels. */
488 if (integer_zerop (cond
))
490 expand_start_null_loop ();
491 expand_stmt (DO_BODY (t
));
492 expand_end_null_loop ();
497 emit_line_note (input_filename
, lineno
);
498 expand_start_loop_continue_elsewhere (1);
500 expand_stmt (DO_BODY (t
));
502 expand_loop_continue_here ();
503 cond
= expand_cond (cond
);
504 emit_line_note (input_filename
, lineno
);
505 expand_exit_loop_if_false (0, cond
);
510 /* Build the node for a return statement and return it. */
513 build_return_stmt (expr
)
516 return (build_stmt (RETURN_STMT
, expr
));
519 /* Generate the RTL for STMT, which is a RETURN_STMT. */
522 genrtl_return_stmt (stmt
)
525 tree expr
= RETURN_EXPR (stmt
);
527 emit_line_note (input_filename
, lineno
);
529 expand_null_return ();
532 expand_start_target_temps ();
533 expand_return (expr
);
534 expand_end_target_temps ();
538 /* Generate the RTL for T, which is a FOR_STMT. */
545 const char *saved_filename
;
548 if (NEW_FOR_SCOPE_P (t
))
549 genrtl_do_pushlevel ();
551 expand_stmt (FOR_INIT_STMT (t
));
553 /* Expand the initialization. */
555 emit_line_note (input_filename
, lineno
);
556 expand_start_loop_continue_elsewhere (1);
557 genrtl_do_pushlevel ();
558 cond
= expand_cond (FOR_COND (t
));
560 /* Save the filename and line number so that we expand the FOR_EXPR
561 we can reset them back to the saved values. */
562 saved_filename
= input_filename
;
563 saved_lineno
= lineno
;
565 /* Expand the condition. */
566 emit_line_note (input_filename
, lineno
);
568 expand_exit_loop_if_false (0, cond
);
570 /* Expand the body. */
571 genrtl_do_pushlevel ();
572 expand_stmt (FOR_BODY (t
));
574 /* Expand the increment expression. */
575 input_filename
= saved_filename
;
576 lineno
= saved_lineno
;
577 emit_line_note (input_filename
, lineno
);
578 expand_loop_continue_here ();
580 genrtl_expr_stmt (FOR_EXPR (t
));
584 /* Build a break statement node and return it. */
589 return (build_stmt (BREAK_STMT
));
592 /* Generate the RTL for a BREAK_STMT. */
597 emit_line_note (input_filename
, lineno
);
598 if ( ! expand_exit_something ())
599 error ("break statement not within loop or switch");
602 /* Build a continue statement node and return it. */
605 build_continue_stmt ()
607 return (build_stmt (CONTINUE_STMT
));
610 /* Generate the RTL for a CONTINUE_STMT. */
613 genrtl_continue_stmt ()
615 emit_line_note (input_filename
, lineno
);
616 if (! expand_continue_loop (0))
617 error ("continue statement not within a loop");
620 /* Generate the RTL for T, which is a SCOPE_STMT. */
623 genrtl_scope_stmt (t
)
626 if (!SCOPE_NO_CLEANUPS_P (t
))
628 if (SCOPE_BEGIN_P (t
))
629 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t
),
630 SCOPE_STMT_BLOCK (t
));
631 else if (SCOPE_END_P (t
))
632 expand_end_bindings (NULL_TREE
, !SCOPE_NULLIFIED_P (t
), 0);
634 else if (!SCOPE_NULLIFIED_P (t
))
636 rtx note
= emit_note (NULL
,
638 ? NOTE_INSN_BLOCK_BEG
639 : NOTE_INSN_BLOCK_END
));
640 NOTE_BLOCK (note
) = SCOPE_STMT_BLOCK (t
);
644 /* Generate the RTL for T, which is a SWITCH_STMT. */
647 genrtl_switch_stmt (t
)
651 genrtl_do_pushlevel ();
653 cond
= expand_cond (SWITCH_COND (t
));
654 if (cond
== error_mark_node
)
655 /* The code is in error, but we don't want expand_end_case to
657 cond
= boolean_false_node
;
659 emit_line_note (input_filename
, lineno
);
660 expand_start_case (1, cond
, TREE_TYPE (cond
), "switch statement");
661 expand_stmt (SWITCH_BODY (t
));
662 expand_end_case (cond
);
665 /* Create a CASE_LABEL tree node and return it. */
668 build_case_label (low_value
, high_value
, label_decl
)
673 return build_stmt (CASE_LABEL
, low_value
, high_value
, label_decl
);
677 /* Generate the RTL for a CASE_LABEL. */
680 genrtl_case_label (case_label
)
686 cleanup
= last_cleanup_this_contour ();
689 static int explained
= 0;
690 warning_with_decl (TREE_PURPOSE (cleanup
),
691 "destructor needed for `%#D'");
692 warning ("where case label appears here");
695 warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
700 add_case_node (CASE_LOW (case_label
), CASE_HIGH (case_label
),
701 CASE_LABEL_DECL (case_label
), &duplicate
);
704 /* Generate the RTL for T, which is a COMPOUND_STMT. */
707 genrtl_compound_stmt (t
)
710 expand_stmt (COMPOUND_BODY (t
));
713 /* Generate the RTL for an ASM_STMT. */
716 genrtl_asm_stmt (cv_qualifier
, string
, output_operands
,
717 input_operands
, clobbers
, asm_input_p
)
720 tree output_operands
;
725 if (cv_qualifier
!= NULL_TREE
726 && cv_qualifier
!= ridpointers
[(int) RID_VOLATILE
])
728 warning ("%s qualifier ignored on asm",
729 IDENTIFIER_POINTER (cv_qualifier
));
730 cv_qualifier
= NULL_TREE
;
733 emit_line_note (input_filename
, lineno
);
737 c_expand_asm_operands (string
, output_operands
, input_operands
,
738 clobbers
, cv_qualifier
!= NULL_TREE
,
739 input_filename
, lineno
);
742 /* Generate the RTL for a DECL_CLEANUP. */
745 genrtl_decl_cleanup (decl
, cleanup
)
749 if (!decl
|| (DECL_SIZE (decl
) && TREE_TYPE (decl
) != error_mark_node
))
750 expand_decl_cleanup (decl
, cleanup
);
753 /* We're about to expand T, a statement. Set up appropriate context
754 for the substitution. */
760 if (!STMT_LINENO_FOR_FN_P (t
))
761 lineno
= STMT_LINENO (t
);
762 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
765 /* Generate the RTL for the statement T, its substatements, and any
766 other statements at its nesting level. */
772 while (t
&& t
!= error_mark_node
)
774 int saved_stmts_are_full_exprs_p
;
776 /* Set up context appropriately for handling this statement. */
777 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
780 switch (TREE_CODE (t
))
783 genrtl_return_stmt (t
);
787 genrtl_expr_stmt (EXPR_STMT_EXPR (t
));
791 genrtl_decl_stmt (t
);
799 genrtl_while_stmt (t
);
811 genrtl_compound_stmt (t
);
815 genrtl_break_stmt ();
819 genrtl_continue_stmt ();
823 genrtl_switch_stmt (t
);
827 genrtl_case_label (t
);
831 expand_label (LABEL_STMT_LABEL (t
));
835 genrtl_goto_stmt (GOTO_DESTINATION (t
));
839 genrtl_asm_stmt (ASM_CV_QUAL (t
), ASM_STRING (t
),
840 ASM_OUTPUTS (t
), ASM_INPUTS (t
),
841 ASM_CLOBBERS (t
), ASM_INPUT_P (t
));
845 genrtl_scope_stmt (t
);
849 if (lang_expand_stmt
)
850 (*lang_expand_stmt
) (t
);
856 /* Restore saved state. */
857 current_stmt_tree ()->stmts_are_full_exprs_p
=
858 saved_stmts_are_full_exprs_p
;
860 /* Go on to the next statement in this scope. */