OSDN Git Service

* c-common.h (enum rid): Remove RID_BOUNDED, RID_UNBOUNDED.
[pf3gnuchains/gcc-fork.git] / gcc / c-semantics.c
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, 2003 Free Software Foundation, Inc.
5    Written by Benjamin Chelf (chelf@codesourcery.com).
6
7 This file is part of GCC.
8
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
12 version.
13
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
17 for more details.
18
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
22 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "function.h"
30 #include "splay-tree.h"
31 #include "varray.h"
32 #include "c-common.h"
33 #include "except.h"
34 #include "toplev.h"
35 #include "flags.h"
36 #include "ggc.h"
37 #include "rtl.h"
38 #include "expr.h"
39 #include "output.h"
40 #include "timevar.h"
41 #include "predict.h"
42 #include "tree-inline.h"
43
44 /* If non-NULL, the address of a language-specific function for
45    expanding statements.  */
46 void (*lang_expand_stmt) PARAMS ((tree));
47
48 /* If non-NULL, the address of a language-specific function for
49    expanding a DECL_STMT.  After the language-independent cases are
50    handled, this function will be called.  If this function is not
51    defined, it is assumed that declarations other than those for
52    variables and labels do not require any RTL generation.  */
53 void (*lang_expand_decl_stmt) PARAMS ((tree));
54
55 static tree find_reachable_label_1      PARAMS ((tree *, int *, void *));
56 static tree find_reachable_label        PARAMS ((tree));
57 static bool expand_unreachable_if_stmt  PARAMS ((tree));
58 static tree expand_unreachable_stmt     PARAMS ((tree, int));
59
60 /* Create an empty statement tree rooted at T.  */
61
62 void
63 begin_stmt_tree (t)
64      tree *t;
65 {
66   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
67      what follows.  We remove the extraneous statement in
68      finish_stmt_tree.  */
69   *t = build_nt (EXPR_STMT, void_zero_node);
70   last_tree = *t;
71   last_expr_type = NULL_TREE;
72   last_expr_filename = input_filename;
73 }
74
75 /* T is a statement.  Add it to the statement-tree.  */
76
77 tree
78 add_stmt (t)
79      tree t;
80 {
81   if (input_filename != last_expr_filename)
82     {
83       /* If the filename has changed, also add in a FILE_STMT.  Do a string
84          compare first, though, as it might be an equivalent string.  */
85       int add = (strcmp (input_filename, last_expr_filename) != 0);
86       last_expr_filename = input_filename;
87       if (add)
88         {
89           tree pos = build_nt (FILE_STMT, get_identifier (input_filename));
90           add_stmt (pos);
91         }
92     }
93
94   /* Add T to the statement-tree.  */
95   TREE_CHAIN (last_tree) = t;
96   last_tree = t;
97   
98   /* When we expand a statement-tree, we must know whether or not the
99      statements are full-expressions.  We record that fact here.  */
100   STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
101
102   /* Keep track of the number of statements in this function.  */
103   if (current_function_decl)
104     ++DECL_NUM_STMTS (current_function_decl);
105
106   return t;
107 }
108
109 /* Create a declaration statement for the declaration given by the
110    DECL.  */
111
112 void
113 add_decl_stmt (decl)
114      tree decl;
115 {
116   tree decl_stmt;
117
118   /* We need the type to last until instantiation time.  */
119   decl_stmt = build_stmt (DECL_STMT, decl);
120   add_stmt (decl_stmt); 
121 }
122
123 /* Add a scope-statement to the statement-tree.  BEGIN_P indicates
124    whether this statements opens or closes a scope.  PARTIAL_P is true
125    for a partial scope, i.e, the scope that begins after a label when
126    an object that needs a cleanup is created.  If BEGIN_P is nonzero,
127    returns a new TREE_LIST representing the top of the SCOPE_STMT
128    stack.  The TREE_PURPOSE is the new SCOPE_STMT.  If BEGIN_P is
129    zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
130    and whose TREE_PURPOSE is the matching SCOPE_STMT with
131    SCOPE_BEGIN_P set.  */
132
133 tree
134 add_scope_stmt (begin_p, partial_p)
135      int begin_p;
136      int partial_p;
137 {
138   tree *stack_ptr = current_scope_stmt_stack ();
139   tree ss;
140   tree top = *stack_ptr;
141
142   /* Build the statement.  */
143   ss = build_stmt (SCOPE_STMT, NULL_TREE);
144   SCOPE_BEGIN_P (ss) = begin_p;
145   SCOPE_PARTIAL_P (ss) = partial_p;
146
147   /* Keep the scope stack up to date.  */
148   if (begin_p)
149     {
150       top = tree_cons (ss, NULL_TREE, top);
151       *stack_ptr = top;
152     }
153   else
154     {
155       if (partial_p != SCOPE_PARTIAL_P (TREE_PURPOSE (top)))
156         abort ();
157       TREE_VALUE (top) = ss;
158       *stack_ptr = TREE_CHAIN (top);
159     }
160
161   /* Add the new statement to the statement-tree.  */
162   add_stmt (ss);
163
164   return top;
165 }
166
167 /* Finish the statement tree rooted at T.  */
168
169 void
170 finish_stmt_tree (t)
171      tree *t;
172 {
173   tree stmt;
174   
175   /* Remove the fake extra statement added in begin_stmt_tree.  */
176   stmt = TREE_CHAIN (*t);
177   *t = stmt;
178   last_tree = NULL_TREE;
179
180   if (cfun && stmt)
181     {
182       /* The line-number recorded in the outermost statement in a function
183          is the line number of the end of the function.  */
184       STMT_LINENO (stmt) = input_line;
185       STMT_LINENO_FOR_FN_P (stmt) = 1;
186     }
187 }
188
189 /* Build a generic statement based on the given type of node and
190    arguments. Similar to `build_nt', except that we set
191    STMT_LINENO to be the current line number.  */
192 /* ??? This should be obsolete with the lineno_stmt productions
193    in the grammar.  */
194
195 tree
196 build_stmt (enum tree_code code, ...)
197 {
198   tree t;
199   int length;
200   int i;
201   va_list p;
202   
203   va_start (p, code);
204
205   t = make_node (code);
206   length = TREE_CODE_LENGTH (code);
207   STMT_LINENO (t) = input_line;
208
209   for (i = 0; i < length; i++)
210     TREE_OPERAND (t, i) = va_arg (p, tree);
211
212   va_end (p);
213   return t;
214 }
215
216 /* Some statements, like for-statements or if-statements, require a
217    condition.  This condition can be a declaration.  If T is such a
218    declaration it is processed, and an expression appropriate to use
219    as the condition is returned.  Otherwise, T itself is returned.  */
220
221 tree
222 expand_cond (t)
223      tree t;
224 {
225   if (t && TREE_CODE (t) == TREE_LIST)
226     {
227       expand_stmt (TREE_PURPOSE (t));
228       return TREE_VALUE (t);
229     }
230   else 
231     return t;
232 }
233
234 /* Create RTL for the local static variable DECL.  */
235
236 void
237 make_rtl_for_local_static (decl)
238      tree decl;
239 {
240   const char *asmspec = NULL;
241
242   /* If we inlined this variable, we could see it's declaration
243      again.  */
244   if (TREE_ASM_WRITTEN (decl))
245     return;
246
247   /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
248      either we already created RTL for this DECL (and since it was a
249      local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
250      clashes with other local statics with the same name by a previous
251      call to make_decl_rtl), or the user explicitly requested a
252      particular assembly name for this variable, using the GNU
253      extension for this purpose:
254
255        int i asm ("j");
256
257      There's no way to know which case we're in, here.  But, it turns
258      out we're safe.  If there's already RTL, then
259      rest_of_decl_compilation ignores the ASMSPEC parameter, so we
260      may as well not pass it in.  If there isn't RTL, then we didn't
261      already create RTL, which means that the modification to
262      DECL_ASSEMBLER_NAME came only via the explicit extension.  */
263   if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
264       && !DECL_RTL_SET_P (decl))
265     asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
266
267   rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
268 }
269
270 /* Let the back-end know about DECL.  */
271
272 void
273 emit_local_var (decl)
274      tree decl;
275 {
276   /* Create RTL for this variable.  */
277   if (!DECL_RTL_SET_P (decl))
278     {
279       if (DECL_C_HARD_REGISTER (decl))
280         /* The user specified an assembler name for this variable.
281            Set that up now.  */
282         rest_of_decl_compilation
283           (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
284            /*top_level=*/0, /*at_end=*/0);
285       else
286         expand_decl (decl);
287     }
288
289   if (DECL_INITIAL (decl))
290     {
291       /* Actually do the initialization.  */
292       if (stmts_are_full_exprs_p ())
293         expand_start_target_temps ();
294
295       expand_decl_init (decl);
296
297       if (stmts_are_full_exprs_p ())
298         expand_end_target_temps ();
299     }
300 }
301
302 /* Helper for generating the RTL at the beginning of a scope.  */
303
304 void
305 genrtl_do_pushlevel ()
306 {
307   emit_line_note (input_filename, input_line);
308   clear_last_expr ();
309 }
310
311 /* Generate the RTL for DESTINATION, which is a GOTO_STMT.  */
312
313 void
314 genrtl_goto_stmt (destination)
315      tree destination;
316 {
317   if (TREE_CODE (destination) == IDENTIFIER_NODE)
318     abort ();
319   
320   /* We warn about unused labels with -Wunused.  That means we have to
321      mark the used labels as used.  */
322   if (TREE_CODE (destination) == LABEL_DECL)
323     TREE_USED (destination) = 1;
324   
325   emit_line_note (input_filename, input_line);
326   
327   if (TREE_CODE (destination) == LABEL_DECL)
328     {
329       label_rtx (destination);
330       expand_goto (destination); 
331     }
332   else
333     expand_computed_goto (destination);
334 }
335
336 /* Generate the RTL for EXPR, which is an EXPR_STMT.  Provided just
337    for backward compatibility.  genrtl_expr_stmt_value() should be
338    used for new code.  */
339
340 void
341 genrtl_expr_stmt (expr)
342      tree expr;
343 {
344   genrtl_expr_stmt_value (expr, -1, 1);
345 }
346
347 /* Generate the RTL for EXPR, which is an EXPR_STMT.  WANT_VALUE tells
348    whether to (1) save the value of the expression, (0) discard it or
349    (-1) use expr_stmts_for_value to tell.  The use of -1 is
350    deprecated, and retained only for backward compatibility.
351    MAYBE_LAST is nonzero if this EXPR_STMT might be the last statement
352    in expression statement.  */
353
354 void 
355 genrtl_expr_stmt_value (expr, want_value, maybe_last)
356      tree expr;
357      int want_value, maybe_last;
358 {
359   if (expr != NULL_TREE)
360     {
361       emit_line_note (input_filename, input_line);
362       
363       if (stmts_are_full_exprs_p ())
364         expand_start_target_temps ();
365       
366       if (expr != error_mark_node)
367         expand_expr_stmt_value (expr, want_value, maybe_last);
368       
369       if (stmts_are_full_exprs_p ())
370         expand_end_target_temps ();
371     }
372 }
373
374 /* Generate the RTL for T, which is a DECL_STMT.  */
375
376 void
377 genrtl_decl_stmt (t)
378      tree t;
379 {
380   tree decl;
381   emit_line_note (input_filename, input_line);
382   decl = DECL_STMT_DECL (t);
383   /* If this is a declaration for an automatic local
384      variable, initialize it.  Note that we might also see a
385      declaration for a namespace-scope object (declared with
386      `extern').  We don't have to handle the initialization
387      of those objects here; they can only be declarations,
388      rather than definitions.  */
389   if (TREE_CODE (decl) == VAR_DECL 
390       && !TREE_STATIC (decl)
391       && !DECL_EXTERNAL (decl))
392     {
393       /* Let the back-end know about this variable.  */
394       if (!anon_aggr_type_p (TREE_TYPE (decl)))
395         emit_local_var (decl);
396       else
397         expand_anon_union_decl (decl, NULL_TREE, 
398                                 DECL_ANON_UNION_ELEMS (decl));
399     }
400   else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
401     make_rtl_for_local_static (decl);
402   else if (TREE_CODE (decl) == LABEL_DECL 
403            && C_DECLARED_LABEL_FLAG (decl))
404     declare_nonlocal_label (decl);
405   else if (lang_expand_decl_stmt)
406     (*lang_expand_decl_stmt) (t);
407 }
408
409 /* Generate the RTL for T, which is an IF_STMT.  */
410
411 void
412 genrtl_if_stmt (t)
413      tree t;
414 {
415   tree cond;
416   genrtl_do_pushlevel ();
417   cond = expand_cond (IF_COND (t));
418   emit_line_note (input_filename, input_line);
419   expand_start_cond (cond, 0);
420   if (THEN_CLAUSE (t))
421     {
422       tree nextt = THEN_CLAUSE (t);
423       
424       if (cond && integer_zerop (cond))
425         nextt = expand_unreachable_stmt (nextt, warn_notreached);
426       expand_stmt (nextt);
427     }
428
429   if (ELSE_CLAUSE (t))
430     {
431       tree nextt = ELSE_CLAUSE (t);
432       expand_start_else ();
433       if (cond && integer_nonzerop (cond))
434         nextt = expand_unreachable_stmt (nextt, warn_notreached);
435       expand_stmt (nextt);
436     }
437   expand_end_cond ();
438 }
439
440 /* Generate the RTL for T, which is a WHILE_STMT.  */
441
442 void
443 genrtl_while_stmt (t)
444      tree t;
445 {
446   tree cond = WHILE_COND (t);
447
448   emit_nop ();
449   emit_line_note (input_filename, input_line);
450   expand_start_loop (1); 
451   genrtl_do_pushlevel ();
452
453   if (cond && !integer_nonzerop (cond))
454     {
455       cond = expand_cond (cond);
456       emit_line_note (input_filename, input_line);
457       expand_exit_loop_top_cond (0, cond);
458       genrtl_do_pushlevel ();
459     }
460   
461   expand_stmt (WHILE_BODY (t));
462
463   expand_end_loop ();
464 }
465
466 /* Generate the RTL for T, which is a DO_STMT.  */
467
468 void
469 genrtl_do_stmt (t)
470      tree t;
471 {
472   tree cond = DO_COND (t);
473
474   /* Recognize the common special-case of do { ... } while (0) and do
475      not emit the loop widgetry in this case.  In particular this
476      avoids cluttering the rtl with dummy loop notes, which can affect
477      alignment of adjacent labels.  COND can be NULL due to parse
478      errors.  */
479   if (!cond || integer_zerop (cond))
480     {
481       expand_start_null_loop ();
482       expand_stmt (DO_BODY (t));
483       expand_end_null_loop ();
484     }
485   else if (integer_nonzerop (cond))
486     {
487       emit_nop ();
488       emit_line_note (input_filename, input_line);
489       expand_start_loop (1);
490
491       expand_stmt (DO_BODY (t));
492
493       emit_line_note (input_filename, input_line);
494       expand_end_loop ();
495     }
496   else
497     {
498       emit_nop ();
499       emit_line_note (input_filename, input_line);
500       expand_start_loop_continue_elsewhere (1);
501
502       expand_stmt (DO_BODY (t));
503
504       expand_loop_continue_here ();
505       cond = expand_cond (cond);
506       emit_line_note (input_filename, input_line);
507       expand_exit_loop_if_false (0, cond);
508       expand_end_loop ();
509     }
510 }
511
512 /* Build the node for a return statement and return it.  */
513
514 tree
515 build_return_stmt (expr)
516      tree expr;
517 {
518   return (build_stmt (RETURN_STMT, expr));
519 }
520
521 /* Generate the RTL for STMT, which is a RETURN_STMT.  */
522
523 void
524 genrtl_return_stmt (stmt)
525      tree stmt;
526 {
527   tree expr;
528
529   expr = RETURN_STMT_EXPR (stmt);
530
531   emit_line_note (input_filename, input_line);
532   if (!expr)
533     expand_null_return ();
534   else
535     {
536       expand_start_target_temps ();
537       expand_return (expr);
538       expand_end_target_temps ();
539     }
540 }
541
542 /* Generate the RTL for T, which is a FOR_STMT.  */
543
544 void
545 genrtl_for_stmt (t)
546      tree t;
547 {
548   tree cond = FOR_COND (t);
549   location_t saved_loc;
550
551   if (NEW_FOR_SCOPE_P (t))
552     genrtl_do_pushlevel ();
553
554   expand_stmt (FOR_INIT_STMT (t));
555
556   /* Expand the initialization.  */
557   emit_nop ();
558   emit_line_note (input_filename, input_line);
559   if (FOR_EXPR (t))
560     expand_start_loop_continue_elsewhere (1); 
561   else
562     expand_start_loop (1);
563   genrtl_do_pushlevel ();
564
565   /* Save the filename and line number so that we expand the FOR_EXPR
566      we can reset them back to the saved values.  */
567   saved_loc = input_location;
568
569   /* Expand the condition.  */
570   if (cond && !integer_nonzerop (cond))
571     {
572       cond = expand_cond (cond);
573       emit_line_note (input_filename, input_line);
574       expand_exit_loop_top_cond (0, cond);
575       genrtl_do_pushlevel ();
576     }
577
578   /* Expand the body.  */
579   expand_stmt (FOR_BODY (t));
580
581   /* Expand the increment expression.  */
582   input_location = saved_loc;
583   emit_line_note (input_filename, input_line);
584   if (FOR_EXPR (t))
585     {
586       expand_loop_continue_here ();
587       genrtl_expr_stmt (FOR_EXPR (t));
588     }
589   expand_end_loop ();
590 }
591
592 /* Build a break statement node and return it.  */
593
594 tree
595 build_break_stmt ()
596 {
597   return (build_stmt (BREAK_STMT));
598 }
599
600 /* Generate the RTL for a BREAK_STMT.  */
601
602 void
603 genrtl_break_stmt ()
604 {
605   emit_line_note (input_filename, input_line);
606   if ( ! expand_exit_something ())
607     error ("break statement not within loop or switch");
608 }
609
610 /* Build a continue statement node and return it.  */
611
612 tree
613 build_continue_stmt ()
614 {
615   return (build_stmt (CONTINUE_STMT));
616 }
617
618 /* Generate the RTL for a CONTINUE_STMT.  */
619
620 void
621 genrtl_continue_stmt ()
622 {
623   emit_line_note (input_filename, input_line);
624   if (! expand_continue_loop (0))
625     error ("continue statement not within a loop");   
626 }
627
628 /* Generate the RTL for T, which is a SCOPE_STMT.  */
629
630 void
631 genrtl_scope_stmt (t)
632      tree t;
633 {
634   tree block = SCOPE_STMT_BLOCK (t);
635
636   if (!SCOPE_NO_CLEANUPS_P (t))
637     {
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);
642     }
643   else if (!SCOPE_NULLIFIED_P (t))
644     {
645       rtx note = emit_note (NULL,
646                             (SCOPE_BEGIN_P (t) 
647                              ? NOTE_INSN_BLOCK_BEG
648                              : NOTE_INSN_BLOCK_END));
649       NOTE_BLOCK (note) = block;
650     }
651
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))
655     {
656       tree fn;
657
658       for (fn = BLOCK_VARS (block); fn; fn = TREE_CHAIN (fn))
659         {
660           if (TREE_CODE (fn) == FUNCTION_DECL 
661               && DECL_CONTEXT (fn) == current_function_decl
662               && DECL_SAVED_INSNS (fn)
663               && !TREE_ASM_WRITTEN (fn)
664               && TREE_ADDRESSABLE (fn))
665             {
666               push_function_context ();
667               output_inline_function (fn);
668               pop_function_context ();
669             }
670         }
671     }
672 }
673
674 /* Generate the RTL for T, which is a SWITCH_STMT.  */
675
676 void
677 genrtl_switch_stmt (t)
678      tree t;
679 {
680   tree cond;
681   genrtl_do_pushlevel ();
682  
683   cond = expand_cond (SWITCH_COND (t));
684   if (cond == error_mark_node)
685     /* The code is in error, but we don't want expand_end_case to
686        crash.  */
687     cond = boolean_false_node;
688
689   emit_line_note (input_filename, input_line);
690   expand_start_case (1, cond, TREE_TYPE (cond), "switch statement");
691   expand_stmt (expand_unreachable_stmt (SWITCH_BODY (t), warn_notreached));
692   expand_end_case_type (cond, SWITCH_TYPE (t));
693 }
694
695 /* Create a CASE_LABEL tree node and return it.  */
696
697 tree
698 build_case_label (low_value, high_value, label_decl)
699      tree low_value;
700      tree high_value;
701      tree label_decl;
702 {
703   return build_stmt (CASE_LABEL, low_value, high_value, label_decl);
704 }
705
706
707 /* Generate the RTL for a CASE_LABEL.  */
708
709 void 
710 genrtl_case_label (case_label)
711      tree case_label;
712 {
713   tree duplicate;
714   tree cleanup;
715
716   cleanup = last_cleanup_this_contour ();
717   if (cleanup)
718     {
719       static int explained = 0;
720       warning ("destructor needed for `%#D'", (TREE_PURPOSE (cleanup)));
721       warning ("where case label appears here");
722       if (!explained)
723         {
724           warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
725           explained = 1;
726         }
727     }
728
729   add_case_node (CASE_LOW (case_label), CASE_HIGH (case_label), 
730                  CASE_LABEL_DECL (case_label), &duplicate);
731 }
732
733 /* Generate the RTL for T, which is a COMPOUND_STMT.  */
734
735 void
736 genrtl_compound_stmt (t)
737     tree t;
738 {
739 #ifdef ENABLE_CHECKING
740   struct nesting *n = current_nesting_level ();
741 #endif
742
743   expand_stmt (COMPOUND_BODY (t));
744
745 #ifdef ENABLE_CHECKING
746   /* Make sure that we've pushed and popped the same number of levels.  */
747   if (!COMPOUND_STMT_NO_SCOPE (t) && n != current_nesting_level ())
748     abort ();
749 #endif
750 }
751
752 /* Generate the RTL for an ASM_STMT.  */
753
754 void
755 genrtl_asm_stmt (cv_qualifier, string, output_operands,
756                  input_operands, clobbers, asm_input_p)
757      tree cv_qualifier;
758      tree string;
759      tree output_operands;
760      tree input_operands;
761      tree clobbers;
762      int asm_input_p;
763 {
764   if (cv_qualifier != NULL_TREE
765       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
766     {
767       warning ("%s qualifier ignored on asm",
768                IDENTIFIER_POINTER (cv_qualifier));
769       cv_qualifier = NULL_TREE;
770     }
771
772   emit_line_note (input_filename, input_line);
773   if (asm_input_p)
774     expand_asm (string, cv_qualifier != NULL_TREE);
775   else
776     c_expand_asm_operands (string, output_operands, input_operands, 
777                            clobbers, cv_qualifier != NULL_TREE,
778                            input_filename, input_line);
779 }
780
781 /* Generate the RTL for a CLEANUP_STMT.  */
782
783 void 
784 genrtl_cleanup_stmt (t)
785      tree t;
786 {
787   tree decl = CLEANUP_DECL (t);
788   if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
789     expand_decl_cleanup_eh (decl, CLEANUP_EXPR (t), CLEANUP_EH_ONLY (t));
790 }
791
792 /* We're about to expand T, a statement.  Set up appropriate context
793    for the substitution.  */
794
795 void
796 prep_stmt (t)
797      tree t;
798 {
799   if (!STMT_LINENO_FOR_FN_P (t))
800     input_line = STMT_LINENO (t);
801   current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
802 }
803
804 /* Generate the RTL for the statement T, its substatements, and any
805    other statements at its nesting level.  */
806
807 void
808 expand_stmt (t)
809      tree t;
810 {
811   while (t && t != error_mark_node)
812     {
813       int saved_stmts_are_full_exprs_p;
814
815       /* Set up context appropriately for handling this statement.  */
816       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
817       prep_stmt (t);
818
819       switch (TREE_CODE (t))
820         {
821         case FILE_STMT:
822           input_filename = FILE_STMT_FILENAME (t);
823           break;
824
825         case RETURN_STMT:
826           genrtl_return_stmt (t);
827           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
828           goto process_t;
829
830         case EXPR_STMT:
831           genrtl_expr_stmt_value (EXPR_STMT_EXPR (t), TREE_ADDRESSABLE (t),
832                                   TREE_CHAIN (t) == NULL
833                                   || (TREE_CODE (TREE_CHAIN (t)) == SCOPE_STMT
834                                       && TREE_CHAIN (TREE_CHAIN (t)) == NULL));
835           break;
836
837         case DECL_STMT:
838           genrtl_decl_stmt (t);
839           break;
840
841         case FOR_STMT:
842           genrtl_for_stmt (t);
843           break;
844
845         case WHILE_STMT:
846           genrtl_while_stmt (t);
847           break;
848
849         case DO_STMT:
850           genrtl_do_stmt (t);
851           break;
852
853         case IF_STMT:
854           genrtl_if_stmt (t);
855           break;
856
857         case COMPOUND_STMT:
858           genrtl_compound_stmt (t);
859           break;
860
861         case BREAK_STMT:
862           genrtl_break_stmt ();
863           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
864           goto process_t;
865
866         case CONTINUE_STMT:
867           genrtl_continue_stmt ();
868           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
869           goto process_t;
870
871         case SWITCH_STMT:
872           genrtl_switch_stmt (t);
873           break;
874
875         case CASE_LABEL:
876           genrtl_case_label (t);
877           break;
878
879         case LABEL_STMT:
880           expand_label (LABEL_STMT_LABEL (t));
881           break;
882
883         case GOTO_STMT:
884           /* Emit information for branch prediction.  */
885           if (!GOTO_FAKE_P (t)
886               && TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL
887               && flag_guess_branch_prob)
888             {
889               rtx note = emit_note (NULL, NOTE_INSN_PREDICTION);
890
891               NOTE_PREDICTION (note) = NOTE_PREDICT (PRED_GOTO, NOT_TAKEN);
892             }
893           genrtl_goto_stmt (GOTO_DESTINATION (t));
894           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
895           goto process_t;
896
897         case ASM_STMT:
898           genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
899                            ASM_OUTPUTS (t), ASM_INPUTS (t),
900                            ASM_CLOBBERS (t), ASM_INPUT_P (t));
901           break;
902
903         case SCOPE_STMT:
904           genrtl_scope_stmt (t);
905           break;
906
907         case CLEANUP_STMT:
908           genrtl_cleanup_stmt (t);
909           break;
910
911         default:
912           if (lang_expand_stmt)
913             (*lang_expand_stmt) (t);
914           else 
915             abort ();
916           break;
917         }
918
919       /* Go on to the next statement in this scope.  */
920       t = TREE_CHAIN (t);
921
922     process_t:
923       /* Restore saved state.  */
924       current_stmt_tree ()->stmts_are_full_exprs_p
925         = saved_stmts_are_full_exprs_p;
926     }
927 }
928 \f
929 /* If *TP is a potentially reachable label, return nonzero.  */
930
931 static tree
932 find_reachable_label_1 (tp, walk_subtrees, data)
933      tree *tp;
934      int *walk_subtrees ATTRIBUTE_UNUSED;
935      void *data ATTRIBUTE_UNUSED;
936 {
937   switch (TREE_CODE (*tp))
938     {
939     case LABEL_STMT:
940     case CASE_LABEL:
941       return *tp;
942
943     default:
944       break;
945     }
946   return NULL_TREE;
947 }
948
949 /* Determine whether expression EXP contains a potentially
950    reachable label.  */
951 static tree
952 find_reachable_label (exp)
953      tree exp;
954 {
955   location_t saved_loc = input_location;
956   tree ret = walk_tree (&exp, find_reachable_label_1, NULL, NULL);
957   input_location = saved_loc;
958   return ret;
959 }
960
961 /* Expand an unreachable if statement, T.  This function returns
962    true if the IF_STMT contains a potentially reachable code_label.  */
963 static bool
964 expand_unreachable_if_stmt (t)
965      tree t;
966 {
967   tree n;
968   
969   if (find_reachable_label (IF_COND (t)) != NULL_TREE)
970     {
971       genrtl_if_stmt (t);
972       return true;
973     }
974
975   if (THEN_CLAUSE (t) && ELSE_CLAUSE (t))
976     {
977       n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);
978       
979       if (n != NULL_TREE)
980         {
981           rtx label;
982           expand_stmt (n);
983           label = gen_label_rtx ();
984           emit_jump (label);
985           expand_stmt (expand_unreachable_stmt (ELSE_CLAUSE (t), 0));
986           emit_label (label);
987           return true;
988         }
989       else
990         n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
991     }
992   else if (THEN_CLAUSE (t))
993     n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);
994   else if (ELSE_CLAUSE (t))
995     n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
996   else
997     n = NULL_TREE;
998   
999   expand_stmt (n);
1000   
1001   return n != NULL_TREE;
1002 }
1003
1004 /* Expand an unreachable statement list.  This function skips all
1005    statements preceding the first potentially reachable label and
1006    then returns the label (or, in same cases, the statement after
1007    one containing the label).  */
1008 static tree
1009 expand_unreachable_stmt (t, warn)
1010      tree t;
1011      int warn;
1012 {
1013   int saved;
1014
1015   while (t && t != error_mark_node)
1016     {
1017       if (warn)
1018         switch (TREE_CODE (t))
1019           {
1020           case BREAK_STMT:
1021           case CONTINUE_STMT:
1022           case EXPR_STMT:
1023           case GOTO_STMT:
1024           case IF_STMT:
1025           case RETURN_STMT:
1026             if (!STMT_LINENO_FOR_FN_P (t))
1027               input_line = STMT_LINENO (t);
1028             warning("will never be executed");
1029             warn = false;
1030             break;
1031
1032           default:
1033             break;
1034           }
1035
1036       switch (TREE_CODE (t))
1037         {
1038         case GOTO_STMT:
1039         case CONTINUE_STMT:
1040         case BREAK_STMT:
1041           break;
1042
1043         case FILE_STMT:
1044           input_filename = FILE_STMT_FILENAME (t);
1045           break;
1046
1047         case RETURN_STMT:
1048           if (find_reachable_label (RETURN_STMT_EXPR (t)) != NULL_TREE)
1049             return t;
1050           break;
1051
1052         case EXPR_STMT:
1053           if (find_reachable_label (EXPR_STMT_EXPR (t)) != NULL_TREE)
1054             return t;
1055           break;
1056
1057         case IF_STMT:
1058           if (expand_unreachable_if_stmt (t))
1059             return TREE_CHAIN (t);
1060           break;
1061
1062         case COMPOUND_STMT:
1063           {
1064             tree n;
1065             n = expand_unreachable_stmt (COMPOUND_BODY (t), warn);
1066             if (n != NULL_TREE)
1067               {
1068                 expand_stmt (n);
1069                 return TREE_CHAIN (t);
1070               }
1071             warn = false;
1072             break;
1073           }
1074
1075         case SCOPE_STMT:
1076           saved = stmts_are_full_exprs_p ();
1077           prep_stmt (t);
1078           genrtl_scope_stmt (t);
1079           current_stmt_tree ()->stmts_are_full_exprs_p = saved;
1080           break;
1081
1082         default:
1083           return t;
1084         }
1085       t = TREE_CHAIN (t);
1086     }
1087   return NULL_TREE;
1088 }
1089