]> gcc.gnu.org Git - gcc.git/blame - gcc/stmt.c
Makefile.in, [...]: replace "GNU CC" with "GCC".
[gcc.git] / gcc / stmt.c
CommitLineData
28d81abb 1/* Expands front end tree to back end RTL for GNU C-Compiler
4559fd9e 2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
69107307 3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
28d81abb 4
1322177d 5This file is part of GCC.
28d81abb 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
28d81abb 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
28d81abb
RK
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
28d81abb 21
28d81abb
RK
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"
670ee920 37#include "system.h"
ccd043a9 38
28d81abb
RK
39#include "rtl.h"
40#include "tree.h"
6baf1cc8 41#include "tm_p.h"
28d81abb 42#include "flags.h"
6adb4e3a 43#include "except.h"
28d81abb 44#include "function.h"
28d81abb 45#include "insn-config.h"
28d81abb 46#include "expr.h"
e78d8e51 47#include "libfuncs.h"
28d81abb
RK
48#include "hard-reg-set.h"
49#include "obstack.h"
50#include "loop.h"
51#include "recog.h"
ca695ac9 52#include "machmode.h"
10f0ad3d 53#include "toplev.h"
d6f4ec51 54#include "output.h"
87ff9c8e 55#include "ggc.h"
ca695ac9 56
28d81abb
RK
57#define obstack_chunk_alloc xmalloc
58#define obstack_chunk_free free
59struct obstack stmt_obstack;
60
18543a22
ILT
61/* Assume that case vectors are not pc-relative. */
62#ifndef CASE_VECTOR_PC_RELATIVE
63#define CASE_VECTOR_PC_RELATIVE 0
64#endif
28d81abb
RK
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
5720c7e7
RK
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.
28d81abb
RK
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
de14fd73
RK
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
28d81abb
RK
87 and nodes on the right having higher values. We then output the tree
88 in order. */
89
90struct 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 */
57641239 98 int balance;
28d81abb
RK
99};
100
101typedef struct case_node case_node;
102typedef 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. */
e7749837 107static short cost_table_[129];
28d81abb 108static int use_cost_table;
2a2137c4
RH
109static 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)]
28d81abb
RK
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
140struct 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.
0f41302f 156 This may be the end of the if or the next else/elseif. */
28d81abb
RK
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;
8afad312
JW
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;
28d81abb
RK
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;
b93a436e 179 /* Nonzero => value to restore stack to on exit. */
28d81abb
RK
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.
e976b8b2 196 The tail of this list can be 0,
28d81abb
RK
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. */
3f1d071b 205 int n_function_calls;
e976b8b2
MS
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. */
3f1d071b 213 int block_target_temp_slot_level;
e976b8b2
MS
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;
28d81abb
RK
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;
57641239
RK
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. */
28d81abb
RK
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;
28d81abb 246 /* Name of this kind of statement, for warnings. */
dff01034 247 const char *printname;
a11759a3
JR
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;
28d81abb 252 } case_stmt;
28d81abb
RK
253 } data;
254};
255
28d81abb
RK
256/* Allocate and return a new `struct nesting'. */
257
258#define ALLOC_NESTING() \
259 (struct nesting *) obstack_alloc (&stmt_obstack, sizeof (struct nesting))
260
6ed1d6c5
RS
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. */
28d81abb
RK
265
266#define POPSTACK(STACK) \
6ed1d6c5
RS
267do { 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; \
6ed1d6c5 280 nesting_depth = nesting_stack->depth - 1; \
28d81abb 281 nesting_stack = this->all; \
28d81abb 282 obstack_free (&stmt_obstack, this); } \
6ed1d6c5 283 while (this != target); } while (0)
28d81abb
RK
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
293struct 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;
7629c936
RS
303 /* The BLOCK for the place where this goto was found. */
304 tree context;
28d81abb
RK
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.
e976b8b2 316 The tail of this list can be 0,
28d81abb
RK
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
28d81abb
RK
324/* Within any binding contour that must restore a stack level,
325 all labels are recorded with a chain of these structures. */
326
327struct label_chain
328{
329 /* Points to following fixup. */
330 struct label_chain *next;
331 tree label;
332};
e9a25f70 333
3f1d071b
BS
334struct 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. */
47ee9bcb 375 const char *x_emit_filename;
3f1d071b
BS
376 int x_emit_lineno;
377
378 struct goto_fixup *x_goto_fixup_chain;
379};
380
01d939e8
BS
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)
e9a25f70
JL
395
396/* Non-zero if we are using EH to handle cleanus. */
397static int using_eh_for_cleanups_p = 0;
398
cdadb1dd
KG
399static int n_occurrences PARAMS ((int, const char *));
400static void expand_goto_internal PARAMS ((tree, rtx, rtx));
401static int expand_fixup PARAMS ((tree, rtx, rtx));
402static rtx expand_nl_handler_label PARAMS ((rtx, rtx));
403static void expand_nl_goto_receiver PARAMS ((void));
404static void expand_nl_goto_receivers PARAMS ((struct nesting *));
405static void fixup_gotos PARAMS ((struct nesting *, rtx, tree,
cfc3d13f 406 rtx, int));
396ad517 407static void expand_null_return_1 PARAMS ((rtx));
cdadb1dd
KG
408static void expand_value_return PARAMS ((rtx));
409static int tail_recursion_args PARAMS ((tree, tree));
410static void expand_cleanups PARAMS ((tree, tree, int, int));
411static void check_seenlabel PARAMS ((void));
412static void do_jump_if_equal PARAMS ((rtx, rtx, rtx, int));
413static int estimate_case_costs PARAMS ((case_node_ptr));
414static void group_case_nodes PARAMS ((case_node_ptr));
415static void balance_case_nodes PARAMS ((case_node_ptr *,
cfc3d13f 416 case_node_ptr));
cdadb1dd
KG
417static int node_has_low_bound PARAMS ((case_node_ptr, tree));
418static int node_has_high_bound PARAMS ((case_node_ptr, tree));
419static int node_is_bounded PARAMS ((case_node_ptr, tree));
420static void emit_jump_if_reachable PARAMS ((rtx));
421static void emit_case_nodes PARAMS ((rtx, case_node_ptr, rtx, tree));
cdadb1dd
KG
422static struct case_node *case_tree2list PARAMS ((case_node *, case_node *));
423static void mark_cond_nesting PARAMS ((struct nesting *));
424static void mark_loop_nesting PARAMS ((struct nesting *));
425static void mark_block_nesting PARAMS ((struct nesting *));
426static void mark_case_nesting PARAMS ((struct nesting *));
0876ac08 427static void mark_case_node PARAMS ((struct case_node *));
cdadb1dd 428static void mark_goto_fixup PARAMS ((struct goto_fixup *));
1f8f4a0b 429static void free_case_nodes PARAMS ((case_node_ptr));
28d81abb 430\f
e9a25f70
JL
431void
432using_eh_for_cleanups ()
433{
434 using_eh_for_cleanups_p = 1;
435}
436
87ff9c8e
RH
437/* Mark N (known to be a cond-nesting) for GC. */
438
439static void
440mark_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
455static void
456mark_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
474static void
475mark_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
1f8f4a0b
MM
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 }
87ff9c8e
RH
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
504static void
505mark_case_nesting (n)
506 struct nesting *n;
507{
508 while (n)
509 {
87ff9c8e
RH
510 ggc_mark_rtx (n->exit_label);
511 ggc_mark_rtx (n->data.case_stmt.start);
512
87ff9c8e
RH
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
0876ac08 517 mark_case_node (n->data.case_stmt.case_list);
87ff9c8e
RH
518 n = n->next;
519 }
520}
521
0876ac08
RK
522/* Mark C for GC. */
523
524static void
525mark_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
87ff9c8e
RH
539/* Mark G for GC. */
540
541static void
542mark_goto_fixup (g)
543 struct goto_fixup *g;
544{
545 while (g)
546 {
bedda2da 547 ggc_mark (g);
87ff9c8e
RH
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
21cd906e
MM
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
0a8a198c 561 reclaim the memory. */
21cd906e
MM
562
563void
0a8a198c 564free_stmt_status (f)
21cd906e 565 struct function *f;
21cd906e
MM
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. */
5faf03ae
MM
570 if (f->stmt)
571 free (f->stmt);
fa51b01b 572 f->stmt = NULL;
21cd906e
MM
573}
574
87ff9c8e
RH
575/* Mark P for GC. */
576
577void
fa51b01b 578mark_stmt_status (p)
87ff9c8e
RH
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
28d81abb
RK
597void
598init_stmt ()
599{
600 gcc_obstack_init (&stmt_obstack);
28d81abb
RK
601}
602
603void
604init_stmt_for_function ()
605{
01d939e8 606 cfun->stmt = (struct stmt_status *) xmalloc (sizeof (struct stmt_status));
3f1d071b 607
28d81abb
RK
608 /* We are not currently within any block, conditional, loop or case. */
609 block_stack = 0;
0b931590 610 stack_block_stack = 0;
28d81abb
RK
611 loop_stack = 0;
612 case_stack = 0;
613 cond_stack = 0;
614 nesting_stack = 0;
615 nesting_depth = 0;
616
3f1d071b 617 current_block_start_count = 0;
28d81abb
RK
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;
21cd906e 625 last_expr_value = NULL_RTX;
28d81abb 626}
3f1d071b
BS
627\f
628/* Return nonzero if anything is pushed on the loop, condition, or case
629 stack. */
630int
631in_control_zone_p ()
632{
633 return cond_stack || loop_stack || case_stack;
28d81abb
RK
634}
635
3f1d071b 636/* Record the current file and line. Called from emit_line_note. */
28d81abb 637void
3f1d071b 638set_file_and_line_for_stmt (file, line)
47ee9bcb 639 const char *file;
3f1d071b
BS
640 int line;
641{
61d84605
MM
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 }
28d81abb 650}
3f1d071b 651
28d81abb
RK
652/* Emit a no-op instruction. */
653
654void
655emit_nop ()
656{
ca695ac9
JB
657 rtx last_insn;
658
b93a436e
JL
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 ());
28d81abb
RK
665}
666\f
667/* Return the rtx-label that corresponds to a LABEL_DECL,
668 creating it if necessary. */
669
670rtx
671label_rtx (label)
672 tree label;
673{
674 if (TREE_CODE (label) != LABEL_DECL)
675 abort ();
676
19e7881c
MM
677 if (!DECL_RTL_SET_P (label))
678 SET_DECL_RTL (label, gen_label_rtx ());
28d81abb 679
19e7881c 680 return DECL_RTL (label);
28d81abb
RK
681}
682
19e7881c 683
28d81abb
RK
684/* Add an unconditional jump to LABEL as the next sequential instruction. */
685
686void
687emit_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
698void
699expand_computed_goto (exp)
700 tree exp;
701{
b93a436e 702 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
ed9a9db1
RK
703
704#ifdef POINTERS_EXTEND_UNSIGNED
b93a436e 705 x = convert_memory_address (Pmode, x);
ed9a9db1 706#endif
ffa1a1ce 707
b93a436e
JL
708 emit_queue ();
709 /* Be sure the function is executable. */
7d384cc0 710 if (current_function_check_memory_usage)
ebb1b59a 711 emit_library_call (chkr_check_exec_libfunc, LCT_CONST_MAKE_BLOCK,
b93a436e 712 VOIDmode, 1, x, ptr_mode);
17f5f329 713
b93a436e
JL
714 do_pending_stack_adjust ();
715 emit_indirect_jump (x);
acd693d1
RH
716
717 current_function_has_computed_jump = 1;
28d81abb
RK
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
733void
734expand_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 {
1f8f4a0b 746 p = (struct label_chain *) ggc_alloc (sizeof (struct label_chain));
28d81abb
RK
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
756void
757declare_nonlocal_label (label)
758 tree label;
759{
ba716ac9
BS
760 rtx slot = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
761
28d81abb
RK
762 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
763 LABEL_PRESERVE_P (label_rtx (label)) = 1;
ba716ac9 764 if (nonlocal_goto_handler_slots == 0)
28d81abb 765 {
59257ff7
RK
766 emit_stack_save (SAVE_NONLOCAL,
767 &nonlocal_goto_stack_level,
768 PREV_INSN (tail_recursion_reentry));
28d81abb 769 }
ba716ac9
BS
770 nonlocal_goto_handler_slots
771 = gen_rtx_EXPR_LIST (VOIDmode, slot, nonlocal_goto_handler_slots);
28d81abb
RK
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
778void
779expand_goto (label)
780 tree label;
781{
ca695ac9
JB
782 tree context;
783
28d81abb 784 /* Check for a nonlocal goto to a containing function. */
ca695ac9 785 context = decl_function_context (label);
28d81abb
RK
786 if (context != 0 && context != current_function_decl)
787 {
788 struct function *p = find_function_data (context);
38a448ca 789 rtx label_ref = gen_rtx_LABEL_REF (Pmode, label_rtx (label));
4b01bd16 790 rtx handler_slot, static_chain, save_area, insn;
ba716ac9
BS
791 tree link;
792
793 /* Find the corresponding handler slot for this label. */
49ad7cfa
BS
794 handler_slot = p->x_nonlocal_goto_handler_slots;
795 for (link = p->x_nonlocal_labels; TREE_VALUE (link) != label;
ba716ac9
BS
796 link = TREE_CHAIN (link))
797 handler_slot = XEXP (handler_slot, 1);
798 handler_slot = XEXP (handler_slot, 0);
dd132134 799
28d81abb 800 p->has_nonlocal_label = 1;
c1255328 801 current_function_has_nonlocal_goto = 1;
dd132134 802 LABEL_REF_NONLOCAL_P (label_ref) = 1;
59257ff7
RK
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
157229c3
RH
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
28d81abb
RK
822#if HAVE_nonlocal_goto
823 if (HAVE_nonlocal_goto)
157229c3
RH
824 emit_insn (gen_nonlocal_goto (static_chain, handler_slot,
825 save_area, label_ref));
28d81abb
RK
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. */
157229c3
RH
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. */
38a448ca
RH
839 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
840 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
157229c3 841 emit_indirect_jump (handler_slot);
28d81abb 842 }
4b01bd16
RH
843
844 /* Search backwards to the jump insn and mark it as a
845 non-local goto. */
7c2b017c
AM
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 }
4381f7c2 857 }
28d81abb 858 else
37366632 859 expand_goto_internal (label, label_rtx (label), NULL_RTX);
28d81abb
RK
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
867static void
868expand_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 {
50d1b7a1 897 expand_cleanups (block->data.block.cleanups, NULL_TREE, 1, 1);
28d81abb
RK
898 do_pending_stack_adjust ();
899 }
900 }
901
902 if (stack_level)
903 {
0f41302f
MS
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. */
28d81abb
RK
907 clear_pending_stack_adjust ();
908 do_pending_stack_adjust ();
7393c642
RK
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
c4a6c0f3
RK
913 && (((TREE_CODE (TREE_TYPE (current_function_decl))
914 == FUNCTION_TYPE)
915 && (TYPE_RETURNS_STACK_DEPRESSED
916 (TREE_TYPE (current_function_decl))))))
7393c642
RK
917 ;
918 else
919 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
28d81abb
RK
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
023b57e6
RS
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.
28d81abb
RK
951
952 Value is nonzero if a fixup is made. */
953
954static int
955expand_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
f8a83ee3 1018 = (struct goto_fixup *) ggc_alloc (sizeof (struct goto_fixup));
28d81abb
RK
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 ();
28d81abb
RK
1027 fixup->target = tree_label;
1028 fixup->target_rtl = rtl_label;
023b57e6
RS
1029
1030 /* Create a BLOCK node and a corresponding matched set of
12f61228 1031 NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes at
023b57e6
RS
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
0679e3fc
JM
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. */
023b57e6
RS
1041
1042 {
1043 register rtx original_before_jump
1044 = last_insn ? last_insn : get_last_insn ();
0679e3fc 1045 rtx start;
a97901e6 1046 rtx end;
e6fd097e
MM
1047 tree block;
1048
1049 block = make_node (BLOCK);
1050 TREE_USED (block) = 1;
1051
01d939e8 1052 if (!cfun->x_whole_function_mode_p)
a97901e6
MM
1053 insert_block (block);
1054 else
e6fd097e 1055 {
4381f7c2 1056 BLOCK_CHAIN (block)
a97901e6
MM
1057 = BLOCK_CHAIN (DECL_INITIAL (current_function_decl));
1058 BLOCK_CHAIN (DECL_INITIAL (current_function_decl))
1059 = block;
e6fd097e 1060 }
023b57e6
RS
1061
1062 start_sequence ();
6496a589 1063 start = emit_note (NULL, NOTE_INSN_BLOCK_BEG);
01d939e8 1064 if (cfun->x_whole_function_mode_p)
a97901e6 1065 NOTE_BLOCK (start) = block;
6496a589
KG
1066 fixup->before_jump = emit_note (NULL, NOTE_INSN_DELETED);
1067 end = emit_note (NULL, NOTE_INSN_BLOCK_END);
01d939e8 1068 if (cfun->x_whole_function_mode_p)
a97901e6 1069 NOTE_BLOCK (end) = block;
e6fd097e 1070 fixup->context = block;
023b57e6 1071 end_sequence ();
0679e3fc 1072 emit_insns_after (start, original_before_jump);
023b57e6
RS
1073 }
1074
3f1d071b 1075 fixup->block_start_count = current_block_start_count;
28d81abb
RK
1076 fixup->stack_level = 0;
1077 fixup->cleanup_list_list
e976b8b2 1078 = ((block->data.block.outer_cleanups
28d81abb 1079 || block->data.block.cleanups)
37366632 1080 ? tree_cons (NULL_TREE, block->data.block.cleanups,
28d81abb
RK
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}
cfc3d13f
RK
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. */
ca695ac9 1092
cfc3d13f
RK
1093void
1094expand_fixups (first_insn)
1095 rtx first_insn;
1096{
9714cf43 1097 fixup_gotos (NULL, NULL_RTX, NULL_TREE, first_insn, 0);
cfc3d13f 1098}
ca695ac9 1099
28d81abb
RK
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
704f4dca 1113static void
28d81abb
RK
1114fixup_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 {
7629c936 1141 register rtx cleanup_insns;
7629c936 1142
28d81abb 1143 /* If this fixup jumped into this contour from before the beginning
14a774a9
RK
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. */
28d81abb
RK
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)
14a774a9 1154 && INSN_UID (first_insn) < INSN_UID (f->target_rtl)
28d81abb 1155 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
33bc3ff5 1156 && ! DECL_ERROR_ISSUED (f->target))
28d81abb
RK
1157 {
1158 error_with_decl (f->target,
1159 "label `%s' used before containing binding contour");
1160 /* Prevent multiple errors for one label. */
33bc3ff5 1161 DECL_ERROR_ISSUED (f->target) = 1;
28d81abb
RK
1162 }
1163
7629c936
RS
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
023b57e6
RS
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
7629c936 1174 pushlevel (0);
023b57e6 1175 set_block (f->context);
7629c936
RS
1176
1177 /* Expand the cleanups for blocks this jump exits. */
28d81abb
RK
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)
7629c936 1186 {
50d1b7a1 1187 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
7629c936
RS
1188 /* Pop any pushes done in the cleanups,
1189 in case function is about to return. */
1190 do_pending_stack_adjust ();
1191 }
28d81abb
RK
1192 }
1193
1194 /* Restore stack level for the biggest contour that this
1195 jump jumps out of. */
7393c642
RK
1196 if (f->stack_level
1197 && ! (f->target_rtl == return_label
c4a6c0f3
RK
1198 && ((TREE_CODE (TREE_TYPE (current_function_decl))
1199 == FUNCTION_TYPE)
4381f7c2 1200 && (TYPE_RETURNS_STACK_DEPRESSED
c4a6c0f3 1201 (TREE_TYPE (current_function_decl))))))
59257ff7 1202 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
7629c936
RS
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 ();
023b57e6 1214 poplevel (1, 0, 0);
7629c936
RS
1215
1216 end_sequence ();
1217 emit_insns_after (cleanup_insns, f->before_jump);
1218
28d81abb
RK
1219 f->before_jump = 0;
1220 }
1221 }
1222
6bc2f582
RK
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
0f41302f 1225 of scope when the block ends. */
28d81abb
RK
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
6bc2f582 1232 when the fixup is later finalized. */
28d81abb 1233 && thisblock != 0
6bc2f582
RK
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. */
28d81abb
RK
1236 && (thisblock->data.block.block_start_count
1237 <= f->block_start_count))
1238 {
1239 tree lists = f->cleanup_list_list;
6bc2f582
RK
1240 rtx cleanup_insns;
1241
28d81abb
RK
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)
6bc2f582
RK
1246 {
1247 start_sequence ();
1248 pushlevel (0);
1249 set_block (f->context);
1250 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
f0959e58 1251 do_pending_stack_adjust ();
6bc2f582
RK
1252 cleanup_insns = get_insns ();
1253 poplevel (1, 0, 0);
1254 end_sequence ();
412c00dc
RK
1255 if (cleanup_insns != 0)
1256 f->before_jump
1257 = emit_insns_after (cleanup_insns, f->before_jump);
6bc2f582 1258
e07ed33f 1259 f->cleanup_list_list = TREE_CHAIN (lists);
6bc2f582 1260 }
28d81abb
RK
1261
1262 if (stack_level)
1263 f->stack_level = stack_level;
1264 }
1265}
2a230e9d
BS
1266\f
1267/* Return the number of times character C occurs in string S. */
1268static int
1269n_occurrences (c, s)
1270 int c;
dff01034 1271 const char *s;
2a230e9d
BS
1272{
1273 int n = 0;
1274 while (*s)
1275 n += (*s++ == c);
1276 return n;
1277}
28d81abb
RK
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
1283void
1284expand_asm (body)
1285 tree body;
1286{
7d384cc0 1287 if (current_function_check_memory_usage)
17f5f329 1288 {
c5c76735 1289 error ("`asm' cannot be used in function where memory usage is checked");
17f5f329
RK
1290 return;
1291 }
1292
28d81abb
RK
1293 if (TREE_CODE (body) == ADDR_EXPR)
1294 body = TREE_OPERAND (body, 0);
1295
38a448ca
RH
1296 emit_insn (gen_rtx_ASM_INPUT (VOIDmode,
1297 TREE_STRING_POINTER (body)));
28d81abb
RK
1298 last_expr_type = 0;
1299}
1300
40b18c0a
MM
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
1314bool
1315parse_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
28d81abb
RK
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
1474void
1475expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
1476 tree string, outputs, inputs, clobbers;
1477 int vol;
3b304f5b 1478 const char *filename;
28d81abb
RK
1479 int line;
1480{
1481 rtvec argvec, constraints;
1482 rtx body;
1483 int ninputs = list_length (inputs);
1484 int noutputs = list_length (outputs);
235c5021 1485 int ninout = 0;
b4ccaa16 1486 int nclobbers;
28d81abb
RK
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));
235c5021 1491 int *inout_opnum = (int *) alloca (noutputs * sizeof (int));
947255ed 1492 rtx *real_output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
235c5021
RK
1493 enum machine_mode *inout_mode
1494 = (enum machine_mode *) alloca (noutputs * sizeof (enum machine_mode));
2e2ed7d2
BS
1495 const char **output_constraints
1496 = alloca (noutputs * sizeof (const char *));
28d81abb
RK
1497 /* The insn we have emitted. */
1498 rtx insn;
1b3d8f8a 1499 int old_generating_concat_p = generating_concat_p;
28d81abb 1500
e5e809f4 1501 /* An ASM with no outputs needs to be treated as volatile, for now. */
296f8acc
JL
1502 if (noutputs == 0)
1503 vol = 1;
1504
7d384cc0 1505 if (current_function_check_memory_usage)
17f5f329
RK
1506 {
1507 error ("`asm' cannot be used with `-fcheck-memory-usage'");
1508 return;
1509 }
1510
57bcb97a
RH
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
c5c76735
JL
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
b4ccaa16
RS
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 {
47ee9bcb 1530 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
14a774a9 1531
c09e6498
RS
1532 i = decode_reg_name (regname);
1533 if (i >= 0 || i == -4)
b4ccaa16 1534 ++nclobbers;
7859e3ac
DE
1535 else if (i == -2)
1536 error ("unknown register name `%s' in `asm'", regname);
b4ccaa16
RS
1537 }
1538
28d81abb
RK
1539 last_expr_type = 0;
1540
2a230e9d
BS
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
f62a15e3
BS
1549 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1550 {
1551 error ("too many alternatives in `asm'");
1552 return;
1553 }
4381f7c2 1554
2a230e9d
BS
1555 tmp = outputs;
1556 while (tmp)
1557 {
47ee9bcb 1558 const char *constraint = TREE_STRING_POINTER (TREE_PURPOSE (tmp));
14a774a9 1559
2a230e9d
BS
1560 if (n_occurrences (',', constraint) != nalternatives)
1561 {
1562 error ("operand constraints for `asm' differ in number of alternatives");
1563 return;
1564 }
14a774a9 1565
2a230e9d
BS
1566 if (TREE_CHAIN (tmp))
1567 tmp = TREE_CHAIN (tmp);
1568 else
1569 tmp = next, next = 0;
1570 }
1571 }
1572
28d81abb
RK
1573 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1574 {
1575 tree val = TREE_VALUE (tail);
b50a024d 1576 tree type = TREE_TYPE (val);
520a57c8 1577 const char *constraint;
40b18c0a
MM
1578 bool is_inout;
1579 bool allows_reg;
1580 bool allows_mem;
28d81abb
RK
1581
1582 /* If there's an erroneous arg, emit no insn. */
40b18c0a 1583 if (type == error_mark_node)
28d81abb
RK
1584 return;
1585
d09a75ae
RK
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. */
28d81abb 1590
2a230e9d 1591 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
2e2ed7d2 1592 output_constraints[i] = constraint;
1afbe1c4 1593
40b18c0a
MM
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;
d09a75ae 1604
d09a75ae
RK
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. */
28d81abb 1609
1b3d8f8a
GK
1610 generating_concat_p = 0;
1611
947255ed 1612 real_output_rtx[i] = NULL_RTX;
1afbe1c4
RH
1613 if ((TREE_CODE (val) == INDIRECT_REF
1614 && allows_mem)
2f939d94 1615 || (DECL_P (val)
1afbe1c4 1616 && (allows_mem || GET_CODE (DECL_RTL (val)) == REG)
b50a024d 1617 && ! (GET_CODE (DECL_RTL (val)) == REG
d09a75ae 1618 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
235c5021 1619 || ! allows_reg
2a230e9d 1620 || is_inout)
d09a75ae
RK
1621 {
1622 if (! allows_reg)
1623 mark_addressable (TREE_VALUE (tail));
1624
1625 output_rtx[i]
17f5f329
RK
1626 = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode,
1627 EXPAND_MEMORY_USE_WO);
d09a75ae
RK
1628
1629 if (! allows_reg && GET_CODE (output_rtx[i]) != MEM)
1630 error ("output number %d not directly addressable", i);
1b3d8f8a
GK
1631 if ((! allows_mem && GET_CODE (output_rtx[i]) == MEM)
1632 || GET_CODE (output_rtx[i]) == CONCAT)
947255ed
RH
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 }
d09a75ae 1639 }
b50a024d 1640 else
e619bb8d 1641 {
17e71da2 1642 output_rtx[i] = assign_temp (type, 0, 0, 1);
b50a024d
RK
1643 TREE_VALUE (tail) = make_tree (type, output_rtx[i]);
1644 }
235c5021 1645
1b3d8f8a
GK
1646 generating_concat_p = old_generating_concat_p;
1647
2a230e9d 1648 if (is_inout)
235c5021
RK
1649 {
1650 inout_mode[ninout] = TYPE_MODE (TREE_TYPE (TREE_VALUE (tail)));
1651 inout_opnum[ninout++] = i;
1652 }
28d81abb
RK
1653 }
1654
235c5021 1655 ninputs += ninout;
28d81abb
RK
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
6462bb43
AO
1667 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
1668 : GET_MODE (output_rtx[0])),
1669 TREE_STRING_POINTER (string),
4381f7c2 1670 empty_string, 0, argvec, constraints,
21a427cc 1671 filename, line);
c85f7c16 1672
78418280 1673 MEM_VOLATILE_P (body) = vol;
28d81abb
RK
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;
1f06ee8d 1682 int allows_reg = 0, allows_mem = 0;
520a57c8 1683 const char *constraint, *orig_constraint;
2a230e9d 1684 int c_len;
1f06ee8d 1685 rtx op;
28d81abb
RK
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;
2a230e9d
BS
1692
1693 /* ??? Can this happen, and does the error message make any sense? */
28d81abb
RK
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
2a230e9d 1701 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
520a57c8 1702 c_len = strlen (constraint);
1f06ee8d 1703 orig_constraint = constraint;
28d81abb 1704
2a230e9d
BS
1705 /* Make sure constraint has neither `=', `+', nor '&'. */
1706
1707 for (j = 0; j < c_len; j++)
1708 switch (constraint[j])
28d81abb 1709 {
2a230e9d 1710 case '+': case '=': case '&':
1f06ee8d
RH
1711 if (constraint == orig_constraint)
1712 {
14a774a9
RK
1713 error ("input operand constraint contains `%c'",
1714 constraint[j]);
1f06ee8d
RH
1715 return;
1716 }
1717 break;
65fed0cb 1718
2a230e9d 1719 case '%':
1f06ee8d
RH
1720 if (constraint == orig_constraint
1721 && i + 1 == ninputs - ninout)
2a230e9d
BS
1722 {
1723 error ("`%%' constraint used with last operand");
1724 return;
1725 }
1726 break;
1727
1f06ee8d
RH
1728 case 'V': case 'm': case 'o':
1729 allows_mem = 1;
1730 break;
1731
1732 case '<': case '>':
afa223c6 1733 case '?': case '!': case '*': case '#':
0551c32d 1734 case 'E': case 'F': case 'G': case 'H':
65fed0cb
RK
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 ',':
65fed0cb
RK
1738 break;
1739
7b7a33b3
JW
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':
cd76ea33 1746 case '5': case '6': case '7': case '8': case '9':
2a230e9d 1747 if (constraint[j] >= '0' + noutputs)
956d6950
JL
1748 {
1749 error
1750 ("matching constraint references invalid operand number");
1751 return;
1752 }
cd76ea33 1753
1f06ee8d 1754 /* Try and find the real constraint for this dup. */
1afbe1c4
RH
1755 if ((j == 0 && c_len == 1)
1756 || (j == 1 && c_len == 2 && constraint[0] == '%'))
1f06ee8d
RH
1757 {
1758 tree o = outputs;
14a774a9 1759
1f06ee8d
RH
1760 for (j = constraint[j] - '0'; j > 0; --j)
1761 o = TREE_CHAIN (o);
4381f7c2 1762
1f06ee8d 1763 constraint = TREE_STRING_POINTER (TREE_PURPOSE (o));
520a57c8 1764 c_len = strlen (constraint);
1f06ee8d
RH
1765 j = 0;
1766 break;
1767 }
1768
4381f7c2 1769 /* Fall through. */
cd76ea33 1770
1f06ee8d 1771 case 'p': case 'r':
65fed0cb
RK
1772 allows_reg = 1;
1773 break;
1f06ee8d 1774
0551c32d 1775 case 'g': case 'X':
1f06ee8d
RH
1776 allows_reg = 1;
1777 allows_mem = 1;
1778 break;
c2cba7a9
RH
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;
28d81abb
RK
1800 }
1801
1f06ee8d 1802 if (! allows_reg && allows_mem)
65fed0cb
RK
1803 mark_addressable (TREE_VALUE (tail));
1804
1f06ee8d 1805 op = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
65fed0cb 1806
1b3d8f8a
GK
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
1afbe1c4 1812 if (asm_operand_ok (op, constraint) <= 0)
65fed0cb 1813 {
1f06ee8d
RH
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
d96ecaf4 1823 || GET_CODE (op) == ADDRESSOF
1f06ee8d
RH
1824 || GET_CODE (op) == CONCAT)
1825 {
1826 tree type = TREE_TYPE (TREE_VALUE (tail));
1da68f56
RK
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);
65fed0cb 1831
1f06ee8d
RH
1832 emit_move_insn (memloc, op);
1833 op = memloc;
1834 }
14a774a9 1835
1f06ee8d
RH
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);
65fed0cb 1847 }
1b3d8f8a 1848 generating_concat_p = old_generating_concat_p;
6462bb43 1849 ASM_OPERANDS_INPUT (body, i) = op;
2a230e9d 1850
6462bb43 1851 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
38a448ca 1852 = gen_rtx_ASM_INPUT (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1f06ee8d 1853 orig_constraint);
28d81abb
RK
1854 i++;
1855 }
1856
14a774a9
RK
1857 /* Protect all the operands from the queue now that they have all been
1858 evaluated. */
28d81abb 1859
1b3d8f8a
GK
1860 generating_concat_p = 0;
1861
235c5021 1862 for (i = 0; i < ninputs - ninout; i++)
6462bb43
AO
1863 ASM_OPERANDS_INPUT (body, i)
1864 = protect_from_queue (ASM_OPERANDS_INPUT (body, i), 0);
28d81abb
RK
1865
1866 for (i = 0; i < noutputs; i++)
1867 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1868
4381f7c2 1869 /* For in-out operands, copy output rtx to input rtx. */
235c5021
RK
1870 for (i = 0; i < ninout; i++)
1871 {
235c5021
RK
1872 int j = inout_opnum[i];
1873
6462bb43 1874 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
235c5021 1875 = output_rtx[j];
6462bb43 1876 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
a8a05998 1877 = gen_rtx_ASM_INPUT (inout_mode[i], digit_string (j));
235c5021
RK
1878 }
1879
1b3d8f8a
GK
1880 generating_concat_p = old_generating_concat_p;
1881
28d81abb
RK
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 {
6462bb43 1889 ASM_OPERANDS_OUTPUT_CONSTRAINT (body)
2e2ed7d2 1890 = output_constraints[0];
38a448ca 1891 insn = emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
28d81abb 1892 }
14a774a9 1893
28d81abb
RK
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 }
14a774a9 1899
28d81abb
RK
1900 else
1901 {
1902 rtx obody = body;
1903 int num = noutputs;
14a774a9
RK
1904
1905 if (num == 0)
1906 num = 1;
1907
38a448ca 1908 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
28d81abb
RK
1909
1910 /* For each output operand, store a SET. */
28d81abb
RK
1911 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1912 {
1913 XVECEXP (body, 0, i)
38a448ca
RH
1914 = gen_rtx_SET (VOIDmode,
1915 output_rtx[i],
c5c76735 1916 gen_rtx_ASM_OPERANDS
6462bb43 1917 (GET_MODE (output_rtx[i]),
c5c76735 1918 TREE_STRING_POINTER (string),
2e2ed7d2 1919 output_constraints[i],
c5c76735
JL
1920 i, argvec, constraints,
1921 filename, line));
1922
28d81abb
RK
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
b4ccaa16 1934 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
28d81abb 1935 {
47ee9bcb 1936 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
b4ac57ab 1937 int j = decode_reg_name (regname);
28d81abb 1938
b4ac57ab 1939 if (j < 0)
28d81abb 1940 {
c09e6498 1941 if (j == -3) /* `cc', which is not a register */
dcfedcd0
RK
1942 continue;
1943
c09e6498
RS
1944 if (j == -4) /* `memory', don't cache memory across asm */
1945 {
bffc6177 1946 XVECEXP (body, 0, i++)
38a448ca 1947 = gen_rtx_CLOBBER (VOIDmode,
c5c76735
JL
1948 gen_rtx_MEM
1949 (BLKmode,
1950 gen_rtx_SCRATCH (VOIDmode)));
c09e6498
RS
1951 continue;
1952 }
1953
956d6950 1954 /* Ignore unknown register, error already signaled. */
cc1f5387 1955 continue;
28d81abb
RK
1956 }
1957
1958 /* Use QImode since that's guaranteed to clobber just one reg. */
b4ccaa16 1959 XVECEXP (body, 0, i++)
38a448ca 1960 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (QImode, j));
28d81abb
RK
1961 }
1962
1963 insn = emit_insn (body);
1964 }
1965
947255ed
RH
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
28d81abb
RK
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
1978void
1979expand_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 {
9790cefd
RH
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 }
078721e1 1995 else if (warn_unused_value)
28d81abb
RK
1996 warn_if_unused_value (exp);
1997 }
b6ec8c5f
RK
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
8f17b5c5
MM
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. */
a2cf7deb
CB
2007 last_expr_value = expand_expr (exp,
2008 (expr_stmts_for_value
2009 ? NULL_RTX : const0_rtx),
2010 VOIDmode, 0);
8f17b5c5 2011 last_expr_type = TREE_TYPE (exp);
28d81abb
RK
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 {
6a5bbbe6
RS
2018 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode)
2019 ;
2020 else if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
28d81abb
RK
2021 copy_to_reg (last_expr_value);
2022 else
ddbe9812
RS
2023 {
2024 rtx lab = gen_label_rtx ();
4381f7c2 2025
ddbe9812 2026 /* Compare the value with itself to reference it. */
c5d5d461
JL
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);
ddbe9812
RS
2033 emit_label (lab);
2034 }
28d81abb
RK
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
150a992a 2052int
28d81abb
RK
2053warn_if_unused_value (exp)
2054 tree exp;
2055{
2056 if (TREE_USED (exp))
2057 return 0;
2058
9790cefd
RH
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
eca93b05
RH
2065 /* If this is an expression with side effects, don't warn. */
2066 if (TREE_SIDE_EFFECTS (exp))
2067 return 0;
2068
28d81abb
RK
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:
81797aba 2081 case TRY_CATCH_EXPR:
28d81abb
RK
2082 case WITH_CLEANUP_EXPR:
2083 case EXIT_EXPR:
28d81abb
RK
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
de73f171
RK
2090 case SAVE_EXPR:
2091 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2092
28d81abb
RK
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:
a646a211
JM
2099 if (TREE_NO_UNUSED_WARNING (exp))
2100 return 0;
28d81abb
RK
2101 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
2102 return 1;
4d23e509
RS
2103 /* Let people do `(foo (), 0)' without a warning. */
2104 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2105 return 0;
28d81abb
RK
2106 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2107
2108 case NOP_EXPR:
2109 case CONVERT_EXPR:
b4ac57ab 2110 case NON_LVALUE_EXPR:
28d81abb
RK
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.
55cd1c09
JW
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
de73f171
RK
2124 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
2125 || TREE_CODE (tem) == CALL_EXPR)
55cd1c09
JW
2126 return 0;
2127 }
d1e1adfb 2128 goto warn;
28d81abb 2129
d1e1adfb
JM
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));
4381f7c2
KH
2135 /* Fall through. */
2136
28d81abb 2137 default:
ddbe9812 2138 /* Referencing a volatile value is a side effect, so don't warn. */
2f939d94 2139 if ((DECL_P (exp)
ddbe9812
RS
2140 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
2141 && TREE_THIS_VOLATILE (exp))
2142 return 0;
8d5e6e25
RK
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
d1e1adfb 2151 warn:
28d81abb
RK
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
2160void
2161clear_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
2170tree
2171expand_start_stmt_expr ()
2172{
ca695ac9
JB
2173 tree t;
2174
28d81abb
RK
2175 /* Make the RTL_EXPR node temporary, not momentary,
2176 so that rtl_expr_chain doesn't become garbage. */
ca695ac9 2177 t = make_node (RTL_EXPR);
33c6ab80 2178 do_pending_stack_adjust ();
591ccf92 2179 start_sequence_for_rtl_expr (t);
28d81abb
RK
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
2197tree
2198expand_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
28d81abb
RK
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
2243void
2244expand_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
b93a436e 2264 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
28d81abb
RK
2265}
2266
2267/* Generate RTL between then-clause and the elseif-clause
2268 of an if-then-elseif-.... */
2269
2270void
2271expand_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 ();
37366632 2279 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
28d81abb
RK
2280}
2281
2282/* Generate RTL between the then-clause and the else-clause
2283 of an if-then-else. */
2284
2285void
2286expand_start_else ()
2287{
2288 if (cond_stack->data.cond.endif_label == 0)
2289 cond_stack->data.cond.endif_label = gen_label_rtx ();
ca695ac9 2290
28d81abb
RK
2291 emit_jump (cond_stack->data.cond.endif_label);
2292 emit_label (cond_stack->data.cond.next_label);
0f41302f 2293 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
28d81abb
RK
2294}
2295
d947ba59
RK
2296/* After calling expand_start_else, turn this "else" into an "else if"
2297 by providing another condition. */
2298
2299void
2300expand_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
28d81abb
RK
2307/* Generate RTL for the end of an if-then.
2308 Pop the record for it off of cond_stack. */
2309
2310void
2311expand_end_cond ()
2312{
2313 struct nesting *thiscond = cond_stack;
2314
b93a436e
JL
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);
28d81abb
RK
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
2332struct nesting *
2333expand_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 ();
8afad312 2345 thisloop->data.loop.alt_end_label = 0;
28d81abb
RK
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 ();
6496a589 2353 emit_note (NULL, NOTE_INSN_LOOP_BEG);
28d81abb
RK
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
2362struct nesting *
2363expand_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
f0de0c5d
RH
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
2374struct nesting *
2375expand_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;
ba89764a 2387 thisloop->data.loop.continue_label = thisloop->data.loop.end_label;
f0de0c5d
RH
2388 thisloop->exit_label = thisloop->data.loop.end_label;
2389 loop_stack = thisloop;
2390 nesting_stack = thisloop;
2391
2392 return thisloop;
2393}
2394
28d81abb
RK
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
2400void
2401expand_loop_continue_here ()
2402{
2403 do_pending_stack_adjust ();
6496a589 2404 emit_note (NULL, NOTE_INSN_LOOP_CONT);
28d81abb
RK
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
2411void
2412expand_end_loop ()
2413{
0720f6fb
MM
2414 rtx start_label = loop_stack->data.loop.start_label;
2415 rtx insn = get_last_insn ();
a7d308f7 2416 int needs_end_jump = 1;
28d81abb
RK
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
a7d308f7
R
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
4381f7c2
KH
2460 for (prev = PREV_INSN (jump);; prev = PREV_INSN (prev))
2461 {
a7d308f7
R
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.
93de5c31
MM
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;
0720f6fb 2505 end_label:
4381f7c2 2506
93de5c31
MM
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;
0720f6fb 2515 end_label:
93de5c31
MM
2516
2517 Here, the `test' may actually consist of some reasonably complex
2518 code, terminating in a test. */
0720f6fb 2519
28d81abb 2520 if (optimize
a7d308f7 2521 && needs_end_jump
28d81abb
RK
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 {
93de5c31 2528 int eh_regions = 0;
0720f6fb
MM
2529 int num_insns = 0;
2530 rtx last_test_insn = NULL_RTX;
93de5c31 2531
28d81abb
RK
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 {
4381f7c2 2537 if (GET_CODE (insn) == NOTE)
93de5c31
MM
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;
4381f7c2 2554 if (eh_regions < 0)
93de5c31
MM
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 }
28d81abb 2565
f114df20
JL
2566 /* We must not walk into a nested loop. */
2567 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2568 break;
2569
93de5c31
MM
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 }
28d81abb
RK
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
4381f7c2 2581 if (eh_regions > 0)
93de5c31
MM
2582 /* We don't want to move a partial EH region. Consider:
2583
2584 while ( ( { try {
4381f7c2 2585 if (cond ()) 0;
93de5c31
MM
2586 else {
2587 bar();
2588 1;
2589 }
4381f7c2 2590 } catch (...) {
93de5c31
MM
2591 1;
2592 } )) {
2593 body;
4381f7c2 2594 }
93de5c31
MM
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
4381f7c2 2600 constructs are certainy legal in LISP.
93de5c31
MM
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
4381f7c2 2606 EH_END note.)
93de5c31
MM
2607
2608 So we don't look for tests within an EH region. */
2609 continue;
2610
4381f7c2 2611 if (GET_CODE (insn) == JUMP_INSN
28d81abb 2612 && GET_CODE (PATTERN (insn)) == SET
0720f6fb
MM
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
4381f7c2 2636 && ((XEXP (dest1, 0)
0720f6fb 2637 == loop_stack->data.loop.alt_end_label)
4381f7c2 2638 || (XEXP (dest1, 0)
0720f6fb
MM
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 }
28d81abb
RK
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;
93de5c31 2660 rtx next_insn;
28d81abb 2661
b4ac57ab 2662 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
28d81abb
RK
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));
93de5c31
MM
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
28d81abb
RK
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
a7d308f7
R
2709 if (needs_end_jump)
2710 {
2711 emit_jump (start_label);
6496a589 2712 emit_note (NULL, NOTE_INSN_LOOP_END);
a7d308f7 2713 }
28d81abb
RK
2714 emit_label (loop_stack->data.loop.end_label);
2715
2716 POPSTACK (loop_stack);
2717
2718 last_expr_type = 0;
2719}
2720
f0de0c5d
RH
2721/* Finish a null loop, aka do { } while (0). */
2722
2723void
2724expand_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
28d81abb
RK
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
2739int
2740expand_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;
37366632
RK
2748 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2749 NULL_RTX);
28d81abb
RK
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
2756int
2757expand_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;
37366632 2765 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
28d81abb
RK
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
2773int
2774expand_exit_loop_if_false (whichloop, cond)
2775 struct nesting *whichloop;
2776 tree cond;
2777{
b93a436e
JL
2778 rtx label = gen_label_rtx ();
2779 rtx last_insn;
28d81abb 2780 last_expr_type = 0;
b93a436e 2781
28d81abb
RK
2782 if (whichloop == 0)
2783 whichloop = loop_stack;
2784 if (whichloop == 0)
2785 return 0;
b93a436e
JL
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. */
d902c7ea 2789
b93a436e
JL
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);
ca695ac9 2797
28d81abb
RK
2798 return 1;
2799}
2800
4a050cc2
JL
2801/* Return nonzero if the loop nest is empty. Else return zero. */
2802
2803int
2804stmt_loop_nest_empty ()
2805{
930b9832
JL
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. */
abe79fd4 2809 return (cfun->stmt == NULL || loop_stack == NULL);
4a050cc2
JL
2810}
2811
28d81abb
RK
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
2819int
2820preserve_subexpressions_p ()
2821{
2822 rtx insn;
2823
2824 if (flag_expensive_optimizations)
2825 return 1;
2826
01d939e8 2827 if (optimize == 0 || cfun == 0 || cfun->stmt == 0 || loop_stack == 0)
28d81abb
RK
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
2847int
2848expand_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 {
37366632 2855 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
28d81abb
RK
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
2865void
2866expand_null_return ()
2867{
bd695e1e
RH
2868 rtx last_insn = get_last_insn ();
2869
4381f7c2 2870 /* If this function was declared to return a value, but we
bd695e1e
RH
2871 didn't, clobber the return registers so that they are not
2872 propogated live to the rest of the function. */
c13fde05 2873 clobber_return_register ();
28d81abb 2874
396ad517 2875 expand_null_return_1 (last_insn);
28d81abb
RK
2876}
2877
2878/* Generate RTL to return from the current function, with value VAL. */
2879
8d800403 2880static void
28d81abb
RK
2881expand_value_return (val)
2882 rtx val;
2883{
28d81abb
RK
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)
77636079 2891 {
77636079 2892 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
37877eb0 2893#ifdef PROMOTE_FUNCTION_RETURN
77636079 2894 int unsignedp = TREE_UNSIGNED (type);
14a774a9
RK
2895 enum machine_mode old_mode
2896 = DECL_MODE (DECL_RESULT (current_function_decl));
28612f9e 2897 enum machine_mode mode
14a774a9 2898 = promote_mode (type, old_mode, &unsignedp, 1);
77636079 2899
14a774a9
RK
2900 if (mode != old_mode)
2901 val = convert_modes (mode, old_mode, val, unsignedp);
77636079 2902#endif
14a774a9
RK
2903 if (GET_CODE (return_reg) == PARALLEL)
2904 emit_group_load (return_reg, val, int_size_in_bytes (type),
19caa751 2905 TYPE_ALIGN (type));
14a774a9 2906 else
77636079
RS
2907 emit_move_insn (return_reg, val);
2908 }
14a774a9 2909
396ad517 2910 expand_null_return_1 (last_insn);
28d81abb
RK
2911}
2912
2913/* Output a return with no value. If LAST_INSN is nonzero,
396ad517 2914 pretend that the return takes place after LAST_INSN. */
28d81abb
RK
2915
2916static void
396ad517 2917expand_null_return_1 (last_insn)
28d81abb 2918 rtx last_insn;
28d81abb
RK
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
396ad517
JDA
2926 if (end_label == 0)
2927 end_label = return_label = gen_label_rtx ();
37366632 2928 expand_goto_internal (NULL_TREE, end_label, last_insn);
28d81abb
RK
2929}
2930\f
2931/* Generate RTL to evaluate the expression RETVAL and return it
2932 from the current function. */
2933
2934void
2935expand_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;
19e7881c 2947 rtx result_rtl;
28d81abb 2948 register rtx val = 0;
28d81abb 2949 tree retval_rhs;
28d81abb
RK
2950
2951 /* If function wants no value, give it none. */
2952 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2953 {
37366632 2954 expand_expr (retval, NULL_RTX, VOIDmode, 0);
7e70e7c5 2955 emit_queue ();
28d81abb
RK
2956 expand_null_return ();
2957 return;
2958 }
2959
ea11ca7e 2960 if (retval == error_mark_node)
c9407e4c
MM
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 }
ea11ca7e 2967 else if (TREE_CODE (retval) == RESULT_DECL)
28d81abb
RK
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);
71653180 2972 else if (VOID_TYPE_P (TREE_TYPE (retval)))
28d81abb
RK
2973 /* Recognize tail-recursive call to void function. */
2974 retval_rhs = retval;
2975 else
2976 retval_rhs = NULL_TREE;
2977
7cc8342c 2978 last_insn = get_last_insn ();
28d81abb
RK
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 ();
a0a34f94
RK
2993 tree expr;
2994
37366632 2995 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
1483bddb 2996 start_cleanup_deferral ();
dd98f85c 2997 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
a0a34f94
RK
2998 DECL_RESULT (current_function_decl),
2999 TREE_OPERAND (retval_rhs, 1));
3000 TREE_SIDE_EFFECTS (expr) = 1;
3001 expand_return (expr);
28d81abb 3002 emit_label (label);
a0a34f94 3003
dd98f85c 3004 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
a0a34f94
RK
3005 DECL_RESULT (current_function_decl),
3006 TREE_OPERAND (retval_rhs, 2));
3007 TREE_SIDE_EFFECTS (expr) = 1;
3008 expand_return (expr);
1483bddb 3009 end_cleanup_deferral ();
28d81abb
RK
3010 return;
3011 }
3012
19e7881c
MM
3013 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3014
4c485b63
JL
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
3ffeb8f1
JW
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. */
4c485b63
JL
3022
3023 if (retval_rhs != 0
3024 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
14a774a9 3025 && GET_CODE (result_rtl) == REG)
4c485b63 3026 {
770ae6cc
RK
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));
4c485b63 3032 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
770ae6cc
RK
3033 unsigned int bitsize
3034 = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)), BITS_PER_WORD);
4c485b63 3035 rtx *result_pseudos = (rtx *) alloca (sizeof (rtx) * n_regs);
c16ddde3 3036 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
4c485b63 3037 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
af55da56 3038 enum machine_mode tmpmode, result_reg_mode;
4c485b63 3039
2954d7db
RK
3040 if (bytes == 0)
3041 {
3042 expand_null_return ();
3043 return;
3044 }
3045
a7f875d7
RK
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
4381f7c2 3054 /* Copy the structure BITSIZE bits at a time. */
a7f875d7
RK
3055 for (bitpos = 0, xbitpos = big_endian_correction;
3056 bitpos < bytes * BITS_PER_UNIT;
3057 bitpos += bitsize, xbitpos += bitsize)
4c485b63 3058 {
a7f875d7 3059 /* We need a new destination pseudo each time xbitpos is
abc95ed3 3060 on a word boundary and when xbitpos == big_endian_correction
a7f875d7
RK
3061 (the first time through). */
3062 if (xbitpos % BITS_PER_WORD == 0
3063 || xbitpos == big_endian_correction)
4c485b63 3064 {
a7f875d7
RK
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. */
38a448ca 3070 emit_insn (gen_rtx_CLOBBER (VOIDmode, dst));
4c485b63 3071 }
a7f875d7
RK
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,
19caa751
RK
3085 NULL_RTX, word_mode, word_mode,
3086 bitsize, BITS_PER_WORD),
3087 bitsize, BITS_PER_WORD);
4c485b63
JL
3088 }
3089
4c485b63
JL
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. */
4c485b63 3093 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
0c61f541 3094 tmpmode != VOIDmode;
4c485b63 3095 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
2954d7db
RK
3096 /* Have we found a large enough mode? */
3097 if (GET_MODE_SIZE (tmpmode) >= bytes)
3098 break;
4c485b63
JL
3099
3100 /* No suitable mode found. */
0c61f541 3101 if (tmpmode == VOIDmode)
3ffeb8f1 3102 abort ();
4c485b63 3103
14a774a9 3104 PUT_MODE (result_rtl, tmpmode);
3ffeb8f1 3105
af55da56
JW
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
3ffeb8f1 3112 emit_queue ();
3ffeb8f1 3113 for (i = 0; i < n_regs; i++)
af55da56 3114 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3ffeb8f1 3115 result_pseudos[i]);
4c485b63 3116
af55da56
JW
3117 if (tmpmode != result_reg_mode)
3118 result_reg = gen_lowpart (tmpmode, result_reg);
3119
4c485b63
JL
3120 expand_value_return (result_reg);
3121 }
7cc8342c
RH
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)))
28d81abb 3126 {
14a774a9
RK
3127 /* Calculate the return value into a temporary (usually a pseudo
3128 reg). */
1da68f56
RK
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);
dd98f85c
JM
3133 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
3134 val = force_not_mem (val);
28d81abb 3135 emit_queue ();
28d81abb
RK
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. */
cba389cd 3143 expand_expr (retval, const0_rtx, VOIDmode, 0);
28d81abb 3144 emit_queue ();
14a774a9 3145 expand_value_return (result_rtl);
28d81abb
RK
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
3152int
3153drop_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
b06775f9
RH
3161/* Attempt to optimize a potential tail recursion call into a goto.
3162 ARGUMENTS are the arguments to a CALL_EXPR; LAST_INSN indicates
4381f7c2
KH
3163 where to place the jump to the tail recursion label.
3164
b06775f9 3165 Return TRUE if the call was optimized into a goto. */
642cac7b 3166
564ea051 3167int
b06775f9
RH
3168optimize_tail_recursion (arguments, last_insn)
3169 tree arguments;
642cac7b
JL
3170 rtx last_insn;
3171{
b06775f9
RH
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)))
642cac7b
JL
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 ();
564ea051 3185 return 1;
642cac7b 3186 }
564ea051 3187 return 0;
642cac7b
JL
3188}
3189
28d81abb
RK
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
3196static int
3197tail_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 {
5c7fe359
RK
3213 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (a)))
3214 != TYPE_MAIN_VARIANT (TREE_TYPE (f)))
28d81abb
RK
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++)
37366632 3227 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
28d81abb
RK
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]))
4381f7c2
KH
3238 {
3239 copy = 1;
3240 break;
3241 }
28d81abb
RK
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 {
98f3b471 3251 if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
28d81abb
RK
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
8e91754e
MM
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
4381f7c2 3276 when expand_end_bindings is called.
a97901e6
MM
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. */
28d81abb
RK
3281
3282void
a97901e6 3283expand_start_bindings_and_block (flags, block)
8e91754e 3284 int flags;
a97901e6 3285 tree block;
28d81abb
RK
3286{
3287 struct nesting *thisblock = ALLOC_NESTING ();
8e91754e
MM
3288 rtx note;
3289 int exit_flag = ((flags & 1) != 0);
3290 int block_flag = ((flags & 2) == 0);
4381f7c2 3291
a97901e6
MM
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 ();
8e91754e 3296
a97901e6
MM
3297 /* Create a note to mark the beginning of the block. */
3298 if (block_flag)
3299 {
6496a589 3300 note = emit_note (NULL, NOTE_INSN_BLOCK_BEG);
a97901e6
MM
3301 NOTE_BLOCK (note) = block;
3302 }
3303 else
6496a589 3304 note = emit_note (NULL, NOTE_INSN_DELETED);
4381f7c2 3305
28d81abb
RK
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;
3f1d071b 3313 thisblock->data.block.n_function_calls = 0;
e976b8b2 3314 thisblock->data.block.exception_region = 0;
3f1d071b 3315 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
e976b8b2
MS
3316
3317 thisblock->data.block.conditional_code = 0;
3318 thisblock->data.block.last_unconditional_cleanup = note;
a571f7a0
MM
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. */
6496a589 3325 emit_note (NULL, NOTE_INSN_DELETED);
e976b8b2
MS
3326 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
3327
28d81abb
RK
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;
28d81abb
RK
3336 thisblock->data.block.label_chain = 0;
3337 thisblock->data.block.innermost_stack_block = stack_block_stack;
3338 thisblock->data.block.first_insn = note;
3f1d071b 3339 thisblock->data.block.block_start_count = ++current_block_start_count;
28d81abb
RK
3340 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3341 block_stack = thisblock;
3342 nesting_stack = thisblock;
3343
b93a436e
JL
3344 /* Make a new level for allocating stack slots. */
3345 push_temp_slots ();
28d81abb
RK
3346}
3347
e976b8b2
MS
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
3354void
3355expand_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. */
8e91754e 3363 expand_start_bindings (2);
e976b8b2
MS
3364
3365 target_temp_slot_level = temp_slot_level;
3366}
3367
3368void
3369expand_end_target_temps ()
3370{
3371 expand_end_bindings (NULL_TREE, 0, 0);
4381f7c2 3372
e976b8b2
MS
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
deb5e280
JM
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
4381f7c2 3386 *that* node in turn will point to the relevant FUNCTION_DECL node. */
deb5e280
JM
3387
3388int
3389is_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
e976b8b2
MS
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
3412int
3413conditional_context ()
3414{
3415 return block_stack && block_stack->data.block.conditional_code;
3416}
3417
91088ddb
JM
3418/* Return an opaque pointer to the current nesting level, so frontend code
3419 can check its own sanity. */
3420
3421struct nesting *
3422current_nesting_level ()
3423{
3424 return cfun ? block_stack : 0;
3425}
3426
ba716ac9
BS
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
e881bb1b 3430static rtx
ba716ac9
BS
3431expand_nl_handler_label (slot, before_insn)
3432 rtx slot, before_insn;
3433{
3434 rtx insns;
3435 rtx handler_label = gen_label_rtx ();
3436
0045d504 3437 /* Don't let cleanup_cfg delete the handler. */
ba716ac9
BS
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);
e881bb1b
RH
3447
3448 return handler_label;
ba716ac9
BS
3449}
3450
3451/* Emit code to restore vital registers at the beginning of a nonlocal goto
3452 handler. */
3453static void
3454expand_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
b6a1cbae 3484 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
ba716ac9
BS
3485 if (elim_regs[i].from == ARG_POINTER_REGNUM
3486 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3487 break;
3488
b6a1cbae 3489 if (i == ARRAY_SIZE (elim_regs))
ba716ac9
BS
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
3516static void
3517expand_nl_goto_receivers (thisblock)
3518 struct nesting *thisblock;
3519{
3520 tree link;
3521 rtx afterward = gen_label_rtx ();
3522 rtx insns, slot;
e881bb1b 3523 rtx label_list;
ba716ac9
BS
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;
e881bb1b 3547 label_list = NULL_RTX;
ba716ac9
BS
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 {
e881bb1b
RH
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
ba716ac9
BS
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 {
e881bb1b
RH
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);
ba716ac9
BS
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
e881bb1b 3588 nonlocal_goto_handler_labels = label_list;
ba716ac9
BS
3589 emit_label (afterward);
3590}
3591
ba716677
MM
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
3596void
3597warn_about_unused_variables (vars)
3598 tree vars;
3599{
3600 tree decl;
3601
078721e1 3602 if (warn_unused_variable)
ba716677 3603 for (decl = vars; decl; decl = TREE_CHAIN (decl))
4381f7c2 3604 if (TREE_CODE (decl) == VAR_DECL
ba716677
MM
3605 && ! TREE_USED (decl)
3606 && ! DECL_IN_SYSTEM_HEADER (decl)
4381f7c2 3607 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
ba716677
MM
3608 warning_with_decl (decl, "unused variable `%s'");
3609}
3610
28d81abb 3611/* Generate RTL code to terminate a binding contour.
e97b5c12
MM
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
28d81abb
RK
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
3623void
3624expand_end_bindings (vars, mark_ends, dont_jump_in)
3625 tree vars;
3626 int mark_ends;
3627 int dont_jump_in;
3628{
16842c15 3629 register struct nesting *thisblock = block_stack;
e976b8b2 3630
ba716677
MM
3631 /* If any of the variables in this scope were not used, warn the
3632 user. */
3633 warn_about_unused_variables (vars);
28d81abb 3634
28d81abb
RK
3635 if (thisblock->exit_label)
3636 {
3637 do_pending_stack_adjust ();
3638 emit_label (thisblock->exit_label);
3639 }
3640
ba716ac9 3641 /* If necessary, make handlers for nonlocal gotos taking
28d81abb 3642 place in the function calls in this block. */
3f1d071b 3643 if (function_call_count != thisblock->data.block.n_function_calls
28d81abb
RK
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)))
ba716ac9 3652 expand_nl_goto_receivers (thisblock);
28d81abb 3653
72eb1038
BH
3654 /* Don't allow jumping into a block that has a stack level.
3655 Cleanups are allowed, though. */
28d81abb 3656 if (dont_jump_in
72eb1038 3657 || thisblock->data.block.stack_level != 0)
28d81abb
RK
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
72eb1038 3668 come from outside all saved stack-levels. */
28d81abb
RK
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 {
04da69d3
JM
3682 int reachable;
3683 rtx insn;
28d81abb 3684
50d1b7a1
MS
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;
28d81abb 3690
04da69d3
JM
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);
d1ee23e5 3695 reachable = (! insn || GET_CODE (insn) != BARRIER);
4381f7c2 3696
50d1b7a1
MS
3697 /* Do the cleanups. */
3698 expand_cleanups (thisblock->data.block.cleanups, NULL_TREE, 0, reachable);
3699 if (reachable)
3700 do_pending_stack_adjust ();
28d81abb 3701
50d1b7a1
MS
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);
ba716ac9 3712 if (nonlocal_goto_handler_slots != 0)
50d1b7a1
MS
3713 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3714 NULL_RTX);
28d81abb
RK
3715 }
3716
3717 /* Any gotos out of this block must also do these things.
59257ff7
RK
3718 Also report any gotos with fixups that came to labels in this
3719 level. */
28d81abb
RK
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
c7d2d61d
RS
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)
a97901e6 3732 {
6496a589 3733 rtx note = emit_note (NULL, NOTE_INSN_BLOCK_END);
a97901e6
MM
3734 NOTE_BLOCK (note) = NOTE_BLOCK (thisblock->data.block.first_insn);
3735 }
c7d2d61d
RS
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
e976b8b2 3740 /* Restore the temporary level of TARGET_EXPRs. */
3f1d071b 3741 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
e976b8b2 3742
28d81abb
RK
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
7393c642
RK
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
3755void
3756save_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
28d81abb 3769/* Generate RTL for the automatic variable declaration DECL.
ec5cd386 3770 (Other kinds of declarations are simply ignored if seen here.) */
28d81abb
RK
3771
3772void
3773expand_decl (decl)
3774 register tree decl;
3775{
3f1d071b 3776 struct nesting *thisblock;
ca695ac9
JB
3777 tree type;
3778
ca695ac9 3779 type = TREE_TYPE (decl);
28d81abb 3780
eabb9ed0
RK
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 }
28d81abb 3791
eabb9ed0
RK
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'. */
28d81abb
RK
3796 if (TREE_CODE (decl) != VAR_DECL)
3797 return;
eabb9ed0 3798
44fe2e80 3799 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
28d81abb
RK
3800 return;
3801
3f1d071b
BS
3802 thisblock = block_stack;
3803
28d81abb
RK
3804 /* Create the RTL representation for the variable. */
3805
3806 if (type == error_mark_node)
19e7881c 3807 SET_DECL_RTL (decl, gen_rtx_MEM (BLKmode, const0_rtx));
1da68f56 3808
28d81abb
RK
3809 else if (DECL_SIZE (decl) == 0)
3810 /* Variable with incomplete type. */
3811 {
abde42f7 3812 rtx x;
28d81abb
RK
3813 if (DECL_INITIAL (decl) == 0)
3814 /* Error message was already done; now avoid a crash. */
abde42f7 3815 x = gen_rtx_MEM (BLKmode, const0_rtx);
28d81abb
RK
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. */
abde42f7 3819 x = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3bdf5ad1 3820
abde42f7
JH
3821 set_mem_attributes (x, decl, 1);
3822 SET_DECL_RTL (decl, x);
28d81abb
RK
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)
d29c259b 3830 && (DECL_REGISTER (decl) || optimize)
e5e809f4 3831 /* if -fcheck-memory-usage, check all variables. */
7d384cc0 3832 && ! current_function_check_memory_usage)
28d81abb
RK
3833 {
3834 /* Automatic variable that can go in a register. */
98f3b471 3835 int unsignedp = TREE_UNSIGNED (type);
28612f9e
RK
3836 enum machine_mode reg_mode
3837 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
98f3b471 3838
19e7881c 3839 SET_DECL_RTL (decl, gen_reg_rtx (reg_mode));
7f070d5e
RK
3840 mark_user_reg (DECL_RTL (decl));
3841
e5e809f4 3842 if (POINTER_TYPE_P (type))
7f070d5e 3843 mark_reg_pointer (DECL_RTL (decl),
bdb429a5 3844 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
258a120b
JM
3845
3846 maybe_set_unchanging (DECL_RTL (decl), decl);
d96a6d1a
JM
3847
3848 /* If something wants our address, try to use ADDRESSOF. */
3849 if (TREE_ADDRESSABLE (decl))
3850 put_var_into_stack (decl);
28d81abb 3851 }
0df15c2c 3852
4559fd9e 3853 else if (TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
5e4ef18a 3854 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
05bccae2
RK
3855 && 0 < compare_tree_int (DECL_SIZE_UNIT (decl),
3856 STACK_CHECK_MAX_VAR_SIZE)))
28d81abb
RK
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. */
19e7881c 3866 if (DECL_RTL_SET_P (decl))
28d81abb
RK
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
19e7881c
MM
3874 SET_DECL_RTL (decl,
3875 assign_temp (TREE_TYPE (decl), 1, 1, 1));
28d81abb
RK
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)));
11cf4d18 3880 DECL_USER_ALIGN (decl) = 0;
28d81abb
RK
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 }
28d81abb
RK
3888 }
3889 else
3890 /* Dynamic-size object: must push space on the stack. */
3891 {
abde42f7 3892 rtx address, size, x;
28d81abb
RK
3893
3894 /* Record the stack pointer on entry to block, if have
3895 not already done so. */
7393c642
RK
3896 do_pending_stack_adjust ();
3897 save_stack_pointer ();
28d81abb 3898
4d9af632
JM
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
28d81abb 3905 /* Compute the variable's size, in bytes. */
4559fd9e 3906 size = expand_expr (DECL_SIZE_UNIT (decl), NULL_RTX, VOIDmode, 0);
28d81abb
RK
3907 free_temp_slots ();
3908
ff91ad08 3909 /* Allocate space on the stack for the variable. Note that
4381f7c2 3910 DECL_ALIGN says how the variable is to be aligned and we
ff91ad08
RK
3911 cannot use it to conclude anything about the alignment of
3912 the size. */
37366632 3913 address = allocate_dynamic_stack_space (size, NULL_RTX,
ff91ad08 3914 TYPE_ALIGN (TREE_TYPE (decl)));
28d81abb 3915
28d81abb 3916 /* Reference the variable indirect through that rtx. */
abde42f7
JH
3917 x = gen_rtx_MEM (DECL_MODE (decl), address);
3918 set_mem_attributes (x, decl, 1);
3919 SET_DECL_RTL (decl, x);
28d81abb 3920
2207e295 3921
28d81abb
RK
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
11cf4d18 3928 DECL_USER_ALIGN (decl) = 0;
28d81abb 3929 }
28d81abb
RK
3930}
3931\f
3932/* Emit code to perform the initialization of a declaration DECL. */
3933
3934void
3935expand_decl_init (decl)
3936 tree decl;
3937{
b4ac57ab
RS
3938 int was_used = TREE_USED (decl);
3939
3564e40e
RK
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
28d81abb
RK
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));
e5e809f4 3962
28d81abb 3963 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
e5e809f4 3964 || code == POINTER_TYPE || code == REFERENCE_TYPE)
28d81abb
RK
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
b4ac57ab
RS
3976 /* Don't let the initialization count as "using" the variable. */
3977 TREE_USED (decl) = was_used;
3978
28d81abb 3979 /* Free any temporaries we made while initializing the decl. */
ae8c59c0 3980 preserve_temp_slots (NULL_RTX);
28d81abb
RK
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
4847c938
MS
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
e976b8b2
MS
3989 happens in exception handling, for gotos, returns, breaks that
3990 leave the current scope.
28d81abb
RK
3991
3992 If CLEANUP is nonzero and DECL is zero, we record a cleanup
3993 that is not associated with any particular variable. */
3994
3995int
3996expand_decl_cleanup (decl, cleanup)
3997 tree decl, cleanup;
3998{
3f1d071b 3999 struct nesting *thisblock;
28d81abb
RK
4000
4001 /* Error if we are not in any block. */
01d939e8 4002 if (cfun == 0 || block_stack == 0)
28d81abb
RK
4003 return 0;
4004
3f1d071b
BS
4005 thisblock = block_stack;
4006
28d81abb
RK
4007 /* Record the cleanup if there is one. */
4008
4009 if (cleanup != 0)
4010 {
e976b8b2
MS
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
e976b8b2 4033 cond = build_decl (VAR_DECL, NULL_TREE, type_for_mode (word_mode, 1));
19e7881c 4034 SET_DECL_RTL (cond, flag);
e976b8b2
MS
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
e976b8b2
MS
4042 cleanups = thisblock->data.block.cleanup_ptr;
4043 }
4044
4847c938 4045 cleanup = unsave_expr (cleanup);
e976b8b2 4046
1f8f4a0b 4047 t = *cleanups = tree_cons (decl, cleanup, *cleanups);
e976b8b2
MS
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 }
4847c938 4057
52a11cbf 4058 if (! using_eh_for_cleanups_p)
e976b8b2 4059 TREE_ADDRESSABLE (t) = 1;
52a11cbf
RH
4060 else
4061 expand_eh_region_start ();
4062
e976b8b2
MS
4063 if (cond_context)
4064 {
4065 seq = get_insns ();
4066 end_sequence ();
7e82801f
MS
4067 if (seq)
4068 thisblock->data.block.last_unconditional_cleanup
4069 = emit_insns_after (seq,
4070 thisblock->data.block.last_unconditional_cleanup);
e976b8b2
MS
4071 }
4072 else
4073 {
4074 thisblock->data.block.last_unconditional_cleanup
4075 = get_last_insn ();
ef97beff
JJ
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. */
6496a589 4082 emit_note (NULL, NOTE_INSN_DELETED);
e976b8b2
MS
4083 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
4084 }
28d81abb
RK
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
4093void
4094expand_anon_union_decl (decl, cleanup, decl_elts)
4095 tree decl, cleanup, decl_elts;
4096{
01d939e8 4097 struct nesting *thisblock = cfun == 0 ? 0 : block_stack;
28d81abb 4098 rtx x;
8a693bd0 4099 tree t;
28d81abb 4100
8a693bd0
MM
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 }
4381f7c2 4108
ec5cd386
RK
4109 expand_decl (decl);
4110 expand_decl_cleanup (decl, cleanup);
28d81abb
RK
4111 x = DECL_RTL (decl);
4112
8a693bd0
MM
4113 /* Go through the elements, assigning RTL to each. */
4114 for (t = decl_elts; t; t = TREE_CHAIN (t))
28d81abb 4115 {
8a693bd0
MM
4116 tree decl_elt = TREE_VALUE (t);
4117 tree cleanup_elt = TREE_PURPOSE (t);
28d81abb
RK
4118 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
4119
7b9032dd
JM
4120 /* Propagate the union's alignment to the elements. */
4121 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
11cf4d18 4122 DECL_USER_ALIGN (decl_elt) = DECL_USER_ALIGN (decl);
7b9032dd
JM
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
05bccae2 4129 = mode_for_size_tree (DECL_SIZE (decl_elt), MODE_INT, 1);
7b9032dd 4130
28d81abb
RK
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))
19e7881c 4136 SET_DECL_RTL (decl_elt, x);
28d81abb 4137 else
f1ec5147 4138 SET_DECL_RTL (decl_elt, adjust_address_nv (x, mode, 0));
28d81abb
RK
4139 }
4140 else if (GET_CODE (x) == REG)
4141 {
4142 if (mode == GET_MODE (x))
19e7881c 4143 SET_DECL_RTL (decl_elt, x);
28d81abb 4144 else
ddef6bc7 4145 SET_DECL_RTL (decl_elt, gen_lowpart_SUBREG (mode, x));
28d81abb
RK
4146 }
4147 else
4148 abort ();
4149
4150 /* Record the cleanup if there is one. */
4151
4152 if (cleanup != 0)
4153 thisblock->data.block.cleanups
1f8f4a0b
MM
4154 = tree_cons (decl_elt, cleanup_elt,
4155 thisblock->data.block.cleanups);
28d81abb
RK
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
4e44807b
MS
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
50d1b7a1
MS
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. */
28d81abb
RK
4172
4173static void
50d1b7a1 4174expand_cleanups (list, dont_do, in_fixup, reachable)
28d81abb
RK
4175 tree list;
4176 tree dont_do;
4e44807b 4177 int in_fixup;
50d1b7a1 4178 int reachable;
28d81abb
RK
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)
50d1b7a1 4185 expand_cleanups (TREE_VALUE (tail), dont_do, in_fixup, reachable);
28d81abb
RK
4186 else
4187 {
52a11cbf
RH
4188 if (! in_fixup && using_eh_for_cleanups_p)
4189 expand_eh_region_end_cleanup (TREE_VALUE (tail));
61d6b1cc 4190
50d1b7a1
MS
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. */
9762d48d 4201
52a11cbf
RH
4202 /* We may need to protect from outer cleanups. */
4203 if (in_fixup && using_eh_for_cleanups_p)
4204 {
4205 expand_eh_region_start ();
e5e809f4 4206
52a11cbf
RH
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);
e5e809f4 4213
50d1b7a1
MS
4214 free_temp_slots ();
4215 }
28d81abb
RK
4216 }
4217 }
4218}
4219
e976b8b2
MS
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
956d6950 4224 expression (tree) is expanded that is within a conditional context. */
e976b8b2
MS
4225
4226void
956d6950 4227start_cleanup_deferral ()
e976b8b2 4228{
e3eef942
JW
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;
e976b8b2
MS
4233}
4234
4235/* Mark the end of a conditional region of code. Because cleanup
956d6950 4236 deferrals may be nested, we may still be in a conditional region
e976b8b2
MS
4237 after we end the currently deferred cleanups, only after we end all
4238 deferred cleanups, are we back in unconditional code. */
4239
4240void
956d6950 4241end_cleanup_deferral ()
e976b8b2 4242{
e3eef942
JW
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;
e976b8b2
MS
4247}
4248
28d81abb
RK
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
4255void
4256move_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
4267tree
4268last_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
4280int
4281any_pending_cleanups (this_contour)
4282 int this_contour;
4283{
4284 struct nesting *block;
4285
01d939e8 4286 if (cfun == NULL || cfun->stmt == NULL || block_stack == 0)
28d81abb
RK
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
e976b8b2 4292 && block_stack->data.block.outer_cleanups == 0)
28d81abb
RK
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
4314void
4315expand_start_case (exit_flag, expr, type, printname)
4316 int exit_flag;
4317 tree expr;
4318 tree type;
dff01034 4319 const char *printname;
28d81abb
RK
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;
28d81abb 4333 thiscase->data.case_stmt.printname = printname;
a11759a3 4334 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
28d81abb
RK
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)
6496a589 4343 emit_note (NULL, NOTE_INSN_DELETED);
28d81abb
RK
4344
4345 thiscase->data.case_stmt.start = get_last_insn ();
4c581243 4346
956d6950 4347 start_cleanup_deferral ();
28d81abb
RK
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
4355void
4356expand_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;
28d81abb
RK
4370 case_stack = thiscase;
4371 nesting_stack = thiscase;
956d6950 4372 start_cleanup_deferral ();
28d81abb
RK
4373}
4374
4375/* End a dummy case statement. */
4376
4377void
4378expand_end_case_dummy ()
4379{
956d6950 4380 end_cleanup_deferral ();
28d81abb
RK
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
4387tree
4388case_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
a11759a3
JR
4395static void
4396check_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);
0dacbd0e
JW
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)
4381f7c2
KH
4422 warning_with_file_and_line (NOTE_SOURCE_FILE (insn),
4423 NOTE_LINE_NUMBER (insn),
0dacbd0e
JW
4424 "unreachable code at beginning of %s",
4425 case_stack->data.case_stmt.printname);
a11759a3
JR
4426 break;
4427 }
4428 }
4429 }
4430}
4431
28d81abb
RK
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).
f52fba84
PE
4434 The function CONVERTER, when applied to arguments T and V,
4435 converts the value V to the type T.
28d81abb
RK
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.
e976b8b2 4442 If we are jumping into the scope of a cleanup or var-sized array, return 5.
28d81abb
RK
4443 Return 0 on success.
4444
4445 Extended to handle range statements. */
4446
4447int
f52fba84 4448pushcase (value, converter, label, duplicate)
28d81abb 4449 register tree value;
cdadb1dd 4450 tree (*converter) PARAMS ((tree, tree));
28d81abb
RK
4451 register tree label;
4452 tree *duplicate;
4453{
28d81abb
RK
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
2f985ca6
JW
4472 /* Convert VALUE to the type in which the comparisons are nominally done. */
4473 if (value != 0)
4474 value = (*converter) (nominal_type, value);
4475
feb60352
R
4476 check_seenlabel ();
4477
28d81abb
RK
4478 /* Fail if this value is out of range for the actual type of the index
4479 (which may be narrower than NOMINAL_TYPE). */
14a774a9
RK
4480 if (value != 0
4481 && (TREE_CONSTANT_OVERFLOW (value)
4482 || ! int_fits_type_p (value, index_type)))
28d81abb
RK
4483 return 3;
4484
56cb9733 4485 return add_case_node (value, value, label, duplicate);
28d81abb
RK
4486}
4487
956d6950
JL
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. */
28d81abb
RK
4496
4497int
f52fba84 4498pushcase_range (value1, value2, converter, label, duplicate)
28d81abb 4499 register tree value1, value2;
cdadb1dd 4500 tree (*converter) PARAMS ((tree, tree));
28d81abb
RK
4501 register tree label;
4502 tree *duplicate;
4503{
28d81abb
RK
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
a11759a3 4522 check_seenlabel ();
28d81abb 4523
956d6950
JL
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)
1974bfb1 4527 value1 = TYPE_MIN_VALUE (index_type);
956d6950 4528 if (value2 == 0)
1974bfb1 4529 value2 = TYPE_MAX_VALUE (index_type);
956d6950
JL
4530
4531 /* Fail if the range is empty. Do this before any conversion since
4532 we want to allow out-of-range empty ranges. */
14a774a9 4533 if (value2 != 0 && tree_int_cst_lt (value2, value1))
956d6950
JL
4534 return 4;
4535
4381f7c2 4536 /* If the max was unbounded, use the max of the nominal_type we are
e1ee5cdc
RH
4537 converting to. Do this after the < check above to suppress false
4538 positives. */
14a774a9 4539 if (value2 == 0)
e1ee5cdc 4540 value2 = TYPE_MAX_VALUE (nominal_type);
28d81abb 4541
2f985ca6
JW
4542 value1 = (*converter) (nominal_type, value1);
4543 value2 = (*converter) (nominal_type, value2);
4544
28d81abb 4545 /* Fail if these values are out of range. */
956d6950
JL
4546 if (TREE_CONSTANT_OVERFLOW (value1)
4547 || ! int_fits_type_p (value1, index_type))
28d81abb
RK
4548 return 3;
4549
956d6950
JL
4550 if (TREE_CONSTANT_OVERFLOW (value2)
4551 || ! int_fits_type_p (value2, index_type))
28d81abb
RK
4552 return 3;
4553
57641239
RK
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
56cb9733 4561int
57641239
RK
4562add_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
56cb9733
MM
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
57641239
RK
4588 q = &case_stack->data.case_stmt.case_list;
4589 p = *q;
4590
69d4ca36 4591 while ((r = *q))
28d81abb 4592 {
57641239
RK
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
28d81abb 4604 {
57641239
RK
4605 /* We have an overlap; this is an error. */
4606 *duplicate = p->code_label;
28d81abb
RK
4607 return 2;
4608 }
4609 }
4610
1da68f56 4611 /* Add this label to the chain, and succeed. */
28d81abb 4612
1f8f4a0b 4613 r = (struct case_node *) xmalloc (sizeof (struct case_node));
1da68f56 4614 r->low = low;
28d81abb 4615
57641239 4616 /* If the bounds are equal, turn this into the one-value case. */
57641239
RK
4617 if (tree_int_cst_equal (low, high))
4618 r->high = r->low;
4619 else
1da68f56 4620 r->high = high;
57641239
RK
4621
4622 r->code_label = label;
28d81abb
RK
4623 expand_label (label);
4624
57641239
RK
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 */
51723711 4647 if ((p->left = s = r->right))
57641239
RK
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
51723711 4656 if ((r->parent = s))
57641239
RK
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 {
5720c7e7
RK
4669 /* LR-Rotation */
4670
57641239
RK
4671 int b2;
4672 struct case_node *t = r->right;
4673
51723711 4674 if ((p->left = s = t->right))
57641239
RK
4675 s->parent = p;
4676
4677 t->right = p;
51723711 4678 if ((r->right = s = t->left))
57641239
RK
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
51723711 4692 if ((t->parent = s))
57641239
RK
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
51723711 4726 if ((p->right = s = r->left))
57641239
RK
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;
51723711 4734 if ((r->parent = s))
57641239
RK
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
51723711 4753 if ((p->right = s = t->left))
57641239
RK
4754 s->parent = p;
4755
4756 t->left = p;
4757
51723711 4758 if ((r->left = s = t->right))
57641239
RK
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
51723711 4772 if ((t->parent = s))
57641239
RK
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 }
28d81abb
RK
4796
4797 return 0;
4798}
4799\f
94d6511c 4800/* Returns the number of possible values of TYPE.
770ae6cc
RK
4801 Returns -1 if the number is unknown, variable, or if the number does not
4802 fit in a HOST_WIDE_INT.
94d6511c
PB
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
4808HOST_WIDE_INT
4809all_cases_count (type, spareness)
4810 tree type;
4811 int *spareness;
4812{
770ae6cc
RK
4813 tree t;
4814 HOST_WIDE_INT count, minval, lastval;
4815
94d6511c
PB
4816 *spareness = 0;
4817
4818 switch (TREE_CODE (type))
4819 {
94d6511c
PB
4820 case BOOLEAN_TYPE:
4821 count = 2;
4822 break;
770ae6cc 4823
94d6511c
PB
4824 case CHAR_TYPE:
4825 count = 1 << BITS_PER_UNIT;
4826 break;
770ae6cc 4827
94d6511c
PB
4828 default:
4829 case INTEGER_TYPE:
770ae6cc
RK
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);
94d6511c 4837 else
770ae6cc 4838 return -1;
94d6511c 4839 break;
770ae6cc 4840
94d6511c 4841 case ENUMERAL_TYPE:
770ae6cc
RK
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);
94d6511c 4849 count = 0;
770ae6cc 4850
94d6511c
PB
4851 for (t = TYPE_VALUES (type); t != NULL_TREE; t = TREE_CHAIN (t))
4852 {
770ae6cc
RK
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)
94d6511c 4858 *spareness = 1;
770ae6cc 4859
94d6511c
PB
4860 count++;
4861 }
94d6511c 4862 }
770ae6cc 4863
94d6511c
PB
4864 return count;
4865}
4866
94d6511c 4867#define BITARRAY_TEST(ARRAY, INDEX) \
0f41302f
MS
4868 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4869 & (1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR)))
94d6511c 4870#define BITARRAY_SET(ARRAY, INDEX) \
0f41302f
MS
4871 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4872 |= 1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR))
94d6511c
PB
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
9faa82d8 4879 The time needed is proportional to COUNT, unless
94d6511c
PB
4880 SPARSENESS is 2, in which case quadratic time is needed. */
4881
df03cab5 4882void
94d6511c
PB
4883mark_seen_cases (type, cases_seen, count, sparseness)
4884 tree type;
4885 unsigned char *cases_seen;
770ae6cc 4886 HOST_WIDE_INT count;
94d6511c
PB
4887 int sparseness;
4888{
94d6511c 4889 tree next_node_to_try = NULL_TREE;
770ae6cc 4890 HOST_WIDE_INT next_node_offset = 0;
94d6511c 4891
5720c7e7 4892 register struct case_node *n, *root = case_stack->data.case_stmt.case_list;
94d6511c 4893 tree val = make_node (INTEGER_CST);
770ae6cc 4894
94d6511c 4895 TREE_TYPE (val) = type;
5720c7e7 4896 if (! root)
4381f7c2
KH
4897 /* Do nothing. */
4898 ;
5720c7e7 4899 else if (sparseness == 2)
94d6511c 4900 {
5720c7e7 4901 tree t;
770ae6cc 4902 unsigned HOST_WIDE_INT xlo;
5720c7e7
RK
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);
4381f7c2 4908 for (t = TYPE_VALUES (type), xlo = 0; t != NULL_TREE;
5720c7e7 4909 t = TREE_CHAIN (t), xlo++)
94d6511c 4910 {
5720c7e7
RK
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
94d6511c 4915 {
5720c7e7
RK
4916 /* Keep going past elements distinctly greater than VAL. */
4917 if (tree_int_cst_lt (val, n->low))
4918 n = n->left;
4381f7c2 4919
5720c7e7
RK
4920 /* or distinctly less than VAL. */
4921 else if (tree_int_cst_lt (n->high, val))
4922 n = n->right;
4381f7c2 4923
5720c7e7 4924 else
94d6511c 4925 {
5720c7e7
RK
4926 /* We have found a matching range. */
4927 BITARRAY_SET (cases_seen, xlo);
4928 break;
94d6511c
PB
4929 }
4930 }
5720c7e7
RK
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);
770ae6cc 4938
5720c7e7
RK
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);
4381f7c2 4943 while (! tree_int_cst_lt (n->high, val))
94d6511c 4944 {
5720c7e7
RK
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
770ae6cc
RK
4949 unsigned HOST_WIDE_INT xlo;
4950 HOST_WIDE_INT xhi;
5720c7e7 4951 tree t;
770ae6cc 4952
94d6511c
PB
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)
5720c7e7
RK
4976 {
4977 xlo = -1;
4978 break;
4979 }
94d6511c
PB
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 }
4381f7c2 4994
770ae6cc 4995 if (xhi == 0 && xlo < (unsigned HOST_WIDE_INT) count)
94d6511c 4996 BITARRAY_SET (cases_seen, xlo);
770ae6cc 4997
5720c7e7
RK
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));
94d6511c 5001 }
94d6511c
PB
5002 }
5003 }
5004}
5005
28d81abb
RK
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
5017void
5018check_for_full_enumeration_handling (type)
5019 tree type;
5020{
5021 register struct case_node *n;
28d81abb 5022 register tree chain;
28d81abb 5023
0f41302f 5024 /* True iff the selector type is a numbered set mode. */
94d6511c
PB
5025 int sparseness = 0;
5026
0f41302f 5027 /* The number of possible selector values. */
94d6511c
PB
5028 HOST_WIDE_INT size;
5029
5030 /* For each possible selector value. a one iff it has been matched
0f41302f 5031 by a case value alternative. */
94d6511c
PB
5032 unsigned char *cases_seen;
5033
0f41302f 5034 /* The allocated size of cases_seen, in chars. */
770ae6cc 5035 HOST_WIDE_INT bytes_needed;
94d6511c 5036
94d6511c
PB
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;
28d81abb 5042
94d6511c 5043 if (size > 0 && size < 600000
c5c76735 5044 /* We deliberately use calloc here, not cmalloc, so that we can suppress
4381f7c2 5045 this optimization if we don't have enough memory rather than
c5c76735 5046 aborting, as xmalloc would do. */
f4ce9d90
KG
5047 && (cases_seen =
5048 (unsigned char *) really_call_calloc (bytes_needed, 1)) != NULL)
28d81abb 5049 {
770ae6cc 5050 HOST_WIDE_INT i;
94d6511c 5051 tree v = TYPE_VALUES (type);
28d81abb 5052
94d6511c
PB
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
0f41302f 5056 increase monotonically, O(N*log(N)) time may be needed. */
94d6511c
PB
5057
5058 mark_seen_cases (type, cases_seen, size, sparseness);
5059
770ae6cc 5060 for (i = 0; v != NULL_TREE && i < size; i++, v = TREE_CHAIN (v))
4381f7c2 5061 if (BITARRAY_TEST (cases_seen, i) == 0)
770ae6cc
RK
5062 warning ("enumeration value `%s' not handled in switch",
5063 IDENTIFIER_POINTER (TREE_PURPOSE (v)));
94d6511c
PB
5064
5065 free (cases_seen);
28d81abb
RK
5066 }
5067
5068 /* Now we go the other way around; we warn if there are case
ac2a9454 5069 expressions that don't correspond to enumerators. This can
28d81abb 5070 occur since C and C++ don't enforce type-checking of
0f41302f 5071 assignments to enumeration variables. */
28d81abb 5072
5720c7e7
RK
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);
28d81abb
RK
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)
3b24f55b
RS
5086 {
5087 if (TYPE_NAME (type) == 0)
e016950d
KG
5088 warning ("case value `%ld' not in enumerated type",
5089 (long) TREE_INT_CST_LOW (n->low));
3b24f55b 5090 else
e016950d
KG
5091 warning ("case value `%ld' not in enumerated type `%s'",
5092 (long) TREE_INT_CST_LOW (n->low),
3b24f55b
RS
5093 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5094 == IDENTIFIER_NODE)
5095 ? TYPE_NAME (type)
5096 : DECL_NAME (TYPE_NAME (type))));
5097 }
1ddde1cd
RS
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)
3b24f55b
RS
5106 {
5107 if (TYPE_NAME (type) == 0)
e016950d
KG
5108 warning ("case value `%ld' not in enumerated type",
5109 (long) TREE_INT_CST_LOW (n->high));
3b24f55b 5110 else
e016950d
KG
5111 warning ("case value `%ld' not in enumerated type `%s'",
5112 (long) TREE_INT_CST_LOW (n->high),
3b24f55b
RS
5113 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5114 == IDENTIFIER_NODE)
5115 ? TYPE_NAME (type)
5116 : DECL_NAME (TYPE_NAME (type))));
5117 }
1ddde1cd 5118 }
28d81abb 5119 }
28d81abb 5120}
ca695ac9 5121
1f8f4a0b
MM
5122/* Free CN, and its children. */
5123
5124static void
5125free_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
28d81abb 5136\f
ad82abb8 5137
28d81abb 5138/* Terminate a case (Pascal) or switch (C) statement
9ab0ddd7 5139 in which ORIG_INDEX is the expression to be tested.
28d81abb
RK
5140 Generate the code to test it and jump to the right place. */
5141
5142void
5143expand_end_case (orig_index)
5144 tree orig_index;
5145{
95d75019 5146 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE, orig_minval;
28d81abb
RK
5147 rtx default_label = 0;
5148 register struct case_node *n;
85066503 5149 unsigned int count;
28d81abb 5150 rtx index;
ca695ac9 5151 rtx table_label;
28d81abb
RK
5152 int ncases;
5153 rtx *labelvec;
5154 register int i;
5155 rtx before_case;
5156 register struct nesting *thiscase = case_stack;
1b0cb6fc 5157 tree index_expr, index_type;
ca695ac9
JB
5158 int unsignedp;
5159
03c03770
AS
5160 /* Don't crash due to previous errors. */
5161 if (thiscase == NULL)
5162 return;
5163
ca695ac9
JB
5164 table_label = gen_label_rtx ();
5165 index_expr = thiscase->data.case_stmt.index_expr;
1b0cb6fc
RK
5166 index_type = TREE_TYPE (index_expr);
5167 unsignedp = TREE_UNSIGNED (index_type);
28d81abb
RK
5168
5169 do_pending_stack_adjust ();
5170
feb60352
R
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. */
a11759a3
JR
5178 check_seenlabel ();
5179
28d81abb 5180 /* An ERROR_MARK occurs for various reasons including invalid data type. */
1b0cb6fc 5181 if (index_type != error_mark_node)
28d81abb
RK
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
28d81abb
RK
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
5720c7e7
RK
5204 if (thiscase->data.case_stmt.case_list
5205 && thiscase->data.case_stmt.case_list->left)
b059139c 5206 thiscase->data.case_stmt.case_list
4381f7c2 5207 = case_tree2list (thiscase->data.case_stmt.case_list, 0);
b059139c 5208
28d81abb
RK
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
1b0cb6fc
RK
5224 n->low = convert (index_type, n->low);
5225 n->high = convert (index_type, n->high);
28d81abb
RK
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
3474db0e
RS
5246 orig_minval = minval;
5247
28d81abb
RK
5248 /* Compute span of values. */
5249 if (count != 0)
1b0cb6fc 5250 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
28d81abb 5251
956d6950 5252 end_cleanup_deferral ();
4c581243 5253
1b0cb6fc 5254 if (count == 0)
28d81abb
RK
5255 {
5256 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
5257 emit_queue ();
5258 emit_jump (default_label);
5259 }
3474db0e 5260
28d81abb
RK
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. */
4f73c5dd 5265
ad82abb8 5266 else if (count < case_values_threshold ()
05bccae2 5267 || compare_tree_int (range, 10 * count) > 0
f0c988c8
BS
5268 /* RANGE may be signed, and really large ranges will show up
5269 as negative numbers. */
5270 || compare_tree_int (range, 0) < 0
3f6fe18e
RK
5271#ifndef ASM_OUTPUT_ADDR_DIFF_ELT
5272 || flag_pic
5273#endif
28d81abb 5274 || TREE_CODE (index_expr) == INTEGER_CST
b4ac57ab
RS
5275 || (TREE_CODE (index_expr) == COMPOUND_EXPR
5276 && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
28d81abb 5277 {
37366632 5278 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
28d81abb
RK
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
ef89d648 5286 && ! have_insn_for (COMPARE, GET_MODE (index)))
28d81abb
RK
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))
ef89d648 5291 if (have_insn_for (COMPARE, wider_mode))
28d81abb
RK
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),
e9a042b6 5313 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
1b0cb6fc 5314 index_expr = convert (index_type, index_expr);
28d81abb
RK
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. */
1b0cb6fc
RK
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
28d81abb
RK
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
28d81abb 5350 && estimate_case_costs (thiscase->data.case_stmt.case_list));
9714cf43 5351 balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
28d81abb 5352 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
1b0cb6fc 5353 default_label, index_type);
28d81abb
RK
5354 emit_jump_if_reachable (default_label);
5355 }
5356 }
5357 else
5358 {
ad82abb8
ZW
5359 if (! try_casesi (index_type, index_expr, minval, range,
5360 table_label, default_label))
28d81abb 5361 {
ecc9dd93 5362 index_type = thiscase->data.case_stmt.nominal_type;
ad82abb8
ZW
5363 if (! try_tablejump (index_type, index_expr, minval, range,
5364 table_label, default_label))
5365 abort ();
28d81abb 5366 }
ad82abb8 5367
28d81abb
RK
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));
961192e1 5372 memset ((char *) labelvec, 0, ncases * sizeof (rtx));
28d81abb
RK
5373
5374 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5375 {
37366632 5376 register HOST_WIDE_INT i
3474db0e 5377 = TREE_INT_CST_LOW (n->low) - TREE_INT_CST_LOW (orig_minval);
28d81abb
RK
5378
5379 while (1)
5380 {
5381 labelvec[i]
38a448ca 5382 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
3474db0e 5383 if (i + TREE_INT_CST_LOW (orig_minval)
28d81abb
RK
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)
38a448ca 5393 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
28d81abb
RK
5394
5395 /* Output the table */
5396 emit_label (table_label);
5397
18543a22 5398 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
38a448ca
RH
5399 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
5400 gen_rtx_LABEL_REF (Pmode, table_label),
33f7f353 5401 gen_rtvec_v (ncases, labelvec),
4381f7c2 5402 const0_rtx, const0_rtx));
28d81abb 5403 else
38a448ca
RH
5404 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
5405 gen_rtvec_v (ncases, labelvec)));
28d81abb
RK
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
915f619f
JW
5417 before_case = squeeze_notes (NEXT_INSN (before_case), get_last_insn ());
5418 reorder_insns (before_case, get_last_insn (),
28d81abb
RK
5419 thiscase->data.case_stmt.start);
5420 }
4c581243 5421 else
956d6950 5422 end_cleanup_deferral ();
1b0cb6fc 5423
28d81abb
RK
5424 if (thiscase->exit_label)
5425 emit_label (thiscase->exit_label);
5426
1f8f4a0b 5427 free_case_nodes (case_stack->data.case_stmt.case_list);
28d81abb
RK
5428 POPSTACK (case_stack);
5429
5430 free_temp_slots ();
5431}
5432
57641239
RK
5433/* Convert the tree NODE into a list linked by the right field, with the left
5434 field zeroed. RIGHT is used for recursion; it is a list to be placed
5435 rightmost in the resulting list. */
5436
5437static struct case_node *
5438case_tree2list (node, right)
5439 struct case_node *node, *right;
5440{
5441 struct case_node *left;
5442
5443 if (node->right)
5444 right = case_tree2list (node->right, right);
5445
5446 node->right = right;
51723711 5447 if ((left = node->left))
57641239
RK
5448 {
5449 node->left = 0;
5450 return case_tree2list (left, node);
5451 }
5452
5453 return node;
5454}
ca695ac9 5455
28d81abb
RK
5456/* Generate code to jump to LABEL if OP1 and OP2 are equal. */
5457
5458static void
5459do_jump_if_equal (op1, op2, label, unsignedp)
5460 rtx op1, op2, label;
5461 int unsignedp;
5462{
5463 if (GET_CODE (op1) == CONST_INT
5464 && GET_CODE (op2) == CONST_INT)
5465 {
5466 if (INTVAL (op1) == INTVAL (op2))
5467 emit_jump (label);
5468 }
5469 else
5470 {
5471 enum machine_mode mode = GET_MODE (op1);
5472 if (mode == VOIDmode)
5473 mode = GET_MODE (op2);
c5d5d461
JL
5474 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX, mode, unsignedp,
5475 0, label);
28d81abb
RK
5476 }
5477}
5478\f
5479/* Not all case values are encountered equally. This function
5480 uses a heuristic to weight case labels, in cases where that
5481 looks like a reasonable thing to do.
5482
5483 Right now, all we try to guess is text, and we establish the
5484 following weights:
5485
5486 chars above space: 16
5487 digits: 16
5488 default: 12
5489 space, punct: 8
5490 tab: 4
5491 newline: 2
5492 other "\" chars: 1
5493 remaining chars: 0
5494
5495 If we find any cases in the switch that are not either -1 or in the range
5496 of valid ASCII characters, or are control characters other than those
5497 commonly used with "\", don't treat this switch scanning text.
5498
5499 Return 1 if these nodes are suitable for cost estimation, otherwise
5500 return 0. */
5501
5502static int
5503estimate_case_costs (node)
5504 case_node_ptr node;
5505{
f2d1f0ba 5506 tree min_ascii = integer_minus_one_node;
28d81abb
RK
5507 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5508 case_node_ptr n;
5509 int i;
5510
5511 /* If we haven't already made the cost table, make it now. Note that the
5512 lower bound of the table is -1, not zero. */
5513
2a2137c4 5514 if (! cost_table_initialized)
28d81abb 5515 {
2a2137c4 5516 cost_table_initialized = 1;
28d81abb
RK
5517
5518 for (i = 0; i < 128; i++)
5519 {
e9a780ec 5520 if (ISALNUM (i))
2a2137c4 5521 COST_TABLE (i) = 16;
e9a780ec 5522 else if (ISPUNCT (i))
2a2137c4 5523 COST_TABLE (i) = 8;
e9a780ec 5524 else if (ISCNTRL (i))
2a2137c4 5525 COST_TABLE (i) = -1;
28d81abb
RK
5526 }
5527
2a2137c4
RH
5528 COST_TABLE (' ') = 8;
5529 COST_TABLE ('\t') = 4;
5530 COST_TABLE ('\0') = 4;
5531 COST_TABLE ('\n') = 2;
5532 COST_TABLE ('\f') = 1;
5533 COST_TABLE ('\v') = 1;
5534 COST_TABLE ('\b') = 1;
28d81abb
RK
5535 }
5536
5537 /* See if all the case expressions look like text. It is text if the
5538 constant is >= -1 and the highest constant is <= 127. Do all comparisons
5539 as signed arithmetic since we don't want to ever access cost_table with a
5540 value less than -1. Also check that none of the constants in a range
5541 are strange control characters. */
5542
5543 for (n = node; n; n = n->right)
5544 {
5545 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5546 return 0;
5547
05bccae2
RK
5548 for (i = (HOST_WIDE_INT) TREE_INT_CST_LOW (n->low);
5549 i <= (HOST_WIDE_INT) TREE_INT_CST_LOW (n->high); i++)
2a2137c4 5550 if (COST_TABLE (i) < 0)
28d81abb
RK
5551 return 0;
5552 }
5553
5554 /* All interesting values are within the range of interesting
5555 ASCII characters. */
5556 return 1;
5557}
5558
5559/* Scan an ordered list of case nodes
5560 combining those with consecutive values or ranges.
5561
5562 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
5563
5564static void
5565group_case_nodes (head)
5566 case_node_ptr head;
5567{
5568 case_node_ptr node = head;
5569
5570 while (node)
5571 {
5572 rtx lb = next_real_insn (label_rtx (node->code_label));
ad7e369f 5573 rtx lb2;
28d81abb
RK
5574 case_node_ptr np = node;
5575
5576 /* Try to group the successors of NODE with NODE. */
5577 while (((np = np->right) != 0)
5578 /* Do they jump to the same place? */
ad7e369f
JL
5579 && ((lb2 = next_real_insn (label_rtx (np->code_label))) == lb
5580 || (lb != 0 && lb2 != 0
5581 && simplejump_p (lb)
5582 && simplejump_p (lb2)
5583 && rtx_equal_p (SET_SRC (PATTERN (lb)),
5584 SET_SRC (PATTERN (lb2)))))
28d81abb
RK
5585 /* Are their ranges consecutive? */
5586 && tree_int_cst_equal (np->low,
5587 fold (build (PLUS_EXPR,
5588 TREE_TYPE (node->high),
5589 node->high,
5590 integer_one_node)))
5591 /* An overflow is not consecutive. */
5592 && tree_int_cst_lt (node->high,
5593 fold (build (PLUS_EXPR,
5594 TREE_TYPE (node->high),
5595 node->high,
5596 integer_one_node))))
5597 {
5598 node->high = np->high;
5599 }
5600 /* NP is the first node after NODE which can't be grouped with it.
5601 Delete the nodes in between, and move on to that node. */
5602 node->right = np;
5603 node = np;
5604 }
5605}
5606
5607/* Take an ordered list of case nodes
5608 and transform them into a near optimal binary tree,
6dc42e49 5609 on the assumption that any target code selection value is as
28d81abb
RK
5610 likely as any other.
5611
5612 The transformation is performed by splitting the ordered
5613 list into two equal sections plus a pivot. The parts are
5614 then attached to the pivot as left and right branches. Each
38e01259 5615 branch is then transformed recursively. */
28d81abb
RK
5616
5617static void
5618balance_case_nodes (head, parent)
5619 case_node_ptr *head;
5620 case_node_ptr parent;
5621{
5622 register case_node_ptr np;
5623
5624 np = *head;
5625 if (np)
5626 {
5627 int cost = 0;
5628 int i = 0;
5629 int ranges = 0;
5630 register case_node_ptr *npp;
5631 case_node_ptr left;
5632
5633 /* Count the number of entries on branch. Also count the ranges. */
5634
5635 while (np)
5636 {
5637 if (!tree_int_cst_equal (np->low, np->high))
5638 {
5639 ranges++;
5640 if (use_cost_table)
2a2137c4 5641 cost += COST_TABLE (TREE_INT_CST_LOW (np->high));
28d81abb
RK
5642 }
5643
5644 if (use_cost_table)
2a2137c4 5645 cost += COST_TABLE (TREE_INT_CST_LOW (np->low));
28d81abb
RK
5646
5647 i++;
5648 np = np->right;
5649 }
5650
5651 if (i > 2)
5652 {
5653 /* Split this list if it is long enough for that to help. */
5654 npp = head;
5655 left = *npp;
5656 if (use_cost_table)
5657 {
5658 /* Find the place in the list that bisects the list's total cost,
5659 Here I gets half the total cost. */
5660 int n_moved = 0;
5661 i = (cost + 1) / 2;
5662 while (1)
5663 {
5664 /* Skip nodes while their cost does not reach that amount. */
5665 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
2a2137c4
RH
5666 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->high));
5667 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->low));
28d81abb
RK
5668 if (i <= 0)
5669 break;
5670 npp = &(*npp)->right;
5671 n_moved += 1;
5672 }
5673 if (n_moved == 0)
5674 {
5675 /* Leave this branch lopsided, but optimize left-hand
5676 side and fill in `parent' fields for right-hand side. */
5677 np = *head;
5678 np->parent = parent;
5679 balance_case_nodes (&np->left, np);
5680 for (; np->right; np = np->right)
5681 np->right->parent = np;
5682 return;
5683 }
5684 }
5685 /* If there are just three nodes, split at the middle one. */
5686 else if (i == 3)
5687 npp = &(*npp)->right;
5688 else
5689 {
5690 /* Find the place in the list that bisects the list's total cost,
5691 where ranges count as 2.
5692 Here I gets half the total cost. */
5693 i = (i + ranges + 1) / 2;
5694 while (1)
5695 {
5696 /* Skip nodes while their cost does not reach that amount. */
5697 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5698 i--;
5699 i--;
5700 if (i <= 0)
5701 break;
5702 npp = &(*npp)->right;
5703 }
5704 }
5705 *head = np = *npp;
5706 *npp = 0;
5707 np->parent = parent;
5708 np->left = left;
5709
5710 /* Optimize each of the two split parts. */
5711 balance_case_nodes (&np->left, np);
5712 balance_case_nodes (&np->right, np);
5713 }
5714 else
5715 {
5716 /* Else leave this branch as one level,
5717 but fill in `parent' fields. */
5718 np = *head;
5719 np->parent = parent;
5720 for (; np->right; np = np->right)
5721 np->right->parent = np;
5722 }
5723 }
5724}
5725\f
5726/* Search the parent sections of the case node tree
5727 to see if a test for the lower bound of NODE would be redundant.
5728 INDEX_TYPE is the type of the index expression.
5729
5730 The instructions to generate the case decision tree are
5731 output in the same order as nodes are processed so it is
5732 known that if a parent node checks the range of the current
5733 node minus one that the current node is bounded at its lower
5734 span. Thus the test would be redundant. */
5735
5736static int
5737node_has_low_bound (node, index_type)
5738 case_node_ptr node;
5739 tree index_type;
5740{
5741 tree low_minus_one;
5742 case_node_ptr pnode;
5743
5744 /* If the lower bound of this node is the lowest value in the index type,
5745 we need not test it. */
5746
5747 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
5748 return 1;
5749
5750 /* If this node has a left branch, the value at the left must be less
5751 than that at this node, so it cannot be bounded at the bottom and
5752 we need not bother testing any further. */
5753
5754 if (node->left)
5755 return 0;
5756
5757 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
5758 node->low, integer_one_node));
5759
5760 /* If the subtraction above overflowed, we can't verify anything.
5761 Otherwise, look for a parent that tests our value - 1. */
5762
5763 if (! tree_int_cst_lt (low_minus_one, node->low))
5764 return 0;
5765
5766 for (pnode = node->parent; pnode; pnode = pnode->parent)
5767 if (tree_int_cst_equal (low_minus_one, pnode->high))
5768 return 1;
5769
5770 return 0;
5771}
5772
5773/* Search the parent sections of the case node tree
5774 to see if a test for the upper bound of NODE would be redundant.
5775 INDEX_TYPE is the type of the index expression.
5776
5777 The instructions to generate the case decision tree are
5778 output in the same order as nodes are processed so it is
5779 known that if a parent node checks the range of the current
5780 node plus one that the current node is bounded at its upper
5781 span. Thus the test would be redundant. */
5782
5783static int
5784node_has_high_bound (node, index_type)
5785 case_node_ptr node;
5786 tree index_type;
5787{
5788 tree high_plus_one;
5789 case_node_ptr pnode;
5790
e1ee5cdc
RH
5791 /* If there is no upper bound, obviously no test is needed. */
5792
5793 if (TYPE_MAX_VALUE (index_type) == NULL)
5794 return 1;
5795
28d81abb
RK
5796 /* If the upper bound of this node is the highest value in the type
5797 of the index expression, we need not test against it. */
5798
5799 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
5800 return 1;
5801
5802 /* If this node has a right branch, the value at the right must be greater
5803 than that at this node, so it cannot be bounded at the top and
5804 we need not bother testing any further. */
5805
5806 if (node->right)
5807 return 0;
5808
5809 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
5810 node->high, integer_one_node));
5811
5812 /* If the addition above overflowed, we can't verify anything.
5813 Otherwise, look for a parent that tests our value + 1. */
5814
5815 if (! tree_int_cst_lt (node->high, high_plus_one))
5816 return 0;
5817
5818 for (pnode = node->parent; pnode; pnode = pnode->parent)
5819 if (tree_int_cst_equal (high_plus_one, pnode->low))
5820 return 1;
5821
5822 return 0;
5823}
5824
5825/* Search the parent sections of the
5826 case node tree to see if both tests for the upper and lower
5827 bounds of NODE would be redundant. */
5828
5829static int
5830node_is_bounded (node, index_type)
5831 case_node_ptr node;
5832 tree index_type;
5833{
5834 return (node_has_low_bound (node, index_type)
5835 && node_has_high_bound (node, index_type));
5836}
5837
5838/* Emit an unconditional jump to LABEL unless it would be dead code. */
5839
5840static void
5841emit_jump_if_reachable (label)
5842 rtx label;
5843{
5844 if (GET_CODE (get_last_insn ()) != BARRIER)
5845 emit_jump (label);
5846}
5847\f
5848/* Emit step-by-step code to select a case for the value of INDEX.
5849 The thus generated decision tree follows the form of the
5850 case-node binary tree NODE, whose nodes represent test conditions.
5851 INDEX_TYPE is the type of the index of the switch.
5852
5853 Care is taken to prune redundant tests from the decision tree
5854 by detecting any boundary conditions already checked by
5855 emitted rtx. (See node_has_high_bound, node_has_low_bound
5856 and node_is_bounded, above.)
5857
5858 Where the test conditions can be shown to be redundant we emit
5859 an unconditional jump to the target code. As a further
5860 optimization, the subordinates of a tree node are examined to
5861 check for bounded nodes. In this case conditional and/or
5862 unconditional jumps as a result of the boundary check for the
5863 current node are arranged to target the subordinates associated
38e01259 5864 code for out of bound conditions on the current node.
28d81abb 5865
f72aed24 5866 We can assume that when control reaches the code generated here,
28d81abb
RK
5867 the index value has already been compared with the parents
5868 of this node, and determined to be on the same side of each parent
5869 as this node is. Thus, if this node tests for the value 51,
5870 and a parent tested for 52, we don't need to consider
5871 the possibility of a value greater than 51. If another parent
5872 tests for the value 50, then this node need not test anything. */
5873
5874static void
5875emit_case_nodes (index, node, default_label, index_type)
5876 rtx index;
5877 case_node_ptr node;
5878 rtx default_label;
5879 tree index_type;
5880{
5881 /* If INDEX has an unsigned type, we must make unsigned branches. */
5882 int unsignedp = TREE_UNSIGNED (index_type);
28d81abb 5883 enum machine_mode mode = GET_MODE (index);
69107307 5884 enum machine_mode imode = TYPE_MODE (index_type);
28d81abb
RK
5885
5886 /* See if our parents have already tested everything for us.
5887 If they have, emit an unconditional jump for this node. */
5888 if (node_is_bounded (node, index_type))
5889 emit_jump (label_rtx (node->code_label));
5890
5891 else if (tree_int_cst_equal (node->low, node->high))
5892 {
5893 /* Node is single valued. First see if the index expression matches
0f41302f 5894 this node and then check our children, if any. */
28d81abb 5895
69107307
AO
5896 do_jump_if_equal (index,
5897 convert_modes (mode, imode,
5898 expand_expr (node->low, NULL_RTX,
5899 VOIDmode, 0),
5900 unsignedp),
28d81abb
RK
5901 label_rtx (node->code_label), unsignedp);
5902
5903 if (node->right != 0 && node->left != 0)
5904 {
5905 /* This node has children on both sides.
5906 Dispatch to one side or the other
5907 by comparing the index value with this node's value.
5908 If one subtree is bounded, check that one first,
5909 so we can avoid real branches in the tree. */
5910
5911 if (node_is_bounded (node->right, index_type))
5912 {
4381f7c2 5913 emit_cmp_and_jump_insns (index,
69107307
AO
5914 convert_modes
5915 (mode, imode,
5916 expand_expr (node->high, NULL_RTX,
5917 VOIDmode, 0),
5918 unsignedp),
4381f7c2
KH
5919 GT, NULL_RTX, mode, unsignedp, 0,
5920 label_rtx (node->right->code_label));
28d81abb
RK
5921 emit_case_nodes (index, node->left, default_label, index_type);
5922 }
5923
5924 else if (node_is_bounded (node->left, index_type))
5925 {
4381f7c2 5926 emit_cmp_and_jump_insns (index,
69107307
AO
5927 convert_modes
5928 (mode, imode,
5929 expand_expr (node->high, NULL_RTX,
5930 VOIDmode, 0),
5931 unsignedp),
c5d5d461
JL
5932 LT, NULL_RTX, mode, unsignedp, 0,
5933 label_rtx (node->left->code_label));
28d81abb
RK
5934 emit_case_nodes (index, node->right, default_label, index_type);
5935 }
5936
5937 else
5938 {
5939 /* Neither node is bounded. First distinguish the two sides;
5940 then emit the code for one side at a time. */
5941
4381f7c2 5942 tree test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
28d81abb
RK
5943
5944 /* See if the value is on the right. */
4381f7c2 5945 emit_cmp_and_jump_insns (index,
69107307
AO
5946 convert_modes
5947 (mode, imode,
5948 expand_expr (node->high, NULL_RTX,
5949 VOIDmode, 0),
5950 unsignedp),
c5d5d461
JL
5951 GT, NULL_RTX, mode, unsignedp, 0,
5952 label_rtx (test_label));
28d81abb
RK
5953
5954 /* Value must be on the left.
5955 Handle the left-hand subtree. */
5956 emit_case_nodes (index, node->left, default_label, index_type);
5957 /* If left-hand subtree does nothing,
5958 go to default. */
5959 emit_jump_if_reachable (default_label);
5960
5961 /* Code branches here for the right-hand subtree. */
5962 expand_label (test_label);
5963 emit_case_nodes (index, node->right, default_label, index_type);
5964 }
5965 }
5966
5967 else if (node->right != 0 && node->left == 0)
5968 {
5969 /* Here we have a right child but no left so we issue conditional
5970 branch to default and process the right child.
5971
5972 Omit the conditional branch to default if we it avoid only one
5973 right child; it costs too much space to save so little time. */
5974
de14fd73 5975 if (node->right->right || node->right->left
28d81abb
RK
5976 || !tree_int_cst_equal (node->right->low, node->right->high))
5977 {
5978 if (!node_has_low_bound (node, index_type))
5979 {
4381f7c2 5980 emit_cmp_and_jump_insns (index,
69107307
AO
5981 convert_modes
5982 (mode, imode,
5983 expand_expr (node->high, NULL_RTX,
5984 VOIDmode, 0),
5985 unsignedp),
c5d5d461
JL
5986 LT, NULL_RTX, mode, unsignedp, 0,
5987 default_label);
28d81abb
RK
5988 }
5989
5990 emit_case_nodes (index, node->right, default_label, index_type);
5991 }
5992 else
5993 /* We cannot process node->right normally
5994 since we haven't ruled out the numbers less than
5995 this node's value. So handle node->right explicitly. */
5996 do_jump_if_equal (index,
69107307
AO
5997 convert_modes
5998 (mode, imode,
5999 expand_expr (node->right->low, NULL_RTX,
6000 VOIDmode, 0),
6001 unsignedp),
28d81abb
RK
6002 label_rtx (node->right->code_label), unsignedp);
6003 }
6004
6005 else if (node->right == 0 && node->left != 0)
6006 {
6007 /* Just one subtree, on the left. */
4381f7c2 6008 if (node->left->left || node->left->right
28d81abb
RK
6009 || !tree_int_cst_equal (node->left->low, node->left->high))
6010 {
6011 if (!node_has_high_bound (node, index_type))
6012 {
69107307
AO
6013 emit_cmp_and_jump_insns (index,
6014 convert_modes
6015 (mode, imode,
6016 expand_expr (node->high, NULL_RTX,
6017 VOIDmode, 0),
6018 unsignedp),
c5d5d461
JL
6019 GT, NULL_RTX, mode, unsignedp, 0,
6020 default_label);
28d81abb
RK
6021 }
6022
6023 emit_case_nodes (index, node->left, default_label, index_type);
6024 }
6025 else
6026 /* We cannot process node->left normally
6027 since we haven't ruled out the numbers less than
6028 this node's value. So handle node->left explicitly. */
6029 do_jump_if_equal (index,
69107307
AO
6030 convert_modes
6031 (mode, imode,
6032 expand_expr (node->left->low, NULL_RTX,
6033 VOIDmode, 0),
6034 unsignedp),
28d81abb
RK
6035 label_rtx (node->left->code_label), unsignedp);
6036 }
6037 }
6038 else
6039 {
6040 /* Node is a range. These cases are very similar to those for a single
6041 value, except that we do not start by testing whether this node
6042 is the one to branch to. */
6043
6044 if (node->right != 0 && node->left != 0)
6045 {
6046 /* Node has subtrees on both sides.
6047 If the right-hand subtree is bounded,
6048 test for it first, since we can go straight there.
6049 Otherwise, we need to make a branch in the control structure,
6050 then handle the two subtrees. */
6051 tree test_label = 0;
6052
28d81abb
RK
6053 if (node_is_bounded (node->right, index_type))
6054 /* Right hand node is fully bounded so we can eliminate any
6055 testing and branch directly to the target code. */
69107307
AO
6056 emit_cmp_and_jump_insns (index,
6057 convert_modes
6058 (mode, imode,
6059 expand_expr (node->high, NULL_RTX,
6060 VOIDmode, 0),
6061 unsignedp),
c5d5d461
JL
6062 GT, NULL_RTX, mode, unsignedp, 0,
6063 label_rtx (node->right->code_label));
28d81abb
RK
6064 else
6065 {
6066 /* Right hand node requires testing.
6067 Branch to a label where we will handle it later. */
6068
6069 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4381f7c2 6070 emit_cmp_and_jump_insns (index,
69107307
AO
6071 convert_modes
6072 (mode, imode,
6073 expand_expr (node->high, NULL_RTX,
6074 VOIDmode, 0),
6075 unsignedp),
c5d5d461
JL
6076 GT, NULL_RTX, mode, unsignedp, 0,
6077 label_rtx (test_label));
28d81abb
RK
6078 }
6079
6080 /* Value belongs to this node or to the left-hand subtree. */
6081
69107307
AO
6082 emit_cmp_and_jump_insns (index,
6083 convert_modes
6084 (mode, imode,
6085 expand_expr (node->low, NULL_RTX,
6086 VOIDmode, 0),
6087 unsignedp),
c5d5d461
JL
6088 GE, NULL_RTX, mode, unsignedp, 0,
6089 label_rtx (node->code_label));
28d81abb
RK
6090
6091 /* Handle the left-hand subtree. */
6092 emit_case_nodes (index, node->left, default_label, index_type);
6093
6094 /* If right node had to be handled later, do that now. */
6095
6096 if (test_label)
6097 {
6098 /* If the left-hand subtree fell through,
6099 don't let it fall into the right-hand subtree. */
6100 emit_jump_if_reachable (default_label);
6101
6102 expand_label (test_label);
6103 emit_case_nodes (index, node->right, default_label, index_type);
6104 }
6105 }
6106
6107 else if (node->right != 0 && node->left == 0)
6108 {
6109 /* Deal with values to the left of this node,
6110 if they are possible. */
6111 if (!node_has_low_bound (node, index_type))
6112 {
4381f7c2 6113 emit_cmp_and_jump_insns (index,
69107307
AO
6114 convert_modes
6115 (mode, imode,
6116 expand_expr (node->low, NULL_RTX,
6117 VOIDmode, 0),
6118 unsignedp),
c5d5d461
JL
6119 LT, NULL_RTX, mode, unsignedp, 0,
6120 default_label);
28d81abb
RK
6121 }
6122
6123 /* Value belongs to this node or to the right-hand subtree. */
6124
69107307
AO
6125 emit_cmp_and_jump_insns (index,
6126 convert_modes
6127 (mode, imode,
6128 expand_expr (node->high, NULL_RTX,
6129 VOIDmode, 0),
6130 unsignedp),
c5d5d461
JL
6131 LE, NULL_RTX, mode, unsignedp, 0,
6132 label_rtx (node->code_label));
28d81abb
RK
6133
6134 emit_case_nodes (index, node->right, default_label, index_type);
6135 }
6136
6137 else if (node->right == 0 && node->left != 0)
6138 {
6139 /* Deal with values to the right of this node,
6140 if they are possible. */
6141 if (!node_has_high_bound (node, index_type))
6142 {
4381f7c2 6143 emit_cmp_and_jump_insns (index,
69107307
AO
6144 convert_modes
6145 (mode, imode,
6146 expand_expr (node->high, NULL_RTX,
6147 VOIDmode, 0),
6148 unsignedp),
c5d5d461
JL
6149 GT, NULL_RTX, mode, unsignedp, 0,
6150 default_label);
28d81abb
RK
6151 }
6152
6153 /* Value belongs to this node or to the left-hand subtree. */
6154
4381f7c2 6155 emit_cmp_and_jump_insns (index,
69107307
AO
6156 convert_modes
6157 (mode, imode,
6158 expand_expr (node->low, NULL_RTX,
6159 VOIDmode, 0),
6160 unsignedp),
c5d5d461
JL
6161 GE, NULL_RTX, mode, unsignedp, 0,
6162 label_rtx (node->code_label));
28d81abb
RK
6163
6164 emit_case_nodes (index, node->left, default_label, index_type);
6165 }
6166
6167 else
6168 {
6169 /* Node has no children so we check low and high bounds to remove
6170 redundant tests. Only one of the bounds can exist,
6171 since otherwise this node is bounded--a case tested already. */
923cbdc3
JH
6172 int high_bound = node_has_high_bound (node, index_type);
6173 int low_bound = node_has_low_bound (node, index_type);
28d81abb 6174
923cbdc3 6175 if (!high_bound && low_bound)
28d81abb 6176 {
4381f7c2 6177 emit_cmp_and_jump_insns (index,
69107307
AO
6178 convert_modes
6179 (mode, imode,
6180 expand_expr (node->high, NULL_RTX,
6181 VOIDmode, 0),
6182 unsignedp),
c5d5d461
JL
6183 GT, NULL_RTX, mode, unsignedp, 0,
6184 default_label);
28d81abb
RK
6185 }
6186
923cbdc3 6187 else if (!low_bound && high_bound)
28d81abb 6188 {
4381f7c2 6189 emit_cmp_and_jump_insns (index,
69107307
AO
6190 convert_modes
6191 (mode, imode,
6192 expand_expr (node->low, NULL_RTX,
6193 VOIDmode, 0),
6194 unsignedp),
c5d5d461
JL
6195 LT, NULL_RTX, mode, unsignedp, 0,
6196 default_label);
28d81abb 6197 }
923cbdc3
JH
6198 else if (!low_bound && !high_bound)
6199 {
9312aecc
JDA
6200 /* Widen LOW and HIGH to the same width as INDEX. */
6201 tree type = type_for_mode (mode, unsignedp);
6202 tree low = build1 (CONVERT_EXPR, type, node->low);
6203 tree high = build1 (CONVERT_EXPR, type, node->high);
ef89d648 6204 rtx low_rtx, new_index, new_bound;
9312aecc
JDA
6205
6206 /* Instead of doing two branches, emit one unsigned branch for
6207 (index-low) > (high-low). */
ef89d648
ZW
6208 low_rtx = expand_expr (low, NULL_RTX, mode, 0);
6209 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
6210 NULL_RTX, unsignedp,
6211 OPTAB_WIDEN);
9312aecc
JDA
6212 new_bound = expand_expr (fold (build (MINUS_EXPR, type,
6213 high, low)),
6214 NULL_RTX, mode, 0);
923cbdc3 6215
9312aecc
JDA
6216 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
6217 mode, 1, 0, default_label);
923cbdc3 6218 }
28d81abb
RK
6219
6220 emit_jump (label_rtx (node->code_label));
6221 }
6222 }
6223}
This page took 2.509635 seconds and 5 git commands to generate.