OSDN Git Service

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