OSDN Git Service

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