OSDN Git Service

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