OSDN Git Service

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