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