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