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, 2002 Free Software Foundation, Inc.
5 Written by Benjamin Chelf (chelf@codesourcery.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
30 #include "splay-tree.h"
43 /* If non-NULL, the address of a language-specific function for
44 expanding statements. */
45 void (*lang_expand_stmt
) PARAMS ((tree
));
47 /* If non-NULL, the address of a language-specific function for
48 expanding a DECL_STMT. After the language-independent cases are
49 handled, this function will be called. If this function is not
50 defined, it is assumed that declarations other than those for
51 variables and labels do not require any RTL generation. */
52 void (*lang_expand_decl_stmt
) PARAMS ((tree
));
54 /* Create an empty statement tree rooted at T. */
60 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
61 what follows. We remove the extraneous statement in
63 *t
= build_nt (EXPR_STMT
, void_zero_node
);
65 last_expr_type
= NULL_TREE
;
66 last_expr_filename
= input_filename
;
69 /* T is a statement. Add it to the statement-tree. */
75 if (input_filename
!= last_expr_filename
)
77 /* If the filename has changed, also add in a FILE_STMT. Do a string
78 compare first, though, as it might be an equivalent string. */
79 int add
= (strcmp (input_filename
, last_expr_filename
) != 0);
80 last_expr_filename
= input_filename
;
83 tree pos
= build_nt (FILE_STMT
, get_identifier (input_filename
));
88 /* Add T to the statement-tree. */
89 TREE_CHAIN (last_tree
) = t
;
92 /* When we expand a statement-tree, we must know whether or not the
93 statements are full-expressions. We record that fact here. */
94 STMT_IS_FULL_EXPR_P (last_tree
) = stmts_are_full_exprs_p ();
96 /* Keep track of the number of statements in this function. */
97 if (current_function_decl
)
98 ++DECL_NUM_STMTS (current_function_decl
);
103 /* Create a declaration statement for the declaration given by the
112 /* We need the type to last until instantiation time. */
113 decl_stmt
= build_stmt (DECL_STMT
, decl
);
114 add_stmt (decl_stmt
);
117 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
118 whether this statements opens or closes a scope. PARTIAL_P is true
119 for a partial scope, i.e, the scope that begins after a label when
120 an object that needs a cleanup is created. If BEGIN_P is nonzero,
121 returns a new TREE_LIST representing the top of the SCOPE_STMT
122 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
123 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
124 and whose TREE_PURPOSE is the matching SCOPE_STMT with
125 SCOPE_BEGIN_P set. */
128 add_scope_stmt (begin_p
, partial_p
)
132 tree
*stack_ptr
= current_scope_stmt_stack ();
134 tree top
= *stack_ptr
;
136 /* Build the statement. */
137 ss
= build_stmt (SCOPE_STMT
, NULL_TREE
);
138 SCOPE_BEGIN_P (ss
) = begin_p
;
139 SCOPE_PARTIAL_P (ss
) = partial_p
;
141 /* Keep the scope stack up to date. */
144 top
= tree_cons (ss
, NULL_TREE
, top
);
149 if (partial_p
!= SCOPE_PARTIAL_P (TREE_PURPOSE (top
)))
151 TREE_VALUE (top
) = ss
;
152 *stack_ptr
= TREE_CHAIN (top
);
155 /* Add the new statement to the statement-tree. */
161 /* Finish the statement tree rooted at T. */
169 /* Remove the fake extra statement added in begin_stmt_tree. */
170 stmt
= TREE_CHAIN (*t
);
172 last_tree
= NULL_TREE
;
176 /* The line-number recorded in the outermost statement in a function
177 is the line number of the end of the function. */
178 STMT_LINENO (stmt
) = lineno
;
179 STMT_LINENO_FOR_FN_P (stmt
) = 1;
183 /* Build a generic statement based on the given type of node and
184 arguments. Similar to `build_nt', except that we set
185 STMT_LINENO to be the current line number. */
186 /* ??? This should be obsolete with the lineno_stmt productions
190 build_stmt
VPARAMS ((enum tree_code code
, ...))
197 VA_FIXEDARG (p
, enum tree_code
, code
);
199 t
= make_node (code
);
200 length
= TREE_CODE_LENGTH (code
);
201 STMT_LINENO (t
) = lineno
;
203 for (i
= 0; i
< length
; i
++)
204 TREE_OPERAND (t
, i
) = va_arg (p
, tree
);
210 /* Some statements, like for-statements or if-statements, require a
211 condition. This condition can be a declaration. If T is such a
212 declaration it is processed, and an expression appropriate to use
213 as the condition is returned. Otherwise, T itself is returned. */
219 if (t
&& TREE_CODE (t
) == TREE_LIST
)
221 expand_stmt (TREE_PURPOSE (t
));
222 return TREE_VALUE (t
);
228 /* Create RTL for the local static variable DECL. */
231 make_rtl_for_local_static (decl
)
234 const char *asmspec
= NULL
;
236 /* If we inlined this variable, we could see it's declaration
238 if (TREE_ASM_WRITTEN (decl
))
241 /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
242 either we already created RTL for this DECL (and since it was a
243 local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
244 clashes with other local statics with the same name by a previous
245 call to make_decl_rtl), or the user explicitly requested a
246 particular assembly name for this variable, using the GNU
247 extension for this purpose:
251 There's no way to know which case we're in, here. But, it turns
252 out we're safe. If there's already RTL, then
253 rest_of_decl_compilation ignores the ASMSPEC parameter, so we
254 may as well not pass it in. If there isn't RTL, then we didn't
255 already create RTL, which means that the modification to
256 DECL_ASSEMBLER_NAME came only via the explicit extension. */
257 if (DECL_ASSEMBLER_NAME (decl
) != DECL_NAME (decl
)
258 && !DECL_RTL_SET_P (decl
))
259 asmspec
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
261 rest_of_decl_compilation (decl
, asmspec
, /*top_level=*/0, /*at_end=*/0);
264 /* Let the back-end know about DECL. */
267 emit_local_var (decl
)
270 /* Create RTL for this variable. */
271 if (!DECL_RTL_SET_P (decl
))
273 if (DECL_C_HARD_REGISTER (decl
))
274 /* The user specified an assembler name for this variable.
276 rest_of_decl_compilation
277 (decl
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)),
278 /*top_level=*/0, /*at_end=*/0);
283 /* Actually do the initialization. */
284 if (stmts_are_full_exprs_p ())
285 expand_start_target_temps ();
287 expand_decl_init (decl
);
289 if (stmts_are_full_exprs_p ())
290 expand_end_target_temps ();
293 /* Helper for generating the RTL at the beginning of a scope. */
296 genrtl_do_pushlevel ()
298 emit_line_note (input_filename
, lineno
);
302 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
305 genrtl_goto_stmt (destination
)
308 if (TREE_CODE (destination
) == IDENTIFIER_NODE
)
311 /* We warn about unused labels with -Wunused. That means we have to
312 mark the used labels as used. */
313 if (TREE_CODE (destination
) == LABEL_DECL
)
314 TREE_USED (destination
) = 1;
316 emit_line_note (input_filename
, lineno
);
318 if (TREE_CODE (destination
) == LABEL_DECL
)
320 label_rtx (destination
);
321 expand_goto (destination
);
324 expand_computed_goto (destination
);
327 /* Generate the RTL for EXPR, which is an EXPR_STMT. Provided just
328 for backward compatibility. genrtl_expr_stmt_value() should be
329 used for new code. */
332 genrtl_expr_stmt (expr
)
335 genrtl_expr_stmt_value (expr
, -1, 1);
338 /* Generate the RTL for EXPR, which is an EXPR_STMT. WANT_VALUE tells
339 whether to (1) save the value of the expression, (0) discard it or
340 (-1) use expr_stmts_for_value to tell. The use of -1 is
341 deprecated, and retained only for backward compatibility.
342 MAYBE_LAST is nonzero if this EXPR_STMT might be the last statement
343 in expression statement. */
346 genrtl_expr_stmt_value (expr
, want_value
, maybe_last
)
348 int want_value
, maybe_last
;
350 if (expr
!= NULL_TREE
)
352 emit_line_note (input_filename
, lineno
);
354 if (stmts_are_full_exprs_p ())
355 expand_start_target_temps ();
357 if (expr
!= error_mark_node
)
358 expand_expr_stmt_value (expr
, want_value
, maybe_last
);
360 if (stmts_are_full_exprs_p ())
361 expand_end_target_temps ();
365 /* Generate the RTL for T, which is a DECL_STMT. */
372 emit_line_note (input_filename
, lineno
);
373 decl
= DECL_STMT_DECL (t
);
374 /* If this is a declaration for an automatic local
375 variable, initialize it. Note that we might also see a
376 declaration for a namespace-scope object (declared with
377 `extern'). We don't have to handle the initialization
378 of those objects here; they can only be declarations,
379 rather than definitions. */
380 if (TREE_CODE (decl
) == VAR_DECL
381 && !TREE_STATIC (decl
)
382 && !DECL_EXTERNAL (decl
))
384 /* Let the back-end know about this variable. */
385 if (!anon_aggr_type_p (TREE_TYPE (decl
)))
386 emit_local_var (decl
);
388 expand_anon_union_decl (decl
, NULL_TREE
,
389 DECL_ANON_UNION_ELEMS (decl
));
391 else if (TREE_CODE (decl
) == VAR_DECL
&& TREE_STATIC (decl
))
392 make_rtl_for_local_static (decl
);
393 else if (TREE_CODE (decl
) == LABEL_DECL
394 && C_DECLARED_LABEL_FLAG (decl
))
395 declare_nonlocal_label (decl
);
396 else if (lang_expand_decl_stmt
)
397 (*lang_expand_decl_stmt
) (t
);
400 /* Generate the RTL for T, which is an IF_STMT. */
407 genrtl_do_pushlevel ();
408 cond
= expand_cond (IF_COND (t
));
409 emit_line_note (input_filename
, lineno
);
410 expand_start_cond (cond
, 0);
412 expand_stmt (THEN_CLAUSE (t
));
415 expand_start_else ();
416 expand_stmt (ELSE_CLAUSE (t
));
421 /* Generate the RTL for T, which is a WHILE_STMT. */
424 genrtl_while_stmt (t
)
429 emit_line_note (input_filename
, lineno
);
430 expand_start_loop (1);
431 genrtl_do_pushlevel ();
433 cond
= expand_cond (WHILE_COND (t
));
434 emit_line_note (input_filename
, lineno
);
435 expand_exit_loop_top_cond (0, cond
);
436 genrtl_do_pushlevel ();
438 expand_stmt (WHILE_BODY (t
));
443 /* Generate the RTL for T, which is a DO_STMT. */
449 tree cond
= DO_COND (t
);
451 /* Recognize the common special-case of do { ... } while (0) and do
452 not emit the loop widgetry in this case. In particular this
453 avoids cluttering the rtl with dummy loop notes, which can affect
454 alignment of adjacent labels. COND can be NULL due to parse
456 if (!cond
|| integer_zerop (cond
))
458 expand_start_null_loop ();
459 expand_stmt (DO_BODY (t
));
460 expand_end_null_loop ();
462 else if (integer_nonzerop (cond
))
465 emit_line_note (input_filename
, lineno
);
466 expand_start_loop (1);
468 expand_stmt (DO_BODY (t
));
470 emit_line_note (input_filename
, lineno
);
476 emit_line_note (input_filename
, lineno
);
477 expand_start_loop_continue_elsewhere (1);
479 expand_stmt (DO_BODY (t
));
481 expand_loop_continue_here ();
482 cond
= expand_cond (cond
);
483 emit_line_note (input_filename
, lineno
);
484 expand_exit_loop_if_false (0, cond
);
489 /* Build the node for a return statement and return it. */
492 build_return_stmt (expr
)
495 return (build_stmt (RETURN_STMT
, expr
));
498 /* Generate the RTL for STMT, which is a RETURN_STMT. */
501 genrtl_return_stmt (stmt
)
506 expr
= RETURN_STMT_EXPR (stmt
);
508 emit_line_note (input_filename
, lineno
);
510 expand_null_return ();
513 expand_start_target_temps ();
514 expand_return (expr
);
515 expand_end_target_temps ();
519 /* Generate the RTL for T, which is a FOR_STMT. */
526 const char *saved_filename
;
529 if (NEW_FOR_SCOPE_P (t
))
530 genrtl_do_pushlevel ();
532 expand_stmt (FOR_INIT_STMT (t
));
534 /* Expand the initialization. */
536 emit_line_note (input_filename
, lineno
);
538 expand_start_loop_continue_elsewhere (1);
540 expand_start_loop (1);
541 genrtl_do_pushlevel ();
542 cond
= expand_cond (FOR_COND (t
));
544 /* Save the filename and line number so that we expand the FOR_EXPR
545 we can reset them back to the saved values. */
546 saved_filename
= input_filename
;
547 saved_lineno
= lineno
;
549 /* Expand the condition. */
550 emit_line_note (input_filename
, lineno
);
552 expand_exit_loop_top_cond (0, cond
);
554 /* Expand the body. */
555 genrtl_do_pushlevel ();
556 expand_stmt (FOR_BODY (t
));
558 /* Expand the increment expression. */
559 input_filename
= saved_filename
;
560 lineno
= saved_lineno
;
561 emit_line_note (input_filename
, lineno
);
564 expand_loop_continue_here ();
565 genrtl_expr_stmt (FOR_EXPR (t
));
570 /* Build a break statement node and return it. */
575 return (build_stmt (BREAK_STMT
));
578 /* Generate the RTL for a BREAK_STMT. */
583 emit_line_note (input_filename
, lineno
);
584 if ( ! expand_exit_something ())
585 error ("break statement not within loop or switch");
588 /* Build a continue statement node and return it. */
591 build_continue_stmt ()
593 return (build_stmt (CONTINUE_STMT
));
596 /* Generate the RTL for a CONTINUE_STMT. */
599 genrtl_continue_stmt ()
601 emit_line_note (input_filename
, lineno
);
602 if (! expand_continue_loop (0))
603 error ("continue statement not within a loop");
606 /* Generate the RTL for T, which is a SCOPE_STMT. */
609 genrtl_scope_stmt (t
)
612 tree block
= SCOPE_STMT_BLOCK (t
);
614 if (!SCOPE_NO_CLEANUPS_P (t
))
616 if (SCOPE_BEGIN_P (t
))
617 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t
), block
);
618 else if (SCOPE_END_P (t
))
619 expand_end_bindings (NULL_TREE
, !SCOPE_NULLIFIED_P (t
), 0);
621 else if (!SCOPE_NULLIFIED_P (t
))
623 rtx note
= emit_note (NULL
,
625 ? NOTE_INSN_BLOCK_BEG
626 : NOTE_INSN_BLOCK_END
));
627 NOTE_BLOCK (note
) = block
;
630 /* If we're at the end of a scope that contains inlined nested
631 functions, we have to decide whether or not to write them out. */
632 if (block
&& SCOPE_END_P (t
))
636 for (fn
= BLOCK_VARS (block
); fn
; fn
= TREE_CHAIN (fn
))
638 if (TREE_CODE (fn
) == FUNCTION_DECL
639 && DECL_CONTEXT (fn
) == current_function_decl
640 && DECL_SAVED_INSNS (fn
)
641 && !TREE_ASM_WRITTEN (fn
)
642 && TREE_ADDRESSABLE (fn
))
644 push_function_context ();
645 output_inline_function (fn
);
646 pop_function_context ();
652 /* Generate the RTL for T, which is a SWITCH_STMT. */
655 genrtl_switch_stmt (t
)
659 genrtl_do_pushlevel ();
661 cond
= expand_cond (SWITCH_COND (t
));
662 if (cond
== error_mark_node
)
663 /* The code is in error, but we don't want expand_end_case to
665 cond
= boolean_false_node
;
667 emit_line_note (input_filename
, lineno
);
668 expand_start_case (1, cond
, TREE_TYPE (cond
), "switch statement");
669 expand_stmt (SWITCH_BODY (t
));
670 expand_end_case_type (cond
, SWITCH_TYPE (t
));
673 /* Create a CASE_LABEL tree node and return it. */
676 build_case_label (low_value
, high_value
, label_decl
)
681 return build_stmt (CASE_LABEL
, low_value
, high_value
, label_decl
);
685 /* Generate the RTL for a CASE_LABEL. */
688 genrtl_case_label (case_label
)
694 cleanup
= last_cleanup_this_contour ();
697 static int explained
= 0;
698 warning ("destructor needed for `%#D'", (TREE_PURPOSE (cleanup
)));
699 warning ("where case label appears here");
702 warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
707 add_case_node (CASE_LOW (case_label
), CASE_HIGH (case_label
),
708 CASE_LABEL_DECL (case_label
), &duplicate
);
711 /* Generate the RTL for T, which is a COMPOUND_STMT. */
714 genrtl_compound_stmt (t
)
717 #ifdef ENABLE_CHECKING
718 struct nesting
*n
= current_nesting_level ();
721 expand_stmt (COMPOUND_BODY (t
));
723 #ifdef ENABLE_CHECKING
724 /* Make sure that we've pushed and popped the same number of levels. */
725 if (!COMPOUND_STMT_NO_SCOPE (t
) && n
!= current_nesting_level ())
730 /* Generate the RTL for an ASM_STMT. */
733 genrtl_asm_stmt (cv_qualifier
, string
, output_operands
,
734 input_operands
, clobbers
, asm_input_p
)
737 tree output_operands
;
742 if (cv_qualifier
!= NULL_TREE
743 && cv_qualifier
!= ridpointers
[(int) RID_VOLATILE
])
745 warning ("%s qualifier ignored on asm",
746 IDENTIFIER_POINTER (cv_qualifier
));
747 cv_qualifier
= NULL_TREE
;
750 emit_line_note (input_filename
, lineno
);
754 c_expand_asm_operands (string
, output_operands
, input_operands
,
755 clobbers
, cv_qualifier
!= NULL_TREE
,
756 input_filename
, lineno
);
759 /* Generate the RTL for a DECL_CLEANUP. */
762 genrtl_decl_cleanup (t
)
765 tree decl
= CLEANUP_DECL (t
);
766 if (!decl
|| (DECL_SIZE (decl
) && TREE_TYPE (decl
) != error_mark_node
))
767 expand_decl_cleanup_eh (decl
, CLEANUP_EXPR (t
), CLEANUP_EH_ONLY (t
));
770 /* We're about to expand T, a statement. Set up appropriate context
771 for the substitution. */
777 if (!STMT_LINENO_FOR_FN_P (t
))
778 lineno
= STMT_LINENO (t
);
779 current_stmt_tree ()->stmts_are_full_exprs_p
= STMT_IS_FULL_EXPR_P (t
);
782 /* Generate the RTL for the statement T, its substatements, and any
783 other statements at its nesting level. */
789 while (t
&& t
!= error_mark_node
)
791 int saved_stmts_are_full_exprs_p
;
793 /* Set up context appropriately for handling this statement. */
794 saved_stmts_are_full_exprs_p
= stmts_are_full_exprs_p ();
797 switch (TREE_CODE (t
))
800 input_filename
= FILE_STMT_FILENAME (t
);
804 genrtl_return_stmt (t
);
808 genrtl_expr_stmt_value (EXPR_STMT_EXPR (t
), TREE_ADDRESSABLE (t
),
809 TREE_CHAIN (t
) == NULL
810 || (TREE_CODE (TREE_CHAIN (t
)) == SCOPE_STMT
811 && TREE_CHAIN (TREE_CHAIN (t
)) == NULL
));
815 genrtl_decl_stmt (t
);
823 genrtl_while_stmt (t
);
835 genrtl_compound_stmt (t
);
839 genrtl_break_stmt ();
843 genrtl_continue_stmt ();
847 genrtl_switch_stmt (t
);
851 genrtl_case_label (t
);
855 expand_label (LABEL_STMT_LABEL (t
));
859 /* Emit information for branch prediction. */
861 && TREE_CODE (GOTO_DESTINATION (t
)) == LABEL_DECL
)
863 rtx note
= emit_note (NULL
, NOTE_INSN_PREDICTION
);
865 NOTE_PREDICTION (note
) = NOTE_PREDICT (PRED_GOTO
, NOT_TAKEN
);
867 genrtl_goto_stmt (GOTO_DESTINATION (t
));
871 genrtl_asm_stmt (ASM_CV_QUAL (t
), ASM_STRING (t
),
872 ASM_OUTPUTS (t
), ASM_INPUTS (t
),
873 ASM_CLOBBERS (t
), ASM_INPUT_P (t
));
877 genrtl_scope_stmt (t
);
881 genrtl_decl_cleanup (t
);
885 if (lang_expand_stmt
)
886 (*lang_expand_stmt
) (t
);
892 /* Restore saved state. */
893 current_stmt_tree ()->stmts_are_full_exprs_p
894 = saved_stmts_are_full_exprs_p
;
896 /* Go on to the next statement in this scope. */