OSDN Git Service

Use new macro AGGREGATE_TYPE_P.
[pf3gnuchains/gcc-fork.git] / gcc / stmt.c
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2    Copyright (C) 1987, 88, 89, 92, 93, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This file handles the generation of rtl code from tree structure
22    above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
23    It also creates the rtl expressions for parameters and auto variables
24    and has full responsibility for allocating stack slots.
25
26    The functions whose names start with `expand_' are called by the
27    parser to generate RTL instructions for various kinds of constructs.
28
29    Some control and binding constructs require calling several such
30    functions at different times.  For example, a simple if-then
31    is expanded by calling `expand_start_cond' (with the condition-expression
32    as argument) before parsing the then-clause and calling `expand_end_cond'
33    after parsing the then-clause.  */
34
35 #include "config.h"
36
37 #include <stdio.h>
38 #include <ctype.h>
39
40 #include "rtl.h"
41 #include "tree.h"
42 #include "flags.h"
43 #include "function.h"
44 #include "insn-flags.h"
45 #include "insn-config.h"
46 #include "insn-codes.h"
47 #include "expr.h"
48 #include "hard-reg-set.h"
49 #include "obstack.h"
50 #include "loop.h"
51 #include "recog.h"
52 #include "machmode.h"
53
54 #include "bytecode.h"
55 #include "bc-typecd.h"
56 #include "bc-opcode.h"
57 #include "bc-optab.h"
58 #include "bc-emit.h"
59
60 #define obstack_chunk_alloc xmalloc
61 #define obstack_chunk_free free
62 struct obstack stmt_obstack;
63
64 /* Filename and line number of last line-number note,
65    whether we actually emitted it or not.  */
66 char *emit_filename;
67 int emit_lineno;
68
69 /* Nonzero if within a ({...}) grouping, in which case we must
70    always compute a value for each expr-stmt in case it is the last one.  */
71
72 int expr_stmts_for_value;
73
74 /* Each time we expand an expression-statement,
75    record the expr's type and its RTL value here.  */
76
77 static tree last_expr_type;
78 static rtx last_expr_value;
79
80 /* Each time we expand the end of a binding contour (in `expand_end_bindings')
81    and we emit a new NOTE_INSN_BLOCK_END note, we save a pointer to it here.
82    This is used by the `remember_end_note' function to record the endpoint
83    of each generated block in its associated BLOCK node.  */
84
85 static rtx last_block_end_note;
86
87 /* Number of binding contours started so far in this function.  */
88
89 int block_start_count;
90
91 /* Nonzero if function being compiled needs to
92    return the address of where it has put a structure value.  */
93
94 extern int current_function_returns_pcc_struct;
95
96 /* Label that will go on parm cleanup code, if any.
97    Jumping to this label runs cleanup code for parameters, if
98    such code must be run.  Following this code is the logical return label.  */
99
100 extern rtx cleanup_label;
101
102 /* Label that will go on function epilogue.
103    Jumping to this label serves as a "return" instruction
104    on machines which require execution of the epilogue on all returns.  */
105
106 extern rtx return_label;
107
108 /* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
109    So we can mark them all live at the end of the function, if nonopt.  */
110 extern rtx save_expr_regs;
111
112 /* Offset to end of allocated area of stack frame.
113    If stack grows down, this is the address of the last stack slot allocated.
114    If stack grows up, this is the address for the next slot.  */
115 extern int frame_offset;
116
117 /* Label to jump back to for tail recursion, or 0 if we have
118    not yet needed one for this function.  */
119 extern rtx tail_recursion_label;
120
121 /* Place after which to insert the tail_recursion_label if we need one.  */
122 extern rtx tail_recursion_reentry;
123
124 /* Location at which to save the argument pointer if it will need to be
125    referenced.  There are two cases where this is done: if nonlocal gotos
126    exist, or if vars whose is an offset from the argument pointer will be
127    needed by inner routines.  */
128
129 extern rtx arg_pointer_save_area;
130
131 /* Chain of all RTL_EXPRs that have insns in them.  */
132 extern tree rtl_expr_chain;
133
134 #if 0  /* Turned off because 0 seems to work just as well.  */
135 /* Cleanup lists are required for binding levels regardless of whether
136    that binding level has cleanups or not.  This node serves as the
137    cleanup list whenever an empty list is required.  */
138 static tree empty_cleanup_list;
139 #endif
140 \f
141 /* Functions and data structures for expanding case statements.  */
142
143 /* Case label structure, used to hold info on labels within case
144    statements.  We handle "range" labels; for a single-value label
145    as in C, the high and low limits are the same.
146
147    A chain of case nodes is initially maintained via the RIGHT fields
148    in the nodes.  Nodes with higher case values are later in the list.
149
150    Switch statements can be output in one of two forms.  A branch table
151    is used if there are more than a few labels and the labels are dense
152    within the range between the smallest and largest case value.  If a
153    branch table is used, no further manipulations are done with the case
154    node chain.
155
156    The alternative to the use of a branch table is to generate a series
157    of compare and jump insns.  When that is done, we use the LEFT, RIGHT,
158    and PARENT fields to hold a binary tree.  Initially the tree is
159    totally unbalanced, with everything on the right.  We balance the tree
160    with nodes on the left having lower case values than the parent
161    and nodes on the right having higher values.  We then output the tree
162    in order.  */
163
164 struct case_node
165 {
166   struct case_node      *left;  /* Left son in binary tree */
167   struct case_node      *right; /* Right son in binary tree; also node chain */
168   struct case_node      *parent; /* Parent of node in binary tree */
169   tree                  low;    /* Lowest index value for this label */
170   tree                  high;   /* Highest index value for this label */
171   tree                  code_label; /* Label to jump to when node matches */
172 };
173
174 typedef struct case_node case_node;
175 typedef struct case_node *case_node_ptr;
176
177 /* These are used by estimate_case_costs and balance_case_nodes.  */
178
179 /* This must be a signed type, and non-ANSI compilers lack signed char.  */
180 static short *cost_table;
181 static int use_cost_table;
182 \f
183 /* Stack of control and binding constructs we are currently inside.
184
185    These constructs begin when you call `expand_start_WHATEVER'
186    and end when you call `expand_end_WHATEVER'.  This stack records
187    info about how the construct began that tells the end-function
188    what to do.  It also may provide information about the construct
189    to alter the behavior of other constructs within the body.
190    For example, they may affect the behavior of C `break' and `continue'.
191
192    Each construct gets one `struct nesting' object.
193    All of these objects are chained through the `all' field.
194    `nesting_stack' points to the first object (innermost construct).
195    The position of an entry on `nesting_stack' is in its `depth' field.
196
197    Each type of construct has its own individual stack.
198    For example, loops have `loop_stack'.  Each object points to the
199    next object of the same type through the `next' field.
200
201    Some constructs are visible to `break' exit-statements and others
202    are not.  Which constructs are visible depends on the language.
203    Therefore, the data structure allows each construct to be visible
204    or not, according to the args given when the construct is started.
205    The construct is visible if the `exit_label' field is non-null.
206    In that case, the value should be a CODE_LABEL rtx.  */
207
208 struct nesting
209 {
210   struct nesting *all;
211   struct nesting *next;
212   int depth;
213   rtx exit_label;
214   union
215     {
216       /* For conds (if-then and if-then-else statements).  */
217       struct
218         {
219           /* Label for the end of the if construct.
220              There is none if EXITFLAG was not set
221              and no `else' has been seen yet.  */
222           rtx endif_label;
223           /* Label for the end of this alternative.
224              This may be the end of the if or the next else/elseif. */
225           rtx next_label;
226         } cond;
227       /* For loops.  */
228       struct
229         {
230           /* Label at the top of the loop; place to loop back to.  */
231           rtx start_label;
232           /* Label at the end of the whole construct.  */
233           rtx end_label;
234           /* Label for `continue' statement to jump to;
235              this is in front of the stepper of the loop.  */
236           rtx continue_label;
237         } loop;
238       /* For variable binding contours.  */
239       struct
240         {
241           /* Sequence number of this binding contour within the function,
242              in order of entry.  */
243           int block_start_count;
244           /* Nonzero => value to restore stack to on exit.  Complemented by
245              bc_stack_level (see below) when generating bytecodes. */
246           rtx stack_level;
247           /* The NOTE that starts this contour.
248              Used by expand_goto to check whether the destination
249              is within each contour or not.  */
250           rtx first_insn;
251           /* Innermost containing binding contour that has a stack level.  */
252           struct nesting *innermost_stack_block;
253           /* List of cleanups to be run on exit from this contour.
254              This is a list of expressions to be evaluated.
255              The TREE_PURPOSE of each link is the ..._DECL node
256              which the cleanup pertains to.  */
257           tree cleanups;
258           /* List of cleanup-lists of blocks containing this block,
259              as they were at the locus where this block appears.
260              There is an element for each containing block,
261              ordered innermost containing block first.
262              The tail of this list can be 0 (was empty_cleanup_list),
263              if all remaining elements would be empty lists.
264              The element's TREE_VALUE is the cleanup-list of that block,
265              which may be null.  */
266           tree outer_cleanups;
267           /* Chain of labels defined inside this binding contour.
268              For contours that have stack levels or cleanups.  */
269           struct label_chain *label_chain;
270           /* Number of function calls seen, as of start of this block.  */
271           int function_call_count;
272           /* Bytecode specific: stack level to restore stack to on exit.  */
273           int bc_stack_level;
274         } block;
275       /* For switch (C) or case (Pascal) statements,
276          and also for dummies (see `expand_start_case_dummy').  */
277       struct
278         {
279           /* The insn after which the case dispatch should finally
280              be emitted.  Zero for a dummy.  */
281           rtx start;
282           /* For bytecodes, the case table is in-lined right in the code.
283              A label is needed for skipping over this block. It is only
284              used when generating bytecodes. */
285           rtx skip_label;
286           /* A list of case labels, kept in ascending order by value
287              as the list is built.
288              During expand_end_case, this list may be rearranged into a
289              nearly balanced binary tree.  */
290           struct case_node *case_list;
291           /* Label to jump to if no case matches.  */
292           tree default_label;
293           /* The expression to be dispatched on.  */
294           tree index_expr;
295           /* Type that INDEX_EXPR should be converted to.  */
296           tree nominal_type;
297           /* Number of range exprs in case statement.  */
298           int num_ranges;
299           /* Name of this kind of statement, for warnings.  */
300           char *printname;
301           /* Nonzero if a case label has been seen in this case stmt.  */
302           char seenlabel;
303         } case_stmt;
304       /* For exception contours.  */
305       struct
306         {
307           /* List of exceptions raised.  This is a TREE_LIST
308              of whatever you want.  */
309           tree raised;
310           /* List of exceptions caught.  This is also a TREE_LIST
311              of whatever you want.  As a special case, it has the
312              value `void_type_node' if it handles default exceptions.  */
313           tree handled;
314
315           /* First insn of TRY block, in case resumptive model is needed.  */
316           rtx first_insn;
317           /* Label for the catch clauses.  */
318           rtx except_label;
319           /* Label for unhandled exceptions.  */
320           rtx unhandled_label;
321           /* Label at the end of whole construct.  */
322           rtx after_label;
323           /* Label which "escapes" the exception construct.
324              Like EXIT_LABEL for BREAK construct, but for exceptions.  */
325           rtx escape_label;
326         } except_stmt;
327     } data;
328 };
329
330 /* Chain of all pending binding contours.  */
331 struct nesting *block_stack;
332
333 /* If any new stacks are added here, add them to POPSTACKS too.  */
334
335 /* Chain of all pending binding contours that restore stack levels
336    or have cleanups.  */
337 struct nesting *stack_block_stack;
338
339 /* Chain of all pending conditional statements.  */
340 struct nesting *cond_stack;
341
342 /* Chain of all pending loops.  */
343 struct nesting *loop_stack;
344
345 /* Chain of all pending case or switch statements.  */
346 struct nesting *case_stack;
347
348 /* Chain of all pending exception contours.  */
349 struct nesting *except_stack;
350
351 /* Separate chain including all of the above,
352    chained through the `all' field.  */
353 struct nesting *nesting_stack;
354
355 /* Number of entries on nesting_stack now.  */
356 int nesting_depth;
357
358 /* Allocate and return a new `struct nesting'.  */
359
360 #define ALLOC_NESTING() \
361  (struct nesting *) obstack_alloc (&stmt_obstack, sizeof (struct nesting))
362
363 /* Pop the nesting stack element by element until we pop off
364    the element which is at the top of STACK.
365    Update all the other stacks, popping off elements from them
366    as we pop them from nesting_stack.  */
367
368 #define POPSTACK(STACK)                                 \
369 do { struct nesting *target = STACK;                    \
370      struct nesting *this;                              \
371      do { this = nesting_stack;                         \
372           if (loop_stack == this)                       \
373             loop_stack = loop_stack->next;              \
374           if (cond_stack == this)                       \
375             cond_stack = cond_stack->next;              \
376           if (block_stack == this)                      \
377             block_stack = block_stack->next;            \
378           if (stack_block_stack == this)                \
379             stack_block_stack = stack_block_stack->next; \
380           if (case_stack == this)                       \
381             case_stack = case_stack->next;              \
382           if (except_stack == this)                     \
383             except_stack = except_stack->next;          \
384           nesting_depth = nesting_stack->depth - 1;     \
385           nesting_stack = this->all;                    \
386           obstack_free (&stmt_obstack, this); }         \
387      while (this != target); } while (0)
388 \f
389 /* In some cases it is impossible to generate code for a forward goto
390    until the label definition is seen.  This happens when it may be necessary
391    for the goto to reset the stack pointer: we don't yet know how to do that.
392    So expand_goto puts an entry on this fixup list.
393    Each time a binding contour that resets the stack is exited,
394    we check each fixup.
395    If the target label has now been defined, we can insert the proper code.  */
396
397 struct goto_fixup
398 {
399   /* Points to following fixup.  */
400   struct goto_fixup *next;
401   /* Points to the insn before the jump insn.
402      If more code must be inserted, it goes after this insn.  */
403   rtx before_jump;
404   /* The LABEL_DECL that this jump is jumping to, or 0
405      for break, continue or return.  */
406   tree target;
407   /* The BLOCK for the place where this goto was found.  */
408   tree context;
409   /* The CODE_LABEL rtx that this is jumping to.  */
410   rtx target_rtl;
411   /* Number of binding contours started in current function
412      before the label reference.  */
413   int block_start_count;
414   /* The outermost stack level that should be restored for this jump.
415      Each time a binding contour that resets the stack is exited,
416      if the target label is *not* yet defined, this slot is updated.  */
417   rtx stack_level;
418   /* List of lists of cleanup expressions to be run by this goto.
419      There is one element for each block that this goto is within.
420      The tail of this list can be 0 (was empty_cleanup_list),
421      if all remaining elements would be empty.
422      The TREE_VALUE contains the cleanup list of that block as of the
423      time this goto was seen.
424      The TREE_ADDRESSABLE flag is 1 for a block that has been exited.  */
425   tree cleanup_list_list;
426
427   /* Bytecode specific members follow */
428
429   /* The label that this jump is jumping to, or 0 for break, continue
430      or return.  */
431   struct bc_label *bc_target;
432
433   /* The label we use for the fixup patch */
434   struct bc_label *label;
435
436   /* True (non-0) if fixup has been handled */
437   int bc_handled:1;
438
439   /* Like stack_level above, except refers to the interpreter stack */
440   int bc_stack_level;
441 };
442
443 static struct goto_fixup *goto_fixup_chain;
444
445 /* Within any binding contour that must restore a stack level,
446    all labels are recorded with a chain of these structures.  */
447
448 struct label_chain
449 {
450   /* Points to following fixup.  */
451   struct label_chain *next;
452   tree label;
453 };
454 static void expand_goto_internal        PROTO((tree, rtx, rtx));
455 static void bc_expand_goto_internal     PROTO((enum bytecode_opcode,
456                                                struct bc_label *, tree));
457 static int expand_fixup                 PROTO((tree, rtx, rtx));
458 static void bc_expand_fixup             PROTO((enum bytecode_opcode,
459                                                struct bc_label *, int));
460 static void fixup_gotos                 PROTO((struct nesting *, rtx, tree,
461                                                rtx, int));
462 static void bc_fixup_gotos              PROTO((struct nesting *, int, tree,
463                                                rtx, int));
464 static int warn_if_unused_value         PROTO((tree));
465 static void bc_expand_start_cond        PROTO((tree, int));
466 static void bc_expand_end_cond          PROTO((void));
467 static void bc_expand_start_else        PROTO((void));
468 static void bc_expand_end_loop          PROTO((void));
469 static void bc_expand_end_bindings      PROTO((tree, int, int));
470 static void bc_expand_decl              PROTO((tree, tree));
471 static void bc_expand_variable_local_init PROTO((tree));
472 static void bc_expand_decl_init         PROTO((tree));
473 static void expand_null_return_1        PROTO((rtx, int));
474 static int tail_recursion_args          PROTO((tree, tree));
475 static void expand_cleanups             PROTO((tree, tree));
476 static void bc_expand_start_case        PROTO((struct nesting *, tree,
477                                                tree, char *));
478 static int bc_pushcase                  PROTO((tree, tree));
479 static void bc_check_for_full_enumeration_handling PROTO((tree));
480 static void bc_expand_end_case          PROTO((tree));
481 static void do_jump_if_equal            PROTO((rtx, rtx, rtx, int));
482 static int estimate_case_costs          PROTO((case_node_ptr));
483 static void group_case_nodes            PROTO((case_node_ptr));
484 static void balance_case_nodes          PROTO((case_node_ptr *,
485                                                case_node_ptr));
486 static int node_has_low_bound           PROTO((case_node_ptr, tree));
487 static int node_has_high_bound          PROTO((case_node_ptr, tree));
488 static int node_is_bounded              PROTO((case_node_ptr, tree));
489 static void emit_jump_if_reachable      PROTO((rtx));
490 static void emit_case_nodes             PROTO((rtx, case_node_ptr, rtx, tree));
491
492 int bc_expand_exit_loop_if_false ();
493 void bc_expand_start_cond ();
494 void bc_expand_end_cond ();
495 void bc_expand_start_else ();
496 void bc_expand_end_bindings ();
497 void bc_expand_start_case ();
498 void bc_check_for_full_enumeration_handling ();
499 void bc_expand_end_case ();
500 void bc_expand_decl ();
501
502 extern rtx bc_allocate_local ();
503 extern rtx bc_allocate_variable_array ();
504 \f
505 void
506 init_stmt ()
507 {
508   gcc_obstack_init (&stmt_obstack);
509 #if 0
510   empty_cleanup_list = build_tree_list (NULL_TREE, NULL_TREE);
511 #endif
512 }
513
514 void
515 init_stmt_for_function ()
516 {
517   /* We are not currently within any block, conditional, loop or case.  */
518   block_stack = 0;
519   stack_block_stack = 0;
520   loop_stack = 0;
521   case_stack = 0;
522   cond_stack = 0;
523   nesting_stack = 0;
524   nesting_depth = 0;
525
526   block_start_count = 0;
527
528   /* No gotos have been expanded yet.  */
529   goto_fixup_chain = 0;
530
531   /* We are not processing a ({...}) grouping.  */
532   expr_stmts_for_value = 0;
533   last_expr_type = 0;
534 }
535
536 void
537 save_stmt_status (p)
538      struct function *p;
539 {
540   p->block_stack = block_stack;
541   p->stack_block_stack = stack_block_stack;
542   p->cond_stack = cond_stack;
543   p->loop_stack = loop_stack;
544   p->case_stack = case_stack;
545   p->nesting_stack = nesting_stack;
546   p->nesting_depth = nesting_depth;
547   p->block_start_count = block_start_count;
548   p->last_expr_type = last_expr_type;
549   p->last_expr_value = last_expr_value;
550   p->expr_stmts_for_value = expr_stmts_for_value;
551   p->emit_filename = emit_filename;
552   p->emit_lineno = emit_lineno;
553   p->goto_fixup_chain = goto_fixup_chain;
554 }
555
556 void
557 restore_stmt_status (p)
558      struct function *p;
559 {
560   block_stack = p->block_stack;
561   stack_block_stack = p->stack_block_stack;
562   cond_stack = p->cond_stack;
563   loop_stack = p->loop_stack;
564   case_stack = p->case_stack;
565   nesting_stack = p->nesting_stack;
566   nesting_depth = p->nesting_depth;
567   block_start_count = p->block_start_count;
568   last_expr_type = p->last_expr_type;
569   last_expr_value = p->last_expr_value;
570   expr_stmts_for_value = p->expr_stmts_for_value;
571   emit_filename = p->emit_filename;
572   emit_lineno = p->emit_lineno;
573   goto_fixup_chain = p->goto_fixup_chain;
574 }
575 \f
576 /* Emit a no-op instruction.  */
577
578 void
579 emit_nop ()
580 {
581   rtx last_insn;
582
583   if (!output_bytecode)
584     {
585       last_insn = get_last_insn ();
586       if (!optimize
587           && (GET_CODE (last_insn) == CODE_LABEL
588               || prev_real_insn (last_insn) == 0))
589         emit_insn (gen_nop ());
590     }
591 }
592 \f
593 /* Return the rtx-label that corresponds to a LABEL_DECL,
594    creating it if necessary.  */
595
596 rtx
597 label_rtx (label)
598      tree label;
599 {
600   if (TREE_CODE (label) != LABEL_DECL)
601     abort ();
602
603   if (DECL_RTL (label))
604     return DECL_RTL (label);
605
606   return DECL_RTL (label) = gen_label_rtx ();
607 }
608
609 /* Add an unconditional jump to LABEL as the next sequential instruction.  */
610
611 void
612 emit_jump (label)
613      rtx label;
614 {
615   do_pending_stack_adjust ();
616   emit_jump_insn (gen_jump (label));
617   emit_barrier ();
618 }
619
620 /* Emit code to jump to the address
621    specified by the pointer expression EXP.  */
622
623 void
624 expand_computed_goto (exp)
625      tree exp;
626 {
627   if (output_bytecode)
628     {
629       bc_expand_expr (exp);
630       bc_emit_instruction (jumpP);
631     }
632   else
633     {
634       rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
635       emit_queue ();
636       emit_indirect_jump (x);
637     }
638 }
639 \f
640 /* Handle goto statements and the labels that they can go to.  */
641
642 /* Specify the location in the RTL code of a label LABEL,
643    which is a LABEL_DECL tree node.
644
645    This is used for the kind of label that the user can jump to with a
646    goto statement, and for alternatives of a switch or case statement.
647    RTL labels generated for loops and conditionals don't go through here;
648    they are generated directly at the RTL level, by other functions below.
649
650    Note that this has nothing to do with defining label *names*.
651    Languages vary in how they do that and what that even means.  */
652
653 void
654 expand_label (label)
655      tree label;
656 {
657   struct label_chain *p;
658
659   if (output_bytecode)
660     {
661       if (! DECL_RTL (label))
662         DECL_RTL (label) = bc_gen_rtx ((char *) 0, 0, bc_get_bytecode_label ());
663       if (! bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (DECL_RTL (label))))
664         error ("multiply defined label");
665       return;
666     }
667
668   do_pending_stack_adjust ();
669   emit_label (label_rtx (label));
670   if (DECL_NAME (label))
671     LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
672
673   if (stack_block_stack != 0)
674     {
675       p = (struct label_chain *) oballoc (sizeof (struct label_chain));
676       p->next = stack_block_stack->data.block.label_chain;
677       stack_block_stack->data.block.label_chain = p;
678       p->label = label;
679     }
680 }
681
682 /* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
683    from nested functions.  */
684
685 void
686 declare_nonlocal_label (label)
687      tree label;
688 {
689   nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
690   LABEL_PRESERVE_P (label_rtx (label)) = 1;
691   if (nonlocal_goto_handler_slot == 0)
692     {
693       nonlocal_goto_handler_slot
694         = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
695       emit_stack_save (SAVE_NONLOCAL,
696                        &nonlocal_goto_stack_level,
697                        PREV_INSN (tail_recursion_reentry));
698     }
699 }
700
701 /* Generate RTL code for a `goto' statement with target label LABEL.
702    LABEL should be a LABEL_DECL tree node that was or will later be
703    defined with `expand_label'.  */
704
705 void
706 expand_goto (label)
707      tree label;
708 {
709   tree context;
710
711   if (output_bytecode)
712     {
713       expand_goto_internal (label, label_rtx (label), NULL_RTX);
714       return;
715     }
716
717   /* Check for a nonlocal goto to a containing function.  */
718   context = decl_function_context (label);
719   if (context != 0 && context != current_function_decl)
720     {
721       struct function *p = find_function_data (context);
722       rtx label_ref = gen_rtx (LABEL_REF, Pmode, label_rtx (label));
723       rtx temp;
724
725       p->has_nonlocal_label = 1;
726       current_function_has_nonlocal_goto = 1;
727       LABEL_REF_NONLOCAL_P (label_ref) = 1;
728
729       /* Copy the rtl for the slots so that they won't be shared in
730          case the virtual stack vars register gets instantiated differently
731          in the parent than in the child.  */
732
733 #if HAVE_nonlocal_goto
734       if (HAVE_nonlocal_goto)
735         emit_insn (gen_nonlocal_goto (lookup_static_chain (label),
736                                       copy_rtx (p->nonlocal_goto_handler_slot),
737                                       copy_rtx (p->nonlocal_goto_stack_level),
738                                       label_ref));
739       else
740 #endif
741         {
742           rtx addr;
743
744           /* Restore frame pointer for containing function.
745              This sets the actual hard register used for the frame pointer
746              to the location of the function's incoming static chain info.
747              The non-local goto handler will then adjust it to contain the
748              proper value and reload the argument pointer, if needed.  */
749           emit_move_insn (hard_frame_pointer_rtx, lookup_static_chain (label));
750
751           /* We have now loaded the frame pointer hardware register with
752              the address of that corresponds to the start of the virtual
753              stack vars.  So replace virtual_stack_vars_rtx in all
754              addresses we use with stack_pointer_rtx.  */
755
756           /* Get addr of containing function's current nonlocal goto handler,
757              which will do any cleanups and then jump to the label.  */
758           addr = copy_rtx (p->nonlocal_goto_handler_slot);
759           temp = copy_to_reg (replace_rtx (addr, virtual_stack_vars_rtx,
760                                            hard_frame_pointer_rtx));
761           
762           /* Restore the stack pointer.  Note this uses fp just restored.  */
763           addr = p->nonlocal_goto_stack_level;
764           if (addr)
765             addr = replace_rtx (copy_rtx (addr),
766                                 virtual_stack_vars_rtx,
767                                 hard_frame_pointer_rtx);
768
769           emit_stack_restore (SAVE_NONLOCAL, addr, NULL_RTX);
770
771           /* Put in the static chain register the nonlocal label address.  */
772           emit_move_insn (static_chain_rtx, label_ref);
773           /* USE of hard_frame_pointer_rtx added for consistency; not clear if
774              really needed.  */
775           emit_insn (gen_rtx (USE, VOIDmode, hard_frame_pointer_rtx));
776           emit_insn (gen_rtx (USE, VOIDmode, stack_pointer_rtx));
777           emit_insn (gen_rtx (USE, VOIDmode, static_chain_rtx));
778           emit_indirect_jump (temp);
779         }
780      }
781   else
782     expand_goto_internal (label, label_rtx (label), NULL_RTX);
783 }
784
785 /* Generate RTL code for a `goto' statement with target label BODY.
786    LABEL should be a LABEL_REF.
787    LAST_INSN, if non-0, is the rtx we should consider as the last
788    insn emitted (for the purposes of cleaning up a return).  */
789
790 static void
791 expand_goto_internal (body, label, last_insn)
792      tree body;
793      rtx label;
794      rtx last_insn;
795 {
796   struct nesting *block;
797   rtx stack_level = 0;
798
799   /* NOTICE!  If a bytecode instruction other than `jump' is needed,
800      then the caller has to call bc_expand_goto_internal()
801      directly. This is rather an exceptional case, and there aren't
802      that many places where this is necessary. */
803   if (output_bytecode)
804     {
805       expand_goto_internal (body, label, last_insn);
806       return;
807     }
808
809   if (GET_CODE (label) != CODE_LABEL)
810     abort ();
811
812   /* If label has already been defined, we can tell now
813      whether and how we must alter the stack level.  */
814
815   if (PREV_INSN (label) != 0)
816     {
817       /* Find the innermost pending block that contains the label.
818          (Check containment by comparing insn-uids.)
819          Then restore the outermost stack level within that block,
820          and do cleanups of all blocks contained in it.  */
821       for (block = block_stack; block; block = block->next)
822         {
823           if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
824             break;
825           if (block->data.block.stack_level != 0)
826             stack_level = block->data.block.stack_level;
827           /* Execute the cleanups for blocks we are exiting.  */
828           if (block->data.block.cleanups != 0)
829             {
830               expand_cleanups (block->data.block.cleanups, NULL_TREE);
831               do_pending_stack_adjust ();
832             }
833         }
834
835       if (stack_level)
836         {
837           /* Ensure stack adjust isn't done by emit_jump, as this would clobber
838              the stack pointer.  This one should be deleted as dead by flow. */
839           clear_pending_stack_adjust ();
840           do_pending_stack_adjust ();
841           emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
842         }
843
844       if (body != 0 && DECL_TOO_LATE (body))
845         error ("jump to `%s' invalidly jumps into binding contour",
846                IDENTIFIER_POINTER (DECL_NAME (body)));
847     }
848   /* Label not yet defined: may need to put this goto
849      on the fixup list.  */
850   else if (! expand_fixup (body, label, last_insn))
851     {
852       /* No fixup needed.  Record that the label is the target
853          of at least one goto that has no fixup.  */
854       if (body != 0)
855         TREE_ADDRESSABLE (body) = 1;
856     }
857
858   emit_jump (label);
859 }
860 \f
861 /* Generate a jump with OPCODE to the given bytecode LABEL which is
862    found within BODY. */
863
864 static void
865 bc_expand_goto_internal (opcode, label, body)
866      enum bytecode_opcode opcode;
867      struct bc_label *label;
868      tree body;
869 {
870   struct nesting *block;
871   int stack_level = -1;
872
873   /* If the label is defined, adjust the stack as necessary.
874      If it's not defined, we have to push the reference on the
875      fixup list. */
876
877   if (label->defined)
878     {
879
880       /* Find the innermost pending block that contains the label.
881          (Check containment by comparing bytecode uids.)  Then restore the
882          outermost stack level within that block.  */
883
884       for (block = block_stack; block; block = block->next)
885         {
886           if (BYTECODE_BC_LABEL (block->data.block.first_insn)->uid < label->uid)
887             break;
888           if (block->data.block.bc_stack_level)
889             stack_level = block->data.block.bc_stack_level;
890
891           /* Execute the cleanups for blocks we are exiting.  */
892           if (block->data.block.cleanups != 0)
893             {
894               expand_cleanups (block->data.block.cleanups, NULL_TREE);
895               do_pending_stack_adjust ();
896             }
897         }
898
899       /* Restore the stack level. If we need to adjust the stack, we
900          must do so after the jump, since the jump may depend on
901          what's on the stack.  Thus, any stack-modifying conditional
902          jumps (these are the only ones that rely on what's on the
903          stack) go into the fixup list. */
904
905       if (stack_level >= 0
906           && stack_depth != stack_level
907           && opcode != jump)
908
909         bc_expand_fixup (opcode, label, stack_level);
910       else
911         {
912           if (stack_level >= 0)
913             bc_adjust_stack (stack_depth - stack_level);
914
915           if (body && DECL_BIT_FIELD (body))
916             error ("jump to `%s' invalidly jumps into binding contour",
917                    IDENTIFIER_POINTER (DECL_NAME (body)));
918           
919           /* Emit immediate jump */
920           bc_emit_bytecode (opcode);
921           bc_emit_bytecode_labelref (label);
922           
923 #ifdef DEBUG_PRINT_CODE
924           fputc ('\n', stderr);
925 #endif
926         }
927     }
928   else
929     /* Put goto in the fixup list */
930     bc_expand_fixup (opcode, label, stack_level);
931 }
932 \f
933 /* Generate if necessary a fixup for a goto
934    whose target label in tree structure (if any) is TREE_LABEL
935    and whose target in rtl is RTL_LABEL.
936
937    If LAST_INSN is nonzero, we pretend that the jump appears
938    after insn LAST_INSN instead of at the current point in the insn stream.
939
940    The fixup will be used later to insert insns just before the goto.
941    Those insns will restore the stack level as appropriate for the
942    target label, and will (in the case of C++) also invoke any object
943    destructors which have to be invoked when we exit the scopes which
944    are exited by the goto.
945
946    Value is nonzero if a fixup is made.  */
947
948 static int
949 expand_fixup (tree_label, rtl_label, last_insn)
950      tree tree_label;
951      rtx rtl_label;
952      rtx last_insn;
953 {
954   struct nesting *block, *end_block;
955
956   /* See if we can recognize which block the label will be output in.
957      This is possible in some very common cases.
958      If we succeed, set END_BLOCK to that block.
959      Otherwise, set it to 0.  */
960
961   if (cond_stack
962       && (rtl_label == cond_stack->data.cond.endif_label
963           || rtl_label == cond_stack->data.cond.next_label))
964     end_block = cond_stack;
965   /* If we are in a loop, recognize certain labels which
966      are likely targets.  This reduces the number of fixups
967      we need to create.  */
968   else if (loop_stack
969       && (rtl_label == loop_stack->data.loop.start_label
970           || rtl_label == loop_stack->data.loop.end_label
971           || rtl_label == loop_stack->data.loop.continue_label))
972     end_block = loop_stack;
973   else
974     end_block = 0;
975
976   /* Now set END_BLOCK to the binding level to which we will return.  */
977
978   if (end_block)
979     {
980       struct nesting *next_block = end_block->all;
981       block = block_stack;
982
983       /* First see if the END_BLOCK is inside the innermost binding level.
984          If so, then no cleanups or stack levels are relevant.  */
985       while (next_block && next_block != block)
986         next_block = next_block->all;
987
988       if (next_block)
989         return 0;
990
991       /* Otherwise, set END_BLOCK to the innermost binding level
992          which is outside the relevant control-structure nesting.  */
993       next_block = block_stack->next;
994       for (block = block_stack; block != end_block; block = block->all)
995         if (block == next_block)
996           next_block = next_block->next;
997       end_block = next_block;
998     }
999
1000   /* Does any containing block have a stack level or cleanups?
1001      If not, no fixup is needed, and that is the normal case
1002      (the only case, for standard C).  */
1003   for (block = block_stack; block != end_block; block = block->next)
1004     if (block->data.block.stack_level != 0
1005         || block->data.block.cleanups != 0)
1006       break;
1007
1008   if (block != end_block)
1009     {
1010       /* Ok, a fixup is needed.  Add a fixup to the list of such.  */
1011       struct goto_fixup *fixup
1012         = (struct goto_fixup *) oballoc (sizeof (struct goto_fixup));
1013       /* In case an old stack level is restored, make sure that comes
1014          after any pending stack adjust.  */
1015       /* ?? If the fixup isn't to come at the present position,
1016          doing the stack adjust here isn't useful.  Doing it with our
1017          settings at that location isn't useful either.  Let's hope
1018          someone does it!  */
1019       if (last_insn == 0)
1020         do_pending_stack_adjust ();
1021       fixup->target = tree_label;
1022       fixup->target_rtl = rtl_label;
1023
1024       /* Create a BLOCK node and a corresponding matched set of
1025          NOTE_INSN_BEGIN_BLOCK and NOTE_INSN_END_BLOCK notes at
1026          this point.  The notes will encapsulate any and all fixup
1027          code which we might later insert at this point in the insn
1028          stream.  Also, the BLOCK node will be the parent (i.e. the
1029          `SUPERBLOCK') of any other BLOCK nodes which we might create
1030          later on when we are expanding the fixup code.  */
1031
1032       {
1033         register rtx original_before_jump
1034           = last_insn ? last_insn : get_last_insn ();
1035
1036         start_sequence ();
1037         pushlevel (0);
1038         fixup->before_jump = emit_note (NULL_PTR, NOTE_INSN_BLOCK_BEG);
1039         last_block_end_note = emit_note (NULL_PTR, NOTE_INSN_BLOCK_END);
1040         fixup->context = poplevel (1, 0, 0);  /* Create the BLOCK node now! */
1041         end_sequence ();
1042         emit_insns_after (fixup->before_jump, original_before_jump);
1043       }
1044
1045       fixup->block_start_count = block_start_count;
1046       fixup->stack_level = 0;
1047       fixup->cleanup_list_list
1048         = (((block->data.block.outer_cleanups
1049 #if 0
1050              && block->data.block.outer_cleanups != empty_cleanup_list
1051 #endif
1052              )
1053             || block->data.block.cleanups)
1054            ? tree_cons (NULL_TREE, block->data.block.cleanups,
1055                         block->data.block.outer_cleanups)
1056            : 0);
1057       fixup->next = goto_fixup_chain;
1058       goto_fixup_chain = fixup;
1059     }
1060
1061   return block != 0;
1062 }
1063
1064
1065 /* Generate bytecode jump with OPCODE to a fixup routine that links to LABEL.
1066    Make the fixup restore the stack level to STACK_LEVEL.  */
1067
1068 static void
1069 bc_expand_fixup (opcode, label, stack_level)
1070      enum bytecode_opcode opcode;
1071      struct bc_label *label;
1072      int stack_level;
1073 {
1074   struct goto_fixup *fixup
1075     = (struct goto_fixup *) oballoc (sizeof (struct goto_fixup));
1076
1077   fixup->label  = bc_get_bytecode_label ();
1078   fixup->bc_target = label;
1079   fixup->bc_stack_level = stack_level;
1080   fixup->bc_handled = FALSE;
1081
1082   fixup->next = goto_fixup_chain;
1083   goto_fixup_chain = fixup;
1084
1085   /* Insert a jump to the fixup code */
1086   bc_emit_bytecode (opcode);
1087   bc_emit_bytecode_labelref (fixup->label);
1088
1089 #ifdef DEBUG_PRINT_CODE
1090   fputc ('\n', stderr);
1091 #endif
1092 }
1093 \f
1094 /* Expand any needed fixups in the outputmost binding level of the
1095    function.  FIRST_INSN is the first insn in the function.  */
1096
1097 void
1098 expand_fixups (first_insn)
1099      rtx first_insn;
1100 {
1101   fixup_gotos (NULL_PTR, NULL_RTX, NULL_TREE, first_insn, 0);
1102 }
1103
1104 /* When exiting a binding contour, process all pending gotos requiring fixups.
1105    THISBLOCK is the structure that describes the block being exited.
1106    STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
1107    CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
1108    FIRST_INSN is the insn that began this contour.
1109
1110    Gotos that jump out of this contour must restore the
1111    stack level and do the cleanups before actually jumping.
1112
1113    DONT_JUMP_IN nonzero means report error there is a jump into this
1114    contour from before the beginning of the contour.
1115    This is also done if STACK_LEVEL is nonzero.  */
1116
1117 static void
1118 fixup_gotos (thisblock, stack_level, cleanup_list, first_insn, dont_jump_in)
1119      struct nesting *thisblock;
1120      rtx stack_level;
1121      tree cleanup_list;
1122      rtx first_insn;
1123      int dont_jump_in;
1124 {
1125   register struct goto_fixup *f, *prev;
1126
1127   if (output_bytecode)
1128     {
1129       /* ??? The second arg is the bc stack level, which is not the same
1130          as STACK_LEVEL.  I have no idea what should go here, so I'll
1131          just pass 0.  */
1132       bc_fixup_gotos (thisblock, 0, cleanup_list, first_insn, dont_jump_in);
1133       return;
1134     }
1135
1136   /* F is the fixup we are considering; PREV is the previous one.  */
1137   /* We run this loop in two passes so that cleanups of exited blocks
1138      are run first, and blocks that are exited are marked so
1139      afterwards.  */
1140
1141   for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1142     {
1143       /* Test for a fixup that is inactive because it is already handled.  */
1144       if (f->before_jump == 0)
1145         {
1146           /* Delete inactive fixup from the chain, if that is easy to do.  */
1147           if (prev != 0)
1148             prev->next = f->next;
1149         }
1150       /* Has this fixup's target label been defined?
1151          If so, we can finalize it.  */
1152       else if (PREV_INSN (f->target_rtl) != 0)
1153         {
1154           register rtx cleanup_insns;
1155
1156           /* Get the first non-label after the label
1157              this goto jumps to.  If that's before this scope begins,
1158              we don't have a jump into the scope.  */
1159           rtx after_label = f->target_rtl;
1160           while (after_label != 0 && GET_CODE (after_label) == CODE_LABEL)
1161             after_label = NEXT_INSN (after_label);
1162
1163           /* If this fixup jumped into this contour from before the beginning
1164              of this contour, report an error.  */
1165           /* ??? Bug: this does not detect jumping in through intermediate
1166              blocks that have stack levels or cleanups.
1167              It detects only a problem with the innermost block
1168              around the label.  */
1169           if (f->target != 0
1170               && (dont_jump_in || stack_level || cleanup_list)
1171               /* If AFTER_LABEL is 0, it means the jump goes to the end
1172                  of the rtl, which means it jumps into this scope.  */
1173               && (after_label == 0
1174                   || INSN_UID (first_insn) < INSN_UID (after_label))
1175               && INSN_UID (first_insn) > INSN_UID (f->before_jump)
1176               && ! DECL_REGISTER (f->target))
1177             {
1178               error_with_decl (f->target,
1179                                "label `%s' used before containing binding contour");
1180               /* Prevent multiple errors for one label.  */
1181               DECL_REGISTER (f->target) = 1;
1182             }
1183
1184           /* We will expand the cleanups into a sequence of their own and
1185              then later on we will attach this new sequence to the insn
1186              stream just ahead of the actual jump insn.  */
1187
1188           start_sequence ();
1189
1190           /* Temporarily restore the lexical context where we will
1191              logically be inserting the fixup code.  We do this for the
1192              sake of getting the debugging information right.  */
1193
1194           pushlevel (0);
1195           set_block (f->context);
1196
1197           /* Expand the cleanups for blocks this jump exits.  */
1198           if (f->cleanup_list_list)
1199             {
1200               tree lists;
1201               for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
1202                 /* Marked elements correspond to blocks that have been closed.
1203                    Do their cleanups.  */
1204                 if (TREE_ADDRESSABLE (lists)
1205                     && TREE_VALUE (lists) != 0)
1206                   {
1207                     expand_cleanups (TREE_VALUE (lists), 0);
1208                     /* Pop any pushes done in the cleanups,
1209                        in case function is about to return.  */
1210                     do_pending_stack_adjust ();
1211                   }
1212             }
1213
1214           /* Restore stack level for the biggest contour that this
1215              jump jumps out of.  */
1216           if (f->stack_level)
1217             emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
1218
1219           /* Finish up the sequence containing the insns which implement the
1220              necessary cleanups, and then attach that whole sequence to the
1221              insn stream just ahead of the actual jump insn.  Attaching it
1222              at that point insures that any cleanups which are in fact
1223              implicit C++ object destructions (which must be executed upon
1224              leaving the block) appear (to the debugger) to be taking place
1225              in an area of the generated code where the object(s) being
1226              destructed are still "in scope".  */
1227
1228           cleanup_insns = get_insns ();
1229           poplevel (1, 0, 0);
1230
1231           end_sequence ();
1232           emit_insns_after (cleanup_insns, f->before_jump);
1233
1234
1235           f->before_jump = 0;
1236         }
1237     }
1238
1239   /* Mark the cleanups of exited blocks so that they are executed
1240      by the code above.  */
1241   for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1242     if (f->before_jump != 0
1243         && PREV_INSN (f->target_rtl) == 0
1244         /* Label has still not appeared.  If we are exiting a block with
1245            a stack level to restore, that started before the fixup,
1246            mark this stack level as needing restoration
1247            when the fixup is later finalized.
1248            Also mark the cleanup_list_list element for F
1249            that corresponds to this block, so that ultimately
1250            this block's cleanups will be executed by the code above.  */
1251         && thisblock != 0
1252         /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared,
1253            it means the label is undefined.  That's erroneous, but possible.  */
1254         && (thisblock->data.block.block_start_count
1255             <= f->block_start_count))
1256       {
1257         tree lists = f->cleanup_list_list;
1258         for (; lists; lists = TREE_CHAIN (lists))
1259           /* If the following elt. corresponds to our containing block
1260              then the elt. must be for this block.  */
1261           if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
1262             TREE_ADDRESSABLE (lists) = 1;
1263
1264         if (stack_level)
1265           f->stack_level = stack_level;
1266       }
1267 }
1268
1269
1270 /* When exiting a binding contour, process all pending gotos requiring fixups.
1271    Note: STACK_DEPTH is not altered.
1272
1273    The arguments are currently not used in the bytecode compiler, but we may
1274    need them one day for languages other than C.
1275
1276    THISBLOCK is the structure that describes the block being exited.
1277    STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
1278    CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
1279    FIRST_INSN is the insn that began this contour.
1280
1281    Gotos that jump out of this contour must restore the
1282    stack level and do the cleanups before actually jumping.
1283
1284    DONT_JUMP_IN nonzero means report error there is a jump into this
1285    contour from before the beginning of the contour.
1286    This is also done if STACK_LEVEL is nonzero.  */
1287
1288 static void
1289 bc_fixup_gotos (thisblock, stack_level, cleanup_list, first_insn, dont_jump_in)
1290      struct nesting *thisblock;
1291      int stack_level;
1292      tree cleanup_list;
1293      rtx first_insn;
1294      int dont_jump_in;
1295 {
1296   register struct goto_fixup *f, *prev;
1297   int saved_stack_depth;
1298
1299   /* F is the fixup we are considering; PREV is the previous one.  */
1300
1301   for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1302     {
1303       /* Test for a fixup that is inactive because it is already handled.  */
1304       if (f->before_jump == 0)
1305         {
1306           /* Delete inactive fixup from the chain, if that is easy to do.  */
1307           if (prev)
1308             prev->next = f->next;
1309         }
1310
1311       /* Emit code to restore the stack and continue */
1312       bc_emit_bytecode_labeldef (f->label);
1313
1314       /* Save stack_depth across call, since bc_adjust_stack () will alter
1315          the perceived stack depth via the instructions generated. */
1316
1317       if (f->bc_stack_level >= 0)
1318         {
1319           saved_stack_depth = stack_depth;
1320           bc_adjust_stack (stack_depth - f->bc_stack_level);
1321           stack_depth = saved_stack_depth;
1322         }
1323
1324       bc_emit_bytecode (jump);
1325       bc_emit_bytecode_labelref (f->bc_target);
1326
1327 #ifdef DEBUG_PRINT_CODE
1328   fputc ('\n', stderr);
1329 #endif
1330     }
1331
1332   goto_fixup_chain = NULL;
1333 }
1334 \f
1335 /* Generate RTL for an asm statement (explicit assembler code).
1336    BODY is a STRING_CST node containing the assembler code text,
1337    or an ADDR_EXPR containing a STRING_CST.  */
1338
1339 void
1340 expand_asm (body)
1341      tree body;
1342 {
1343   if (output_bytecode)
1344     {
1345       error ("`asm' is illegal when generating bytecode");
1346       return;
1347     }
1348
1349   if (TREE_CODE (body) == ADDR_EXPR)
1350     body = TREE_OPERAND (body, 0);
1351
1352   emit_insn (gen_rtx (ASM_INPUT, VOIDmode,
1353                       TREE_STRING_POINTER (body)));
1354   last_expr_type = 0;
1355 }
1356
1357 /* Generate RTL for an asm statement with arguments.
1358    STRING is the instruction template.
1359    OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
1360    Each output or input has an expression in the TREE_VALUE and
1361    a constraint-string in the TREE_PURPOSE.
1362    CLOBBERS is a list of STRING_CST nodes each naming a hard register
1363    that is clobbered by this insn.
1364
1365    Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
1366    Some elements of OUTPUTS may be replaced with trees representing temporary
1367    values.  The caller should copy those temporary values to the originally
1368    specified lvalues.
1369
1370    VOL nonzero means the insn is volatile; don't optimize it.  */
1371
1372 void
1373 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
1374      tree string, outputs, inputs, clobbers;
1375      int vol;
1376      char *filename;
1377      int line;
1378 {
1379   rtvec argvec, constraints;
1380   rtx body;
1381   int ninputs = list_length (inputs);
1382   int noutputs = list_length (outputs);
1383   int nclobbers;
1384   tree tail;
1385   register int i;
1386   /* Vector of RTX's of evaluated output operands.  */
1387   rtx *output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1388   /* The insn we have emitted.  */
1389   rtx insn;
1390
1391   if (output_bytecode)
1392     {
1393       error ("`asm' is illegal when generating bytecode");
1394       return;
1395     }
1396
1397   /* Count the number of meaningful clobbered registers, ignoring what
1398      we would ignore later.  */
1399   nclobbers = 0;
1400   for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1401     {
1402       char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1403       i = decode_reg_name (regname);
1404       if (i >= 0 || i == -4)
1405         ++nclobbers;
1406     }
1407
1408   last_expr_type = 0;
1409
1410   for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1411     {
1412       tree val = TREE_VALUE (tail);
1413       tree val1;
1414       int j;
1415       int found_equal;
1416
1417       /* If there's an erroneous arg, emit no insn.  */
1418       if (TREE_TYPE (val) == error_mark_node)
1419         return;
1420
1421       /* Make sure constraint has `=' and does not have `+'.  */
1422
1423       found_equal = 0;
1424       for (j = 0; j < TREE_STRING_LENGTH (TREE_PURPOSE (tail)); j++)
1425         {
1426           if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '+')
1427             {
1428               error ("output operand constraint contains `+'");
1429               return;
1430             }
1431           if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '=')
1432             found_equal = 1;
1433         }
1434       if (! found_equal)
1435         {
1436           error ("output operand constraint lacks `='");
1437           return;
1438         }
1439
1440       /* If an output operand is not a variable or indirect ref,
1441          or a part of one,
1442          create a SAVE_EXPR which is a pseudo-reg
1443          to act as an intermediate temporary.
1444          Make the asm insn write into that, then copy it to
1445          the real output operand.  */
1446
1447       while (TREE_CODE (val) == COMPONENT_REF
1448              || TREE_CODE (val) == ARRAY_REF)
1449         val = TREE_OPERAND (val, 0);
1450
1451       if (TREE_CODE (val) != VAR_DECL
1452           && TREE_CODE (val) != PARM_DECL
1453           && TREE_CODE (val) != INDIRECT_REF)
1454         {
1455           TREE_VALUE (tail) = save_expr (TREE_VALUE (tail));
1456           /* If it's a constant, print error now so don't crash later.  */
1457           if (TREE_CODE (TREE_VALUE (tail)) != SAVE_EXPR)
1458             {
1459               error ("invalid output in `asm'");
1460               return;
1461             }
1462         }
1463
1464       output_rtx[i] = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
1465     }
1466
1467   if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1468     {
1469       error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1470       return;
1471     }
1472
1473   /* Make vectors for the expression-rtx and constraint strings.  */
1474
1475   argvec = rtvec_alloc (ninputs);
1476   constraints = rtvec_alloc (ninputs);
1477
1478   body = gen_rtx (ASM_OPERANDS, VOIDmode,
1479                   TREE_STRING_POINTER (string), "", 0, argvec, constraints,
1480                   filename, line);
1481   MEM_VOLATILE_P (body) = vol;
1482
1483   /* Eval the inputs and put them into ARGVEC.
1484      Put their constraints into ASM_INPUTs and store in CONSTRAINTS.  */
1485
1486   i = 0;
1487   for (tail = inputs; tail; tail = TREE_CHAIN (tail))
1488     {
1489       int j;
1490
1491       /* If there's an erroneous arg, emit no insn,
1492          because the ASM_INPUT would get VOIDmode
1493          and that could cause a crash in reload.  */
1494       if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1495         return;
1496       if (TREE_PURPOSE (tail) == NULL_TREE)
1497         {
1498           error ("hard register `%s' listed as input operand to `asm'",
1499                  TREE_STRING_POINTER (TREE_VALUE (tail)) );
1500           return;
1501         }
1502
1503       /* Make sure constraint has neither `=' nor `+'.  */
1504
1505       for (j = 0; j < TREE_STRING_LENGTH (TREE_PURPOSE (tail)); j++)
1506         if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '='
1507             || TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '+')
1508           {
1509             error ("input operand constraint contains `%c'",
1510                    TREE_STRING_POINTER (TREE_PURPOSE (tail))[j]);
1511             return;
1512           }
1513
1514       XVECEXP (body, 3, i)      /* argvec */
1515         = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
1516       XVECEXP (body, 4, i)      /* constraints */
1517         = gen_rtx (ASM_INPUT, TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1518                    TREE_STRING_POINTER (TREE_PURPOSE (tail)));
1519       i++;
1520     }
1521
1522   /* Protect all the operands from the queue,
1523      now that they have all been evaluated.  */
1524
1525   for (i = 0; i < ninputs; i++)
1526     XVECEXP (body, 3, i) = protect_from_queue (XVECEXP (body, 3, i), 0);
1527
1528   for (i = 0; i < noutputs; i++)
1529     output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1530
1531   /* Now, for each output, construct an rtx
1532      (set OUTPUT (asm_operands INSN OUTPUTNUMBER OUTPUTCONSTRAINT
1533                                ARGVEC CONSTRAINTS))
1534      If there is more than one, put them inside a PARALLEL.  */
1535
1536   if (noutputs == 1 && nclobbers == 0)
1537     {
1538       XSTR (body, 1) = TREE_STRING_POINTER (TREE_PURPOSE (outputs));
1539       insn = emit_insn (gen_rtx (SET, VOIDmode, output_rtx[0], body));
1540     }
1541   else if (noutputs == 0 && nclobbers == 0)
1542     {
1543       /* No output operands: put in a raw ASM_OPERANDS rtx.  */
1544       insn = emit_insn (body);
1545     }
1546   else
1547     {
1548       rtx obody = body;
1549       int num = noutputs;
1550       if (num == 0) num = 1;
1551       body = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (num + nclobbers));
1552
1553       /* For each output operand, store a SET.  */
1554
1555       for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1556         {
1557           XVECEXP (body, 0, i)
1558             = gen_rtx (SET, VOIDmode,
1559                        output_rtx[i],
1560                        gen_rtx (ASM_OPERANDS, VOIDmode,
1561                                 TREE_STRING_POINTER (string),
1562                                 TREE_STRING_POINTER (TREE_PURPOSE (tail)),
1563                                 i, argvec, constraints,
1564                                 filename, line));
1565           MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1566         }
1567
1568       /* If there are no outputs (but there are some clobbers)
1569          store the bare ASM_OPERANDS into the PARALLEL.  */
1570
1571       if (i == 0)
1572         XVECEXP (body, 0, i++) = obody;
1573
1574       /* Store (clobber REG) for each clobbered register specified.  */
1575
1576       for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1577         {
1578           char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1579           int j = decode_reg_name (regname);
1580
1581           if (j < 0)
1582             {
1583               if (j == -3)      /* `cc', which is not a register */
1584                 continue;
1585
1586               if (j == -4)      /* `memory', don't cache memory across asm */
1587                 {
1588                   XVECEXP (body, 0, i++)
1589                     = gen_rtx (CLOBBER, VOIDmode,
1590                                gen_rtx (MEM, QImode,
1591                                         gen_rtx (SCRATCH, VOIDmode, 0)));
1592                   continue;
1593                 }
1594
1595               error ("unknown register name `%s' in `asm'", regname);
1596               return;
1597             }
1598
1599           /* Use QImode since that's guaranteed to clobber just one reg.  */
1600           XVECEXP (body, 0, i++)
1601             = gen_rtx (CLOBBER, VOIDmode, gen_rtx (REG, QImode, j));
1602         }
1603
1604       insn = emit_insn (body);
1605     }
1606
1607   free_temp_slots ();
1608 }
1609 \f
1610 /* Generate RTL to evaluate the expression EXP
1611    and remember it in case this is the VALUE in a ({... VALUE; }) constr.  */
1612
1613 void
1614 expand_expr_stmt (exp)
1615      tree exp;
1616 {
1617   if (output_bytecode)
1618     {
1619       int org_stack_depth = stack_depth;
1620
1621       bc_expand_expr (exp);
1622
1623       /* Restore stack depth */
1624       if (stack_depth < org_stack_depth)
1625         abort ();
1626       
1627       bc_emit_instruction (drop);
1628
1629       last_expr_type = TREE_TYPE (exp);
1630       return;
1631     }
1632
1633   /* If -W, warn about statements with no side effects,
1634      except for an explicit cast to void (e.g. for assert()), and
1635      except inside a ({...}) where they may be useful.  */
1636   if (expr_stmts_for_value == 0 && exp != error_mark_node)
1637     {
1638       if (! TREE_SIDE_EFFECTS (exp) && (extra_warnings || warn_unused)
1639           && !(TREE_CODE (exp) == CONVERT_EXPR
1640                && TREE_TYPE (exp) == void_type_node))
1641         warning_with_file_and_line (emit_filename, emit_lineno,
1642                                     "statement with no effect");
1643       else if (warn_unused)
1644         warn_if_unused_value (exp);
1645     }
1646   last_expr_type = TREE_TYPE (exp);
1647   if (! flag_syntax_only)
1648     last_expr_value = expand_expr (exp,
1649                                    (expr_stmts_for_value
1650                                     ? NULL_RTX : const0_rtx),
1651                                    VOIDmode, 0);
1652
1653   /* If all we do is reference a volatile value in memory,
1654      copy it to a register to be sure it is actually touched.  */
1655   if (last_expr_value != 0 && GET_CODE (last_expr_value) == MEM
1656       && TREE_THIS_VOLATILE (exp))
1657     {
1658       if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode)
1659         ;
1660       else if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
1661         copy_to_reg (last_expr_value);
1662       else
1663         {
1664           rtx lab = gen_label_rtx ();
1665           
1666           /* Compare the value with itself to reference it.  */
1667           emit_cmp_insn (last_expr_value, last_expr_value, EQ,
1668                          expand_expr (TYPE_SIZE (last_expr_type),
1669                                       NULL_RTX, VOIDmode, 0),
1670                          BLKmode, 0,
1671                          TYPE_ALIGN (last_expr_type) / BITS_PER_UNIT);
1672           emit_jump_insn ((*bcc_gen_fctn[(int) EQ]) (lab));
1673           emit_label (lab);
1674         }
1675     }
1676
1677   /* If this expression is part of a ({...}) and is in memory, we may have
1678      to preserve temporaries.  */
1679   preserve_temp_slots (last_expr_value);
1680
1681   /* Free any temporaries used to evaluate this expression.  Any temporary
1682      used as a result of this expression will already have been preserved
1683      above.  */
1684   free_temp_slots ();
1685
1686   emit_queue ();
1687 }
1688
1689 /* Warn if EXP contains any computations whose results are not used.
1690    Return 1 if a warning is printed; 0 otherwise.  */
1691
1692 static int
1693 warn_if_unused_value (exp)
1694      tree exp;
1695 {
1696   if (TREE_USED (exp))
1697     return 0;
1698
1699   switch (TREE_CODE (exp))
1700     {
1701     case PREINCREMENT_EXPR:
1702     case POSTINCREMENT_EXPR:
1703     case PREDECREMENT_EXPR:
1704     case POSTDECREMENT_EXPR:
1705     case MODIFY_EXPR:
1706     case INIT_EXPR:
1707     case TARGET_EXPR:
1708     case CALL_EXPR:
1709     case METHOD_CALL_EXPR:
1710     case RTL_EXPR:
1711     case WITH_CLEANUP_EXPR:
1712     case EXIT_EXPR:
1713       /* We don't warn about COND_EXPR because it may be a useful
1714          construct if either arm contains a side effect.  */
1715     case COND_EXPR:
1716       return 0;
1717
1718     case BIND_EXPR:
1719       /* For a binding, warn if no side effect within it.  */
1720       return warn_if_unused_value (TREE_OPERAND (exp, 1));
1721
1722     case TRUTH_ORIF_EXPR:
1723     case TRUTH_ANDIF_EXPR:
1724       /* In && or ||, warn if 2nd operand has no side effect.  */
1725       return warn_if_unused_value (TREE_OPERAND (exp, 1));
1726
1727     case COMPOUND_EXPR:
1728       if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
1729         return 1;
1730       /* Let people do `(foo (), 0)' without a warning.  */
1731       if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1732         return 0;
1733       return warn_if_unused_value (TREE_OPERAND (exp, 1));
1734
1735     case NOP_EXPR:
1736     case CONVERT_EXPR:
1737     case NON_LVALUE_EXPR:
1738       /* Don't warn about values cast to void.  */
1739       if (TREE_TYPE (exp) == void_type_node)
1740         return 0;
1741       /* Don't warn about conversions not explicit in the user's program.  */
1742       if (TREE_NO_UNUSED_WARNING (exp))
1743         return 0;
1744       /* Assignment to a cast usually results in a cast of a modify.
1745          Don't complain about that.  There can be an arbitrary number of
1746          casts before the modify, so we must loop until we find the first
1747          non-cast expression and then test to see if that is a modify.  */
1748       {
1749         tree tem = TREE_OPERAND (exp, 0);
1750
1751         while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
1752           tem = TREE_OPERAND (tem, 0);
1753
1754         if (TREE_CODE (tem) == MODIFY_EXPR)
1755           return 0;
1756       }
1757       /* ... fall through ... */
1758
1759     default:
1760       /* Referencing a volatile value is a side effect, so don't warn.  */
1761       if ((TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
1762            || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
1763           && TREE_THIS_VOLATILE (exp))
1764         return 0;
1765       warning_with_file_and_line (emit_filename, emit_lineno,
1766                                   "value computed is not used");
1767       return 1;
1768     }
1769 }
1770
1771 /* Clear out the memory of the last expression evaluated.  */
1772
1773 void
1774 clear_last_expr ()
1775 {
1776   last_expr_type = 0;
1777 }
1778
1779 /* Begin a statement which will return a value.
1780    Return the RTL_EXPR for this statement expr.
1781    The caller must save that value and pass it to expand_end_stmt_expr.  */
1782
1783 tree
1784 expand_start_stmt_expr ()
1785 {
1786   int momentary;
1787   tree t;
1788
1789   /* When generating bytecode just note down the stack depth */
1790   if (output_bytecode)
1791     return (build_int_2 (stack_depth, 0));
1792
1793   /* Make the RTL_EXPR node temporary, not momentary,
1794      so that rtl_expr_chain doesn't become garbage.  */
1795   momentary = suspend_momentary ();
1796   t = make_node (RTL_EXPR);
1797   resume_momentary (momentary);
1798   start_sequence_for_rtl_expr (t);
1799   NO_DEFER_POP;
1800   expr_stmts_for_value++;
1801   return t;
1802 }
1803
1804 /* Restore the previous state at the end of a statement that returns a value.
1805    Returns a tree node representing the statement's value and the
1806    insns to compute the value.
1807
1808    The nodes of that expression have been freed by now, so we cannot use them.
1809    But we don't want to do that anyway; the expression has already been
1810    evaluated and now we just want to use the value.  So generate a RTL_EXPR
1811    with the proper type and RTL value.
1812
1813    If the last substatement was not an expression,
1814    return something with type `void'.  */
1815
1816 tree
1817 expand_end_stmt_expr (t)
1818      tree t;
1819 {
1820   if (output_bytecode)
1821     {
1822       int i;
1823       tree t;
1824       
1825       
1826       /* At this point, all expressions have been evaluated in order.
1827          However, all expression values have been popped when evaluated,
1828          which means we have to recover the last expression value.  This is
1829          the last value removed by means of a `drop' instruction.  Instead
1830          of adding code to inhibit dropping the last expression value, it
1831          is here recovered by undoing the `drop'.  Since `drop' is
1832          equivalent to `adjustackSI [1]', it can be undone with `adjstackSI
1833          [-1]'. */
1834       
1835       bc_adjust_stack (-1);
1836       
1837       if (!last_expr_type)
1838         last_expr_type = void_type_node;
1839       
1840       t = make_node (RTL_EXPR);
1841       TREE_TYPE (t) = last_expr_type;
1842       RTL_EXPR_RTL (t) = NULL;
1843       RTL_EXPR_SEQUENCE (t) = NULL;
1844       
1845       /* Don't consider deleting this expr or containing exprs at tree level.  */
1846       TREE_THIS_VOLATILE (t) = 1;
1847       
1848       last_expr_type = 0;
1849       return t;
1850     }
1851
1852   OK_DEFER_POP;
1853
1854   if (last_expr_type == 0)
1855     {
1856       last_expr_type = void_type_node;
1857       last_expr_value = const0_rtx;
1858     }
1859   else if (last_expr_value == 0)
1860     /* There are some cases where this can happen, such as when the
1861        statement is void type.  */
1862     last_expr_value = const0_rtx;
1863   else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
1864     /* Remove any possible QUEUED.  */
1865     last_expr_value = protect_from_queue (last_expr_value, 0);
1866
1867   emit_queue ();
1868
1869   TREE_TYPE (t) = last_expr_type;
1870   RTL_EXPR_RTL (t) = last_expr_value;
1871   RTL_EXPR_SEQUENCE (t) = get_insns ();
1872
1873   rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
1874
1875   end_sequence ();
1876
1877   /* Don't consider deleting this expr or containing exprs at tree level.  */
1878   TREE_SIDE_EFFECTS (t) = 1;
1879   /* Propagate volatility of the actual RTL expr.  */
1880   TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
1881
1882   last_expr_type = 0;
1883   expr_stmts_for_value--;
1884
1885   return t;
1886 }
1887 \f
1888 /* The exception handling nesting looks like this:
1889
1890                 <-- Level N-1
1891     {           <-- exception handler block
1892                 <-- Level N
1893                 <-- in an exception handler
1894         {       <-- try block
1895         :       <-- in a TRY block
1896         :       <-- in an exception handler
1897         :
1898         }
1899
1900         {       <-- except block
1901         :       <-- in an except block
1902         :       <-- in an exception handler
1903         :
1904         }
1905
1906     }
1907 */
1908
1909 /* Return nonzero iff in a try block at level LEVEL.  */
1910
1911 int
1912 in_try_block (level)
1913      int level;
1914 {
1915   struct nesting *n = except_stack;
1916   while (1)
1917     {
1918       while (n && n->data.except_stmt.after_label != 0)
1919         n = n->next;
1920       if (n == 0)
1921         return 0;
1922       if (level == 0)
1923         return n != 0;
1924       level--;
1925       n = n->next;
1926     }
1927 }
1928
1929 /* Return nonzero iff in an except block at level LEVEL.  */
1930
1931 int
1932 in_except_block (level)
1933      int level;
1934 {
1935   struct nesting *n = except_stack;
1936   while (1)
1937     {
1938       while (n && n->data.except_stmt.after_label == 0)
1939         n = n->next;
1940       if (n == 0)
1941         return 0;
1942       if (level == 0)
1943         return n != 0;
1944       level--;
1945       n = n->next;
1946     }
1947 }
1948
1949 /* Return nonzero iff in an exception handler at level LEVEL.  */
1950
1951 int
1952 in_exception_handler (level)
1953      int level;
1954 {
1955   struct nesting *n = except_stack;
1956   while (n && level--)
1957     n = n->next;
1958   return n != 0;
1959 }
1960
1961 /* Record the fact that the current exception nesting raises
1962    exception EX.  If not in an exception handler, return 0.  */
1963 int
1964 expand_raise (ex)
1965      tree ex;
1966 {
1967   tree *raises_ptr;
1968
1969   if (except_stack == 0)
1970     return 0;
1971   raises_ptr = &except_stack->data.except_stmt.raised;
1972   if (! value_member (ex, *raises_ptr))
1973     *raises_ptr = tree_cons (NULL_TREE, ex, *raises_ptr);
1974   return 1;
1975 }
1976
1977 /* Generate RTL for the start of a try block.
1978
1979    TRY_CLAUSE is the condition to test to enter the try block.  */
1980
1981 void
1982 expand_start_try (try_clause, exitflag, escapeflag)
1983      tree try_clause;
1984      int exitflag;
1985      int escapeflag;
1986 {
1987   struct nesting *thishandler = ALLOC_NESTING ();
1988
1989   /* Make an entry on cond_stack for the cond we are entering.  */
1990
1991   thishandler->next = except_stack;
1992   thishandler->all = nesting_stack;
1993   thishandler->depth = ++nesting_depth;
1994   thishandler->data.except_stmt.raised = 0;
1995   thishandler->data.except_stmt.handled = 0;
1996   thishandler->data.except_stmt.first_insn = get_insns ();
1997   thishandler->data.except_stmt.except_label = gen_label_rtx ();
1998   thishandler->data.except_stmt.unhandled_label = 0;
1999   thishandler->data.except_stmt.after_label = 0;
2000   thishandler->data.except_stmt.escape_label
2001     = escapeflag ? thishandler->data.except_stmt.except_label : 0;
2002   thishandler->exit_label = exitflag ? gen_label_rtx () : 0;
2003   except_stack = thishandler;
2004   nesting_stack = thishandler;
2005
2006   do_jump (try_clause, thishandler->data.except_stmt.except_label, NULL_RTX);
2007 }
2008
2009 /* End of a TRY block.  Nothing to do for now.  */
2010
2011 void
2012 expand_end_try ()
2013 {
2014   except_stack->data.except_stmt.after_label = gen_label_rtx ();
2015   expand_goto_internal (NULL_TREE, except_stack->data.except_stmt.after_label,
2016                         NULL_RTX);
2017 }
2018
2019 /* Start an `except' nesting contour.
2020    EXITFLAG says whether this contour should be able to `exit' something.
2021    ESCAPEFLAG says whether this contour should be escapable.  */
2022
2023 void
2024 expand_start_except (exitflag, escapeflag)
2025      int exitflag;
2026      int escapeflag;
2027 {
2028   if (exitflag)
2029     {
2030       struct nesting *n;
2031       /* An `exit' from catch clauses goes out to next exit level,
2032          if there is one.  Otherwise, it just goes to the end
2033          of the construct.  */
2034       for (n = except_stack->next; n; n = n->next)
2035         if (n->exit_label != 0)
2036           {
2037             except_stack->exit_label = n->exit_label;
2038             break;
2039           }
2040       if (n == 0)
2041         except_stack->exit_label = except_stack->data.except_stmt.after_label;
2042     }
2043   if (escapeflag)
2044     {
2045       struct nesting *n;
2046       /* An `escape' from catch clauses goes out to next escape level,
2047          if there is one.  Otherwise, it just goes to the end
2048          of the construct.  */
2049       for (n = except_stack->next; n; n = n->next)
2050         if (n->data.except_stmt.escape_label != 0)
2051           {
2052             except_stack->data.except_stmt.escape_label
2053               = n->data.except_stmt.escape_label;
2054             break;
2055           }
2056       if (n == 0)
2057         except_stack->data.except_stmt.escape_label
2058           = except_stack->data.except_stmt.after_label;
2059     }
2060   do_pending_stack_adjust ();
2061   emit_label (except_stack->data.except_stmt.except_label);
2062 }
2063
2064 /* Generate code to `escape' from an exception contour.  This
2065    is like `exiting', but does not conflict with constructs which
2066    use `exit_label'.
2067
2068    Return nonzero if this contour is escapable, otherwise
2069    return zero, and language-specific code will emit the
2070    appropriate error message.  */
2071 int
2072 expand_escape_except ()
2073 {
2074   struct nesting *n;
2075   last_expr_type = 0;
2076   for (n = except_stack; n; n = n->next)
2077     if (n->data.except_stmt.escape_label != 0)
2078       {
2079         expand_goto_internal (NULL_TREE,
2080                               n->data.except_stmt.escape_label, NULL_RTX);
2081         return 1;
2082       }
2083
2084   return 0;
2085 }
2086
2087 /* Finish processing and `except' contour.
2088    Culls out all exceptions which might be raise but not
2089    handled, and returns the list to the caller.
2090    Language-specific code is responsible for dealing with these
2091    exceptions.  */
2092
2093 tree
2094 expand_end_except ()
2095 {
2096   struct nesting *n;
2097   tree raised = NULL_TREE;
2098
2099   do_pending_stack_adjust ();
2100   emit_label (except_stack->data.except_stmt.after_label);
2101
2102   n = except_stack->next;
2103   if (n)
2104     {
2105       /* Propagate exceptions raised but not handled to next
2106          highest level.  */
2107       tree handled = except_stack->data.except_stmt.raised;
2108       if (handled != void_type_node)
2109         {
2110           tree prev = NULL_TREE;
2111           raised = except_stack->data.except_stmt.raised;
2112           while (handled)
2113             {
2114               tree this_raise;
2115               for (this_raise = raised, prev = 0; this_raise;
2116                    this_raise = TREE_CHAIN (this_raise))
2117                 {
2118                   if (value_member (TREE_VALUE (this_raise), handled))
2119                     {
2120                       if (prev)
2121                         TREE_CHAIN (prev) = TREE_CHAIN (this_raise);
2122                       else
2123                         {
2124                           raised = TREE_CHAIN (raised);
2125                           if (raised == NULL_TREE)
2126                             goto nada;
2127                         }
2128                     }
2129                   else
2130                     prev = this_raise;
2131                 }
2132               handled = TREE_CHAIN (handled);
2133             }
2134           if (prev == NULL_TREE)
2135             prev = raised;
2136           if (prev)
2137             TREE_CHAIN (prev) = n->data.except_stmt.raised;
2138         nada:
2139           n->data.except_stmt.raised = raised;
2140         }
2141     }
2142
2143   POPSTACK (except_stack);
2144   last_expr_type = 0;
2145   return raised;
2146 }
2147
2148 /* Record that exception EX is caught by this exception handler.
2149    Return nonzero if in exception handling construct, otherwise return 0.  */
2150 int
2151 expand_catch (ex)
2152      tree ex;
2153 {
2154   tree *raises_ptr;
2155
2156   if (except_stack == 0)
2157     return 0;
2158   raises_ptr = &except_stack->data.except_stmt.handled;
2159   if (*raises_ptr != void_type_node
2160       && ex != NULL_TREE
2161       && ! value_member (ex, *raises_ptr))
2162     *raises_ptr = tree_cons (NULL_TREE, ex, *raises_ptr);
2163   return 1;
2164 }
2165
2166 /* Record that this exception handler catches all exceptions.
2167    Return nonzero if in exception handling construct, otherwise return 0.  */
2168
2169 int
2170 expand_catch_default ()
2171 {
2172   if (except_stack == 0)
2173     return 0;
2174   except_stack->data.except_stmt.handled = void_type_node;
2175   return 1;
2176 }
2177
2178 int
2179 expand_end_catch ()
2180 {
2181   if (except_stack == 0 || except_stack->data.except_stmt.after_label == 0)
2182     return 0;
2183   expand_goto_internal (NULL_TREE, except_stack->data.except_stmt.after_label,
2184                         NULL_RTX);
2185   return 1;
2186 }
2187 \f
2188 /* Generate RTL for the start of an if-then.  COND is the expression
2189    whose truth should be tested.
2190
2191    If EXITFLAG is nonzero, this conditional is visible to
2192    `exit_something'.  */
2193
2194 void
2195 expand_start_cond (cond, exitflag)
2196      tree cond;
2197      int exitflag;
2198 {
2199   struct nesting *thiscond = ALLOC_NESTING ();
2200
2201   /* Make an entry on cond_stack for the cond we are entering.  */
2202
2203   thiscond->next = cond_stack;
2204   thiscond->all = nesting_stack;
2205   thiscond->depth = ++nesting_depth;
2206   thiscond->data.cond.next_label = gen_label_rtx ();
2207   /* Before we encounter an `else', we don't need a separate exit label
2208      unless there are supposed to be exit statements
2209      to exit this conditional.  */
2210   thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
2211   thiscond->data.cond.endif_label = thiscond->exit_label;
2212   cond_stack = thiscond;
2213   nesting_stack = thiscond;
2214
2215   if (output_bytecode)
2216     bc_expand_start_cond (cond, exitflag);
2217   else
2218     do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
2219 }
2220
2221 /* Generate RTL between then-clause and the elseif-clause
2222    of an if-then-elseif-....  */
2223
2224 void
2225 expand_start_elseif (cond)
2226      tree cond;
2227 {
2228   if (cond_stack->data.cond.endif_label == 0)
2229     cond_stack->data.cond.endif_label = gen_label_rtx ();
2230   emit_jump (cond_stack->data.cond.endif_label);
2231   emit_label (cond_stack->data.cond.next_label);
2232   cond_stack->data.cond.next_label = gen_label_rtx ();
2233   do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2234 }
2235
2236 /* Generate RTL between the then-clause and the else-clause
2237    of an if-then-else.  */
2238
2239 void
2240 expand_start_else ()
2241 {
2242   if (cond_stack->data.cond.endif_label == 0)
2243     cond_stack->data.cond.endif_label = gen_label_rtx ();
2244
2245   if (output_bytecode)
2246     {
2247       bc_expand_start_else ();
2248       return;
2249     }
2250
2251   emit_jump (cond_stack->data.cond.endif_label);
2252   emit_label (cond_stack->data.cond.next_label);
2253   cond_stack->data.cond.next_label = 0;  /* No more _else or _elseif calls. */
2254 }
2255
2256 /* Generate RTL for the end of an if-then.
2257    Pop the record for it off of cond_stack.  */
2258
2259 void
2260 expand_end_cond ()
2261 {
2262   struct nesting *thiscond = cond_stack;
2263
2264   if (output_bytecode)
2265     bc_expand_end_cond ();
2266   else
2267     {
2268       do_pending_stack_adjust ();
2269       if (thiscond->data.cond.next_label)
2270         emit_label (thiscond->data.cond.next_label);
2271       if (thiscond->data.cond.endif_label)
2272         emit_label (thiscond->data.cond.endif_label);
2273     }
2274
2275   POPSTACK (cond_stack);
2276   last_expr_type = 0;
2277 }
2278
2279
2280 /* Generate code for the start of an if-then.  COND is the expression
2281    whose truth is to be tested; if EXITFLAG is nonzero this conditional
2282    is to be visible to exit_something.  It is assumed that the caller
2283    has pushed the previous context on the cond stack. */
2284
2285 static void
2286 bc_expand_start_cond (cond, exitflag)
2287      tree cond;
2288      int exitflag;
2289 {
2290   struct nesting *thiscond = cond_stack;
2291
2292   thiscond->data.case_stmt.nominal_type = cond;
2293   if (! exitflag)
2294     thiscond->exit_label = gen_label_rtx ();
2295   bc_expand_expr (cond);
2296   bc_emit_bytecode (xjumpifnot);
2297   bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thiscond->exit_label));
2298
2299 #ifdef DEBUG_PRINT_CODE
2300   fputc ('\n', stderr);
2301 #endif
2302 }
2303
2304 /* Generate the label for the end of an if with
2305    no else- clause.  */
2306
2307 static void
2308 bc_expand_end_cond ()
2309 {
2310   struct nesting *thiscond = cond_stack;
2311
2312   bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thiscond->exit_label));
2313 }
2314
2315 /* Generate code for the start of the else- clause of
2316    an if-then-else.  */
2317
2318 static void
2319 bc_expand_start_else ()
2320 {
2321   struct nesting *thiscond = cond_stack;
2322
2323   thiscond->data.cond.endif_label = thiscond->exit_label;
2324   thiscond->exit_label = gen_label_rtx ();
2325   bc_emit_bytecode (jump);
2326   bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thiscond->exit_label));
2327
2328 #ifdef DEBUG_PRINT_CODE
2329   fputc ('\n', stderr);
2330 #endif
2331
2332   bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thiscond->data.cond.endif_label));
2333 }
2334 \f
2335 /* Generate RTL for the start of a loop.  EXIT_FLAG is nonzero if this
2336    loop should be exited by `exit_something'.  This is a loop for which
2337    `expand_continue' will jump to the top of the loop.
2338
2339    Make an entry on loop_stack to record the labels associated with
2340    this loop.  */
2341
2342 struct nesting *
2343 expand_start_loop (exit_flag)
2344      int exit_flag;
2345 {
2346   register struct nesting *thisloop = ALLOC_NESTING ();
2347
2348   /* Make an entry on loop_stack for the loop we are entering.  */
2349
2350   thisloop->next = loop_stack;
2351   thisloop->all = nesting_stack;
2352   thisloop->depth = ++nesting_depth;
2353   thisloop->data.loop.start_label = gen_label_rtx ();
2354   thisloop->data.loop.end_label = gen_label_rtx ();
2355   thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
2356   thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
2357   loop_stack = thisloop;
2358   nesting_stack = thisloop;
2359
2360   if (output_bytecode)
2361     {
2362       bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thisloop->data.loop.start_label));
2363       return thisloop;
2364     }
2365
2366   do_pending_stack_adjust ();
2367   emit_queue ();
2368   emit_note (NULL_PTR, NOTE_INSN_LOOP_BEG);
2369   emit_label (thisloop->data.loop.start_label);
2370
2371   return thisloop;
2372 }
2373
2374 /* Like expand_start_loop but for a loop where the continuation point
2375    (for expand_continue_loop) will be specified explicitly.  */
2376
2377 struct nesting *
2378 expand_start_loop_continue_elsewhere (exit_flag)
2379      int exit_flag;
2380 {
2381   struct nesting *thisloop = expand_start_loop (exit_flag);
2382   loop_stack->data.loop.continue_label = gen_label_rtx ();
2383   return thisloop;
2384 }
2385
2386 /* Specify the continuation point for a loop started with
2387    expand_start_loop_continue_elsewhere.
2388    Use this at the point in the code to which a continue statement
2389    should jump.  */
2390
2391 void
2392 expand_loop_continue_here ()
2393 {
2394   if (output_bytecode)
2395     {
2396       bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (loop_stack->data.loop.continue_label));
2397       return;
2398     }
2399   do_pending_stack_adjust ();
2400   emit_note (NULL_PTR, NOTE_INSN_LOOP_CONT);
2401   emit_label (loop_stack->data.loop.continue_label);
2402 }
2403
2404 /* End a loop.  */
2405
2406 static void
2407 bc_expand_end_loop ()
2408 {
2409   struct nesting *thisloop = loop_stack;
2410
2411   bc_emit_bytecode (jump);
2412   bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thisloop->data.loop.start_label));
2413
2414 #ifdef DEBUG_PRINT_CODE
2415   fputc ('\n', stderr);
2416 #endif
2417
2418   bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thisloop->exit_label));
2419   POPSTACK (loop_stack);
2420   last_expr_type = 0;
2421 }
2422
2423
2424 /* Finish a loop.  Generate a jump back to the top and the loop-exit label.
2425    Pop the block off of loop_stack.  */
2426
2427 void
2428 expand_end_loop ()
2429 {
2430   register rtx insn;
2431   register rtx start_label;
2432   rtx last_test_insn = 0;
2433   int num_insns = 0;
2434     
2435   if (output_bytecode)
2436     {
2437       bc_expand_end_loop ();
2438       return;
2439     }
2440
2441   insn = get_last_insn ();
2442   start_label = loop_stack->data.loop.start_label;
2443
2444   /* Mark the continue-point at the top of the loop if none elsewhere.  */
2445   if (start_label == loop_stack->data.loop.continue_label)
2446     emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
2447
2448   do_pending_stack_adjust ();
2449
2450   /* If optimizing, perhaps reorder the loop.  If the loop
2451      starts with a conditional exit, roll that to the end
2452      where it will optimize together with the jump back.
2453
2454      We look for the last conditional branch to the exit that we encounter
2455      before hitting 30 insns or a CALL_INSN.  If we see an unconditional
2456      branch to the exit first, use it.
2457
2458      We must also stop at NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes
2459      because moving them is not valid.  */
2460
2461   if (optimize
2462       &&
2463       ! (GET_CODE (insn) == JUMP_INSN
2464          && GET_CODE (PATTERN (insn)) == SET
2465          && SET_DEST (PATTERN (insn)) == pc_rtx
2466          && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE))
2467     {
2468       /* Scan insns from the top of the loop looking for a qualified
2469          conditional exit.  */
2470       for (insn = NEXT_INSN (loop_stack->data.loop.start_label); insn;
2471            insn = NEXT_INSN (insn))
2472         {
2473           if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == CODE_LABEL)
2474             break;
2475
2476           if (GET_CODE (insn) == NOTE
2477               && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2478                   || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2479             break;
2480
2481           if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == INSN)
2482             num_insns++;
2483
2484           if (last_test_insn && num_insns > 30)
2485             break;
2486
2487           if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == SET
2488               && SET_DEST (PATTERN (insn)) == pc_rtx
2489               && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE
2490               && ((GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == LABEL_REF
2491                    && (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0)
2492                        == loop_stack->data.loop.end_label))
2493                   || (GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 2)) == LABEL_REF
2494                       && (XEXP (XEXP (SET_SRC (PATTERN (insn)), 2), 0)
2495                           == loop_stack->data.loop.end_label))))
2496             last_test_insn = insn;
2497
2498           if (last_test_insn == 0 && GET_CODE (insn) == JUMP_INSN
2499               && GET_CODE (PATTERN (insn)) == SET
2500               && SET_DEST (PATTERN (insn)) == pc_rtx
2501               && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF
2502               && (XEXP (SET_SRC (PATTERN (insn)), 0)
2503                   == loop_stack->data.loop.end_label))
2504             /* Include BARRIER.  */
2505             last_test_insn = NEXT_INSN (insn);
2506         }
2507
2508       if (last_test_insn != 0 && last_test_insn != get_last_insn ())
2509         {
2510           /* We found one.  Move everything from there up
2511              to the end of the loop, and add a jump into the loop
2512              to jump to there.  */
2513           register rtx newstart_label = gen_label_rtx ();
2514           register rtx start_move = start_label;
2515
2516           /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
2517              then we want to move this note also.  */
2518           if (GET_CODE (PREV_INSN (start_move)) == NOTE
2519               && (NOTE_LINE_NUMBER (PREV_INSN (start_move))
2520                   == NOTE_INSN_LOOP_CONT))
2521             start_move = PREV_INSN (start_move);
2522
2523           emit_label_after (newstart_label, PREV_INSN (start_move));
2524           reorder_insns (start_move, last_test_insn, get_last_insn ());
2525           emit_jump_insn_after (gen_jump (start_label),
2526                                 PREV_INSN (newstart_label));
2527           emit_barrier_after (PREV_INSN (newstart_label));
2528           start_label = newstart_label;
2529         }
2530     }
2531
2532   emit_jump (start_label);
2533   emit_note (NULL_PTR, NOTE_INSN_LOOP_END);
2534   emit_label (loop_stack->data.loop.end_label);
2535
2536   POPSTACK (loop_stack);
2537
2538   last_expr_type = 0;
2539 }
2540
2541 /* Generate a jump to the current loop's continue-point.
2542    This is usually the top of the loop, but may be specified
2543    explicitly elsewhere.  If not currently inside a loop,
2544    return 0 and do nothing; caller will print an error message.  */
2545
2546 int
2547 expand_continue_loop (whichloop)
2548      struct nesting *whichloop;
2549 {
2550   last_expr_type = 0;
2551   if (whichloop == 0)
2552     whichloop = loop_stack;
2553   if (whichloop == 0)
2554     return 0;
2555   expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2556                         NULL_RTX);
2557   return 1;
2558 }
2559
2560 /* Generate a jump to exit the current loop.  If not currently inside a loop,
2561    return 0 and do nothing; caller will print an error message.  */
2562
2563 int
2564 expand_exit_loop (whichloop)
2565      struct nesting *whichloop;
2566 {
2567   last_expr_type = 0;
2568   if (whichloop == 0)
2569     whichloop = loop_stack;
2570   if (whichloop == 0)
2571     return 0;
2572   expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
2573   return 1;
2574 }
2575
2576 /* Generate a conditional jump to exit the current loop if COND
2577    evaluates to zero.  If not currently inside a loop,
2578    return 0 and do nothing; caller will print an error message.  */
2579
2580 int
2581 expand_exit_loop_if_false (whichloop, cond)
2582      struct nesting *whichloop;
2583      tree cond;
2584 {
2585   last_expr_type = 0;
2586   if (whichloop == 0)
2587     whichloop = loop_stack;
2588   if (whichloop == 0)
2589     return 0;
2590   if (output_bytecode)
2591     {
2592       bc_expand_expr (cond);
2593       bc_expand_goto_internal (xjumpifnot,
2594                                BYTECODE_BC_LABEL (whichloop->exit_label),
2595                                NULL_TREE);
2596     }
2597   else
2598     do_jump (cond, whichloop->data.loop.end_label, NULL_RTX);
2599
2600   return 1;
2601 }
2602
2603 /* Return non-zero if we should preserve sub-expressions as separate
2604    pseudos.  We never do so if we aren't optimizing.  We always do so
2605    if -fexpensive-optimizations.
2606
2607    Otherwise, we only do so if we are in the "early" part of a loop.  I.e.,
2608    the loop may still be a small one.  */
2609
2610 int
2611 preserve_subexpressions_p ()
2612 {
2613   rtx insn;
2614
2615   if (flag_expensive_optimizations)
2616     return 1;
2617
2618   if (optimize == 0 || loop_stack == 0)
2619     return 0;
2620
2621   insn = get_last_insn_anywhere ();
2622
2623   return (insn
2624           && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2625               < n_non_fixed_regs * 3));
2626
2627 }
2628
2629 /* Generate a jump to exit the current loop, conditional, binding contour
2630    or case statement.  Not all such constructs are visible to this function,
2631    only those started with EXIT_FLAG nonzero.  Individual languages use
2632    the EXIT_FLAG parameter to control which kinds of constructs you can
2633    exit this way.
2634
2635    If not currently inside anything that can be exited,
2636    return 0 and do nothing; caller will print an error message.  */
2637
2638 int
2639 expand_exit_something ()
2640 {
2641   struct nesting *n;
2642   last_expr_type = 0;
2643   for (n = nesting_stack; n; n = n->all)
2644     if (n->exit_label != 0)
2645       {
2646         expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
2647         return 1;
2648       }
2649
2650   return 0;
2651 }
2652 \f
2653 /* Generate RTL to return from the current function, with no value.
2654    (That is, we do not do anything about returning any value.)  */
2655
2656 void
2657 expand_null_return ()
2658 {
2659   struct nesting *block = block_stack;
2660   rtx last_insn = 0;
2661
2662   if (output_bytecode)
2663     {
2664       bc_emit_instruction (ret);
2665       return;
2666     }
2667
2668   /* Does any pending block have cleanups?  */
2669
2670   while (block && block->data.block.cleanups == 0)
2671     block = block->next;
2672
2673   /* If yes, use a goto to return, since that runs cleanups.  */
2674
2675   expand_null_return_1 (last_insn, block != 0);
2676 }
2677
2678 /* Generate RTL to return from the current function, with value VAL.  */
2679
2680 void
2681 expand_value_return (val)
2682      rtx val;
2683 {
2684   struct nesting *block = block_stack;
2685   rtx last_insn = get_last_insn ();
2686   rtx return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
2687
2688   /* Copy the value to the return location
2689      unless it's already there.  */
2690
2691   if (return_reg != val)
2692     {
2693 #ifdef PROMOTE_FUNCTION_RETURN
2694       tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
2695       int unsignedp = TREE_UNSIGNED (type);
2696       enum machine_mode mode
2697         = promote_mode (type, DECL_MODE (DECL_RESULT (current_function_decl)),
2698                         &unsignedp, 1);
2699
2700       if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
2701         convert_move (return_reg, val, unsignedp);
2702       else
2703 #endif
2704         emit_move_insn (return_reg, val);
2705     }
2706   if (GET_CODE (return_reg) == REG
2707       && REGNO (return_reg) < FIRST_PSEUDO_REGISTER)
2708     emit_insn (gen_rtx (USE, VOIDmode, return_reg));
2709
2710   /* Does any pending block have cleanups?  */
2711
2712   while (block && block->data.block.cleanups == 0)
2713     block = block->next;
2714
2715   /* If yes, use a goto to return, since that runs cleanups.
2716      Use LAST_INSN to put cleanups *before* the move insn emitted above.  */
2717
2718   expand_null_return_1 (last_insn, block != 0);
2719 }
2720
2721 /* Output a return with no value.  If LAST_INSN is nonzero,
2722    pretend that the return takes place after LAST_INSN.
2723    If USE_GOTO is nonzero then don't use a return instruction;
2724    go to the return label instead.  This causes any cleanups
2725    of pending blocks to be executed normally.  */
2726
2727 static void
2728 expand_null_return_1 (last_insn, use_goto)
2729      rtx last_insn;
2730      int use_goto;
2731 {
2732   rtx end_label = cleanup_label ? cleanup_label : return_label;
2733
2734   clear_pending_stack_adjust ();
2735   do_pending_stack_adjust ();
2736   last_expr_type = 0;
2737
2738   /* PCC-struct return always uses an epilogue.  */
2739   if (current_function_returns_pcc_struct || use_goto)
2740     {
2741       if (end_label == 0)
2742         end_label = return_label = gen_label_rtx ();
2743       expand_goto_internal (NULL_TREE, end_label, last_insn);
2744       return;
2745     }
2746
2747   /* Otherwise output a simple return-insn if one is available,
2748      unless it won't do the job.  */
2749 #ifdef HAVE_return
2750   if (HAVE_return && use_goto == 0 && cleanup_label == 0)
2751     {
2752       emit_jump_insn (gen_return ());
2753       emit_barrier ();
2754       return;
2755     }
2756 #endif
2757
2758   /* Otherwise jump to the epilogue.  */
2759   expand_goto_internal (NULL_TREE, end_label, last_insn);
2760 }
2761 \f
2762 /* Generate RTL to evaluate the expression RETVAL and return it
2763    from the current function.  */
2764
2765 void
2766 expand_return (retval)
2767      tree retval;
2768 {
2769   /* If there are any cleanups to be performed, then they will
2770      be inserted following LAST_INSN.  It is desirable
2771      that the last_insn, for such purposes, should be the
2772      last insn before computing the return value.  Otherwise, cleanups
2773      which call functions can clobber the return value.  */
2774   /* ??? rms: I think that is erroneous, because in C++ it would
2775      run destructors on variables that might be used in the subsequent
2776      computation of the return value.  */
2777   rtx last_insn = 0;
2778   register rtx val = 0;
2779   register rtx op0;
2780   tree retval_rhs;
2781   int cleanups;
2782   struct nesting *block;
2783
2784   /* Bytecode returns are quite simple, just leave the result on the
2785      arithmetic stack. */
2786   if (output_bytecode)
2787     {
2788       bc_expand_expr (retval);
2789       bc_emit_instruction (ret);
2790       return;
2791     }
2792   
2793   /* If function wants no value, give it none.  */
2794   if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2795     {
2796       expand_expr (retval, NULL_RTX, VOIDmode, 0);
2797       emit_queue ();
2798       expand_null_return ();
2799       return;
2800     }
2801
2802   /* Are any cleanups needed?  E.g. C++ destructors to be run?  */
2803   cleanups = any_pending_cleanups (1);
2804
2805   if (TREE_CODE (retval) == RESULT_DECL)
2806     retval_rhs = retval;
2807   else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
2808            && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
2809     retval_rhs = TREE_OPERAND (retval, 1);
2810   else if (TREE_TYPE (retval) == void_type_node)
2811     /* Recognize tail-recursive call to void function.  */
2812     retval_rhs = retval;
2813   else
2814     retval_rhs = NULL_TREE;
2815
2816   /* Only use `last_insn' if there are cleanups which must be run.  */
2817   if (cleanups || cleanup_label != 0)
2818     last_insn = get_last_insn ();
2819
2820   /* Distribute return down conditional expr if either of the sides
2821      may involve tail recursion (see test below).  This enhances the number
2822      of tail recursions we see.  Don't do this always since it can produce
2823      sub-optimal code in some cases and we distribute assignments into
2824      conditional expressions when it would help.  */
2825
2826   if (optimize && retval_rhs != 0
2827       && frame_offset == 0
2828       && TREE_CODE (retval_rhs) == COND_EXPR
2829       && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
2830           || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
2831     {
2832       rtx label = gen_label_rtx ();
2833       tree expr;
2834
2835       do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
2836       expr = build (MODIFY_EXPR, TREE_TYPE (current_function_decl),
2837                     DECL_RESULT (current_function_decl),
2838                     TREE_OPERAND (retval_rhs, 1));
2839       TREE_SIDE_EFFECTS (expr) = 1;
2840       expand_return (expr);
2841       emit_label (label);
2842
2843       expr = build (MODIFY_EXPR, TREE_TYPE (current_function_decl),
2844                     DECL_RESULT (current_function_decl),
2845                     TREE_OPERAND (retval_rhs, 2));
2846       TREE_SIDE_EFFECTS (expr) = 1;
2847       expand_return (expr);
2848       return;
2849     }
2850
2851   /* For tail-recursive call to current function,
2852      just jump back to the beginning.
2853      It's unsafe if any auto variable in this function
2854      has its address taken; for simplicity,
2855      require stack frame to be empty.  */
2856   if (optimize && retval_rhs != 0
2857       && frame_offset == 0
2858       && TREE_CODE (retval_rhs) == CALL_EXPR
2859       && TREE_CODE (TREE_OPERAND (retval_rhs, 0)) == ADDR_EXPR
2860       && TREE_OPERAND (TREE_OPERAND (retval_rhs, 0), 0) == current_function_decl
2861       /* Finish checking validity, and if valid emit code
2862          to set the argument variables for the new call.  */
2863       && tail_recursion_args (TREE_OPERAND (retval_rhs, 1),
2864                               DECL_ARGUMENTS (current_function_decl)))
2865     {
2866       if (tail_recursion_label == 0)
2867         {
2868           tail_recursion_label = gen_label_rtx ();
2869           emit_label_after (tail_recursion_label,
2870                             tail_recursion_reentry);
2871         }
2872       emit_queue ();
2873       expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
2874       emit_barrier ();
2875       return;
2876     }
2877 #ifdef HAVE_return
2878   /* This optimization is safe if there are local cleanups
2879      because expand_null_return takes care of them.
2880      ??? I think it should also be safe when there is a cleanup label,
2881      because expand_null_return takes care of them, too.
2882      Any reason why not?  */
2883   if (HAVE_return && cleanup_label == 0
2884       && ! current_function_returns_pcc_struct
2885       && BRANCH_COST <= 1)
2886     {
2887       /* If this is  return x == y;  then generate
2888          if (x == y) return 1; else return 0;
2889          if we can do it with explicit return insns and
2890          branches are cheap.  */
2891       if (retval_rhs)
2892         switch (TREE_CODE (retval_rhs))
2893           {
2894           case EQ_EXPR:
2895           case NE_EXPR:
2896           case GT_EXPR:
2897           case GE_EXPR:
2898           case LT_EXPR:
2899           case LE_EXPR:
2900           case TRUTH_ANDIF_EXPR:
2901           case TRUTH_ORIF_EXPR:
2902           case TRUTH_AND_EXPR:
2903           case TRUTH_OR_EXPR:
2904           case TRUTH_NOT_EXPR:
2905           case TRUTH_XOR_EXPR:
2906             op0 = gen_label_rtx ();
2907             jumpifnot (retval_rhs, op0);
2908             expand_value_return (const1_rtx);
2909             emit_label (op0);
2910             expand_value_return (const0_rtx);
2911             return;
2912           }
2913     }
2914 #endif /* HAVE_return */
2915
2916   if (cleanups
2917       && retval_rhs != 0
2918       && TREE_TYPE (retval_rhs) != void_type_node
2919       && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG)
2920     {
2921       /* Calculate the return value into a pseudo reg.  */
2922       val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
2923       emit_queue ();
2924       /* All temporaries have now been used.  */
2925       free_temp_slots ();
2926       /* Return the calculated value, doing cleanups first.  */
2927       expand_value_return (val);
2928     }
2929   else
2930     {
2931       /* No cleanups or no hard reg used;
2932          calculate value into hard return reg.  */
2933       expand_expr (retval, const0_rtx, VOIDmode, 0);
2934       emit_queue ();
2935       free_temp_slots ();
2936       expand_value_return (DECL_RTL (DECL_RESULT (current_function_decl)));
2937     }
2938 }
2939
2940 /* Return 1 if the end of the generated RTX is not a barrier.
2941    This means code already compiled can drop through.  */
2942
2943 int
2944 drop_through_at_end_p ()
2945 {
2946   rtx insn = get_last_insn ();
2947   while (insn && GET_CODE (insn) == NOTE)
2948     insn = PREV_INSN (insn);
2949   return insn && GET_CODE (insn) != BARRIER;
2950 }
2951 \f
2952 /* Emit code to alter this function's formal parms for a tail-recursive call.
2953    ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
2954    FORMALS is the chain of decls of formals.
2955    Return 1 if this can be done;
2956    otherwise return 0 and do not emit any code.  */
2957
2958 static int
2959 tail_recursion_args (actuals, formals)
2960      tree actuals, formals;
2961 {
2962   register tree a = actuals, f = formals;
2963   register int i;
2964   register rtx *argvec;
2965
2966   /* Check that number and types of actuals are compatible
2967      with the formals.  This is not always true in valid C code.
2968      Also check that no formal needs to be addressable
2969      and that all formals are scalars.  */
2970
2971   /* Also count the args.  */
2972
2973   for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
2974     {
2975       if (TREE_TYPE (TREE_VALUE (a)) != TREE_TYPE (f))
2976         return 0;
2977       if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
2978         return 0;
2979     }
2980   if (a != 0 || f != 0)
2981     return 0;
2982
2983   /* Compute all the actuals.  */
2984
2985   argvec = (rtx *) alloca (i * sizeof (rtx));
2986
2987   for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
2988     argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
2989
2990   /* Find which actual values refer to current values of previous formals.
2991      Copy each of them now, before any formal is changed.  */
2992
2993   for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
2994     {
2995       int copy = 0;
2996       register int j;
2997       for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
2998         if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
2999           { copy = 1; break; }
3000       if (copy)
3001         argvec[i] = copy_to_reg (argvec[i]);
3002     }
3003
3004   /* Store the values of the actuals into the formals.  */
3005
3006   for (f = formals, a = actuals, i = 0; f;
3007        f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
3008     {
3009       if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
3010         emit_move_insn (DECL_RTL (f), argvec[i]);
3011       else
3012         convert_move (DECL_RTL (f), argvec[i],
3013                       TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a))));
3014     }
3015
3016   free_temp_slots ();
3017   return 1;
3018 }
3019 \f
3020 /* Generate the RTL code for entering a binding contour.
3021    The variables are declared one by one, by calls to `expand_decl'.
3022
3023    EXIT_FLAG is nonzero if this construct should be visible to
3024    `exit_something'.  */
3025
3026 void
3027 expand_start_bindings (exit_flag)
3028      int exit_flag;
3029 {
3030   struct nesting *thisblock = ALLOC_NESTING ();
3031   rtx note = output_bytecode ? 0 : emit_note (NULL_PTR, NOTE_INSN_BLOCK_BEG);
3032
3033   /* Make an entry on block_stack for the block we are entering.  */
3034
3035   thisblock->next = block_stack;
3036   thisblock->all = nesting_stack;
3037   thisblock->depth = ++nesting_depth;
3038   thisblock->data.block.stack_level = 0;
3039   thisblock->data.block.cleanups = 0;
3040   thisblock->data.block.function_call_count = 0;
3041 #if 0
3042   if (block_stack)
3043     {
3044       if (block_stack->data.block.cleanups == NULL_TREE
3045           && (block_stack->data.block.outer_cleanups == NULL_TREE
3046               || block_stack->data.block.outer_cleanups == empty_cleanup_list))
3047         thisblock->data.block.outer_cleanups = empty_cleanup_list;
3048       else
3049         thisblock->data.block.outer_cleanups
3050           = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3051                        block_stack->data.block.outer_cleanups);
3052     }
3053   else
3054     thisblock->data.block.outer_cleanups = 0;
3055 #endif
3056 #if 1
3057   if (block_stack
3058       && !(block_stack->data.block.cleanups == NULL_TREE
3059            && block_stack->data.block.outer_cleanups == NULL_TREE))
3060     thisblock->data.block.outer_cleanups
3061       = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3062                    block_stack->data.block.outer_cleanups);
3063   else
3064     thisblock->data.block.outer_cleanups = 0;
3065 #endif
3066   thisblock->data.block.label_chain = 0;
3067   thisblock->data.block.innermost_stack_block = stack_block_stack;
3068   thisblock->data.block.first_insn = note;
3069   thisblock->data.block.block_start_count = ++block_start_count;
3070   thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3071   block_stack = thisblock;
3072   nesting_stack = thisblock;
3073
3074   if (!output_bytecode)
3075     {
3076       /* Make a new level for allocating stack slots.  */
3077       push_temp_slots ();
3078     }
3079 }
3080
3081 /* Given a pointer to a BLOCK node, save a pointer to the most recently
3082    generated NOTE_INSN_BLOCK_END in the BLOCK_END_NOTE field of the given
3083    BLOCK node.  */
3084
3085 void
3086 remember_end_note (block)
3087      register tree block;
3088 {
3089   BLOCK_END_NOTE (block) = last_block_end_note;
3090   last_block_end_note = NULL_RTX;
3091 }
3092
3093 /* Generate RTL code to terminate a binding contour.
3094    VARS is the chain of VAR_DECL nodes
3095    for the variables bound in this contour.
3096    MARK_ENDS is nonzero if we should put a note at the beginning
3097    and end of this binding contour.
3098
3099    DONT_JUMP_IN is nonzero if it is not valid to jump into this contour.
3100    (That is true automatically if the contour has a saved stack level.)  */
3101
3102 void
3103 expand_end_bindings (vars, mark_ends, dont_jump_in)
3104      tree vars;
3105      int mark_ends;
3106      int dont_jump_in;
3107 {
3108   register struct nesting *thisblock = block_stack;
3109   register tree decl;
3110
3111   if (output_bytecode)
3112     {
3113       bc_expand_end_bindings (vars, mark_ends, dont_jump_in);
3114       return;
3115     }
3116
3117   if (warn_unused)
3118     for (decl = vars; decl; decl = TREE_CHAIN (decl))
3119       if (! TREE_USED (decl) && TREE_CODE (decl) == VAR_DECL
3120           && ! DECL_IN_SYSTEM_HEADER (decl))
3121         warning_with_decl (decl, "unused variable `%s'");
3122
3123   if (thisblock->exit_label)
3124     {
3125       do_pending_stack_adjust ();
3126       emit_label (thisblock->exit_label);
3127     }
3128
3129   /* If necessary, make a handler for nonlocal gotos taking
3130      place in the function calls in this block.  */
3131   if (function_call_count != thisblock->data.block.function_call_count
3132       && nonlocal_labels
3133       /* Make handler for outermost block
3134          if there were any nonlocal gotos to this function.  */
3135       && (thisblock->next == 0 ? current_function_has_nonlocal_label
3136           /* Make handler for inner block if it has something
3137              special to do when you jump out of it.  */
3138           : (thisblock->data.block.cleanups != 0
3139              || thisblock->data.block.stack_level != 0)))
3140     {
3141       tree link;
3142       rtx afterward = gen_label_rtx ();
3143       rtx handler_label = gen_label_rtx ();
3144       rtx save_receiver = gen_reg_rtx (Pmode);
3145       rtx insns;
3146
3147       /* Don't let jump_optimize delete the handler.  */
3148       LABEL_PRESERVE_P (handler_label) = 1;
3149
3150       /* Record the handler address in the stack slot for that purpose,
3151          during this block, saving and restoring the outer value.  */
3152       if (thisblock->next != 0)
3153         {
3154           emit_move_insn (nonlocal_goto_handler_slot, save_receiver);
3155
3156           start_sequence ();
3157           emit_move_insn (save_receiver, nonlocal_goto_handler_slot);
3158           insns = get_insns ();
3159           end_sequence ();
3160           emit_insns_before (insns, thisblock->data.block.first_insn);
3161         }
3162
3163       start_sequence ();
3164       emit_move_insn (nonlocal_goto_handler_slot,
3165                       gen_rtx (LABEL_REF, Pmode, handler_label));
3166       insns = get_insns ();
3167       end_sequence ();
3168       emit_insns_before (insns, thisblock->data.block.first_insn);
3169
3170       /* Jump around the handler; it runs only when specially invoked.  */
3171       emit_jump (afterward);
3172       emit_label (handler_label);
3173
3174 #ifdef HAVE_nonlocal_goto
3175       if (! HAVE_nonlocal_goto)
3176 #endif
3177         /* First adjust our frame pointer to its actual value.  It was
3178            previously set to the start of the virtual area corresponding to
3179            the stacked variables when we branched here and now needs to be
3180            adjusted to the actual hardware fp value.
3181
3182            Assignments are to virtual registers are converted by
3183            instantiate_virtual_regs into the corresponding assignment
3184            to the underlying register (fp in this case) that makes
3185            the original assignment true.
3186            So the following insn will actually be
3187            decrementing fp by STARTING_FRAME_OFFSET.  */
3188         emit_move_insn (virtual_stack_vars_rtx, frame_pointer_rtx);
3189
3190 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3191       if (fixed_regs[ARG_POINTER_REGNUM])
3192         {
3193 #ifdef ELIMINABLE_REGS
3194           /* If the argument pointer can be eliminated in favor of the
3195              frame pointer, we don't need to restore it.  We assume here
3196              that if such an elimination is present, it can always be used.
3197              This is the case on all known machines; if we don't make this
3198              assumption, we do unnecessary saving on many machines.  */
3199           static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
3200           int i;
3201
3202           for (i = 0; i < sizeof elim_regs / sizeof elim_regs[0]; i++)
3203             if (elim_regs[i].from == ARG_POINTER_REGNUM
3204                 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3205               break;
3206
3207           if (i == sizeof elim_regs / sizeof elim_regs [0])
3208 #endif
3209             {
3210               /* Now restore our arg pointer from the address at which it
3211                  was saved in our stack frame.
3212                  If there hasn't be space allocated for it yet, make
3213                  some now.  */
3214               if (arg_pointer_save_area == 0)
3215                 arg_pointer_save_area
3216                   = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
3217               emit_move_insn (virtual_incoming_args_rtx,
3218                               /* We need a pseudo here, or else
3219                                  instantiate_virtual_regs_1 complains.  */
3220                               copy_to_reg (arg_pointer_save_area));
3221             }
3222         }
3223 #endif
3224
3225       /* The handler expects the desired label address in the static chain
3226          register.  It tests the address and does an appropriate jump
3227          to whatever label is desired.  */
3228       for (link = nonlocal_labels; link; link = TREE_CHAIN (link))
3229         /* Skip any labels we shouldn't be able to jump to from here.  */
3230         if (! DECL_TOO_LATE (TREE_VALUE (link)))
3231           {
3232             rtx not_this = gen_label_rtx ();
3233             rtx this = gen_label_rtx ();
3234             do_jump_if_equal (static_chain_rtx,
3235                               gen_rtx (LABEL_REF, Pmode, DECL_RTL (TREE_VALUE (link))),
3236                               this, 0);
3237             emit_jump (not_this);
3238             emit_label (this);
3239             expand_goto (TREE_VALUE (link));
3240             emit_label (not_this);
3241           }
3242       /* If label is not recognized, abort.  */
3243       emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "abort"), 0,
3244                          VOIDmode, 0);
3245       emit_label (afterward);
3246     }
3247
3248   /* Don't allow jumping into a block that has cleanups or a stack level.  */
3249   if (dont_jump_in
3250       || thisblock->data.block.stack_level != 0
3251       || thisblock->data.block.cleanups != 0)
3252     {
3253       struct label_chain *chain;
3254
3255       /* Any labels in this block are no longer valid to go to.
3256          Mark them to cause an error message.  */
3257       for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
3258         {
3259           DECL_TOO_LATE (chain->label) = 1;
3260           /* If any goto without a fixup came to this label,
3261              that must be an error, because gotos without fixups
3262              come from outside all saved stack-levels and all cleanups.  */
3263           if (TREE_ADDRESSABLE (chain->label))
3264             error_with_decl (chain->label,
3265                              "label `%s' used before containing binding contour");
3266         }
3267     }
3268
3269   /* Restore stack level in effect before the block
3270      (only if variable-size objects allocated).  */
3271   /* Perform any cleanups associated with the block.  */
3272
3273   if (thisblock->data.block.stack_level != 0
3274       || thisblock->data.block.cleanups != 0)
3275     {
3276       /* Don't let cleanups affect ({...}) constructs.  */
3277       int old_expr_stmts_for_value = expr_stmts_for_value;
3278       rtx old_last_expr_value = last_expr_value;
3279       tree old_last_expr_type = last_expr_type;
3280       expr_stmts_for_value = 0;
3281
3282       /* Do the cleanups.  */
3283       expand_cleanups (thisblock->data.block.cleanups, NULL_TREE);
3284       do_pending_stack_adjust ();
3285
3286       expr_stmts_for_value = old_expr_stmts_for_value;
3287       last_expr_value = old_last_expr_value;
3288       last_expr_type = old_last_expr_type;
3289
3290       /* Restore the stack level.  */
3291
3292       if (thisblock->data.block.stack_level != 0)
3293         {
3294           emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3295                               thisblock->data.block.stack_level, NULL_RTX);
3296           if (nonlocal_goto_handler_slot != 0)
3297             emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3298                              NULL_RTX);
3299         }
3300
3301       /* Any gotos out of this block must also do these things.
3302          Also report any gotos with fixups that came to labels in this
3303          level.  */
3304       fixup_gotos (thisblock,
3305                    thisblock->data.block.stack_level,
3306                    thisblock->data.block.cleanups,
3307                    thisblock->data.block.first_insn,
3308                    dont_jump_in);
3309     }
3310
3311   /* Mark the beginning and end of the scope if requested.
3312      We do this now, after running cleanups on the variables
3313      just going out of scope, so they are in scope for their cleanups.  */
3314
3315   if (mark_ends)
3316     last_block_end_note = emit_note (NULL_PTR, NOTE_INSN_BLOCK_END);
3317   else
3318     /* Get rid of the beginning-mark if we don't make an end-mark.  */
3319     NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
3320
3321   /* If doing stupid register allocation, make sure lives of all
3322      register variables declared here extend thru end of scope.  */
3323
3324   if (obey_regdecls)
3325     for (decl = vars; decl; decl = TREE_CHAIN (decl))
3326       {
3327         rtx rtl = DECL_RTL (decl);
3328         if (TREE_CODE (decl) == VAR_DECL && rtl != 0)
3329           use_variable (rtl);
3330       }
3331
3332   /* Restore block_stack level for containing block.  */
3333
3334   stack_block_stack = thisblock->data.block.innermost_stack_block;
3335   POPSTACK (block_stack);
3336
3337   /* Pop the stack slot nesting and free any slots at this level.  */
3338   pop_temp_slots ();
3339 }
3340
3341
3342 /* End a binding contour.
3343    VARS is the chain of VAR_DECL nodes for the variables bound
3344    in this contour.  MARK_ENDS is nonzer if we should put a note
3345    at the beginning and end of this binding contour.
3346    DONT_JUMP_IN is nonzero if it is not valid to jump into this
3347    contour.  */
3348
3349 static void
3350 bc_expand_end_bindings (vars, mark_ends, dont_jump_in)
3351      tree vars;
3352      int mark_ends;
3353      int dont_jump_in;
3354 {
3355   struct nesting *thisbind = nesting_stack;
3356   tree decl;
3357
3358   if (warn_unused)
3359     for (decl = vars; decl; decl = TREE_CHAIN (decl))
3360       if (! TREE_USED (TREE_VALUE (decl)) && TREE_CODE (TREE_VALUE (decl)) == VAR_DECL)
3361         warning_with_decl (decl, "unused variable `%s'");
3362
3363   if (thisbind->exit_label)
3364     bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thisbind->exit_label));
3365
3366   /* Pop block/bindings off stack */
3367   POPSTACK (block_stack);
3368 }
3369 \f
3370 /* Generate RTL for the automatic variable declaration DECL.
3371    (Other kinds of declarations are simply ignored if seen here.)
3372    CLEANUP is an expression to be executed at exit from this binding contour;
3373    for example, in C++, it might call the destructor for this variable.
3374
3375    If CLEANUP contains any SAVE_EXPRs, then you must preevaluate them
3376    either before or after calling `expand_decl' but before compiling
3377    any subsequent expressions.  This is because CLEANUP may be expanded
3378    more than once, on different branches of execution.
3379    For the same reason, CLEANUP may not contain a CALL_EXPR
3380    except as its topmost node--else `preexpand_calls' would get confused.
3381
3382    If CLEANUP is nonzero and DECL is zero, we record a cleanup
3383    that is not associated with any particular variable.
3384
3385    There is no special support here for C++ constructors.
3386    They should be handled by the proper code in DECL_INITIAL.  */
3387
3388 void
3389 expand_decl (decl)
3390      register tree decl;
3391 {
3392   struct nesting *thisblock = block_stack;
3393   tree type;
3394
3395   if (output_bytecode)
3396     {
3397       bc_expand_decl (decl, 0);
3398       return;
3399     }
3400
3401   type = TREE_TYPE (decl);
3402
3403   /* Only automatic variables need any expansion done.
3404      Static and external variables, and external functions,
3405      will be handled by `assemble_variable' (called from finish_decl).
3406      TYPE_DECL and CONST_DECL require nothing.
3407      PARM_DECLs are handled in `assign_parms'.  */
3408
3409   if (TREE_CODE (decl) != VAR_DECL)
3410     return;
3411   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3412     return;
3413
3414   /* Create the RTL representation for the variable.  */
3415
3416   if (type == error_mark_node)
3417     DECL_RTL (decl) = gen_rtx (MEM, BLKmode, const0_rtx);
3418   else if (DECL_SIZE (decl) == 0)
3419     /* Variable with incomplete type.  */
3420     {
3421       if (DECL_INITIAL (decl) == 0)
3422         /* Error message was already done; now avoid a crash.  */
3423         DECL_RTL (decl) = assign_stack_temp (DECL_MODE (decl), 0, 1);
3424       else
3425         /* An initializer is going to decide the size of this array.
3426            Until we know the size, represent its address with a reg.  */
3427         DECL_RTL (decl) = gen_rtx (MEM, BLKmode, gen_reg_rtx (Pmode));
3428     }
3429   else if (DECL_MODE (decl) != BLKmode
3430            /* If -ffloat-store, don't put explicit float vars
3431               into regs.  */
3432            && !(flag_float_store
3433                 && TREE_CODE (type) == REAL_TYPE)
3434            && ! TREE_THIS_VOLATILE (decl)
3435            && ! TREE_ADDRESSABLE (decl)
3436            && (DECL_REGISTER (decl) || ! obey_regdecls))
3437     {
3438       /* Automatic variable that can go in a register.  */
3439       int unsignedp = TREE_UNSIGNED (type);
3440       enum machine_mode reg_mode
3441         = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
3442
3443       if (TREE_CODE (type) == COMPLEX_TYPE)
3444         {
3445           rtx realpart, imagpart;
3446           enum machine_mode partmode = TYPE_MODE (TREE_TYPE (type));
3447
3448           /* For a complex type variable, make a CONCAT of two pseudos
3449              so that the real and imaginary parts
3450              can be allocated separately.  */
3451           realpart = gen_reg_rtx (partmode);
3452           REG_USERVAR_P (realpart) = 1;
3453           imagpart = gen_reg_rtx (partmode);
3454           REG_USERVAR_P (imagpart) = 1;
3455           DECL_RTL (decl) = gen_rtx (CONCAT, reg_mode, realpart, imagpart);
3456         }
3457       else
3458         {
3459           DECL_RTL (decl) = gen_reg_rtx (reg_mode);
3460           if (TREE_CODE (type) == POINTER_TYPE)
3461             mark_reg_pointer (DECL_RTL (decl));
3462           REG_USERVAR_P (DECL_RTL (decl)) = 1;
3463         }
3464     }
3465   else if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3466     {
3467       /* Variable of fixed size that goes on the stack.  */
3468       rtx oldaddr = 0;
3469       rtx addr;
3470
3471       /* If we previously made RTL for this decl, it must be an array
3472          whose size was determined by the initializer.
3473          The old address was a register; set that register now
3474          to the proper address.  */
3475       if (DECL_RTL (decl) != 0)
3476         {
3477           if (GET_CODE (DECL_RTL (decl)) != MEM
3478               || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
3479             abort ();
3480           oldaddr = XEXP (DECL_RTL (decl), 0);
3481         }
3482
3483       DECL_RTL (decl)
3484         = assign_stack_temp (DECL_MODE (decl),
3485                              ((TREE_INT_CST_LOW (DECL_SIZE (decl))
3486                                + BITS_PER_UNIT - 1)
3487                               / BITS_PER_UNIT),
3488                              1);
3489
3490       /* Set alignment we actually gave this decl.  */
3491       DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
3492                            : GET_MODE_BITSIZE (DECL_MODE (decl)));
3493
3494       if (oldaddr)
3495         {
3496           addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
3497           if (addr != oldaddr)
3498             emit_move_insn (oldaddr, addr);
3499         }
3500
3501       /* If this is a memory ref that contains aggregate components,
3502          mark it as such for cse and loop optimize.  */
3503       MEM_IN_STRUCT_P (DECL_RTL (decl)) = AGGREGATE_TYPE_P (TREE_TYPE (decl));
3504 #if 0
3505       /* If this is in memory because of -ffloat-store,
3506          set the volatile bit, to prevent optimizations from
3507          undoing the effects.  */
3508       if (flag_float_store && TREE_CODE (type) == REAL_TYPE)
3509         MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
3510 #endif
3511     }
3512   else
3513     /* Dynamic-size object: must push space on the stack.  */
3514     {
3515       rtx address, size;
3516
3517       /* Record the stack pointer on entry to block, if have
3518          not already done so.  */
3519       if (thisblock->data.block.stack_level == 0)
3520         {
3521           do_pending_stack_adjust ();
3522           emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3523                            &thisblock->data.block.stack_level,
3524                            thisblock->data.block.first_insn);
3525           stack_block_stack = thisblock;
3526         }
3527
3528       /* Compute the variable's size, in bytes.  */
3529       size = expand_expr (size_binop (CEIL_DIV_EXPR,
3530                                       DECL_SIZE (decl),
3531                                       size_int (BITS_PER_UNIT)),
3532                           NULL_RTX, VOIDmode, 0);
3533       free_temp_slots ();
3534
3535       /* This is equivalent to calling alloca.  */
3536       current_function_calls_alloca = 1;
3537
3538       /* Allocate space on the stack for the variable.  */
3539       address = allocate_dynamic_stack_space (size, NULL_RTX,
3540                                               DECL_ALIGN (decl));
3541
3542       if (nonlocal_goto_handler_slot != 0)
3543         emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
3544
3545       /* Reference the variable indirect through that rtx.  */
3546       DECL_RTL (decl) = gen_rtx (MEM, DECL_MODE (decl), address);
3547
3548       /* If this is a memory ref that contains aggregate components,
3549          mark it as such for cse and loop optimize.  */
3550       MEM_IN_STRUCT_P (DECL_RTL (decl)) = AGGREGATE_TYPE_P (TREE_TYPE (decl));
3551
3552       /* Indicate the alignment we actually gave this variable.  */
3553 #ifdef STACK_BOUNDARY
3554       DECL_ALIGN (decl) = STACK_BOUNDARY;
3555 #else
3556       DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
3557 #endif
3558     }
3559
3560   if (TREE_THIS_VOLATILE (decl))
3561     MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
3562 #if 0 /* A variable is not necessarily unchanging
3563          just because it is const.  RTX_UNCHANGING_P
3564          means no change in the function,
3565          not merely no change in the variable's scope.
3566          It is correct to set RTX_UNCHANGING_P if the variable's scope
3567          is the whole function.  There's no convenient way to test that.  */
3568   if (TREE_READONLY (decl))
3569     RTX_UNCHANGING_P (DECL_RTL (decl)) = 1;
3570 #endif
3571
3572   /* If doing stupid register allocation, make sure life of any
3573      register variable starts here, at the start of its scope.  */
3574
3575   if (obey_regdecls)
3576     use_variable (DECL_RTL (decl));
3577 }
3578
3579
3580 /* Generate code for the automatic variable declaration DECL.  For
3581    most variables this just means we give it a stack offset.  The
3582    compiler sometimes emits cleanups without variables and we will
3583    have to deal with those too.  */
3584
3585 static void
3586 bc_expand_decl (decl, cleanup)
3587      tree decl;
3588      tree cleanup;
3589 {
3590   tree type;
3591
3592   if (!decl)
3593     {
3594       /* A cleanup with no variable.  */
3595       if (!cleanup)
3596         abort ();
3597
3598       return;
3599     }
3600
3601   /* Only auto variables need any work.  */
3602   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3603     return;
3604
3605   type = TREE_TYPE (decl);
3606
3607   if (type == error_mark_node)
3608     DECL_RTL (decl) = bc_gen_rtx ((char *) 0, 0, (struct bc_label *) 0);
3609
3610   else if (DECL_SIZE (decl) == 0)
3611
3612     /* Variable with incomplete type.  The stack offset herein will be
3613        fixed later in expand_decl_init ().  */
3614     DECL_RTL (decl) = bc_gen_rtx ((char *) 0, 0, (struct bc_label *) 0);
3615
3616   else if (TREE_CONSTANT (DECL_SIZE (decl)))
3617     {
3618       DECL_RTL (decl) = bc_allocate_local (TREE_INT_CST_LOW (DECL_SIZE (decl)) / BITS_PER_UNIT,
3619                                            DECL_ALIGN (decl));
3620     }
3621   else
3622     DECL_RTL (decl) = bc_allocate_variable_array (DECL_SIZE (decl));
3623 }
3624 \f
3625 /* Emit code to perform the initialization of a declaration DECL.  */
3626
3627 void
3628 expand_decl_init (decl)
3629      tree decl;
3630 {
3631   int was_used = TREE_USED (decl);
3632
3633   if (output_bytecode)
3634     {
3635       bc_expand_decl_init (decl);
3636       return;
3637     }
3638
3639   /* If this is a CONST_DECL, we don't have to generate any code, but
3640      if DECL_INITIAL is a constant, call expand_expr to force TREE_CST_RTL
3641      to be set while in the obstack containing the constant.  If we don't
3642      do this, we can lose if we have functions nested three deep and the middle
3643      function makes a CONST_DECL whose DECL_INITIAL is a STRING_CST while
3644      the innermost function is the first to expand that STRING_CST.  */
3645   if (TREE_CODE (decl) == CONST_DECL)
3646     {
3647       if (DECL_INITIAL (decl) && TREE_CONSTANT (DECL_INITIAL (decl)))
3648         expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
3649                      EXPAND_INITIALIZER);
3650       return;
3651     }
3652
3653   if (TREE_STATIC (decl))
3654     return;
3655
3656   /* Compute and store the initial value now.  */
3657
3658   if (DECL_INITIAL (decl) == error_mark_node)
3659     {
3660       enum tree_code code = TREE_CODE (TREE_TYPE (decl));
3661       if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
3662           || code == POINTER_TYPE)
3663         expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
3664                            0, 0);
3665       emit_queue ();
3666     }
3667   else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
3668     {
3669       emit_line_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3670       expand_assignment (decl, DECL_INITIAL (decl), 0, 0);
3671       emit_queue ();
3672     }
3673
3674   /* Don't let the initialization count as "using" the variable.  */
3675   TREE_USED (decl) = was_used;
3676
3677   /* Free any temporaries we made while initializing the decl.  */
3678   free_temp_slots ();
3679 }
3680
3681 /* Expand initialization for variable-sized types. Allocate array
3682    using newlocalSI and set local variable, which is a pointer to the
3683    storage. */
3684
3685 static void
3686 bc_expand_variable_local_init (decl)
3687      tree decl;
3688 {
3689   /* Evaluate size expression and coerce to SI */
3690   bc_expand_expr (DECL_SIZE (decl));
3691
3692   /* Type sizes are always (?) of TREE_CODE INTEGER_CST, so
3693      no coercion is necessary (?) */
3694
3695 /*  emit_typecode_conversion (preferred_typecode (TYPE_MODE (DECL_SIZE (decl)),
3696                                                 TREE_UNSIGNED (DECL_SIZE (decl))), SIcode); */
3697
3698   /* Emit code to allocate array */
3699   bc_emit_instruction (newlocalSI);
3700
3701   /* Store array pointer in local variable. This is the only instance
3702      where we actually want the address of the pointer to the
3703      variable-size block, rather than the pointer itself.  We avoid
3704      using expand_address() since that would cause the pointer to be
3705      pushed rather than its address. Hence the hard-coded reference;
3706      notice also that the variable is always local (no global
3707      variable-size type variables). */
3708
3709   bc_load_localaddr (DECL_RTL (decl));
3710   bc_emit_instruction (storeP);
3711 }
3712
3713
3714 /* Emit code to initialize a declaration.  */
3715
3716 static void
3717 bc_expand_decl_init (decl)
3718      tree decl;
3719 {
3720   int org_stack_depth;
3721
3722   /* Statical initializers are handled elsewhere */
3723
3724   if (TREE_STATIC (decl))
3725     return;
3726
3727   /* Memory original stack depth */
3728   org_stack_depth = stack_depth;
3729
3730   /* If the type is variable-size, we first create its space (we ASSUME
3731      it CAN'T be static).  We do this regardless of whether there's an
3732      initializer assignment or not. */
3733
3734   if (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
3735     bc_expand_variable_local_init (decl);
3736
3737   /* Expand initializer assignment */
3738   if (DECL_INITIAL (decl) == error_mark_node)
3739     {
3740       enum tree_code code = TREE_CODE (TREE_TYPE (decl));
3741
3742       if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
3743           || code == POINTER_TYPE)
3744
3745         expand_assignment (TREE_TYPE (decl), decl, 0, 0);
3746     }
3747   else if (DECL_INITIAL (decl))
3748     expand_assignment (TREE_TYPE (decl), decl, 0, 0);
3749
3750   /* Restore stack depth */
3751   if (org_stack_depth > stack_depth)
3752     abort ();
3753
3754   bc_adjust_stack (stack_depth - org_stack_depth);
3755 }
3756  
3757
3758 /* CLEANUP is an expression to be executed at exit from this binding contour;
3759    for example, in C++, it might call the destructor for this variable.
3760
3761    If CLEANUP contains any SAVE_EXPRs, then you must preevaluate them
3762    either before or after calling `expand_decl' but before compiling
3763    any subsequent expressions.  This is because CLEANUP may be expanded
3764    more than once, on different branches of execution.
3765    For the same reason, CLEANUP may not contain a CALL_EXPR
3766    except as its topmost node--else `preexpand_calls' would get confused.
3767
3768    If CLEANUP is nonzero and DECL is zero, we record a cleanup
3769    that is not associated with any particular variable.   */
3770
3771 int
3772 expand_decl_cleanup (decl, cleanup)
3773      tree decl, cleanup;
3774 {
3775   struct nesting *thisblock = block_stack;
3776
3777   /* Error if we are not in any block.  */
3778   if (thisblock == 0)
3779     return 0;
3780
3781   /* Record the cleanup if there is one.  */
3782
3783   if (cleanup != 0)
3784     {
3785       thisblock->data.block.cleanups
3786         = temp_tree_cons (decl, cleanup, thisblock->data.block.cleanups);
3787       /* If this block has a cleanup, it belongs in stack_block_stack.  */
3788       stack_block_stack = thisblock;
3789     }
3790   return 1;
3791 }
3792 \f
3793 /* DECL is an anonymous union.  CLEANUP is a cleanup for DECL.
3794    DECL_ELTS is the list of elements that belong to DECL's type.
3795    In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup.  */
3796
3797 void
3798 expand_anon_union_decl (decl, cleanup, decl_elts)
3799      tree decl, cleanup, decl_elts;
3800 {
3801   struct nesting *thisblock = block_stack;
3802   rtx x;
3803
3804   expand_decl (decl, cleanup);
3805   x = DECL_RTL (decl);
3806
3807   while (decl_elts)
3808     {
3809       tree decl_elt = TREE_VALUE (decl_elts);
3810       tree cleanup_elt = TREE_PURPOSE (decl_elts);
3811       enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
3812
3813       /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
3814          instead create a new MEM rtx with the proper mode.  */
3815       if (GET_CODE (x) == MEM)
3816         {
3817           if (mode == GET_MODE (x))
3818             DECL_RTL (decl_elt) = x;
3819           else
3820             {
3821               DECL_RTL (decl_elt) = gen_rtx (MEM, mode, copy_rtx (XEXP (x, 0)));
3822               MEM_IN_STRUCT_P (DECL_RTL (decl_elt)) = MEM_IN_STRUCT_P (x);
3823               RTX_UNCHANGING_P (DECL_RTL (decl_elt)) = RTX_UNCHANGING_P (x);
3824             }
3825         }
3826       else if (GET_CODE (x) == REG)
3827         {
3828           if (mode == GET_MODE (x))
3829             DECL_RTL (decl_elt) = x;
3830           else
3831             DECL_RTL (decl_elt) = gen_rtx (SUBREG, mode, x, 0);
3832         }
3833       else
3834         abort ();
3835
3836       /* Record the cleanup if there is one.  */
3837
3838       if (cleanup != 0)
3839         thisblock->data.block.cleanups
3840           = temp_tree_cons (decl_elt, cleanup_elt,
3841                             thisblock->data.block.cleanups);
3842
3843       decl_elts = TREE_CHAIN (decl_elts);
3844     }
3845 }
3846 \f
3847 /* Expand a list of cleanups LIST.
3848    Elements may be expressions or may be nested lists.
3849
3850    If DONT_DO is nonnull, then any list-element
3851    whose TREE_PURPOSE matches DONT_DO is omitted.
3852    This is sometimes used to avoid a cleanup associated with
3853    a value that is being returned out of the scope.  */
3854
3855 static void
3856 expand_cleanups (list, dont_do)
3857      tree list;
3858      tree dont_do;
3859 {
3860   tree tail;
3861   for (tail = list; tail; tail = TREE_CHAIN (tail))
3862     if (dont_do == 0 || TREE_PURPOSE (tail) != dont_do)
3863       {
3864         if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
3865           expand_cleanups (TREE_VALUE (tail), dont_do);
3866         else
3867           {
3868             /* Cleanups may be run multiple times.  For example,
3869                when exiting a binding contour, we expand the
3870                cleanups associated with that contour.  When a goto
3871                within that binding contour has a target outside that
3872                contour, it will expand all cleanups from its scope to
3873                the target.  Though the cleanups are expanded multiple
3874                times, the control paths are non-overlapping so the
3875                cleanups will not be executed twice.  */
3876             expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
3877             free_temp_slots ();
3878           }
3879       }
3880 }
3881
3882 /* Move all cleanups from the current block_stack
3883    to the containing block_stack, where they are assumed to
3884    have been created.  If anything can cause a temporary to
3885    be created, but not expanded for more than one level of
3886    block_stacks, then this code will have to change.  */
3887
3888 void
3889 move_cleanups_up ()
3890 {
3891   struct nesting *block = block_stack;
3892   struct nesting *outer = block->next;
3893
3894   outer->data.block.cleanups
3895     = chainon (block->data.block.cleanups,
3896                outer->data.block.cleanups);
3897   block->data.block.cleanups = 0;
3898 }
3899
3900 tree
3901 last_cleanup_this_contour ()
3902 {
3903   if (block_stack == 0)
3904     return 0;
3905
3906   return block_stack->data.block.cleanups;
3907 }
3908
3909 /* Return 1 if there are any pending cleanups at this point.
3910    If THIS_CONTOUR is nonzero, check the current contour as well.
3911    Otherwise, look only at the contours that enclose this one.  */
3912
3913 int
3914 any_pending_cleanups (this_contour)
3915      int this_contour;
3916 {
3917   struct nesting *block;
3918
3919   if (block_stack == 0)
3920     return 0;
3921
3922   if (this_contour && block_stack->data.block.cleanups != NULL)
3923     return 1;
3924   if (block_stack->data.block.cleanups == 0
3925       && (block_stack->data.block.outer_cleanups == 0
3926 #if 0
3927           || block_stack->data.block.outer_cleanups == empty_cleanup_list
3928 #endif
3929           ))
3930     return 0;
3931
3932   for (block = block_stack->next; block; block = block->next)
3933     if (block->data.block.cleanups != 0)
3934       return 1;
3935
3936   return 0;
3937 }
3938 \f
3939 /* Enter a case (Pascal) or switch (C) statement.
3940    Push a block onto case_stack and nesting_stack
3941    to accumulate the case-labels that are seen
3942    and to record the labels generated for the statement.
3943
3944    EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
3945    Otherwise, this construct is transparent for `exit_something'.
3946
3947    EXPR is the index-expression to be dispatched on.
3948    TYPE is its nominal type.  We could simply convert EXPR to this type,
3949    but instead we take short cuts.  */
3950
3951 void
3952 expand_start_case (exit_flag, expr, type, printname)
3953      int exit_flag;
3954      tree expr;
3955      tree type;
3956      char *printname;
3957 {
3958   register struct nesting *thiscase = ALLOC_NESTING ();
3959
3960   /* Make an entry on case_stack for the case we are entering.  */
3961
3962   thiscase->next = case_stack;
3963   thiscase->all = nesting_stack;
3964   thiscase->depth = ++nesting_depth;
3965   thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
3966   thiscase->data.case_stmt.case_list = 0;
3967   thiscase->data.case_stmt.index_expr = expr;
3968   thiscase->data.case_stmt.nominal_type = type;
3969   thiscase->data.case_stmt.default_label = 0;
3970   thiscase->data.case_stmt.num_ranges = 0;
3971   thiscase->data.case_stmt.printname = printname;
3972   thiscase->data.case_stmt.seenlabel = 0;
3973   case_stack = thiscase;
3974   nesting_stack = thiscase;
3975
3976   if (output_bytecode)
3977     {
3978       bc_expand_start_case (thiscase, expr, type, printname);
3979       return;
3980     }
3981
3982   do_pending_stack_adjust ();
3983
3984   /* Make sure case_stmt.start points to something that won't
3985      need any transformation before expand_end_case.  */
3986   if (GET_CODE (get_last_insn ()) != NOTE)
3987     emit_note (NULL_PTR, NOTE_INSN_DELETED);
3988
3989   thiscase->data.case_stmt.start = get_last_insn ();
3990 }
3991
3992
3993 /* Enter a case statement. It is assumed that the caller has pushed
3994    the current context onto the case stack. */
3995
3996 static void
3997 bc_expand_start_case (thiscase, expr, type, printname)
3998      struct nesting *thiscase;
3999      tree expr;
4000      tree type;
4001      char *printname;
4002 {
4003   bc_expand_expr (expr);
4004   bc_expand_conversion (TREE_TYPE (expr), type);
4005
4006   /* For cases, the skip is a place we jump to that's emitted after
4007      the size of the jump table is known.  */
4008
4009   thiscase->data.case_stmt.skip_label = gen_label_rtx ();
4010   bc_emit_bytecode (jump);
4011   bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thiscase->data.case_stmt.skip_label));
4012
4013 #ifdef DEBUG_PRINT_CODE
4014   fputc ('\n', stderr);
4015 #endif
4016 }
4017
4018
4019 /* Start a "dummy case statement" within which case labels are invalid
4020    and are not connected to any larger real case statement.
4021    This can be used if you don't want to let a case statement jump
4022    into the middle of certain kinds of constructs.  */
4023
4024 void
4025 expand_start_case_dummy ()
4026 {
4027   register struct nesting *thiscase = ALLOC_NESTING ();
4028
4029   /* Make an entry on case_stack for the dummy.  */
4030
4031   thiscase->next = case_stack;
4032   thiscase->all = nesting_stack;
4033   thiscase->depth = ++nesting_depth;
4034   thiscase->exit_label = 0;
4035   thiscase->data.case_stmt.case_list = 0;
4036   thiscase->data.case_stmt.start = 0;
4037   thiscase->data.case_stmt.nominal_type = 0;
4038   thiscase->data.case_stmt.default_label = 0;
4039   thiscase->data.case_stmt.num_ranges = 0;
4040   case_stack = thiscase;
4041   nesting_stack = thiscase;
4042 }
4043
4044 /* End a dummy case statement.  */
4045
4046 void
4047 expand_end_case_dummy ()
4048 {
4049   POPSTACK (case_stack);
4050 }
4051
4052 /* Return the data type of the index-expression
4053    of the innermost case statement, or null if none.  */
4054
4055 tree
4056 case_index_expr_type ()
4057 {
4058   if (case_stack)
4059     return TREE_TYPE (case_stack->data.case_stmt.index_expr);
4060   return 0;
4061 }
4062 \f
4063 /* Accumulate one case or default label inside a case or switch statement.
4064    VALUE is the value of the case (a null pointer, for a default label).
4065    The function CONVERTER, when applied to arguments T and V,
4066    converts the value V to the type T.
4067
4068    If not currently inside a case or switch statement, return 1 and do
4069    nothing.  The caller will print a language-specific error message.
4070    If VALUE is a duplicate or overlaps, return 2 and do nothing
4071    except store the (first) duplicate node in *DUPLICATE.
4072    If VALUE is out of range, return 3 and do nothing.
4073    If we are jumping into the scope of a cleaup or var-sized array, return 5.
4074    Return 0 on success.
4075
4076    Extended to handle range statements.  */
4077
4078 int
4079 pushcase (value, converter, label, duplicate)
4080      register tree value;
4081      tree (*converter) PROTO((tree, tree));
4082      register tree label;
4083      tree *duplicate;
4084 {
4085   register struct case_node **l;
4086   register struct case_node *n;
4087   tree index_type;
4088   tree nominal_type;
4089
4090   if (output_bytecode)
4091     return bc_pushcase (value, label);
4092
4093   /* Fail if not inside a real case statement.  */
4094   if (! (case_stack && case_stack->data.case_stmt.start))
4095     return 1;
4096
4097   if (stack_block_stack
4098       && stack_block_stack->depth > case_stack->depth)
4099     return 5;
4100
4101   index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4102   nominal_type = case_stack->data.case_stmt.nominal_type;
4103
4104   /* If the index is erroneous, avoid more problems: pretend to succeed.  */
4105   if (index_type == error_mark_node)
4106     return 0;
4107
4108   /* Convert VALUE to the type in which the comparisons are nominally done.  */
4109   if (value != 0)
4110     value = (*converter) (nominal_type, value);
4111
4112   /* If this is the first label, warn if any insns have been emitted.  */
4113   if (case_stack->data.case_stmt.seenlabel == 0)
4114     {
4115       rtx insn;
4116       for (insn = case_stack->data.case_stmt.start;
4117            insn;
4118            insn = NEXT_INSN (insn))
4119         {
4120           if (GET_CODE (insn) == CODE_LABEL)
4121             break;
4122           if (GET_CODE (insn) != NOTE
4123               && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4124             {
4125               warning ("unreachable code at beginning of %s",
4126                        case_stack->data.case_stmt.printname);
4127               break;
4128             }
4129         }
4130     }
4131   case_stack->data.case_stmt.seenlabel = 1;
4132
4133   /* Fail if this value is out of range for the actual type of the index
4134      (which may be narrower than NOMINAL_TYPE).  */
4135   if (value != 0 && ! int_fits_type_p (value, index_type))
4136     return 3;
4137
4138   /* Fail if this is a duplicate or overlaps another entry.  */
4139   if (value == 0)
4140     {
4141       if (case_stack->data.case_stmt.default_label != 0)
4142         {
4143           *duplicate = case_stack->data.case_stmt.default_label;
4144           return 2;
4145         }
4146       case_stack->data.case_stmt.default_label = label;
4147     }
4148   else
4149     {
4150       /* Find the elt in the chain before which to insert the new value,
4151          to keep the chain sorted in increasing order.
4152          But report an error if this element is a duplicate.  */
4153       for (l = &case_stack->data.case_stmt.case_list;
4154            /* Keep going past elements distinctly less than VALUE.  */
4155            *l != 0 && tree_int_cst_lt ((*l)->high, value);
4156            l = &(*l)->right)
4157         ;
4158       if (*l)
4159         {
4160           /* Element we will insert before must be distinctly greater;
4161              overlap means error.  */
4162           if (! tree_int_cst_lt (value, (*l)->low))
4163             {
4164               *duplicate = (*l)->code_label;
4165               return 2;
4166             }
4167         }
4168
4169       /* Add this label to the chain, and succeed.
4170          Copy VALUE so it is on temporary rather than momentary
4171          obstack and will thus survive till the end of the case statement.  */
4172       n = (struct case_node *) oballoc (sizeof (struct case_node));
4173       n->left = 0;
4174       n->right = *l;
4175       n->high = n->low = copy_node (value);
4176       n->code_label = label;
4177       *l = n;
4178     }
4179
4180   expand_label (label);
4181   return 0;
4182 }
4183
4184 /* Like pushcase but this case applies to all values
4185    between VALUE1 and VALUE2 (inclusive).
4186    The return value is the same as that of pushcase
4187    but there is one additional error code:
4188    4 means the specified range was empty.  */
4189
4190 int
4191 pushcase_range (value1, value2, converter, label, duplicate)
4192      register tree value1, value2;
4193      tree (*converter) PROTO((tree, tree));
4194      register tree label;
4195      tree *duplicate;
4196 {
4197   register struct case_node **l;
4198   register struct case_node *n;
4199   tree index_type;
4200   tree nominal_type;
4201
4202   /* Fail if not inside a real case statement.  */
4203   if (! (case_stack && case_stack->data.case_stmt.start))
4204     return 1;
4205
4206   if (stack_block_stack
4207       && stack_block_stack->depth > case_stack->depth)
4208     return 5;
4209
4210   index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4211   nominal_type = case_stack->data.case_stmt.nominal_type;
4212
4213   /* If the index is erroneous, avoid more problems: pretend to succeed.  */
4214   if (index_type == error_mark_node)
4215     return 0;
4216
4217   /* If this is the first label, warn if any insns have been emitted.  */
4218   if (case_stack->data.case_stmt.seenlabel == 0)
4219     {
4220       rtx insn;
4221       for (insn = case_stack->data.case_stmt.start;
4222            insn;
4223            insn = NEXT_INSN (insn))
4224         {
4225           if (GET_CODE (insn) == CODE_LABEL)
4226             break;
4227           if (GET_CODE (insn) != NOTE
4228               && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4229             {
4230               warning ("unreachable code at beginning of %s",
4231                        case_stack->data.case_stmt.printname);
4232               break;
4233             }
4234         }
4235     }
4236   case_stack->data.case_stmt.seenlabel = 1;
4237
4238   /* Convert VALUEs to type in which the comparisons are nominally done.  */
4239   if (value1 == 0)  /* Negative infinity. */
4240     value1 = TYPE_MIN_VALUE(index_type);
4241   value1 = (*converter) (nominal_type, value1);
4242
4243   if (value2 == 0)  /* Positive infinity. */
4244     value2 = TYPE_MAX_VALUE(index_type);
4245   value2 = (*converter) (nominal_type, value2);
4246
4247   /* Fail if these values are out of range.  */
4248   if (! int_fits_type_p (value1, index_type))
4249     return 3;
4250
4251   if (! int_fits_type_p (value2, index_type))
4252     return 3;
4253
4254   /* Fail if the range is empty.  */
4255   if (tree_int_cst_lt (value2, value1))
4256     return 4;
4257
4258   /* If the bounds are equal, turn this into the one-value case.  */
4259   if (tree_int_cst_equal (value1, value2))
4260     return pushcase (value1, converter, label, duplicate);
4261
4262   /* Find the elt in the chain before which to insert the new value,
4263      to keep the chain sorted in increasing order.
4264      But report an error if this element is a duplicate.  */
4265   for (l = &case_stack->data.case_stmt.case_list;
4266        /* Keep going past elements distinctly less than this range.  */
4267        *l != 0 && tree_int_cst_lt ((*l)->high, value1);
4268        l = &(*l)->right)
4269     ;
4270   if (*l)
4271     {
4272       /* Element we will insert before must be distinctly greater;
4273          overlap means error.  */
4274       if (! tree_int_cst_lt (value2, (*l)->low))
4275         {
4276           *duplicate = (*l)->code_label;
4277           return 2;
4278         }
4279     }
4280
4281   /* Add this label to the chain, and succeed.
4282      Copy VALUE1, VALUE2 so they are on temporary rather than momentary
4283      obstack and will thus survive till the end of the case statement.  */
4284
4285   n = (struct case_node *) oballoc (sizeof (struct case_node));
4286   n->left = 0;
4287   n->right = *l;
4288   n->low = copy_node (value1);
4289   n->high = copy_node (value2);
4290   n->code_label = label;
4291   *l = n;
4292
4293   expand_label (label);
4294
4295   case_stack->data.case_stmt.num_ranges++;
4296
4297   return 0;
4298 }
4299
4300
4301 /* Accumulate one case or default label; VALUE is the value of the
4302    case, or nil for a default label.  If not currently inside a case,
4303    return 1 and do nothing.  If VALUE is a duplicate or overlaps, return
4304    2 and do nothing.  If VALUE is out of range, return 3 and do nothing.
4305    Return 0 on success.  This function is a leftover from the earlier
4306    bytecode compiler, which was based on gcc 1.37.  It should be
4307    merged into pushcase. */
4308
4309 static int
4310 bc_pushcase (value, label)
4311      tree value;
4312      tree label;
4313 {
4314   struct nesting *thiscase = case_stack;
4315   struct case_node *case_label, *new_label;
4316
4317   if (! thiscase)
4318     return 1;
4319
4320   /* Fail if duplicate, overlap, or out of type range.  */
4321   if (value)
4322     {
4323       value = convert (thiscase->data.case_stmt.nominal_type, value);
4324       if (! int_fits_type_p (value, thiscase->data.case_stmt.nominal_type))
4325         return 3;
4326
4327       for (case_label = thiscase->data.case_stmt.case_list;
4328            case_label->left; case_label = case_label->left)
4329         if (! tree_int_cst_lt (case_label->left->high, value))
4330           break;
4331
4332       if (case_label != thiscase->data.case_stmt.case_list
4333           && ! tree_int_cst_lt (case_label->high, value)
4334           || case_label->left && ! tree_int_cst_lt (value, case_label->left->low))
4335         return 2;
4336
4337       new_label = (struct case_node *) oballoc (sizeof (struct case_node));
4338       new_label->low = new_label->high = copy_node (value);
4339       new_label->code_label = label;
4340       new_label->left = case_label->left;
4341
4342       case_label->left = new_label;
4343       thiscase->data.case_stmt.num_ranges++;
4344     }
4345   else
4346     {
4347       if (thiscase->data.case_stmt.default_label)
4348         return 2;
4349       thiscase->data.case_stmt.default_label = label;
4350     }
4351
4352   expand_label (label);
4353   return 0;
4354 }
4355 \f
4356 /* Called when the index of a switch statement is an enumerated type
4357    and there is no default label.
4358
4359    Checks that all enumeration literals are covered by the case
4360    expressions of a switch.  Also, warn if there are any extra
4361    switch cases that are *not* elements of the enumerated type.
4362
4363    If all enumeration literals were covered by the case expressions,
4364    turn one of the expressions into the default expression since it should
4365    not be possible to fall through such a switch.  */
4366
4367 void
4368 check_for_full_enumeration_handling (type)
4369      tree type;
4370 {
4371   register struct case_node *n;
4372   register struct case_node **l;
4373   register tree chain;
4374   int all_values = 1;
4375
4376   if (output_bytecode)
4377     {
4378       bc_check_for_full_enumeration_handling (type);
4379       return;
4380     }
4381
4382   /* The time complexity of this loop is currently O(N * M), with
4383      N being the number of members in the enumerated type, and
4384      M being the number of case expressions in the switch. */
4385
4386   for (chain = TYPE_VALUES (type);
4387        chain;
4388        chain = TREE_CHAIN (chain))
4389     {
4390       /* Find a match between enumeral and case expression, if possible.
4391          Quit looking when we've gone too far (since case expressions
4392          are kept sorted in ascending order).  Warn about enumerators not
4393          handled in the switch statement case expression list. */
4394
4395       for (n = case_stack->data.case_stmt.case_list;
4396            n && tree_int_cst_lt (n->high, TREE_VALUE (chain));
4397            n = n->right)
4398         ;
4399
4400       if (!n || tree_int_cst_lt (TREE_VALUE (chain), n->low))
4401         {
4402           if (warn_switch)
4403             warning ("enumeration value `%s' not handled in switch",
4404                      IDENTIFIER_POINTER (TREE_PURPOSE (chain)));
4405           all_values = 0;
4406         }
4407     }
4408
4409   /* Now we go the other way around; we warn if there are case
4410      expressions that don't correspond to enumerators.  This can
4411      occur since C and C++ don't enforce type-checking of
4412      assignments to enumeration variables. */
4413
4414   if (warn_switch)
4415     for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
4416       {
4417         for (chain = TYPE_VALUES (type);
4418              chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
4419              chain = TREE_CHAIN (chain))
4420           ;
4421
4422         if (!chain)
4423           {
4424             if (TYPE_NAME (type) == 0)
4425               warning ("case value `%d' not in enumerated type",
4426                        TREE_INT_CST_LOW (n->low));
4427             else
4428               warning ("case value `%d' not in enumerated type `%s'",
4429                        TREE_INT_CST_LOW (n->low),
4430                        IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
4431                                             == IDENTIFIER_NODE)
4432                                            ? TYPE_NAME (type)
4433                                            : DECL_NAME (TYPE_NAME (type))));
4434           }
4435         if (!tree_int_cst_equal (n->low, n->high))
4436           {
4437             for (chain = TYPE_VALUES (type);
4438                  chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
4439                  chain = TREE_CHAIN (chain))
4440               ;
4441
4442             if (!chain)
4443               {
4444                 if (TYPE_NAME (type) == 0)
4445                   warning ("case value `%d' not in enumerated type",
4446                            TREE_INT_CST_LOW (n->high));
4447                 else
4448                   warning ("case value `%d' not in enumerated type `%s'",
4449                            TREE_INT_CST_LOW (n->high),
4450                            IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
4451                                                 == IDENTIFIER_NODE)
4452                                                ? TYPE_NAME (type)
4453                                                : DECL_NAME (TYPE_NAME (type))));
4454               }
4455           }
4456       }
4457
4458 #if 0
4459   /* ??? This optimization is disabled because it causes valid programs to
4460      fail.  ANSI C does not guarantee that an expression with enum type
4461      will have a value that is the same as one of the enumation literals.  */
4462
4463   /* If all values were found as case labels, make one of them the default
4464      label.  Thus, this switch will never fall through.  We arbitrarily pick
4465      the last one to make the default since this is likely the most
4466      efficient choice.  */
4467
4468   if (all_values)
4469     {
4470       for (l = &case_stack->data.case_stmt.case_list;
4471            (*l)->right != 0;
4472            l = &(*l)->right)
4473         ;
4474
4475       case_stack->data.case_stmt.default_label = (*l)->code_label;
4476       *l = 0;
4477     }
4478 #endif /* 0 */
4479 }
4480
4481
4482 /* Check that all enumeration literals are covered by the case
4483    expressions of a switch.  Also warn if there are any cases
4484    that are not elements of the enumerated type.  */
4485
4486 static void
4487 bc_check_for_full_enumeration_handling (type)
4488      tree type;
4489 {
4490   struct nesting *thiscase = case_stack;
4491   struct case_node *c;
4492   tree e;
4493
4494   /* Check for enums not handled.  */
4495   for (e = TYPE_VALUES (type); e; e = TREE_CHAIN (e))
4496     {
4497       for (c = thiscase->data.case_stmt.case_list->left;
4498            c && tree_int_cst_lt (c->high, TREE_VALUE (e));
4499            c = c->left)
4500         ;
4501       if (! (c && tree_int_cst_equal (c->low, TREE_VALUE (e))))
4502         warning ("enumerated value `%s' not handled in switch",
4503                  IDENTIFIER_POINTER (TREE_PURPOSE (e)));
4504     }
4505
4506   /* Check for cases not in the enumeration.  */
4507   for (c = thiscase->data.case_stmt.case_list->left; c; c = c->left)
4508     {
4509       for (e = TYPE_VALUES (type);
4510            e && !tree_int_cst_equal (c->low, TREE_VALUE (e));
4511            e = TREE_CHAIN (e))
4512         ;
4513       if (! e)
4514         warning ("case value `%d' not in enumerated type `%s'",
4515                  TREE_INT_CST_LOW (c->low),
4516                  IDENTIFIER_POINTER (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
4517                                      ? TYPE_NAME (type)
4518                                      : DECL_NAME (TYPE_NAME (type))));
4519     }
4520 }
4521 \f
4522 /* Terminate a case (Pascal) or switch (C) statement
4523    in which ORIG_INDEX is the expression to be tested.
4524    Generate the code to test it and jump to the right place.  */
4525
4526 void
4527 expand_end_case (orig_index)
4528      tree orig_index;
4529 {
4530   tree minval, maxval, range, orig_minval;
4531   rtx default_label = 0;
4532   register struct case_node *n;
4533   int count;
4534   rtx index;
4535   rtx table_label;
4536   int ncases;
4537   rtx *labelvec;
4538   register int i;
4539   rtx before_case;
4540   register struct nesting *thiscase = case_stack;
4541   tree index_expr;
4542   int unsignedp;
4543
4544   if (output_bytecode)
4545     {
4546       bc_expand_end_case (orig_index);
4547       return;
4548     }
4549
4550   table_label = gen_label_rtx ();
4551   index_expr = thiscase->data.case_stmt.index_expr;
4552   unsignedp = TREE_UNSIGNED (TREE_TYPE (index_expr));
4553
4554   do_pending_stack_adjust ();
4555
4556   /* An ERROR_MARK occurs for various reasons including invalid data type.  */
4557   if (TREE_TYPE (index_expr) != error_mark_node)
4558     {
4559       /* If switch expression was an enumerated type, check that all
4560          enumeration literals are covered by the cases.
4561          No sense trying this if there's a default case, however.  */
4562
4563       if (!thiscase->data.case_stmt.default_label
4564           && TREE_CODE (TREE_TYPE (orig_index)) == ENUMERAL_TYPE
4565           && TREE_CODE (index_expr) != INTEGER_CST)
4566         check_for_full_enumeration_handling (TREE_TYPE (orig_index));
4567
4568       /* If this is the first label, warn if any insns have been emitted.  */
4569       if (thiscase->data.case_stmt.seenlabel == 0)
4570         {
4571           rtx insn;
4572           for (insn = get_last_insn ();
4573                insn != case_stack->data.case_stmt.start;
4574                insn = PREV_INSN (insn))
4575             if (GET_CODE (insn) != NOTE
4576                 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn))!= USE))
4577               {
4578                 warning ("unreachable code at beginning of %s",
4579                          case_stack->data.case_stmt.printname);
4580                 break;
4581               }
4582         }
4583
4584       /* If we don't have a default-label, create one here,
4585          after the body of the switch.  */
4586       if (thiscase->data.case_stmt.default_label == 0)
4587         {
4588           thiscase->data.case_stmt.default_label
4589             = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4590           expand_label (thiscase->data.case_stmt.default_label);
4591         }
4592       default_label = label_rtx (thiscase->data.case_stmt.default_label);
4593
4594       before_case = get_last_insn ();
4595
4596       /* Simplify the case-list before we count it.  */
4597       group_case_nodes (thiscase->data.case_stmt.case_list);
4598
4599       /* Get upper and lower bounds of case values.
4600          Also convert all the case values to the index expr's data type.  */
4601
4602       count = 0;
4603       for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
4604         {
4605           /* Check low and high label values are integers.  */
4606           if (TREE_CODE (n->low) != INTEGER_CST)
4607             abort ();
4608           if (TREE_CODE (n->high) != INTEGER_CST)
4609             abort ();
4610
4611           n->low = convert (TREE_TYPE (index_expr), n->low);
4612           n->high = convert (TREE_TYPE (index_expr), n->high);
4613
4614           /* Count the elements and track the largest and smallest
4615              of them (treating them as signed even if they are not).  */
4616           if (count++ == 0)
4617             {
4618               minval = n->low;
4619               maxval = n->high;
4620             }
4621           else
4622             {
4623               if (INT_CST_LT (n->low, minval))
4624                 minval = n->low;
4625               if (INT_CST_LT (maxval, n->high))
4626                 maxval = n->high;
4627             }
4628           /* A range counts double, since it requires two compares.  */
4629           if (! tree_int_cst_equal (n->low, n->high))
4630             count++;
4631         }
4632
4633       orig_minval = minval;
4634
4635       /* Compute span of values.  */
4636       if (count != 0)
4637         range = fold (build (MINUS_EXPR, TREE_TYPE (index_expr),
4638                              maxval, minval));
4639
4640       if (count == 0 || TREE_CODE (TREE_TYPE (index_expr)) == ERROR_MARK)
4641         {
4642           expand_expr (index_expr, const0_rtx, VOIDmode, 0);
4643           emit_queue ();
4644           emit_jump (default_label);
4645         }
4646
4647       /* If range of values is much bigger than number of values,
4648          make a sequence of conditional branches instead of a dispatch.
4649          If the switch-index is a constant, do it this way
4650          because we can optimize it.  */
4651
4652 #ifndef CASE_VALUES_THRESHOLD
4653 #ifdef HAVE_casesi
4654 #define CASE_VALUES_THRESHOLD (HAVE_casesi ? 4 : 5)
4655 #else
4656       /* If machine does not have a case insn that compares the
4657          bounds, this means extra overhead for dispatch tables
4658          which raises the threshold for using them.  */
4659 #define CASE_VALUES_THRESHOLD 5
4660 #endif /* HAVE_casesi */
4661 #endif /* CASE_VALUES_THRESHOLD */
4662
4663       else if (TREE_INT_CST_HIGH (range) != 0
4664                || count < CASE_VALUES_THRESHOLD
4665                || ((unsigned HOST_WIDE_INT) (TREE_INT_CST_LOW (range))
4666                    > 10 * count)
4667                || TREE_CODE (index_expr) == INTEGER_CST
4668                /* These will reduce to a constant.  */
4669                || (TREE_CODE (index_expr) == CALL_EXPR
4670                    && TREE_CODE (TREE_OPERAND (index_expr, 0)) == ADDR_EXPR
4671                    && TREE_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == FUNCTION_DECL
4672                    && DECL_FUNCTION_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == BUILT_IN_CLASSIFY_TYPE)
4673                || (TREE_CODE (index_expr) == COMPOUND_EXPR
4674                    && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
4675         {
4676           index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
4677
4678           /* If the index is a short or char that we do not have
4679              an insn to handle comparisons directly, convert it to
4680              a full integer now, rather than letting each comparison
4681              generate the conversion.  */
4682
4683           if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
4684               && (cmp_optab->handlers[(int) GET_MODE(index)].insn_code
4685                   == CODE_FOR_nothing))
4686             {
4687               enum machine_mode wider_mode;
4688               for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
4689                    wider_mode = GET_MODE_WIDER_MODE (wider_mode))
4690                 if (cmp_optab->handlers[(int) wider_mode].insn_code
4691                     != CODE_FOR_nothing)
4692                   {
4693                     index = convert_to_mode (wider_mode, index, unsignedp);
4694                     break;
4695                   }
4696             }
4697
4698           emit_queue ();
4699           do_pending_stack_adjust ();
4700
4701           index = protect_from_queue (index, 0);
4702           if (GET_CODE (index) == MEM)
4703             index = copy_to_reg (index);
4704           if (GET_CODE (index) == CONST_INT
4705               || TREE_CODE (index_expr) == INTEGER_CST)
4706             {
4707               /* Make a tree node with the proper constant value
4708                  if we don't already have one.  */
4709               if (TREE_CODE (index_expr) != INTEGER_CST)
4710                 {
4711                   index_expr
4712                     = build_int_2 (INTVAL (index),
4713                                    !unsignedp && INTVAL (index) >= 0 ? 0 : -1);
4714                   index_expr = convert (TREE_TYPE (index_expr), index_expr);
4715                 }
4716
4717               /* For constant index expressions we need only
4718                  issue a unconditional branch to the appropriate
4719                  target code.  The job of removing any unreachable
4720                  code is left to the optimisation phase if the
4721                  "-O" option is specified.  */
4722               for (n = thiscase->data.case_stmt.case_list;
4723                    n;
4724                    n = n->right)
4725                 {
4726                   if (! tree_int_cst_lt (index_expr, n->low)
4727                       && ! tree_int_cst_lt (n->high, index_expr))
4728                     break;
4729                 }
4730               if (n)
4731                 emit_jump (label_rtx (n->code_label));
4732               else
4733                 emit_jump (default_label);
4734             }
4735           else
4736             {
4737               /* If the index expression is not constant we generate
4738                  a binary decision tree to select the appropriate
4739                  target code.  This is done as follows:
4740
4741                  The list of cases is rearranged into a binary tree,
4742                  nearly optimal assuming equal probability for each case.
4743
4744                  The tree is transformed into RTL, eliminating
4745                  redundant test conditions at the same time.
4746
4747                  If program flow could reach the end of the
4748                  decision tree an unconditional jump to the
4749                  default code is emitted.  */
4750
4751               use_cost_table
4752                 = (TREE_CODE (TREE_TYPE (orig_index)) != ENUMERAL_TYPE
4753                    && estimate_case_costs (thiscase->data.case_stmt.case_list));
4754               balance_case_nodes (&thiscase->data.case_stmt.case_list, 
4755                                   NULL_PTR);
4756               emit_case_nodes (index, thiscase->data.case_stmt.case_list,
4757                                default_label, TREE_TYPE (index_expr));
4758               emit_jump_if_reachable (default_label);
4759             }
4760         }
4761       else
4762         {
4763           int win = 0;
4764 #ifdef HAVE_casesi
4765           if (HAVE_casesi)
4766             {
4767               enum machine_mode index_mode = SImode;
4768               int index_bits = GET_MODE_BITSIZE (index_mode);
4769
4770               /* Convert the index to SImode.  */
4771               if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (index_expr)))
4772                   > GET_MODE_BITSIZE (index_mode))
4773                 {
4774                   enum machine_mode omode = TYPE_MODE (TREE_TYPE (index_expr));
4775                   rtx rangertx = expand_expr (range, NULL_RTX, VOIDmode, 0);
4776
4777                   /* We must handle the endpoints in the original mode.  */
4778                   index_expr = build (MINUS_EXPR, TREE_TYPE (index_expr),
4779                                       index_expr, minval);
4780                   minval = integer_zero_node;
4781                   index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
4782                   emit_cmp_insn (rangertx, index, LTU, NULL_RTX, omode, 1, 0);
4783                   emit_jump_insn (gen_bltu (default_label));
4784                   /* Now we can safely truncate.  */
4785                   index = convert_to_mode (index_mode, index, 0);
4786                 }
4787               else
4788                 {
4789                   if (TYPE_MODE (TREE_TYPE (index_expr)) != index_mode)
4790                     index_expr = convert (type_for_size (index_bits, 0),
4791                                           index_expr);
4792                   index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
4793                 }
4794               emit_queue ();
4795               index = protect_from_queue (index, 0);
4796               do_pending_stack_adjust ();
4797
4798               emit_jump_insn (gen_casesi (index, expand_expr (minval, NULL_RTX,
4799                                                               VOIDmode, 0),
4800                                           expand_expr (range, NULL_RTX,
4801                                                        VOIDmode, 0),
4802                                           table_label, default_label));
4803               win = 1;
4804             }
4805 #endif
4806 #ifdef HAVE_tablejump
4807           if (! win && HAVE_tablejump)
4808             {
4809               index_expr = convert (thiscase->data.case_stmt.nominal_type,
4810                                     fold (build (MINUS_EXPR,
4811                                                  TREE_TYPE (index_expr),
4812                                                  index_expr, minval)));
4813               index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
4814               emit_queue ();
4815               index = protect_from_queue (index, 0);
4816               do_pending_stack_adjust ();
4817
4818               do_tablejump (index, TYPE_MODE (TREE_TYPE (index_expr)),
4819                             expand_expr (range, NULL_RTX, VOIDmode, 0),
4820                             table_label, default_label);
4821               win = 1;
4822             }
4823 #endif
4824           if (! win)
4825             abort ();
4826
4827           /* Get table of labels to jump to, in order of case index.  */
4828
4829           ncases = TREE_INT_CST_LOW (range) + 1;
4830           labelvec = (rtx *) alloca (ncases * sizeof (rtx));
4831           bzero (labelvec, ncases * sizeof (rtx));
4832
4833           for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
4834             {
4835               register HOST_WIDE_INT i
4836                 = TREE_INT_CST_LOW (n->low) - TREE_INT_CST_LOW (orig_minval);
4837
4838               while (1)
4839                 {
4840                   labelvec[i]
4841                     = gen_rtx (LABEL_REF, Pmode, label_rtx (n->code_label));
4842                   if (i + TREE_INT_CST_LOW (orig_minval)
4843                       == TREE_INT_CST_LOW (n->high))
4844                     break;
4845                   i++;
4846                 }
4847             }
4848
4849           /* Fill in the gaps with the default.  */
4850           for (i = 0; i < ncases; i++)
4851             if (labelvec[i] == 0)
4852               labelvec[i] = gen_rtx (LABEL_REF, Pmode, default_label);
4853
4854           /* Output the table */
4855           emit_label (table_label);
4856
4857           /* This would be a lot nicer if CASE_VECTOR_PC_RELATIVE
4858              were an expression, instead of an #ifdef/#ifndef.  */
4859           if (
4860 #ifdef CASE_VECTOR_PC_RELATIVE
4861               1 ||
4862 #endif
4863               flag_pic)
4864             emit_jump_insn (gen_rtx (ADDR_DIFF_VEC, CASE_VECTOR_MODE,
4865                                      gen_rtx (LABEL_REF, Pmode, table_label),
4866                                      gen_rtvec_v (ncases, labelvec)));
4867           else
4868             emit_jump_insn (gen_rtx (ADDR_VEC, CASE_VECTOR_MODE,
4869                                      gen_rtvec_v (ncases, labelvec)));
4870
4871           /* If the case insn drops through the table,
4872              after the table we must jump to the default-label.
4873              Otherwise record no drop-through after the table.  */
4874 #ifdef CASE_DROPS_THROUGH
4875           emit_jump (default_label);
4876 #else
4877           emit_barrier ();
4878 #endif
4879         }
4880
4881       before_case = squeeze_notes (NEXT_INSN (before_case), get_last_insn ());
4882       reorder_insns (before_case, get_last_insn (),
4883                      thiscase->data.case_stmt.start);
4884     }
4885   if (thiscase->exit_label)
4886     emit_label (thiscase->exit_label);
4887
4888   POPSTACK (case_stack);
4889
4890   free_temp_slots ();
4891 }
4892
4893
4894 /* Terminate a case statement.  EXPR is the original index
4895    expression.  */
4896
4897 static void
4898 bc_expand_end_case (expr)
4899      tree expr;
4900 {
4901   struct nesting *thiscase = case_stack;
4902   enum bytecode_opcode opcode;
4903   struct bc_label *jump_label;
4904   struct case_node *c;
4905
4906   bc_emit_bytecode (jump);
4907   bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thiscase->exit_label));
4908
4909 #ifdef DEBUG_PRINT_CODE
4910   fputc ('\n', stderr);
4911 #endif
4912
4913   /* Now that the size of the jump table is known, emit the actual
4914      indexed jump instruction.  */
4915   bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thiscase->data.case_stmt.skip_label));
4916
4917   opcode = TYPE_MODE (thiscase->data.case_stmt.nominal_type) == SImode
4918     ? TREE_UNSIGNED (thiscase->data.case_stmt.nominal_type) ? caseSU : caseSI
4919       : TREE_UNSIGNED (thiscase->data.case_stmt.nominal_type) ? caseDU : caseDI;
4920
4921   bc_emit_bytecode (opcode);
4922
4923   /* Now emit the case instructions literal arguments, in order.
4924      In addition to the value on the stack, it uses:
4925      1.  The address of the jump table.
4926      2.  The size of the jump table.
4927      3.  The default label.  */
4928
4929   jump_label = bc_get_bytecode_label ();
4930   bc_emit_bytecode_labelref (jump_label);
4931   bc_emit_bytecode_const ((char *) &thiscase->data.case_stmt.num_ranges,
4932                           sizeof thiscase->data.case_stmt.num_ranges);
4933
4934   if (thiscase->data.case_stmt.default_label)
4935     bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (DECL_RTL (thiscase->data.case_stmt.default_label)));
4936   else
4937     bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (thiscase->exit_label));
4938
4939   /* Output the jump table.  */
4940
4941   bc_align_bytecode (3 /* PTR_ALIGN */);
4942   bc_emit_bytecode_labeldef (jump_label);
4943
4944   if (TYPE_MODE (thiscase->data.case_stmt.nominal_type) == SImode)
4945     for (c = thiscase->data.case_stmt.case_list->left; c; c = c->left)
4946       {
4947         opcode = TREE_INT_CST_LOW (c->low);
4948         bc_emit_bytecode_const ((char *) &opcode, sizeof opcode);
4949
4950         opcode = TREE_INT_CST_LOW (c->high);
4951         bc_emit_bytecode_const ((char *) &opcode, sizeof opcode);
4952
4953         bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (DECL_RTL (c->code_label)));
4954       }
4955   else
4956     if (TYPE_MODE (thiscase->data.case_stmt.nominal_type) == DImode)
4957       for (c = thiscase->data.case_stmt.case_list->left; c; c = c->left)
4958         {
4959           bc_emit_bytecode_DI_const (c->low);
4960           bc_emit_bytecode_DI_const (c->high);
4961
4962           bc_emit_bytecode_labelref (BYTECODE_BC_LABEL (DECL_RTL (c->code_label)));
4963         }
4964     else
4965       /* Bad mode */
4966       abort ();
4967
4968     
4969   bc_emit_bytecode_labeldef (BYTECODE_BC_LABEL (thiscase->exit_label));
4970
4971   /* Possibly issue enumeration warnings.  */
4972
4973   if (!thiscase->data.case_stmt.default_label
4974       && TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
4975       && TREE_CODE (expr) != INTEGER_CST
4976       && warn_switch)
4977     check_for_full_enumeration_handling (TREE_TYPE (expr));
4978
4979
4980 #ifdef DEBUG_PRINT_CODE
4981   fputc ('\n', stderr);
4982 #endif
4983
4984   POPSTACK (case_stack);
4985 }
4986
4987
4988 /* Return unique bytecode ID. */
4989
4990 int 
4991 bc_new_uid ()
4992 {
4993   static int bc_uid = 0;
4994
4995   return (++bc_uid);
4996 }
4997
4998 /* Generate code to jump to LABEL if OP1 and OP2 are equal.  */
4999
5000 static void
5001 do_jump_if_equal (op1, op2, label, unsignedp)
5002      rtx op1, op2, label;
5003      int unsignedp;
5004 {
5005   if (GET_CODE (op1) == CONST_INT
5006       && GET_CODE (op2) == CONST_INT)
5007     {
5008       if (INTVAL (op1) == INTVAL (op2))
5009         emit_jump (label);
5010     }
5011   else
5012     {
5013       enum machine_mode mode = GET_MODE (op1);
5014       if (mode == VOIDmode)
5015         mode = GET_MODE (op2);
5016       emit_cmp_insn (op1, op2, EQ, NULL_RTX, mode, unsignedp, 0);
5017       emit_jump_insn (gen_beq (label));
5018     }
5019 }
5020 \f
5021 /* Not all case values are encountered equally.  This function
5022    uses a heuristic to weight case labels, in cases where that
5023    looks like a reasonable thing to do.
5024
5025    Right now, all we try to guess is text, and we establish the
5026    following weights:
5027
5028         chars above space:      16
5029         digits:                 16
5030         default:                12
5031         space, punct:           8
5032         tab:                    4
5033         newline:                2
5034         other "\" chars:        1
5035         remaining chars:        0
5036
5037    If we find any cases in the switch that are not either -1 or in the range
5038    of valid ASCII characters, or are control characters other than those
5039    commonly used with "\", don't treat this switch scanning text.
5040
5041    Return 1 if these nodes are suitable for cost estimation, otherwise
5042    return 0.  */
5043
5044 static int
5045 estimate_case_costs (node)
5046      case_node_ptr node;
5047 {
5048   tree min_ascii = build_int_2 (-1, -1);
5049   tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5050   case_node_ptr n;
5051   int i;
5052
5053   /* If we haven't already made the cost table, make it now.  Note that the
5054      lower bound of the table is -1, not zero.  */
5055
5056   if (cost_table == NULL)
5057     {
5058       cost_table = ((short *) xmalloc (129 * sizeof (short))) + 1;
5059       bzero (cost_table - 1, 129 * sizeof (short));
5060
5061       for (i = 0; i < 128; i++)
5062         {
5063           if (isalnum (i))
5064             cost_table[i] = 16;
5065           else if (ispunct (i))
5066             cost_table[i] = 8;
5067           else if (iscntrl (i))
5068             cost_table[i] = -1;
5069         }
5070
5071       cost_table[' '] = 8;
5072       cost_table['\t'] = 4;
5073       cost_table['\0'] = 4;
5074       cost_table['\n'] = 2;
5075       cost_table['\f'] = 1;
5076       cost_table['\v'] = 1;
5077       cost_table['\b'] = 1;
5078     }
5079
5080   /* See if all the case expressions look like text.  It is text if the
5081      constant is >= -1 and the highest constant is <= 127.  Do all comparisons
5082      as signed arithmetic since we don't want to ever access cost_table with a
5083      value less than -1.  Also check that none of the constants in a range
5084      are strange control characters.  */
5085
5086   for (n = node; n; n = n->right)
5087     {
5088       if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5089         return 0;
5090
5091       for (i = TREE_INT_CST_LOW (n->low); i <= TREE_INT_CST_LOW (n->high); i++)
5092         if (cost_table[i] < 0)
5093           return 0;
5094     }
5095
5096   /* All interesting values are within the range of interesting
5097      ASCII characters.  */
5098   return 1;
5099 }
5100
5101 /* Scan an ordered list of case nodes
5102    combining those with consecutive values or ranges.
5103
5104    Eg. three separate entries 1: 2: 3: become one entry 1..3:  */
5105
5106 static void
5107 group_case_nodes (head)
5108      case_node_ptr head;
5109 {
5110   case_node_ptr node = head;
5111
5112   while (node)
5113     {
5114       rtx lb = next_real_insn (label_rtx (node->code_label));
5115       case_node_ptr np = node;
5116
5117       /* Try to group the successors of NODE with NODE.  */
5118       while (((np = np->right) != 0)
5119              /* Do they jump to the same place?  */
5120              && next_real_insn (label_rtx (np->code_label)) == lb
5121              /* Are their ranges consecutive?  */
5122              && tree_int_cst_equal (np->low,
5123                                     fold (build (PLUS_EXPR,
5124                                                  TREE_TYPE (node->high),
5125                                                  node->high,
5126                                                  integer_one_node)))
5127              /* An overflow is not consecutive.  */
5128              && tree_int_cst_lt (node->high,
5129                                  fold (build (PLUS_EXPR,
5130                                               TREE_TYPE (node->high),
5131                                               node->high,
5132                                               integer_one_node))))
5133         {
5134           node->high = np->high;
5135         }
5136       /* NP is the first node after NODE which can't be grouped with it.
5137          Delete the nodes in between, and move on to that node.  */
5138       node->right = np;
5139       node = np;
5140     }
5141 }
5142
5143 /* Take an ordered list of case nodes
5144    and transform them into a near optimal binary tree,
5145    on the assumption that any target code selection value is as
5146    likely as any other.
5147
5148    The transformation is performed by splitting the ordered
5149    list into two equal sections plus a pivot.  The parts are
5150    then attached to the pivot as left and right branches.  Each
5151    branch is is then transformed recursively.  */
5152
5153 static void
5154 balance_case_nodes (head, parent)
5155      case_node_ptr *head;
5156      case_node_ptr parent;
5157 {
5158   register case_node_ptr np;
5159
5160   np = *head;
5161   if (np)
5162     {
5163       int cost = 0;
5164       int i = 0;
5165       int ranges = 0;
5166       register case_node_ptr *npp;
5167       case_node_ptr left;
5168
5169       /* Count the number of entries on branch.  Also count the ranges.  */
5170
5171       while (np)
5172         {
5173           if (!tree_int_cst_equal (np->low, np->high))
5174             {
5175               ranges++;
5176               if (use_cost_table)
5177                 cost += cost_table[TREE_INT_CST_LOW (np->high)];
5178             }
5179
5180           if (use_cost_table)
5181             cost += cost_table[TREE_INT_CST_LOW (np->low)];
5182
5183           i++;
5184           np = np->right;
5185         }
5186
5187       if (i > 2)
5188         {
5189           /* Split this list if it is long enough for that to help.  */
5190           npp = head;
5191           left = *npp;
5192           if (use_cost_table)
5193             {
5194               /* Find the place in the list that bisects the list's total cost,
5195                  Here I gets half the total cost.  */
5196               int n_moved = 0;
5197               i = (cost + 1) / 2;
5198               while (1)
5199                 {
5200                   /* Skip nodes while their cost does not reach that amount.  */
5201                   if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5202                     i -= cost_table[TREE_INT_CST_LOW ((*npp)->high)];
5203                   i -= cost_table[TREE_INT_CST_LOW ((*npp)->low)];
5204                   if (i <= 0)
5205                     break;
5206                   npp = &(*npp)->right;
5207                   n_moved += 1;
5208                 }
5209               if (n_moved == 0)
5210                 {
5211                   /* Leave this branch lopsided, but optimize left-hand
5212                      side and fill in `parent' fields for right-hand side.  */
5213                   np = *head;
5214                   np->parent = parent;
5215                   balance_case_nodes (&np->left, np);
5216                   for (; np->right; np = np->right)
5217                     np->right->parent = np;
5218                   return;
5219                 }
5220             }
5221           /* If there are just three nodes, split at the middle one.  */
5222           else if (i == 3)
5223             npp = &(*npp)->right;
5224           else
5225             {
5226               /* Find the place in the list that bisects the list's total cost,
5227                  where ranges count as 2.
5228                  Here I gets half the total cost.  */
5229               i = (i + ranges + 1) / 2;
5230               while (1)
5231                 {
5232                   /* Skip nodes while their cost does not reach that amount.  */
5233                   if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5234                     i--;
5235                   i--;
5236                   if (i <= 0)
5237                     break;
5238                   npp = &(*npp)->right;
5239                 }
5240             }
5241           *head = np = *npp;
5242           *npp = 0;
5243           np->parent = parent;
5244           np->left = left;
5245
5246           /* Optimize each of the two split parts.  */
5247           balance_case_nodes (&np->left, np);
5248           balance_case_nodes (&np->right, np);
5249         }
5250       else
5251         {
5252           /* Else leave this branch as one level,
5253              but fill in `parent' fields.  */
5254           np = *head;
5255           np->parent = parent;
5256           for (; np->right; np = np->right)
5257             np->right->parent = np;
5258         }
5259     }
5260 }
5261 \f
5262 /* Search the parent sections of the case node tree
5263    to see if a test for the lower bound of NODE would be redundant.
5264    INDEX_TYPE is the type of the index expression.
5265
5266    The instructions to generate the case decision tree are
5267    output in the same order as nodes are processed so it is
5268    known that if a parent node checks the range of the current
5269    node minus one that the current node is bounded at its lower
5270    span.  Thus the test would be redundant.  */
5271
5272 static int
5273 node_has_low_bound (node, index_type)
5274      case_node_ptr node;
5275      tree index_type;
5276 {
5277   tree low_minus_one;
5278   case_node_ptr pnode;
5279
5280   /* If the lower bound of this node is the lowest value in the index type,
5281      we need not test it.  */
5282
5283   if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
5284     return 1;
5285
5286   /* If this node has a left branch, the value at the left must be less
5287      than that at this node, so it cannot be bounded at the bottom and
5288      we need not bother testing any further.  */
5289
5290   if (node->left)
5291     return 0;
5292
5293   low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
5294                                node->low, integer_one_node));
5295
5296   /* If the subtraction above overflowed, we can't verify anything.
5297      Otherwise, look for a parent that tests our value - 1.  */
5298
5299   if (! tree_int_cst_lt (low_minus_one, node->low))
5300     return 0;
5301
5302   for (pnode = node->parent; pnode; pnode = pnode->parent)
5303     if (tree_int_cst_equal (low_minus_one, pnode->high))
5304       return 1;
5305
5306   return 0;
5307 }
5308
5309 /* Search the parent sections of the case node tree
5310    to see if a test for the upper bound of NODE would be redundant.
5311    INDEX_TYPE is the type of the index expression.
5312
5313    The instructions to generate the case decision tree are
5314    output in the same order as nodes are processed so it is
5315    known that if a parent node checks the range of the current
5316    node plus one that the current node is bounded at its upper
5317    span.  Thus the test would be redundant.  */
5318
5319 static int
5320 node_has_high_bound (node, index_type)
5321      case_node_ptr node;
5322      tree index_type;
5323 {
5324   tree high_plus_one;
5325   case_node_ptr pnode;
5326
5327   /* If the upper bound of this node is the highest value in the type
5328      of the index expression, we need not test against it.  */
5329
5330   if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
5331     return 1;
5332
5333   /* If this node has a right branch, the value at the right must be greater
5334      than that at this node, so it cannot be bounded at the top and
5335      we need not bother testing any further.  */
5336
5337   if (node->right)
5338     return 0;
5339
5340   high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
5341                                node->high, integer_one_node));
5342
5343   /* If the addition above overflowed, we can't verify anything.
5344      Otherwise, look for a parent that tests our value + 1.  */
5345
5346   if (! tree_int_cst_lt (node->high, high_plus_one))
5347     return 0;
5348
5349   for (pnode = node->parent; pnode; pnode = pnode->parent)
5350     if (tree_int_cst_equal (high_plus_one, pnode->low))
5351       return 1;
5352
5353   return 0;
5354 }
5355
5356 /* Search the parent sections of the
5357    case node tree to see if both tests for the upper and lower
5358    bounds of NODE would be redundant.  */
5359
5360 static int
5361 node_is_bounded (node, index_type)
5362      case_node_ptr node;
5363      tree index_type;
5364 {
5365   return (node_has_low_bound (node, index_type)
5366           && node_has_high_bound (node, index_type));
5367 }
5368
5369 /*  Emit an unconditional jump to LABEL unless it would be dead code.  */
5370
5371 static void
5372 emit_jump_if_reachable (label)
5373      rtx label;
5374 {
5375   if (GET_CODE (get_last_insn ()) != BARRIER)
5376     emit_jump (label);
5377 }
5378 \f
5379 /* Emit step-by-step code to select a case for the value of INDEX.
5380    The thus generated decision tree follows the form of the
5381    case-node binary tree NODE, whose nodes represent test conditions.
5382    INDEX_TYPE is the type of the index of the switch.
5383
5384    Care is taken to prune redundant tests from the decision tree
5385    by detecting any boundary conditions already checked by
5386    emitted rtx.  (See node_has_high_bound, node_has_low_bound
5387    and node_is_bounded, above.)
5388
5389    Where the test conditions can be shown to be redundant we emit
5390    an unconditional jump to the target code.  As a further
5391    optimization, the subordinates of a tree node are examined to
5392    check for bounded nodes.  In this case conditional and/or
5393    unconditional jumps as a result of the boundary check for the
5394    current node are arranged to target the subordinates associated
5395    code for out of bound conditions on the current node node.
5396
5397    We can assume that when control reaches the code generated here,
5398    the index value has already been compared with the parents
5399    of this node, and determined to be on the same side of each parent
5400    as this node is.  Thus, if this node tests for the value 51,
5401    and a parent tested for 52, we don't need to consider
5402    the possibility of a value greater than 51.  If another parent
5403    tests for the value 50, then this node need not test anything.  */
5404
5405 static void
5406 emit_case_nodes (index, node, default_label, index_type)
5407      rtx index;
5408      case_node_ptr node;
5409      rtx default_label;
5410      tree index_type;
5411 {
5412   /* If INDEX has an unsigned type, we must make unsigned branches.  */
5413   int unsignedp = TREE_UNSIGNED (index_type);
5414   typedef rtx rtx_function ();
5415   rtx_function *gen_bgt_pat = unsignedp ? gen_bgtu : gen_bgt;
5416   rtx_function *gen_bge_pat = unsignedp ? gen_bgeu : gen_bge;
5417   rtx_function *gen_blt_pat = unsignedp ? gen_bltu : gen_blt;
5418   rtx_function *gen_ble_pat = unsignedp ? gen_bleu : gen_ble;
5419   enum machine_mode mode = GET_MODE (index);
5420
5421   /* See if our parents have already tested everything for us.
5422      If they have, emit an unconditional jump for this node.  */
5423   if (node_is_bounded (node, index_type))
5424     emit_jump (label_rtx (node->code_label));
5425
5426   else if (tree_int_cst_equal (node->low, node->high))
5427     {
5428       /* Node is single valued.  First see if the index expression matches
5429          this node and then check our children, if any. */
5430
5431       do_jump_if_equal (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
5432                         label_rtx (node->code_label), unsignedp);
5433
5434       if (node->right != 0 && node->left != 0)
5435         {
5436           /* This node has children on both sides.
5437              Dispatch to one side or the other
5438              by comparing the index value with this node's value.
5439              If one subtree is bounded, check that one first,
5440              so we can avoid real branches in the tree.  */
5441
5442           if (node_is_bounded (node->right, index_type))
5443             {
5444               emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5445                                                  VOIDmode, 0),
5446                              GT, NULL_RTX, mode, unsignedp, 0);
5447
5448               emit_jump_insn ((*gen_bgt_pat) (label_rtx (node->right->code_label)));
5449               emit_case_nodes (index, node->left, default_label, index_type);
5450             }
5451
5452           else if (node_is_bounded (node->left, index_type))
5453             {
5454               emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5455                                                  VOIDmode, 0),
5456                              LT, NULL_RTX, mode, unsignedp, 0);
5457               emit_jump_insn ((*gen_blt_pat) (label_rtx (node->left->code_label)));
5458               emit_case_nodes (index, node->right, default_label, index_type);
5459             }
5460
5461           else
5462             {
5463               /* Neither node is bounded.  First distinguish the two sides;
5464                  then emit the code for one side at a time.  */
5465
5466               tree test_label
5467                 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5468
5469               /* See if the value is on the right.  */
5470               emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5471                                                  VOIDmode, 0),
5472                              GT, NULL_RTX, mode, unsignedp, 0);
5473               emit_jump_insn ((*gen_bgt_pat) (label_rtx (test_label)));
5474
5475               /* Value must be on the left.
5476                  Handle the left-hand subtree.  */
5477               emit_case_nodes (index, node->left, default_label, index_type);
5478               /* If left-hand subtree does nothing,
5479                  go to default.  */
5480               emit_jump_if_reachable (default_label);
5481
5482               /* Code branches here for the right-hand subtree.  */
5483               expand_label (test_label);
5484               emit_case_nodes (index, node->right, default_label, index_type);
5485             }
5486         }
5487
5488       else if (node->right != 0 && node->left == 0)
5489         {
5490           /* Here we have a right child but no left so we issue conditional
5491              branch to default and process the right child.
5492
5493              Omit the conditional branch to default if we it avoid only one
5494              right child; it costs too much space to save so little time.  */
5495
5496           if (node->right->right || node->right->left
5497               || !tree_int_cst_equal (node->right->low, node->right->high))
5498             {
5499               if (!node_has_low_bound (node, index_type))
5500                 {
5501                   emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5502                                                      VOIDmode, 0),
5503                                  LT, NULL_RTX, mode, unsignedp, 0);
5504                   emit_jump_insn ((*gen_blt_pat) (default_label));
5505                 }
5506
5507               emit_case_nodes (index, node->right, default_label, index_type);
5508             }
5509           else
5510             /* We cannot process node->right normally
5511                since we haven't ruled out the numbers less than
5512                this node's value.  So handle node->right explicitly.  */
5513             do_jump_if_equal (index,
5514                               expand_expr (node->right->low, NULL_RTX,
5515                                            VOIDmode, 0),
5516                               label_rtx (node->right->code_label), unsignedp);
5517         }
5518
5519       else if (node->right == 0 && node->left != 0)
5520         {
5521           /* Just one subtree, on the left.  */
5522
5523 #if 0 /* The following code and comment were formerly part
5524          of the condition here, but they didn't work
5525          and I don't understand what the idea was.  -- rms.  */
5526           /* If our "most probable entry" is less probable
5527              than the default label, emit a jump to
5528              the default label using condition codes
5529              already lying around.  With no right branch,
5530              a branch-greater-than will get us to the default
5531              label correctly.  */
5532           if (use_cost_table
5533                && cost_table[TREE_INT_CST_LOW (node->high)] < 12)
5534             ;
5535 #endif /* 0 */
5536           if (node->left->left || node->left->right
5537               || !tree_int_cst_equal (node->left->low, node->left->high))
5538             {
5539               if (!node_has_high_bound (node, index_type))
5540                 {
5541                   emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5542                                                      VOIDmode, 0),
5543                                  GT, NULL_RTX, mode, unsignedp, 0);
5544                   emit_jump_insn ((*gen_bgt_pat) (default_label));
5545                 }
5546
5547               emit_case_nodes (index, node->left, default_label, index_type);
5548             }
5549           else
5550             /* We cannot process node->left normally
5551                since we haven't ruled out the numbers less than
5552                this node's value.  So handle node->left explicitly.  */
5553             do_jump_if_equal (index,
5554                               expand_expr (node->left->low, NULL_RTX,
5555                                            VOIDmode, 0),
5556                               label_rtx (node->left->code_label), unsignedp);
5557         }
5558     }
5559   else
5560     {
5561       /* Node is a range.  These cases are very similar to those for a single
5562          value, except that we do not start by testing whether this node
5563          is the one to branch to.  */
5564
5565       if (node->right != 0 && node->left != 0)
5566         {
5567           /* Node has subtrees on both sides.
5568              If the right-hand subtree is bounded,
5569              test for it first, since we can go straight there.
5570              Otherwise, we need to make a branch in the control structure,
5571              then handle the two subtrees.  */
5572           tree test_label = 0;
5573
5574           emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5575                                              VOIDmode, 0),
5576                          GT, NULL_RTX, mode, unsignedp, 0);
5577
5578           if (node_is_bounded (node->right, index_type))
5579             /* Right hand node is fully bounded so we can eliminate any
5580                testing and branch directly to the target code.  */
5581             emit_jump_insn ((*gen_bgt_pat) (label_rtx (node->right->code_label)));
5582           else
5583             {
5584               /* Right hand node requires testing.
5585                  Branch to a label where we will handle it later.  */
5586
5587               test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5588               emit_jump_insn ((*gen_bgt_pat) (label_rtx (test_label)));
5589             }
5590
5591           /* Value belongs to this node or to the left-hand subtree.  */
5592
5593           emit_cmp_insn (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
5594                          GE, NULL_RTX, mode, unsignedp, 0);
5595           emit_jump_insn ((*gen_bge_pat) (label_rtx (node->code_label)));
5596
5597           /* Handle the left-hand subtree.  */
5598           emit_case_nodes (index, node->left, default_label, index_type);
5599
5600           /* If right node had to be handled later, do that now.  */
5601
5602           if (test_label)
5603             {
5604               /* If the left-hand subtree fell through,
5605                  don't let it fall into the right-hand subtree.  */
5606               emit_jump_if_reachable (default_label);
5607
5608               expand_label (test_label);
5609               emit_case_nodes (index, node->right, default_label, index_type);
5610             }
5611         }
5612
5613       else if (node->right != 0 && node->left == 0)
5614         {
5615           /* Deal with values to the left of this node,
5616              if they are possible.  */
5617           if (!node_has_low_bound (node, index_type))
5618             {
5619               emit_cmp_insn (index, expand_expr (node->low, NULL_RTX,
5620                                                  VOIDmode, 0),
5621                              LT, NULL_RTX, mode, unsignedp, 0);
5622               emit_jump_insn ((*gen_blt_pat) (default_label));
5623             }
5624
5625           /* Value belongs to this node or to the right-hand subtree.  */
5626
5627           emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5628                                              VOIDmode, 0),
5629                          LE, NULL_RTX, mode, unsignedp, 0);
5630           emit_jump_insn ((*gen_ble_pat) (label_rtx (node->code_label)));
5631
5632           emit_case_nodes (index, node->right, default_label, index_type);
5633         }
5634
5635       else if (node->right == 0 && node->left != 0)
5636         {
5637           /* Deal with values to the right of this node,
5638              if they are possible.  */
5639           if (!node_has_high_bound (node, index_type))
5640             {
5641               emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5642                                                  VOIDmode, 0),
5643                              GT, NULL_RTX, mode, unsignedp, 0);
5644               emit_jump_insn ((*gen_bgt_pat) (default_label));
5645             }
5646
5647           /* Value belongs to this node or to the left-hand subtree.  */
5648
5649           emit_cmp_insn (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
5650                          GE, NULL_RTX, mode, unsignedp, 0);
5651           emit_jump_insn ((*gen_bge_pat) (label_rtx (node->code_label)));
5652
5653           emit_case_nodes (index, node->left, default_label, index_type);
5654         }
5655
5656       else
5657         {
5658           /* Node has no children so we check low and high bounds to remove
5659              redundant tests.  Only one of the bounds can exist,
5660              since otherwise this node is bounded--a case tested already.  */
5661
5662           if (!node_has_high_bound (node, index_type))
5663             {
5664               emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
5665                                                  VOIDmode, 0),
5666                              GT, NULL_RTX, mode, unsignedp, 0);
5667               emit_jump_insn ((*gen_bgt_pat) (default_label));
5668             }
5669
5670           if (!node_has_low_bound (node, index_type))
5671             {
5672               emit_cmp_insn (index, expand_expr (node->low, NULL_RTX,
5673                                                  VOIDmode, 0),
5674                              LT, NULL_RTX, mode, unsignedp, 0);
5675               emit_jump_insn ((*gen_blt_pat) (default_label));
5676             }
5677
5678           emit_jump (label_rtx (node->code_label));
5679         }
5680     }
5681 }
5682 \f
5683 /* These routines are used by the loop unrolling code.  They copy BLOCK trees
5684    so that the debugging info will be correct for the unrolled loop.  */
5685
5686 /* Indexed by block number, contains a pointer to the N'th block node.  */
5687
5688 static tree *block_vector;
5689
5690 void
5691 find_loop_tree_blocks ()
5692 {
5693   tree block = DECL_INITIAL (current_function_decl);
5694
5695   /* There first block is for the function body, and does not have
5696      corresponding block notes.  Don't include it in the block vector.  */
5697   block = BLOCK_SUBBLOCKS (block);
5698
5699   block_vector = identify_blocks (block, get_insns ());
5700 }
5701
5702 void
5703 unroll_block_trees ()
5704 {
5705   tree block = DECL_INITIAL (current_function_decl);
5706
5707   reorder_blocks (block_vector, block, get_insns ());
5708 }
5709