]> gcc.gnu.org Git - gcc.git/blame - gcc/stmt.c
dominance.c (struct dom_info): Add fake_exit_edge.
[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,
a6dd4094 3 1998, 1999, 2000, 2001, 2002, 2003, 2004 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"
66fd46b6 61#include "regs.h"
28d81abb
RK
62\f
63/* Functions and data structures for expanding case statements. */
64
65/* Case label structure, used to hold info on labels within case
66 statements. We handle "range" labels; for a single-value label
67 as in C, the high and low limits are the same.
68
5720c7e7
RK
69 An AVL tree of case nodes is initially created, and later transformed
70 to a list linked via the RIGHT fields in the nodes. Nodes with
71 higher case values are later in the list.
28d81abb
RK
72
73 Switch statements can be output in one of two forms. A branch table
74 is used if there are more than a few labels and the labels are dense
75 within the range between the smallest and largest case value. If a
76 branch table is used, no further manipulations are done with the case
77 node chain.
78
79 The alternative to the use of a branch table is to generate a series
80 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
81 and PARENT fields to hold a binary tree. Initially the tree is
de14fd73
RK
82 totally unbalanced, with everything on the right. We balance the tree
83 with nodes on the left having lower case values than the parent
28d81abb
RK
84 and nodes on the right having higher values. We then output the tree
85 in order. */
86
e2500fed 87struct case_node GTY(())
28d81abb
RK
88{
89 struct case_node *left; /* Left son in binary tree */
90 struct case_node *right; /* Right son in binary tree; also node chain */
91 struct case_node *parent; /* Parent of node in binary tree */
92 tree low; /* Lowest index value for this label */
93 tree high; /* Highest index value for this label */
94 tree code_label; /* Label to jump to when node matches */
57641239 95 int balance;
28d81abb
RK
96};
97
98typedef struct case_node case_node;
99typedef struct case_node *case_node_ptr;
100
101/* These are used by estimate_case_costs and balance_case_nodes. */
102
103/* This must be a signed type, and non-ANSI compilers lack signed char. */
e7749837 104static short cost_table_[129];
28d81abb 105static int use_cost_table;
2a2137c4
RH
106static int cost_table_initialized;
107
108/* Special care is needed because we allow -1, but TREE_INT_CST_LOW
109 is unsigned. */
cf403648 110#define COST_TABLE(I) cost_table_[(unsigned HOST_WIDE_INT) ((I) + 1)]
28d81abb
RK
111\f
112/* Stack of control and binding constructs we are currently inside.
113
114 These constructs begin when you call `expand_start_WHATEVER'
115 and end when you call `expand_end_WHATEVER'. This stack records
116 info about how the construct began that tells the end-function
117 what to do. It also may provide information about the construct
118 to alter the behavior of other constructs within the body.
119 For example, they may affect the behavior of C `break' and `continue'.
120
121 Each construct gets one `struct nesting' object.
122 All of these objects are chained through the `all' field.
123 `nesting_stack' points to the first object (innermost construct).
124 The position of an entry on `nesting_stack' is in its `depth' field.
125
126 Each type of construct has its own individual stack.
6af8eb57 127 For example, loops have `cond_stack'. Each object points to the
28d81abb
RK
128 next object of the same type through the `next' field.
129
130 Some constructs are visible to `break' exit-statements and others
131 are not. Which constructs are visible depends on the language.
132 Therefore, the data structure allows each construct to be visible
133 or not, according to the args given when the construct is started.
134 The construct is visible if the `exit_label' field is non-null.
135 In that case, the value should be a CODE_LABEL rtx. */
136
e2500fed 137struct nesting GTY(())
28d81abb
RK
138{
139 struct nesting *all;
140 struct nesting *next;
141 int depth;
142 rtx exit_label;
e2500fed
GK
143 enum nesting_desc {
144 COND_NESTING,
e2500fed
GK
145 BLOCK_NESTING,
146 CASE_NESTING
147 } desc;
148 union nesting_u
28d81abb
RK
149 {
150 /* For conds (if-then and if-then-else statements). */
e2500fed 151 struct nesting_cond
28d81abb
RK
152 {
153 /* Label for the end of the if construct.
154 There is none if EXITFLAG was not set
155 and no `else' has been seen yet. */
156 rtx endif_label;
157 /* Label for the end of this alternative.
0f41302f 158 This may be the end of the if or the next else/elseif. */
28d81abb 159 rtx next_label;
e2500fed 160 } GTY ((tag ("COND_NESTING"))) cond;
28d81abb 161 /* For variable binding contours. */
e2500fed 162 struct nesting_block
28d81abb
RK
163 {
164 /* Sequence number of this binding contour within the function,
165 in order of entry. */
166 int block_start_count;
28d81abb
RK
167 /* The NOTE that starts this contour.
168 Used by expand_goto to check whether the destination
169 is within each contour or not. */
170 rtx first_insn;
e976b8b2
MS
171 /* The saved target_temp_slot_level from our outer block.
172 We may reset target_temp_slot_level to be the level of
173 this block, if that is done, target_temp_slot_level
174 reverts to the saved target_temp_slot_level at the very
175 end of the block. */
3f1d071b 176 int block_target_temp_slot_level;
e976b8b2
MS
177 /* True if we are currently emitting insns in an area of
178 output code that is controlled by a conditional
179 expression. This is used by the cleanup handling code to
180 generate conditional cleanup actions. */
181 int conditional_code;
182 /* A place to move the start of the exception region for any
183 of the conditional cleanups, must be at the end or after
184 the start of the last unconditional cleanup, and before any
185 conditional branch points. */
186 rtx last_unconditional_cleanup;
e2500fed 187 } GTY ((tag ("BLOCK_NESTING"))) block;
e8eebd31 188 /* For switch (C) or case (Pascal) statements. */
e2500fed 189 struct nesting_case
28d81abb
RK
190 {
191 /* The insn after which the case dispatch should finally
192 be emitted. Zero for a dummy. */
193 rtx start;
57641239
RK
194 /* A list of case labels; it is first built as an AVL tree.
195 During expand_end_case, this is converted to a list, and may be
196 rearranged into a nearly balanced binary tree. */
28d81abb
RK
197 struct case_node *case_list;
198 /* Label to jump to if no case matches. */
199 tree default_label;
200 /* The expression to be dispatched on. */
201 tree index_expr;
202 /* Type that INDEX_EXPR should be converted to. */
203 tree nominal_type;
28d81abb 204 /* Name of this kind of statement, for warnings. */
dff01034 205 const char *printname;
a11759a3
JR
206 /* Used to save no_line_numbers till we see the first case label.
207 We set this to -1 when we see the first case label in this
208 case statement. */
209 int line_number_status;
e2500fed
GK
210 } GTY ((tag ("CASE_NESTING"))) case_stmt;
211 } GTY ((desc ("%1.desc"))) data;
28d81abb
RK
212};
213
28d81abb
RK
214/* Allocate and return a new `struct nesting'. */
215
703ad42b 216#define ALLOC_NESTING() ggc_alloc (sizeof (struct nesting))
28d81abb 217
6ed1d6c5
RS
218/* Pop the nesting stack element by element until we pop off
219 the element which is at the top of STACK.
220 Update all the other stacks, popping off elements from them
221 as we pop them from nesting_stack. */
28d81abb
RK
222
223#define POPSTACK(STACK) \
6ed1d6c5
RS
224do { struct nesting *target = STACK; \
225 struct nesting *this; \
226 do { this = nesting_stack; \
6ed1d6c5
RS
227 if (cond_stack == this) \
228 cond_stack = cond_stack->next; \
229 if (block_stack == this) \
230 block_stack = block_stack->next; \
6ed1d6c5
RS
231 if (case_stack == this) \
232 case_stack = case_stack->next; \
6ed1d6c5 233 nesting_depth = nesting_stack->depth - 1; \
e2500fed 234 nesting_stack = this->all; } \
6ed1d6c5 235 while (this != target); } while (0)
28d81abb
RK
236\f
237/* In some cases it is impossible to generate code for a forward goto
238 until the label definition is seen. This happens when it may be necessary
239 for the goto to reset the stack pointer: we don't yet know how to do that.
240 So expand_goto puts an entry on this fixup list.
241 Each time a binding contour that resets the stack is exited,
242 we check each fixup.
243 If the target label has now been defined, we can insert the proper code. */
244
e2500fed 245struct goto_fixup GTY(())
28d81abb
RK
246{
247 /* Points to following fixup. */
248 struct goto_fixup *next;
249 /* Points to the insn before the jump insn.
250 If more code must be inserted, it goes after this insn. */
251 rtx before_jump;
252 /* The LABEL_DECL that this jump is jumping to, or 0
253 for break, continue or return. */
254 tree target;
7629c936
RS
255 /* The BLOCK for the place where this goto was found. */
256 tree context;
28d81abb
RK
257 /* The CODE_LABEL rtx that this is jumping to. */
258 rtx target_rtl;
259 /* Number of binding contours started in current function
260 before the label reference. */
261 int block_start_count;
28d81abb 262};
e9a25f70 263
e2500fed 264struct stmt_status GTY(())
3f1d071b
BS
265{
266 /* Chain of all pending binding contours. */
e2500fed 267 struct nesting * x_block_stack;
3f1d071b
BS
268
269 /* If any new stacks are added here, add them to POPSTACKS too. */
270
3f1d071b 271 /* Chain of all pending conditional statements. */
e2500fed 272 struct nesting * x_cond_stack;
3f1d071b 273
3f1d071b 274 /* Chain of all pending case or switch statements. */
e2500fed 275 struct nesting * x_case_stack;
3f1d071b
BS
276
277 /* Separate chain including all of the above,
278 chained through the `all' field. */
e2500fed 279 struct nesting * x_nesting_stack;
3f1d071b
BS
280
281 /* Number of entries on nesting_stack now. */
282 int x_nesting_depth;
283
284 /* Number of binding contours started so far in this function. */
285 int x_block_start_count;
286
c8608cd6
GDR
287 /* Location of last line-number note, whether we actually
288 emitted it or not. */
289 location_t x_emit_locus;
3f1d071b
BS
290
291 struct goto_fixup *x_goto_fixup_chain;
292};
293
01d939e8 294#define block_stack (cfun->stmt->x_block_stack)
01d939e8 295#define cond_stack (cfun->stmt->x_cond_stack)
01d939e8
BS
296#define case_stack (cfun->stmt->x_case_stack)
297#define nesting_stack (cfun->stmt->x_nesting_stack)
298#define nesting_depth (cfun->stmt->x_nesting_depth)
299#define current_block_start_count (cfun->stmt->x_block_start_count)
c8608cd6 300#define emit_locus (cfun->stmt->x_emit_locus)
01d939e8 301#define goto_fixup_chain (cfun->stmt->x_goto_fixup_chain)
e9a25f70 302
272d0bee 303/* Nonzero if we are using EH to handle cleanups. */
6de9cd9a 304int using_eh_for_cleanups_p = 0;
e9a25f70 305
46c5ad27 306static int n_occurrences (int, const char *);
46c5ad27 307static bool decl_conflicts_with_clobbers_p (tree, const HARD_REG_SET);
46c5ad27 308static void expand_nl_goto_receiver (void);
46c5ad27
AJ
309static bool check_operand_nalternatives (tree, tree);
310static bool check_unique_operand_names (tree, tree);
311static char *resolve_operand_name_1 (char *, tree, tree);
ac45df5d 312static void expand_null_return_1 (void);
46c5ad27 313static enum br_predictor return_prediction (rtx);
c988af2b 314static rtx shift_return_value (rtx);
46c5ad27 315static void expand_value_return (rtx);
46c5ad27
AJ
316static void do_jump_if_equal (rtx, rtx, rtx, int);
317static int estimate_case_costs (case_node_ptr);
318static bool same_case_target_p (rtx, rtx);
319static void strip_default_case_nodes (case_node_ptr *, rtx);
320static bool lshift_cheap_p (void);
321static int case_bit_test_cmp (const void *, const void *);
322static void emit_case_bit_tests (tree, tree, tree, tree, case_node_ptr, rtx);
323static void group_case_nodes (case_node_ptr);
324static void balance_case_nodes (case_node_ptr *, case_node_ptr);
325static int node_has_low_bound (case_node_ptr, tree);
326static int node_has_high_bound (case_node_ptr, tree);
327static int node_is_bounded (case_node_ptr, tree);
328static void emit_jump_if_reachable (rtx);
329static void emit_case_nodes (rtx, case_node_ptr, rtx, tree);
330static struct case_node *case_tree2list (case_node *, case_node *);
28d81abb 331\f
e9a25f70 332void
46c5ad27 333using_eh_for_cleanups (void)
e9a25f70
JL
334{
335 using_eh_for_cleanups_p = 1;
336}
337
28d81abb 338void
46c5ad27 339init_stmt_for_function (void)
28d81abb 340{
3a70d621 341 cfun->stmt = ggc_alloc_cleared (sizeof (struct stmt_status));
28d81abb 342}
3f1d071b 343\f
3f1d071b 344/* Record the current file and line. Called from emit_line_note. */
0cea056b 345
28d81abb 346void
0cea056b 347set_file_and_line_for_stmt (location_t location)
3f1d071b 348{
61d84605
MM
349 /* If we're outputting an inline function, and we add a line note,
350 there may be no CFUN->STMT information. So, there's no need to
351 update it. */
352 if (cfun->stmt)
0cea056b 353 emit_locus = location;
28d81abb 354}
3f1d071b 355
28d81abb
RK
356/* Emit a no-op instruction. */
357
358void
46c5ad27 359emit_nop (void)
28d81abb 360{
ca695ac9
JB
361 rtx last_insn;
362
b93a436e
JL
363 last_insn = get_last_insn ();
364 if (!optimize
4b4bf941
JQ
365 && (LABEL_P (last_insn)
366 || (NOTE_P (last_insn)
b93a436e
JL
367 && prev_real_insn (last_insn) == 0)))
368 emit_insn (gen_nop ());
28d81abb
RK
369}
370\f
371/* Return the rtx-label that corresponds to a LABEL_DECL,
372 creating it if necessary. */
373
374rtx
46c5ad27 375label_rtx (tree label)
28d81abb
RK
376{
377 if (TREE_CODE (label) != LABEL_DECL)
378 abort ();
379
19e7881c 380 if (!DECL_RTL_SET_P (label))
6de9cd9a
DN
381 {
382 rtx r = gen_label_rtx ();
383 SET_DECL_RTL (label, r);
384 if (FORCED_LABEL (label) || DECL_NONLOCAL (label))
385 LABEL_PRESERVE_P (r) = 1;
386 }
28d81abb 387
19e7881c 388 return DECL_RTL (label);
28d81abb
RK
389}
390
046e4e36
ZW
391/* As above, but also put it on the forced-reference list of the
392 function that contains it. */
393rtx
46c5ad27 394force_label_rtx (tree label)
046e4e36
ZW
395{
396 rtx ref = label_rtx (label);
397 tree function = decl_function_context (label);
398 struct function *p;
399
400 if (!function)
401 abort ();
402
6de9cd9a 403 if (function != current_function_decl)
046e4e36
ZW
404 p = find_function_data (function);
405 else
406 p = cfun;
407
408 p->expr->x_forced_labels = gen_rtx_EXPR_LIST (VOIDmode, ref,
409 p->expr->x_forced_labels);
410 return ref;
411}
19e7881c 412
28d81abb
RK
413/* Add an unconditional jump to LABEL as the next sequential instruction. */
414
415void
46c5ad27 416emit_jump (rtx label)
28d81abb
RK
417{
418 do_pending_stack_adjust ();
419 emit_jump_insn (gen_jump (label));
420 emit_barrier ();
421}
422
423/* Emit code to jump to the address
424 specified by the pointer expression EXP. */
425
426void
46c5ad27 427expand_computed_goto (tree exp)
28d81abb 428{
b93a436e 429 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
ed9a9db1 430
5ae6cd0d 431 x = convert_memory_address (Pmode, x);
ffa1a1ce 432
eb4e1c01
JH
433 do_pending_stack_adjust ();
434 emit_indirect_jump (x);
28d81abb
RK
435}
436\f
437/* Handle goto statements and the labels that they can go to. */
438
439/* Specify the location in the RTL code of a label LABEL,
440 which is a LABEL_DECL tree node.
441
442 This is used for the kind of label that the user can jump to with a
443 goto statement, and for alternatives of a switch or case statement.
444 RTL labels generated for loops and conditionals don't go through here;
445 they are generated directly at the RTL level, by other functions below.
446
447 Note that this has nothing to do with defining label *names*.
448 Languages vary in how they do that and what that even means. */
449
450void
46c5ad27 451expand_label (tree label)
28d81abb 452{
6de9cd9a 453 rtx label_r = label_rtx (label);
28d81abb
RK
454
455 do_pending_stack_adjust ();
6de9cd9a 456 emit_label (label_r);
28d81abb
RK
457 if (DECL_NAME (label))
458 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
459
6de9cd9a
DN
460 if (DECL_NONLOCAL (label))
461 {
462 expand_nl_goto_receiver ();
463 nonlocal_goto_handler_labels
464 = gen_rtx_EXPR_LIST (VOIDmode, label_r,
465 nonlocal_goto_handler_labels);
466 }
467
468 if (FORCED_LABEL (label))
469 forced_labels = gen_rtx_EXPR_LIST (VOIDmode, label_r, forced_labels);
470
471 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
472 maybe_set_first_label_num (label_r);
28d81abb
RK
473}
474
28d81abb
RK
475/* Generate RTL code for a `goto' statement with target label LABEL.
476 LABEL should be a LABEL_DECL tree node that was or will later be
477 defined with `expand_label'. */
478
479void
46c5ad27 480expand_goto (tree label)
28d81abb 481{
6de9cd9a
DN
482#ifdef ENABLE_CHECKING
483 /* Check for a nonlocal goto to a containing function. Should have
484 gotten translated to __builtin_nonlocal_goto. */
485 tree context = decl_function_context (label);
28d81abb 486 if (context != 0 && context != current_function_decl)
6de9cd9a 487 abort ();
28d81abb 488#endif
4b01bd16 489
ac45df5d 490 emit_jump (label_rtx (label));
28d81abb 491}
2a230e9d
BS
492\f
493/* Return the number of times character C occurs in string S. */
494static int
46c5ad27 495n_occurrences (int c, const char *s)
2a230e9d
BS
496{
497 int n = 0;
498 while (*s)
499 n += (*s++ == c);
500 return n;
501}
28d81abb
RK
502\f
503/* Generate RTL for an asm statement (explicit assembler code).
4c46ea23
EB
504 STRING is a STRING_CST node containing the assembler code text,
505 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
506 insn is volatile; don't optimize it. */
28d81abb
RK
507
508void
46c5ad27 509expand_asm (tree string, int vol)
28d81abb 510{
4c46ea23
EB
511 rtx body;
512
513 if (TREE_CODE (string) == ADDR_EXPR)
514 string = TREE_OPERAND (string, 0);
515
839ee4bc 516 body = gen_rtx_ASM_INPUT (VOIDmode, TREE_STRING_POINTER (string));
4c46ea23
EB
517
518 MEM_VOLATILE_P (body) = vol;
28d81abb 519
4c46ea23 520 emit_insn (body);
28d81abb
RK
521}
522
40b18c0a
MM
523/* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
524 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
525 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
526 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
527 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
528 constraint allows the use of a register operand. And, *IS_INOUT
529 will be true if the operand is read-write, i.e., if it is used as
530 an input as well as an output. If *CONSTRAINT_P is not in
531 canonical form, it will be made canonical. (Note that `+' will be
14b493d6 532 replaced with `=' as part of this process.)
40b18c0a
MM
533
534 Returns TRUE if all went well; FALSE if an error occurred. */
535
536bool
46c5ad27
AJ
537parse_output_constraint (const char **constraint_p, int operand_num,
538 int ninputs, int noutputs, bool *allows_mem,
539 bool *allows_reg, bool *is_inout)
40b18c0a
MM
540{
541 const char *constraint = *constraint_p;
542 const char *p;
543
544 /* Assume the constraint doesn't allow the use of either a register
545 or memory. */
546 *allows_mem = false;
547 *allows_reg = false;
548
549 /* Allow the `=' or `+' to not be at the beginning of the string,
550 since it wasn't explicitly documented that way, and there is a
551 large body of code that puts it last. Swap the character to
552 the front, so as not to uglify any place else. */
553 p = strchr (constraint, '=');
554 if (!p)
555 p = strchr (constraint, '+');
556
557 /* If the string doesn't contain an `=', issue an error
558 message. */
559 if (!p)
560 {
561 error ("output operand constraint lacks `='");
562 return false;
563 }
564
565 /* If the constraint begins with `+', then the operand is both read
566 from and written to. */
567 *is_inout = (*p == '+');
568
40b18c0a
MM
569 /* Canonicalize the output constraint so that it begins with `='. */
570 if (p != constraint || is_inout)
571 {
572 char *buf;
573 size_t c_len = strlen (constraint);
574
575 if (p != constraint)
576 warning ("output constraint `%c' for operand %d is not at the beginning",
577 *p, operand_num);
578
579 /* Make a copy of the constraint. */
580 buf = alloca (c_len + 1);
581 strcpy (buf, constraint);
582 /* Swap the first character and the `=' or `+'. */
583 buf[p - constraint] = buf[0];
584 /* Make sure the first character is an `='. (Until we do this,
585 it might be a `+'.) */
586 buf[0] = '=';
587 /* Replace the constraint with the canonicalized string. */
588 *constraint_p = ggc_alloc_string (buf, c_len);
589 constraint = *constraint_p;
590 }
591
592 /* Loop through the constraint string. */
97488870 593 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
40b18c0a
MM
594 switch (*p)
595 {
596 case '+':
597 case '=':
357351e5 598 error ("operand constraint contains incorrectly positioned '+' or '='");
40b18c0a 599 return false;
786de7eb 600
40b18c0a
MM
601 case '%':
602 if (operand_num + 1 == ninputs + noutputs)
603 {
604 error ("`%%' constraint used with last operand");
605 return false;
606 }
607 break;
608
609 case 'V': case 'm': case 'o':
610 *allows_mem = true;
611 break;
612
613 case '?': case '!': case '*': case '&': case '#':
614 case 'E': case 'F': case 'G': case 'H':
615 case 's': case 'i': case 'n':
616 case 'I': case 'J': case 'K': case 'L': case 'M':
617 case 'N': case 'O': case 'P': case ',':
618 break;
619
620 case '0': case '1': case '2': case '3': case '4':
621 case '5': case '6': case '7': case '8': case '9':
84b72302 622 case '[':
40b18c0a
MM
623 error ("matching constraint not valid in output operand");
624 return false;
625
626 case '<': case '>':
627 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
628 excepting those that expand_call created. So match memory
629 and hope. */
630 *allows_mem = true;
631 break;
632
633 case 'g': case 'X':
634 *allows_reg = true;
635 *allows_mem = true;
636 break;
786de7eb 637
40b18c0a
MM
638 case 'p': case 'r':
639 *allows_reg = true;
640 break;
641
642 default:
643 if (!ISALPHA (*p))
644 break;
97488870 645 if (REG_CLASS_FROM_CONSTRAINT (*p, p) != NO_REGS)
40b18c0a 646 *allows_reg = true;
97488870
R
647#ifdef EXTRA_CONSTRAINT_STR
648 else if (EXTRA_ADDRESS_CONSTRAINT (*p, p))
ccfc6cc8 649 *allows_reg = true;
97488870 650 else if (EXTRA_MEMORY_CONSTRAINT (*p, p))
ccfc6cc8 651 *allows_mem = true;
40b18c0a
MM
652 else
653 {
654 /* Otherwise we can't assume anything about the nature of
655 the constraint except that it isn't purely registers.
656 Treat it like "g" and hope for the best. */
657 *allows_reg = true;
658 *allows_mem = true;
659 }
660#endif
661 break;
662 }
663
664 return true;
665}
666
6be2e1f8
RH
667/* Similar, but for input constraints. */
668
1456deaf 669bool
46c5ad27
AJ
670parse_input_constraint (const char **constraint_p, int input_num,
671 int ninputs, int noutputs, int ninout,
672 const char * const * constraints,
673 bool *allows_mem, bool *allows_reg)
6be2e1f8
RH
674{
675 const char *constraint = *constraint_p;
676 const char *orig_constraint = constraint;
677 size_t c_len = strlen (constraint);
678 size_t j;
f3da0ead 679 bool saw_match = false;
6be2e1f8
RH
680
681 /* Assume the constraint doesn't allow the use of either
682 a register or memory. */
683 *allows_mem = false;
684 *allows_reg = false;
685
686 /* Make sure constraint has neither `=', `+', nor '&'. */
687
97488870 688 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
6be2e1f8
RH
689 switch (constraint[j])
690 {
691 case '+': case '=': case '&':
692 if (constraint == orig_constraint)
693 {
694 error ("input operand constraint contains `%c'", constraint[j]);
695 return false;
696 }
697 break;
698
699 case '%':
700 if (constraint == orig_constraint
701 && input_num + 1 == ninputs - ninout)
702 {
703 error ("`%%' constraint used with last operand");
704 return false;
705 }
706 break;
707
708 case 'V': case 'm': case 'o':
709 *allows_mem = true;
710 break;
711
712 case '<': case '>':
713 case '?': case '!': case '*': case '#':
714 case 'E': case 'F': case 'G': case 'H':
715 case 's': case 'i': case 'n':
716 case 'I': case 'J': case 'K': case 'L': case 'M':
717 case 'N': case 'O': case 'P': case ',':
718 break;
719
720 /* Whether or not a numeric constraint allows a register is
721 decided by the matching constraint, and so there is no need
722 to do anything special with them. We must handle them in
723 the default case, so that we don't unnecessarily force
724 operands to memory. */
725 case '0': case '1': case '2': case '3': case '4':
726 case '5': case '6': case '7': case '8': case '9':
727 {
728 char *end;
729 unsigned long match;
730
f3da0ead
JM
731 saw_match = true;
732
6be2e1f8
RH
733 match = strtoul (constraint + j, &end, 10);
734 if (match >= (unsigned long) noutputs)
735 {
736 error ("matching constraint references invalid operand number");
737 return false;
738 }
739
740 /* Try and find the real constraint for this dup. Only do this
741 if the matching constraint is the only alternative. */
742 if (*end == '\0'
743 && (j == 0 || (j == 1 && constraint[0] == '%')))
744 {
745 constraint = constraints[match];
746 *constraint_p = constraint;
747 c_len = strlen (constraint);
748 j = 0;
97488870
R
749 /* ??? At the end of the loop, we will skip the first part of
750 the matched constraint. This assumes not only that the
751 other constraint is an output constraint, but also that
752 the '=' or '+' come first. */
6be2e1f8
RH
753 break;
754 }
755 else
756 j = end - constraint;
97488870
R
757 /* Anticipate increment at end of loop. */
758 j--;
6be2e1f8
RH
759 }
760 /* Fall through. */
761
762 case 'p': case 'r':
763 *allows_reg = true;
764 break;
765
766 case 'g': case 'X':
767 *allows_reg = true;
768 *allows_mem = true;
769 break;
770
771 default:
772 if (! ISALPHA (constraint[j]))
773 {
774 error ("invalid punctuation `%c' in constraint", constraint[j]);
775 return false;
776 }
97488870
R
777 if (REG_CLASS_FROM_CONSTRAINT (constraint[j], constraint + j)
778 != NO_REGS)
6be2e1f8 779 *allows_reg = true;
97488870
R
780#ifdef EXTRA_CONSTRAINT_STR
781 else if (EXTRA_ADDRESS_CONSTRAINT (constraint[j], constraint + j))
ccfc6cc8 782 *allows_reg = true;
97488870 783 else if (EXTRA_MEMORY_CONSTRAINT (constraint[j], constraint + j))
ccfc6cc8 784 *allows_mem = true;
6be2e1f8
RH
785 else
786 {
787 /* Otherwise we can't assume anything about the nature of
788 the constraint except that it isn't purely registers.
789 Treat it like "g" and hope for the best. */
790 *allows_reg = true;
791 *allows_mem = true;
792 }
793#endif
794 break;
795 }
796
f3da0ead
JM
797 if (saw_match && !*allows_reg)
798 warning ("matching constraint does not allow a register");
799
6be2e1f8
RH
800 return true;
801}
802
6de9cd9a
DN
803/* INPUT is one of the input operands from EXPR, an ASM_EXPR. Returns true
804 if it is an operand which must be passed in memory (i.e. an "m"
805 constraint), false otherwise. */
806
807bool
808asm_op_is_mem_input (tree input, tree expr)
809{
810 const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (input)));
811 tree outputs = ASM_OUTPUTS (expr);
812 int noutputs = list_length (outputs);
813 const char **constraints
814 = (const char **) alloca ((noutputs) * sizeof (const char *));
815 int i = 0;
816 bool allows_mem, allows_reg;
817 tree t;
818
819 /* Collect output constraints. */
820 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
821 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
822
823 /* We pass 0 for input_num, ninputs and ninout; they are only used for
824 error checking which will be done at expand time. */
825 parse_input_constraint (&constraint, 0, 0, noutputs, 0, constraints,
826 &allows_mem, &allows_reg);
827 return (!allows_reg && allows_mem);
828}
829
acb5d088
HPN
830/* Check for overlap between registers marked in CLOBBERED_REGS and
831 anything inappropriate in DECL. Emit error and return TRUE for error,
832 FALSE for ok. */
833
834static bool
46c5ad27 835decl_conflicts_with_clobbers_p (tree decl, const HARD_REG_SET clobbered_regs)
acb5d088
HPN
836{
837 /* Conflicts between asm-declared register variables and the clobber
838 list are not allowed. */
839 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
840 && DECL_REGISTER (decl)
34146b94 841 && REG_P (DECL_RTL (decl))
acb5d088
HPN
842 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
843 {
844 rtx reg = DECL_RTL (decl);
845 unsigned int regno;
846
847 for (regno = REGNO (reg);
848 regno < (REGNO (reg)
66fd46b6 849 + hard_regno_nregs[REGNO (reg)][GET_MODE (reg)]);
acb5d088
HPN
850 regno++)
851 if (TEST_HARD_REG_BIT (clobbered_regs, regno))
852 {
853 error ("asm-specifier for variable `%s' conflicts with asm clobber list",
854 IDENTIFIER_POINTER (DECL_NAME (decl)));
855
856 /* Reset registerness to stop multiple errors emitted for a
857 single variable. */
858 DECL_REGISTER (decl) = 0;
859 return true;
860 }
861 }
862 return false;
863}
864
28d81abb
RK
865/* Generate RTL for an asm statement with arguments.
866 STRING is the instruction template.
867 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
868 Each output or input has an expression in the TREE_VALUE and
2ec37136 869 and a tree list in TREE_PURPOSE which in turn contains a constraint
786de7eb 870 name in TREE_VALUE (or NULL_TREE) and a constraint string
2ec37136 871 in TREE_PURPOSE.
28d81abb
RK
872 CLOBBERS is a list of STRING_CST nodes each naming a hard register
873 that is clobbered by this insn.
874
875 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
876 Some elements of OUTPUTS may be replaced with trees representing temporary
877 values. The caller should copy those temporary values to the originally
878 specified lvalues.
879
880 VOL nonzero means the insn is volatile; don't optimize it. */
881
882void
46c5ad27 883expand_asm_operands (tree string, tree outputs, tree inputs,
177560b2 884 tree clobbers, int vol, location_t locus)
28d81abb 885{
84b72302 886 rtvec argvec, constraintvec;
28d81abb
RK
887 rtx body;
888 int ninputs = list_length (inputs);
889 int noutputs = list_length (outputs);
6be2e1f8 890 int ninout;
b4ccaa16 891 int nclobbers;
acb5d088
HPN
892 HARD_REG_SET clobbered_regs;
893 int clobber_conflict_found = 0;
28d81abb 894 tree tail;
7dc8b126 895 tree t;
b3694847 896 int i;
28d81abb 897 /* Vector of RTX's of evaluated output operands. */
703ad42b
KG
898 rtx *output_rtx = alloca (noutputs * sizeof (rtx));
899 int *inout_opnum = alloca (noutputs * sizeof (int));
900 rtx *real_output_rtx = alloca (noutputs * sizeof (rtx));
235c5021 901 enum machine_mode *inout_mode
703ad42b 902 = alloca (noutputs * sizeof (enum machine_mode));
84b72302 903 const char **constraints
703ad42b 904 = alloca ((noutputs + ninputs) * sizeof (const char *));
1b3d8f8a 905 int old_generating_concat_p = generating_concat_p;
28d81abb 906
e5e809f4 907 /* An ASM with no outputs needs to be treated as volatile, for now. */
296f8acc
JL
908 if (noutputs == 0)
909 vol = 1;
910
84b72302
RH
911 if (! check_operand_nalternatives (outputs, inputs))
912 return;
913
7dc8b126
JM
914 string = resolve_asm_operand_names (string, outputs, inputs);
915
916 /* Collect constraints. */
917 i = 0;
918 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
919 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
920 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
921 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
84b72302 922
57bcb97a
RH
923 /* Sometimes we wish to automatically clobber registers across an asm.
924 Case in point is when the i386 backend moved from cc0 to a hard reg --
f63d1bf7 925 maintaining source-level compatibility means automatically clobbering
57bcb97a 926 the flags register. */
67dfe110 927 clobbers = targetm.md_asm_clobbers (clobbers);
57bcb97a 928
b4ccaa16
RS
929 /* Count the number of meaningful clobbered registers, ignoring what
930 we would ignore later. */
931 nclobbers = 0;
acb5d088 932 CLEAR_HARD_REG_SET (clobbered_regs);
b4ccaa16
RS
933 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
934 {
47ee9bcb 935 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
14a774a9 936
c09e6498
RS
937 i = decode_reg_name (regname);
938 if (i >= 0 || i == -4)
b4ccaa16 939 ++nclobbers;
7859e3ac
DE
940 else if (i == -2)
941 error ("unknown register name `%s' in `asm'", regname);
acb5d088
HPN
942
943 /* Mark clobbered registers. */
944 if (i >= 0)
e54b4cae
EB
945 {
946 /* Clobbering the PIC register is an error */
fc555370 947 if (i == (int) PIC_OFFSET_TABLE_REGNUM)
e54b4cae
EB
948 {
949 error ("PIC register `%s' clobbered in `asm'", regname);
950 return;
951 }
952
953 SET_HARD_REG_BIT (clobbered_regs, i);
954 }
b4ccaa16
RS
955 }
956
6be2e1f8
RH
957 /* First pass over inputs and outputs checks validity and sets
958 mark_addressable if needed. */
959
960 ninout = 0;
28d81abb
RK
961 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
962 {
963 tree val = TREE_VALUE (tail);
b50a024d 964 tree type = TREE_TYPE (val);
6be2e1f8 965 const char *constraint;
40b18c0a
MM
966 bool is_inout;
967 bool allows_reg;
968 bool allows_mem;
28d81abb
RK
969
970 /* If there's an erroneous arg, emit no insn. */
40b18c0a 971 if (type == error_mark_node)
28d81abb
RK
972 return;
973
40b18c0a
MM
974 /* Try to parse the output constraint. If that fails, there's
975 no point in going further. */
6be2e1f8
RH
976 constraint = constraints[i];
977 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
978 &allows_mem, &allows_reg, &is_inout))
979 return;
980
981 if (! allows_reg
982 && (allows_mem
983 || is_inout
984 || (DECL_P (val)
f8cfc6aa 985 && REG_P (DECL_RTL (val))
6be2e1f8 986 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
ae2bcd98 987 lang_hooks.mark_addressable (val);
6be2e1f8
RH
988
989 if (is_inout)
990 ninout++;
991 }
992
993 ninputs += ninout;
994 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
995 {
996 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
997 return;
998 }
999
1000 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
1001 {
1002 bool allows_reg, allows_mem;
1003 const char *constraint;
1004
1005 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
1006 would get VOIDmode and that could cause a crash in reload. */
1007 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1008 return;
1009
1010 constraint = constraints[i + noutputs];
1011 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1012 constraints, &allows_mem, &allows_reg))
40b18c0a 1013 return;
d09a75ae 1014
6be2e1f8 1015 if (! allows_reg && allows_mem)
ae2bcd98 1016 lang_hooks.mark_addressable (TREE_VALUE (tail));
6be2e1f8
RH
1017 }
1018
1019 /* Second pass evaluates arguments. */
1020
1021 ninout = 0;
1022 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1023 {
1024 tree val = TREE_VALUE (tail);
1025 tree type = TREE_TYPE (val);
1026 bool is_inout;
1027 bool allows_reg;
1028 bool allows_mem;
5b50aa9d 1029 rtx op;
6be2e1f8
RH
1030
1031 if (!parse_output_constraint (&constraints[i], i, ninputs,
1032 noutputs, &allows_mem, &allows_reg,
1033 &is_inout))
1034 abort ();
1035
d09a75ae
RK
1036 /* If an output operand is not a decl or indirect ref and our constraint
1037 allows a register, make a temporary to act as an intermediate.
1038 Make the asm insn write into that, then our caller will copy it to
1039 the real output operand. Likewise for promoted variables. */
28d81abb 1040
1b3d8f8a
GK
1041 generating_concat_p = 0;
1042
947255ed 1043 real_output_rtx[i] = NULL_RTX;
1afbe1c4
RH
1044 if ((TREE_CODE (val) == INDIRECT_REF
1045 && allows_mem)
2f939d94 1046 || (DECL_P (val)
f8cfc6aa
JQ
1047 && (allows_mem || REG_P (DECL_RTL (val)))
1048 && ! (REG_P (DECL_RTL (val))
d09a75ae 1049 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
11579f33 1050 || ! allows_reg
2a230e9d 1051 || is_inout)
d09a75ae 1052 {
5b50aa9d 1053 op = expand_expr (val, NULL_RTX, VOIDmode, EXPAND_WRITE);
3c0cb5de 1054 if (MEM_P (op))
5b50aa9d 1055 op = validize_mem (op);
d09a75ae 1056
3c0cb5de 1057 if (! allows_reg && !MEM_P (op))
d09a75ae 1058 error ("output number %d not directly addressable", i);
3c0cb5de 1059 if ((! allows_mem && MEM_P (op))
5b50aa9d 1060 || GET_CODE (op) == CONCAT)
947255ed 1061 {
ad76cef8 1062 real_output_rtx[i] = op;
5b50aa9d 1063 op = gen_reg_rtx (GET_MODE (op));
11579f33 1064 if (is_inout)
5b50aa9d 1065 emit_move_insn (op, real_output_rtx[i]);
947255ed 1066 }
d09a75ae 1067 }
b50a024d 1068 else
e619bb8d 1069 {
5b50aa9d
RH
1070 op = assign_temp (type, 0, 0, 1);
1071 op = validize_mem (op);
1072 TREE_VALUE (tail) = make_tree (type, op);
b50a024d 1073 }
5b50aa9d 1074 output_rtx[i] = op;
235c5021 1075
1b3d8f8a
GK
1076 generating_concat_p = old_generating_concat_p;
1077
2a230e9d 1078 if (is_inout)
235c5021 1079 {
6be2e1f8 1080 inout_mode[ninout] = TYPE_MODE (type);
235c5021
RK
1081 inout_opnum[ninout++] = i;
1082 }
acb5d088
HPN
1083
1084 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1085 clobber_conflict_found = 1;
28d81abb
RK
1086 }
1087
84b72302
RH
1088 /* Make vectors for the expression-rtx, constraint strings,
1089 and named operands. */
28d81abb
RK
1090
1091 argvec = rtvec_alloc (ninputs);
84b72302 1092 constraintvec = rtvec_alloc (ninputs);
28d81abb 1093
6462bb43
AO
1094 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
1095 : GET_MODE (output_rtx[0])),
839ee4bc 1096 TREE_STRING_POINTER (string),
84b72302 1097 empty_string, 0, argvec, constraintvec,
6773e15f 1098 locus);
c85f7c16 1099
78418280 1100 MEM_VOLATILE_P (body) = vol;
28d81abb
RK
1101
1102 /* Eval the inputs and put them into ARGVEC.
1103 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1104
84b72302 1105 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
28d81abb 1106 {
6be2e1f8
RH
1107 bool allows_reg, allows_mem;
1108 const char *constraint;
1109 tree val, type;
1f06ee8d 1110 rtx op;
28d81abb 1111
6be2e1f8
RH
1112 constraint = constraints[i + noutputs];
1113 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1114 constraints, &allows_mem, &allows_reg))
1115 abort ();
2a230e9d 1116
6be2e1f8 1117 generating_concat_p = 0;
65fed0cb 1118
6be2e1f8
RH
1119 val = TREE_VALUE (tail);
1120 type = TREE_TYPE (val);
017e1b43
RH
1121 op = expand_expr (val, NULL_RTX, VOIDmode,
1122 (allows_mem && !allows_reg
1123 ? EXPAND_MEMORY : EXPAND_NORMAL));
65fed0cb 1124
1b3d8f8a 1125 /* Never pass a CONCAT to an ASM. */
1b3d8f8a
GK
1126 if (GET_CODE (op) == CONCAT)
1127 op = force_reg (GET_MODE (op), op);
3c0cb5de 1128 else if (MEM_P (op))
5b50aa9d 1129 op = validize_mem (op);
1b3d8f8a 1130
1afbe1c4 1131 if (asm_operand_ok (op, constraint) <= 0)
65fed0cb 1132 {
11579f33 1133 if (allows_reg)
6be2e1f8 1134 op = force_reg (TYPE_MODE (type), op);
11579f33 1135 else if (!allows_mem)
84b72302
RH
1136 warning ("asm operand %d probably doesn't match constraints",
1137 i + noutputs);
3c0cb5de 1138 else if (MEM_P (op))
6be2e1f8 1139 {
d50ad6af
RH
1140 /* We won't recognize either volatile memory or memory
1141 with a queued address as available a memory_operand
1142 at this point. Ignore it: clearly this *is* a memory. */
6be2e1f8 1143 }
1f06ee8d 1144 else
017e1b43 1145 {
71ed1fdb
RH
1146 warning ("use of memory input without lvalue in "
1147 "asm operand %d is deprecated", i + noutputs);
017e1b43
RH
1148
1149 if (CONSTANT_P (op))
1150 {
9c858681
RS
1151 rtx mem = force_const_mem (TYPE_MODE (type), op);
1152 if (mem)
1153 op = validize_mem (mem);
1154 else
1155 op = force_reg (TYPE_MODE (type), op);
017e1b43 1156 }
f8cfc6aa 1157 if (REG_P (op)
9c858681 1158 || GET_CODE (op) == SUBREG
9c858681 1159 || GET_CODE (op) == CONCAT)
017e1b43
RH
1160 {
1161 tree qual_type = build_qualified_type (type,
1162 (TYPE_QUALS (type)
1163 | TYPE_QUAL_CONST));
1164 rtx memloc = assign_temp (qual_type, 1, 1, 1);
1165 memloc = validize_mem (memloc);
1166 emit_move_insn (memloc, op);
1167 op = memloc;
1168 }
1169 }
65fed0cb 1170 }
6be2e1f8 1171
1b3d8f8a 1172 generating_concat_p = old_generating_concat_p;
6462bb43 1173 ASM_OPERANDS_INPUT (body, i) = op;
2a230e9d 1174
6462bb43 1175 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
839ee4bc 1176 = gen_rtx_ASM_INPUT (TYPE_MODE (type), constraints[i + noutputs]);
acb5d088
HPN
1177
1178 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1179 clobber_conflict_found = 1;
28d81abb
RK
1180 }
1181
14a774a9
RK
1182 /* Protect all the operands from the queue now that they have all been
1183 evaluated. */
28d81abb 1184
1b3d8f8a
GK
1185 generating_concat_p = 0;
1186
4381f7c2 1187 /* For in-out operands, copy output rtx to input rtx. */
235c5021
RK
1188 for (i = 0; i < ninout; i++)
1189 {
235c5021 1190 int j = inout_opnum[i];
84b72302 1191 char buffer[16];
235c5021 1192
6462bb43 1193 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
235c5021 1194 = output_rtx[j];
84b72302
RH
1195
1196 sprintf (buffer, "%d", j);
6462bb43 1197 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
485bad26 1198 = gen_rtx_ASM_INPUT (inout_mode[i], ggc_strdup (buffer));
235c5021
RK
1199 }
1200
1b3d8f8a
GK
1201 generating_concat_p = old_generating_concat_p;
1202
28d81abb 1203 /* Now, for each output, construct an rtx
84b72302
RH
1204 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
1205 ARGVEC CONSTRAINTS OPNAMES))
28d81abb
RK
1206 If there is more than one, put them inside a PARALLEL. */
1207
1208 if (noutputs == 1 && nclobbers == 0)
1209 {
839ee4bc 1210 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
4977bab6 1211 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
28d81abb 1212 }
14a774a9 1213
28d81abb
RK
1214 else if (noutputs == 0 && nclobbers == 0)
1215 {
1216 /* No output operands: put in a raw ASM_OPERANDS rtx. */
4977bab6 1217 emit_insn (body);
28d81abb 1218 }
14a774a9 1219
28d81abb
RK
1220 else
1221 {
1222 rtx obody = body;
1223 int num = noutputs;
14a774a9
RK
1224
1225 if (num == 0)
1226 num = 1;
1227
38a448ca 1228 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
28d81abb
RK
1229
1230 /* For each output operand, store a SET. */
28d81abb
RK
1231 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1232 {
1233 XVECEXP (body, 0, i)
38a448ca
RH
1234 = gen_rtx_SET (VOIDmode,
1235 output_rtx[i],
c5c76735 1236 gen_rtx_ASM_OPERANDS
6462bb43 1237 (GET_MODE (output_rtx[i]),
839ee4bc
RO
1238 TREE_STRING_POINTER (string),
1239 constraints[i], i, argvec, constraintvec,
6773e15f 1240 locus));
c5c76735 1241
28d81abb
RK
1242 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1243 }
1244
1245 /* If there are no outputs (but there are some clobbers)
1246 store the bare ASM_OPERANDS into the PARALLEL. */
1247
1248 if (i == 0)
1249 XVECEXP (body, 0, i++) = obody;
1250
1251 /* Store (clobber REG) for each clobbered register specified. */
1252
b4ccaa16 1253 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
28d81abb 1254 {
47ee9bcb 1255 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
b4ac57ab 1256 int j = decode_reg_name (regname);
acb5d088 1257 rtx clobbered_reg;
28d81abb 1258
b4ac57ab 1259 if (j < 0)
28d81abb 1260 {
c09e6498 1261 if (j == -3) /* `cc', which is not a register */
dcfedcd0
RK
1262 continue;
1263
c09e6498
RS
1264 if (j == -4) /* `memory', don't cache memory across asm */
1265 {
bffc6177 1266 XVECEXP (body, 0, i++)
38a448ca 1267 = gen_rtx_CLOBBER (VOIDmode,
c5c76735
JL
1268 gen_rtx_MEM
1269 (BLKmode,
1270 gen_rtx_SCRATCH (VOIDmode)));
c09e6498
RS
1271 continue;
1272 }
1273
956d6950 1274 /* Ignore unknown register, error already signaled. */
cc1f5387 1275 continue;
28d81abb
RK
1276 }
1277
1278 /* Use QImode since that's guaranteed to clobber just one reg. */
acb5d088
HPN
1279 clobbered_reg = gen_rtx_REG (QImode, j);
1280
1281 /* Do sanity check for overlap between clobbers and respectively
1282 input and outputs that hasn't been handled. Such overlap
1283 should have been detected and reported above. */
1284 if (!clobber_conflict_found)
1285 {
1286 int opno;
1287
1288 /* We test the old body (obody) contents to avoid tripping
1289 over the under-construction body. */
1290 for (opno = 0; opno < noutputs; opno++)
1291 if (reg_overlap_mentioned_p (clobbered_reg, output_rtx[opno]))
1292 internal_error ("asm clobber conflict with output operand");
1293
1294 for (opno = 0; opno < ninputs - ninout; opno++)
1295 if (reg_overlap_mentioned_p (clobbered_reg,
1296 ASM_OPERANDS_INPUT (obody, opno)))
1297 internal_error ("asm clobber conflict with input operand");
1298 }
1299
b4ccaa16 1300 XVECEXP (body, 0, i++)
acb5d088 1301 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
28d81abb
RK
1302 }
1303
4977bab6 1304 emit_insn (body);
28d81abb
RK
1305 }
1306
947255ed
RH
1307 /* For any outputs that needed reloading into registers, spill them
1308 back to where they belong. */
1309 for (i = 0; i < noutputs; ++i)
1310 if (real_output_rtx[i])
1311 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1312
28d81abb
RK
1313 free_temp_slots ();
1314}
84b72302 1315
6de9cd9a
DN
1316void
1317expand_asm_expr (tree exp)
1318{
1319 int noutputs, i;
1320 tree outputs, tail;
1321 tree *o;
1322
1323 if (ASM_INPUT_P (exp))
1324 {
1325 expand_asm (ASM_STRING (exp), ASM_VOLATILE_P (exp));
1326 return;
1327 }
1328
1329 outputs = ASM_OUTPUTS (exp);
1330 noutputs = list_length (outputs);
1331 /* o[I] is the place that output number I should be written. */
1332 o = (tree *) alloca (noutputs * sizeof (tree));
1333
1334 /* Record the contents of OUTPUTS before it is modified. */
1335 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1336 o[i] = TREE_VALUE (tail);
1337
1338 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
1339 OUTPUTS some trees for where the values were actually stored. */
1340 expand_asm_operands (ASM_STRING (exp), outputs, ASM_INPUTS (exp),
1341 ASM_CLOBBERS (exp), ASM_VOLATILE_P (exp),
1342 input_location);
1343
1344 /* Copy all the intermediate outputs into the specified outputs. */
1345 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1346 {
1347 if (o[i] != TREE_VALUE (tail))
1348 {
1349 expand_assignment (o[i], TREE_VALUE (tail), 0);
1350 free_temp_slots ();
1351
1352 /* Restore the original value so that it's correct the next
1353 time we expand this function. */
1354 TREE_VALUE (tail) = o[i];
1355 }
1356 }
6de9cd9a
DN
1357}
1358
84b72302
RH
1359/* A subroutine of expand_asm_operands. Check that all operands have
1360 the same number of alternatives. Return true if so. */
1361
1362static bool
46c5ad27 1363check_operand_nalternatives (tree outputs, tree inputs)
84b72302
RH
1364{
1365 if (outputs || inputs)
1366 {
1367 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1368 int nalternatives
1369 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
1370 tree next = inputs;
1371
1372 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1373 {
1374 error ("too many alternatives in `asm'");
1375 return false;
1376 }
1377
1378 tmp = outputs;
1379 while (tmp)
1380 {
1381 const char *constraint
1382 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
1383
1384 if (n_occurrences (',', constraint) != nalternatives)
1385 {
1386 error ("operand constraints for `asm' differ in number of alternatives");
1387 return false;
1388 }
1389
1390 if (TREE_CHAIN (tmp))
1391 tmp = TREE_CHAIN (tmp);
1392 else
1393 tmp = next, next = 0;
1394 }
1395 }
1396
1397 return true;
1398}
1399
1400/* A subroutine of expand_asm_operands. Check that all operand names
1401 are unique. Return true if so. We rely on the fact that these names
1402 are identifiers, and so have been canonicalized by get_identifier,
1403 so all we need are pointer comparisons. */
1404
1405static bool
46c5ad27 1406check_unique_operand_names (tree outputs, tree inputs)
84b72302
RH
1407{
1408 tree i, j;
1409
1410 for (i = outputs; i ; i = TREE_CHAIN (i))
1411 {
1412 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1413 if (! i_name)
1414 continue;
1415
1416 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
fc552851 1417 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
84b72302
RH
1418 goto failure;
1419 }
1420
1421 for (i = inputs; i ; i = TREE_CHAIN (i))
1422 {
1423 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1424 if (! i_name)
1425 continue;
1426
1427 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
fc552851 1428 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
84b72302
RH
1429 goto failure;
1430 for (j = outputs; j ; j = TREE_CHAIN (j))
fc552851 1431 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
84b72302
RH
1432 goto failure;
1433 }
1434
1435 return true;
1436
1437 failure:
1438 error ("duplicate asm operand name '%s'",
fc552851 1439 TREE_STRING_POINTER (TREE_PURPOSE (TREE_PURPOSE (i))));
84b72302
RH
1440 return false;
1441}
1442
1443/* A subroutine of expand_asm_operands. Resolve the names of the operands
1444 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
1445 STRING and in the constraints to those numbers. */
1446
7dc8b126
JM
1447tree
1448resolve_asm_operand_names (tree string, tree outputs, tree inputs)
84b72302 1449{
7dc8b126 1450 char *buffer;
84b72302 1451 char *p;
40209195 1452 const char *c;
84b72302
RH
1453 tree t;
1454
1456deaf
JM
1455 check_unique_operand_names (outputs, inputs);
1456
7dc8b126
JM
1457 /* Substitute [<name>] in input constraint strings. There should be no
1458 named operands in output constraints. */
1459 for (t = inputs; t ; t = TREE_CHAIN (t))
1460 {
40209195 1461 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
7dc8b126
JM
1462 if (strchr (c, '[') != NULL)
1463 {
1464 p = buffer = xstrdup (c);
1465 while ((p = strchr (p, '[')) != NULL)
1466 p = resolve_operand_name_1 (p, outputs, inputs);
1467 TREE_VALUE (TREE_PURPOSE (t))
1468 = build_string (strlen (buffer), buffer);
1469 free (buffer);
1470 }
1471 }
1472
40209195
JM
1473 /* Now check for any needed substitutions in the template. */
1474 c = TREE_STRING_POINTER (string);
1475 while ((c = strchr (c, '%')) != NULL)
84b72302 1476 {
40209195
JM
1477 if (c[1] == '[')
1478 break;
1479 else if (ISALPHA (c[1]) && c[2] == '[')
1480 break;
7abcb63a
RH
1481 else
1482 {
40209195 1483 c += 1;
7abcb63a
RH
1484 continue;
1485 }
84b72302
RH
1486 }
1487
40209195
JM
1488 if (c)
1489 {
1490 /* OK, we need to make a copy so we can perform the substitutions.
1491 Assume that we will not need extra space--we get to remove '['
1492 and ']', which means we cannot have a problem until we have more
1493 than 999 operands. */
1494 buffer = xstrdup (TREE_STRING_POINTER (string));
1495 p = buffer + (c - TREE_STRING_POINTER (string));
1496
1497 while ((p = strchr (p, '%')) != NULL)
1498 {
1499 if (p[1] == '[')
1500 p += 1;
1501 else if (ISALPHA (p[1]) && p[2] == '[')
1502 p += 2;
1503 else
1504 {
1505 p += 1;
1506 continue;
1507 }
1508
1509 p = resolve_operand_name_1 (p, outputs, inputs);
1510 }
1511
1512 string = build_string (strlen (buffer), buffer);
1513 free (buffer);
1514 }
84b72302 1515
84b72302
RH
1516 return string;
1517}
1518
1519/* A subroutine of resolve_operand_names. P points to the '[' for a
1520 potential named operand of the form [<name>]. In place, replace
786de7eb 1521 the name and brackets with a number. Return a pointer to the
84b72302
RH
1522 balance of the string after substitution. */
1523
1524static char *
46c5ad27 1525resolve_operand_name_1 (char *p, tree outputs, tree inputs)
84b72302
RH
1526{
1527 char *q;
1528 int op;
1529 tree t;
1530 size_t len;
1531
1532 /* Collect the operand name. */
1533 q = strchr (p, ']');
1534 if (!q)
1535 {
1536 error ("missing close brace for named operand");
1537 return strchr (p, '\0');
1538 }
1539 len = q - p - 1;
1540
1541 /* Resolve the name to a number. */
1542 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
1543 {
fc552851
RS
1544 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
1545 if (name)
edd1967d 1546 {
fc552851 1547 const char *c = TREE_STRING_POINTER (name);
edd1967d
RH
1548 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
1549 goto found;
1550 }
84b72302
RH
1551 }
1552 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
1553 {
fc552851
RS
1554 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
1555 if (name)
edd1967d 1556 {
fc552851 1557 const char *c = TREE_STRING_POINTER (name);
edd1967d
RH
1558 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
1559 goto found;
1560 }
84b72302
RH
1561 }
1562
1563 *q = '\0';
1564 error ("undefined named operand '%s'", p + 1);
1565 op = 0;
1566 found:
1567
1568 /* Replace the name with the number. Unfortunately, not all libraries
1569 get the return value of sprintf correct, so search for the end of the
1570 generated string by hand. */
1571 sprintf (p, "%d", op);
1572 p = strchr (p, '\0');
1573
1574 /* Verify the no extra buffer space assumption. */
1575 if (p > q)
1576 abort ();
1577
1578 /* Shift the rest of the buffer down to fill the gap. */
1579 memmove (p, q + 1, strlen (q + 1) + 1);
1580
1581 return p;
1582}
28d81abb 1583\f
4dfa0342 1584/* Generate RTL to evaluate the expression EXP. */
28d81abb
RK
1585
1586void
46c5ad27 1587expand_expr_stmt (tree exp)
1574ef13
AO
1588{
1589 rtx value;
1590 tree type;
b6ec8c5f 1591
4dfa0342 1592 value = expand_expr (exp, const0_rtx, VOIDmode, 0);
1574ef13 1593 type = TREE_TYPE (exp);
28d81abb
RK
1594
1595 /* If all we do is reference a volatile value in memory,
1596 copy it to a register to be sure it is actually touched. */
3c0cb5de 1597 if (value && MEM_P (value) && TREE_THIS_VOLATILE (exp))
28d81abb 1598 {
1574ef13 1599 if (TYPE_MODE (type) == VOIDmode)
6a5bbbe6 1600 ;
1574ef13
AO
1601 else if (TYPE_MODE (type) != BLKmode)
1602 value = copy_to_reg (value);
28d81abb 1603 else
ddbe9812
RS
1604 {
1605 rtx lab = gen_label_rtx ();
4381f7c2 1606
ddbe9812 1607 /* Compare the value with itself to reference it. */
1574ef13
AO
1608 emit_cmp_and_jump_insns (value, value, EQ,
1609 expand_expr (TYPE_SIZE (type),
c5d5d461 1610 NULL_RTX, VOIDmode, 0),
d43e0b7d 1611 BLKmode, 0, lab);
ddbe9812
RS
1612 emit_label (lab);
1613 }
28d81abb
RK
1614 }
1615
4dfa0342 1616 /* Free any temporaries used to evaluate this expression. */
28d81abb 1617 free_temp_slots ();
28d81abb
RK
1618}
1619
1620/* Warn if EXP contains any computations whose results are not used.
b9861bff
RH
1621 Return 1 if a warning is printed; 0 otherwise. LOCUS is the
1622 (potential) location of the expression. */
28d81abb 1623
150a992a 1624int
b9861bff 1625warn_if_unused_value (tree exp, location_t locus)
28d81abb 1626{
b9861bff 1627 restart:
28d81abb
RK
1628 if (TREE_USED (exp))
1629 return 0;
1630
9790cefd
RH
1631 /* Don't warn about void constructs. This includes casting to void,
1632 void function calls, and statement expressions with a final cast
1633 to void. */
1634 if (VOID_TYPE_P (TREE_TYPE (exp)))
1635 return 0;
1636
b9861bff
RH
1637 if (EXPR_LOCUS (exp))
1638 locus = *EXPR_LOCUS (exp);
1639
28d81abb
RK
1640 switch (TREE_CODE (exp))
1641 {
1642 case PREINCREMENT_EXPR:
1643 case POSTINCREMENT_EXPR:
1644 case PREDECREMENT_EXPR:
1645 case POSTDECREMENT_EXPR:
1646 case MODIFY_EXPR:
1647 case INIT_EXPR:
1648 case TARGET_EXPR:
1649 case CALL_EXPR:
81797aba 1650 case TRY_CATCH_EXPR:
28d81abb
RK
1651 case WITH_CLEANUP_EXPR:
1652 case EXIT_EXPR:
28d81abb
RK
1653 return 0;
1654
1655 case BIND_EXPR:
1656 /* For a binding, warn if no side effect within it. */
b9861bff
RH
1657 exp = BIND_EXPR_BODY (exp);
1658 goto restart;
28d81abb 1659
de73f171 1660 case SAVE_EXPR:
b9861bff
RH
1661 exp = TREE_OPERAND (exp, 0);
1662 goto restart;
de73f171 1663
28d81abb
RK
1664 case TRUTH_ORIF_EXPR:
1665 case TRUTH_ANDIF_EXPR:
1666 /* In && or ||, warn if 2nd operand has no side effect. */
b9861bff
RH
1667 exp = TREE_OPERAND (exp, 1);
1668 goto restart;
28d81abb
RK
1669
1670 case COMPOUND_EXPR:
6de9cd9a 1671 if (TREE_NO_WARNING (exp))
a646a211 1672 return 0;
b9861bff 1673 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
28d81abb 1674 return 1;
4d23e509
RS
1675 /* Let people do `(foo (), 0)' without a warning. */
1676 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1677 return 0;
b9861bff
RH
1678 exp = TREE_OPERAND (exp, 1);
1679 goto restart;
28d81abb
RK
1680
1681 case NOP_EXPR:
1682 case CONVERT_EXPR:
b4ac57ab 1683 case NON_LVALUE_EXPR:
28d81abb 1684 /* Don't warn about conversions not explicit in the user's program. */
6de9cd9a 1685 if (TREE_NO_WARNING (exp))
28d81abb
RK
1686 return 0;
1687 /* Assignment to a cast usually results in a cast of a modify.
55cd1c09
JW
1688 Don't complain about that. There can be an arbitrary number of
1689 casts before the modify, so we must loop until we find the first
1690 non-cast expression and then test to see if that is a modify. */
1691 {
1692 tree tem = TREE_OPERAND (exp, 0);
1693
1694 while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
1695 tem = TREE_OPERAND (tem, 0);
1696
de73f171
RK
1697 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
1698 || TREE_CODE (tem) == CALL_EXPR)
55cd1c09
JW
1699 return 0;
1700 }
7133e992 1701 goto maybe_warn;
28d81abb 1702
d1e1adfb
JM
1703 case INDIRECT_REF:
1704 /* Don't warn about automatic dereferencing of references, since
1705 the user cannot control it. */
1706 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
b9861bff
RH
1707 {
1708 exp = TREE_OPERAND (exp, 0);
1709 goto restart;
1710 }
4381f7c2
KH
1711 /* Fall through. */
1712
28d81abb 1713 default:
ddbe9812 1714 /* Referencing a volatile value is a side effect, so don't warn. */
2f939d94 1715 if ((DECL_P (exp)
ddbe9812
RS
1716 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
1717 && TREE_THIS_VOLATILE (exp))
1718 return 0;
8d5e6e25
RK
1719
1720 /* If this is an expression which has no operands, there is no value
1721 to be unused. There are no such language-independent codes,
1722 but front ends may define such. */
1723 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'e'
1724 && TREE_CODE_LENGTH (TREE_CODE (exp)) == 0)
1725 return 0;
1726
7133e992
JJ
1727 maybe_warn:
1728 /* If this is an expression with side effects, don't warn. */
1729 if (TREE_SIDE_EFFECTS (exp))
1730 return 0;
1731
b9861bff 1732 warning ("%Hvalue computed is not used", &locus);
28d81abb
RK
1733 return 1;
1734 }
1735}
28d81abb 1736\f
28d81abb
RK
1737/* Generate RTL for the start of an if-then. COND is the expression
1738 whose truth should be tested.
1739
1740 If EXITFLAG is nonzero, this conditional is visible to
1741 `exit_something'. */
1742
1743void
46c5ad27 1744expand_start_cond (tree cond, int exitflag)
28d81abb
RK
1745{
1746 struct nesting *thiscond = ALLOC_NESTING ();
1747
1748 /* Make an entry on cond_stack for the cond we are entering. */
1749
e2500fed 1750 thiscond->desc = COND_NESTING;
28d81abb
RK
1751 thiscond->next = cond_stack;
1752 thiscond->all = nesting_stack;
1753 thiscond->depth = ++nesting_depth;
1754 thiscond->data.cond.next_label = gen_label_rtx ();
1755 /* Before we encounter an `else', we don't need a separate exit label
1756 unless there are supposed to be exit statements
1757 to exit this conditional. */
1758 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
1759 thiscond->data.cond.endif_label = thiscond->exit_label;
1760 cond_stack = thiscond;
1761 nesting_stack = thiscond;
1762
b93a436e 1763 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
28d81abb
RK
1764}
1765
1766/* Generate RTL between then-clause and the elseif-clause
1767 of an if-then-elseif-.... */
1768
1769void
46c5ad27 1770expand_start_elseif (tree cond)
28d81abb
RK
1771{
1772 if (cond_stack->data.cond.endif_label == 0)
1773 cond_stack->data.cond.endif_label = gen_label_rtx ();
1774 emit_jump (cond_stack->data.cond.endif_label);
1775 emit_label (cond_stack->data.cond.next_label);
1776 cond_stack->data.cond.next_label = gen_label_rtx ();
37366632 1777 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
28d81abb
RK
1778}
1779
1780/* Generate RTL between the then-clause and the else-clause
1781 of an if-then-else. */
1782
1783void
46c5ad27 1784expand_start_else (void)
28d81abb
RK
1785{
1786 if (cond_stack->data.cond.endif_label == 0)
1787 cond_stack->data.cond.endif_label = gen_label_rtx ();
ca695ac9 1788
28d81abb
RK
1789 emit_jump (cond_stack->data.cond.endif_label);
1790 emit_label (cond_stack->data.cond.next_label);
0f41302f 1791 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
28d81abb
RK
1792}
1793
d947ba59
RK
1794/* After calling expand_start_else, turn this "else" into an "else if"
1795 by providing another condition. */
1796
1797void
46c5ad27 1798expand_elseif (tree cond)
d947ba59
RK
1799{
1800 cond_stack->data.cond.next_label = gen_label_rtx ();
1801 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
1802}
1803
28d81abb
RK
1804/* Generate RTL for the end of an if-then.
1805 Pop the record for it off of cond_stack. */
1806
1807void
46c5ad27 1808expand_end_cond (void)
28d81abb
RK
1809{
1810 struct nesting *thiscond = cond_stack;
1811
b93a436e
JL
1812 do_pending_stack_adjust ();
1813 if (thiscond->data.cond.next_label)
1814 emit_label (thiscond->data.cond.next_label);
1815 if (thiscond->data.cond.endif_label)
1816 emit_label (thiscond->data.cond.endif_label);
28d81abb
RK
1817
1818 POPSTACK (cond_stack);
28d81abb
RK
1819}
1820\f
0e9e1e0a 1821/* Return nonzero if we should preserve sub-expressions as separate
28d81abb 1822 pseudos. We never do so if we aren't optimizing. We always do so
6af8eb57 1823 if -fexpensive-optimizations. */
28d81abb
RK
1824
1825int
46c5ad27 1826preserve_subexpressions_p (void)
28d81abb 1827{
28d81abb
RK
1828 if (flag_expensive_optimizations)
1829 return 1;
1830
6af8eb57 1831 if (optimize == 0 || cfun == 0 || cfun->stmt == 0)
28d81abb
RK
1832 return 0;
1833
6af8eb57 1834 return 1;
28d81abb
RK
1835}
1836
28d81abb
RK
1837\f
1838/* Generate RTL to return from the current function, with no value.
1839 (That is, we do not do anything about returning any value.) */
1840
1841void
46c5ad27 1842expand_null_return (void)
28d81abb 1843{
4381f7c2 1844 /* If this function was declared to return a value, but we
bd695e1e 1845 didn't, clobber the return registers so that they are not
a1f300c0 1846 propagated live to the rest of the function. */
c13fde05 1847 clobber_return_register ();
28d81abb 1848
ac45df5d 1849 expand_null_return_1 ();
28d81abb
RK
1850}
1851
6e3077c6
EB
1852/* Generate RTL to return directly from the current function.
1853 (That is, we bypass any return value.) */
1854
1855void
1856expand_naked_return (void)
1857{
ac45df5d 1858 rtx end_label;
6e3077c6
EB
1859
1860 clear_pending_stack_adjust ();
1861 do_pending_stack_adjust ();
6e3077c6 1862
ac45df5d 1863 end_label = naked_return_label;
6e3077c6
EB
1864 if (end_label == 0)
1865 end_label = naked_return_label = gen_label_rtx ();
ac45df5d
RH
1866
1867 emit_jump (end_label);
6e3077c6
EB
1868}
1869
969d70ca
JH
1870/* Try to guess whether the value of return means error code. */
1871static enum br_predictor
46c5ad27 1872return_prediction (rtx val)
969d70ca
JH
1873{
1874 /* Different heuristics for pointers and scalars. */
1875 if (POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
1876 {
1877 /* NULL is usually not returned. */
1878 if (val == const0_rtx)
1879 return PRED_NULL_RETURN;
1880 }
1881 else
1882 {
1883 /* Negative return values are often used to indicate
1884 errors. */
1885 if (GET_CODE (val) == CONST_INT
1886 && INTVAL (val) < 0)
1887 return PRED_NEGATIVE_RETURN;
1888 /* Constant return values are also usually erors,
1889 zero/one often mean booleans so exclude them from the
1890 heuristics. */
1891 if (CONSTANT_P (val)
1892 && (val != const0_rtx && val != const1_rtx))
1893 return PRED_CONST_RETURN;
1894 }
1895 return PRED_NO_PREDICTION;
1896}
1897
c988af2b
RS
1898
1899/* If the current function returns values in the most significant part
1900 of a register, shift return value VAL appropriately. The mode of
1901 the function's return type is known not to be BLKmode. */
1902
1903static rtx
1904shift_return_value (rtx val)
1905{
1906 tree type;
1907
1908 type = TREE_TYPE (DECL_RESULT (current_function_decl));
1909 if (targetm.calls.return_in_msb (type))
1910 {
1911 rtx target;
1912 HOST_WIDE_INT shift;
1913
1914 target = DECL_RTL (DECL_RESULT (current_function_decl));
1915 shift = (GET_MODE_BITSIZE (GET_MODE (target))
1916 - BITS_PER_UNIT * int_size_in_bytes (type));
1917 if (shift > 0)
09b52670 1918 val = expand_shift (LSHIFT_EXPR, GET_MODE (target),
c988af2b 1919 gen_lowpart (GET_MODE (target), val),
09b52670 1920 build_int_2 (shift, 0), target, 1);
c988af2b
RS
1921 }
1922 return val;
1923}
1924
1925
28d81abb
RK
1926/* Generate RTL to return from the current function, with value VAL. */
1927
8d800403 1928static void
46c5ad27 1929expand_value_return (rtx val)
28d81abb 1930{
969d70ca
JH
1931 rtx return_reg;
1932 enum br_predictor pred;
1933
d50672ef
JH
1934 if (flag_guess_branch_prob
1935 && (pred = return_prediction (val)) != PRED_NO_PREDICTION)
969d70ca
JH
1936 {
1937 /* Emit information for branch prediction. */
1938 rtx note;
1939
2e040219 1940 note = emit_note (NOTE_INSN_PREDICTION);
969d70ca
JH
1941
1942 NOTE_PREDICTION (note) = NOTE_PREDICT (pred, NOT_TAKEN);
1943
1944 }
1945
969d70ca 1946 return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
28d81abb
RK
1947
1948 /* Copy the value to the return location
1949 unless it's already there. */
1950
1951 if (return_reg != val)
77636079 1952 {
77636079 1953 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
61f71b34
DD
1954 if (targetm.calls.promote_function_return (TREE_TYPE (current_function_decl)))
1955 {
8df83eae 1956 int unsignedp = TYPE_UNSIGNED (type);
61f71b34
DD
1957 enum machine_mode old_mode
1958 = DECL_MODE (DECL_RESULT (current_function_decl));
1959 enum machine_mode mode
1960 = promote_mode (type, old_mode, &unsignedp, 1);
1961
1962 if (mode != old_mode)
1963 val = convert_modes (mode, old_mode, val, unsignedp);
1964 }
14a774a9 1965 if (GET_CODE (return_reg) == PARALLEL)
6e985040 1966 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
14a774a9 1967 else
77636079
RS
1968 emit_move_insn (return_reg, val);
1969 }
14a774a9 1970
ac45df5d 1971 expand_null_return_1 ();
28d81abb
RK
1972}
1973
ac45df5d 1974/* Output a return with no value. */
28d81abb
RK
1975
1976static void
ac45df5d 1977expand_null_return_1 (void)
28d81abb 1978{
ac45df5d 1979 rtx end_label;
28d81abb
RK
1980
1981 clear_pending_stack_adjust ();
1982 do_pending_stack_adjust ();
28d81abb 1983
ac45df5d 1984 end_label = return_label;
396ad517
JDA
1985 if (end_label == 0)
1986 end_label = return_label = gen_label_rtx ();
ac45df5d 1987 emit_jump (end_label);
28d81abb
RK
1988}
1989\f
1990/* Generate RTL to evaluate the expression RETVAL and return it
1991 from the current function. */
1992
1993void
46c5ad27 1994expand_return (tree retval)
28d81abb 1995{
19e7881c 1996 rtx result_rtl;
b3694847 1997 rtx val = 0;
28d81abb 1998 tree retval_rhs;
28d81abb
RK
1999
2000 /* If function wants no value, give it none. */
2001 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2002 {
37366632 2003 expand_expr (retval, NULL_RTX, VOIDmode, 0);
28d81abb
RK
2004 expand_null_return ();
2005 return;
2006 }
2007
ea11ca7e 2008 if (retval == error_mark_node)
c9407e4c
MM
2009 {
2010 /* Treat this like a return of no value from a function that
2011 returns a value. */
2012 expand_null_return ();
786de7eb 2013 return;
c9407e4c 2014 }
ea11ca7e 2015 else if (TREE_CODE (retval) == RESULT_DECL)
28d81abb 2016 retval_rhs = retval;
ac45df5d
RH
2017 else if ((TREE_CODE (retval) == MODIFY_EXPR
2018 || TREE_CODE (retval) == INIT_EXPR)
28d81abb
RK
2019 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
2020 retval_rhs = TREE_OPERAND (retval, 1);
28d81abb 2021 else
6de9cd9a 2022 retval_rhs = retval;
28d81abb 2023
19e7881c
MM
2024 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
2025
4c485b63
JL
2026 /* If the result is an aggregate that is being returned in one (or more)
2027 registers, load the registers here. The compiler currently can't handle
2028 copying a BLKmode value into registers. We could put this code in a
2029 more general area (for use by everyone instead of just function
2030 call/return), but until this feature is generally usable it is kept here
ac45df5d 2031 (and in expand_call). */
4c485b63
JL
2032
2033 if (retval_rhs != 0
2034 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
f8cfc6aa 2035 && REG_P (result_rtl))
4c485b63 2036 {
770ae6cc
RK
2037 int i;
2038 unsigned HOST_WIDE_INT bitpos, xbitpos;
c988af2b 2039 unsigned HOST_WIDE_INT padding_correction = 0;
770ae6cc
RK
2040 unsigned HOST_WIDE_INT bytes
2041 = int_size_in_bytes (TREE_TYPE (retval_rhs));
4c485b63 2042 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
770ae6cc
RK
2043 unsigned int bitsize
2044 = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)), BITS_PER_WORD);
703ad42b 2045 rtx *result_pseudos = alloca (sizeof (rtx) * n_regs);
c16ddde3 2046 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
4c485b63 2047 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
af55da56 2048 enum machine_mode tmpmode, result_reg_mode;
4c485b63 2049
2954d7db
RK
2050 if (bytes == 0)
2051 {
2052 expand_null_return ();
2053 return;
2054 }
2055
c988af2b
RS
2056 /* If the structure doesn't take up a whole number of words, see
2057 whether the register value should be padded on the left or on
2058 the right. Set PADDING_CORRECTION to the number of padding
2059 bits needed on the left side.
2060
2061 In most ABIs, the structure will be returned at the least end of
2062 the register, which translates to right padding on little-endian
2063 targets and left padding on big-endian targets. The opposite
2064 holds if the structure is returned at the most significant
2065 end of the register. */
2066 if (bytes % UNITS_PER_WORD != 0
2067 && (targetm.calls.return_in_msb (TREE_TYPE (retval_rhs))
2068 ? !BYTES_BIG_ENDIAN
2069 : BYTES_BIG_ENDIAN))
2070 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2071 * BITS_PER_UNIT));
a7f875d7 2072
4381f7c2 2073 /* Copy the structure BITSIZE bits at a time. */
c988af2b 2074 for (bitpos = 0, xbitpos = padding_correction;
a7f875d7
RK
2075 bitpos < bytes * BITS_PER_UNIT;
2076 bitpos += bitsize, xbitpos += bitsize)
4c485b63 2077 {
a7f875d7 2078 /* We need a new destination pseudo each time xbitpos is
c988af2b 2079 on a word boundary and when xbitpos == padding_correction
a7f875d7
RK
2080 (the first time through). */
2081 if (xbitpos % BITS_PER_WORD == 0
c988af2b 2082 || xbitpos == padding_correction)
4c485b63 2083 {
a7f875d7
RK
2084 /* Generate an appropriate register. */
2085 dst = gen_reg_rtx (word_mode);
2086 result_pseudos[xbitpos / BITS_PER_WORD] = dst;
2087
8a38ed86
AM
2088 /* Clear the destination before we move anything into it. */
2089 emit_move_insn (dst, CONST0_RTX (GET_MODE (dst)));
4c485b63 2090 }
a7f875d7
RK
2091
2092 /* We need a new source operand each time bitpos is on a word
2093 boundary. */
2094 if (bitpos % BITS_PER_WORD == 0)
2095 src = operand_subword_force (result_val,
2096 bitpos / BITS_PER_WORD,
2097 BLKmode);
2098
2099 /* Use bitpos for the source extraction (left justified) and
2100 xbitpos for the destination store (right justified). */
2101 store_bit_field (dst, bitsize, xbitpos % BITS_PER_WORD, word_mode,
2102 extract_bit_field (src, bitsize,
2103 bitpos % BITS_PER_WORD, 1,
b3520980 2104 NULL_RTX, word_mode, word_mode));
4c485b63
JL
2105 }
2106
c988af2b
RS
2107 tmpmode = GET_MODE (result_rtl);
2108 if (tmpmode == BLKmode)
2109 {
2110 /* Find the smallest integer mode large enough to hold the
2111 entire structure and use that mode instead of BLKmode
2112 on the USE insn for the return register. */
2113 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2114 tmpmode != VOIDmode;
2115 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
2116 /* Have we found a large enough mode? */
2117 if (GET_MODE_SIZE (tmpmode) >= bytes)
2118 break;
4c485b63 2119
c988af2b
RS
2120 /* No suitable mode found. */
2121 if (tmpmode == VOIDmode)
2122 abort ();
4c485b63 2123
c988af2b
RS
2124 PUT_MODE (result_rtl, tmpmode);
2125 }
3ffeb8f1 2126
af55da56
JW
2127 if (GET_MODE_SIZE (tmpmode) < GET_MODE_SIZE (word_mode))
2128 result_reg_mode = word_mode;
2129 else
2130 result_reg_mode = tmpmode;
2131 result_reg = gen_reg_rtx (result_reg_mode);
2132
3ffeb8f1 2133 for (i = 0; i < n_regs; i++)
af55da56 2134 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3ffeb8f1 2135 result_pseudos[i]);
4c485b63 2136
af55da56
JW
2137 if (tmpmode != result_reg_mode)
2138 result_reg = gen_lowpart (tmpmode, result_reg);
2139
4c485b63
JL
2140 expand_value_return (result_reg);
2141 }
7cc8342c
RH
2142 else if (retval_rhs != 0
2143 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
f8cfc6aa 2144 && (REG_P (result_rtl)
7cc8342c 2145 || (GET_CODE (result_rtl) == PARALLEL)))
28d81abb 2146 {
14a774a9
RK
2147 /* Calculate the return value into a temporary (usually a pseudo
2148 reg). */
1da68f56
RK
2149 tree ot = TREE_TYPE (DECL_RESULT (current_function_decl));
2150 tree nt = build_qualified_type (ot, TYPE_QUALS (ot) | TYPE_QUAL_CONST);
2151
2152 val = assign_temp (nt, 0, 0, 1);
dd98f85c
JM
2153 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
2154 val = force_not_mem (val);
ac45df5d 2155 /* Return the calculated value. */
c988af2b 2156 expand_value_return (shift_return_value (val));
28d81abb
RK
2157 }
2158 else
2159 {
ac45df5d 2160 /* No hard reg used; calculate value into hard return reg. */
cba389cd 2161 expand_expr (retval, const0_rtx, VOIDmode, 0);
14a774a9 2162 expand_value_return (result_rtl);
28d81abb
RK
2163 }
2164}
28d81abb 2165\f
28d81abb
RK
2166/* Generate the RTL code for entering a binding contour.
2167 The variables are declared one by one, by calls to `expand_decl'.
2168
8e91754e
MM
2169 FLAGS is a bitwise or of the following flags:
2170
2171 1 - Nonzero if this construct should be visible to
2172 `exit_something'.
2173
2174 2 - Nonzero if this contour does not require a
2175 NOTE_INSN_BLOCK_BEG note. Virtually all calls from
2176 language-independent code should set this flag because they
2177 will not create corresponding BLOCK nodes. (There should be
2178 a one-to-one correspondence between NOTE_INSN_BLOCK_BEG notes
2179 and BLOCKs.) If this flag is set, MARK_ENDS should be zero
4381f7c2 2180 when expand_end_bindings is called.
a97901e6
MM
2181
2182 If we are creating a NOTE_INSN_BLOCK_BEG note, a BLOCK may
2183 optionally be supplied. If so, it becomes the NOTE_BLOCK for the
2184 note. */
28d81abb
RK
2185
2186void
46c5ad27 2187expand_start_bindings_and_block (int flags, tree block)
28d81abb
RK
2188{
2189 struct nesting *thisblock = ALLOC_NESTING ();
8e91754e
MM
2190 rtx note;
2191 int exit_flag = ((flags & 1) != 0);
2192 int block_flag = ((flags & 2) == 0);
4381f7c2 2193
a97901e6
MM
2194 /* If a BLOCK is supplied, then the caller should be requesting a
2195 NOTE_INSN_BLOCK_BEG note. */
2196 if (!block_flag && block)
2197 abort ();
8e91754e 2198
a97901e6 2199 /* Create a note to mark the beginning of the block. */
1ea463a2 2200 note = emit_note (NOTE_INSN_DELETED);
4381f7c2 2201
28d81abb
RK
2202 /* Make an entry on block_stack for the block we are entering. */
2203
e2500fed 2204 thisblock->desc = BLOCK_NESTING;
28d81abb
RK
2205 thisblock->next = block_stack;
2206 thisblock->all = nesting_stack;
2207 thisblock->depth = ++nesting_depth;
3f1d071b 2208 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
e976b8b2
MS
2209
2210 thisblock->data.block.conditional_code = 0;
2211 thisblock->data.block.last_unconditional_cleanup = note;
a571f7a0
MM
2212 /* When we insert instructions after the last unconditional cleanup,
2213 we don't adjust last_insn. That means that a later add_insn will
2214 clobber the instructions we've just added. The easiest way to
2215 fix this is to just insert another instruction here, so that the
2216 instructions inserted after the last unconditional cleanup are
2217 never the last instruction. */
2e040219 2218 emit_note (NOTE_INSN_DELETED);
e976b8b2 2219
28d81abb 2220 thisblock->data.block.first_insn = note;
3f1d071b 2221 thisblock->data.block.block_start_count = ++current_block_start_count;
28d81abb
RK
2222 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
2223 block_stack = thisblock;
2224 nesting_stack = thisblock;
2225
b93a436e
JL
2226 /* Make a new level for allocating stack slots. */
2227 push_temp_slots ();
28d81abb
RK
2228}
2229
e976b8b2
MS
2230/* Specify the scope of temporaries created by TARGET_EXPRs. Similar
2231 to CLEANUP_POINT_EXPR, but handles cases when a series of calls to
2232 expand_expr are made. After we end the region, we know that all
2233 space for all temporaries that were created by TARGET_EXPRs will be
2234 destroyed and their space freed for reuse. */
2235
2236void
46c5ad27 2237expand_start_target_temps (void)
e976b8b2
MS
2238{
2239 /* This is so that even if the result is preserved, the space
2240 allocated will be freed, as we know that it is no longer in use. */
2241 push_temp_slots ();
2242
2243 /* Start a new binding layer that will keep track of all cleanup
2244 actions to be performed. */
8e91754e 2245 expand_start_bindings (2);
e976b8b2
MS
2246
2247 target_temp_slot_level = temp_slot_level;
2248}
2249
2250void
46c5ad27 2251expand_end_target_temps (void)
e976b8b2
MS
2252{
2253 expand_end_bindings (NULL_TREE, 0, 0);
4381f7c2 2254
e976b8b2
MS
2255 /* This is so that even if the result is preserved, the space
2256 allocated will be freed, as we know that it is no longer in use. */
2257 pop_temp_slots ();
2258}
2259
0e9e1e0a 2260/* Given a pointer to a BLOCK node return nonzero if (and only if) the node
deb5e280
JM
2261 in question represents the outermost pair of curly braces (i.e. the "body
2262 block") of a function or method.
2263
2264 For any BLOCK node representing a "body block" of a function or method, the
2265 BLOCK_SUPERCONTEXT of the node will point to another BLOCK node which
2266 represents the outermost (function) scope for the function or method (i.e.
2267 the one which includes the formal parameters). The BLOCK_SUPERCONTEXT of
4381f7c2 2268 *that* node in turn will point to the relevant FUNCTION_DECL node. */
deb5e280
JM
2269
2270int
46c5ad27 2271is_body_block (tree stmt)
deb5e280 2272{
2896d056
ZW
2273 if (lang_hooks.no_body_blocks)
2274 return 0;
2275
deb5e280
JM
2276 if (TREE_CODE (stmt) == BLOCK)
2277 {
2278 tree parent = BLOCK_SUPERCONTEXT (stmt);
2279
2280 if (parent && TREE_CODE (parent) == BLOCK)
2281 {
2282 tree grandparent = BLOCK_SUPERCONTEXT (parent);
2283
2284 if (grandparent && TREE_CODE (grandparent) == FUNCTION_DECL)
2285 return 1;
2286 }
2287 }
2288
2289 return 0;
2290}
2291
91088ddb
JM
2292/* Return an opaque pointer to the current nesting level, so frontend code
2293 can check its own sanity. */
2294
2295struct nesting *
46c5ad27 2296current_nesting_level (void)
91088ddb
JM
2297{
2298 return cfun ? block_stack : 0;
2299}
2300
ba716ac9
BS
2301/* Emit code to restore vital registers at the beginning of a nonlocal goto
2302 handler. */
2303static void
46c5ad27 2304expand_nl_goto_receiver (void)
ba716ac9 2305{
6de9cd9a 2306 /* Clobber the FP when we get here, so we have to make sure it's
e292dbb0
WH
2307 marked as used by this function. */
2308 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
2309
2310 /* Mark the static chain as clobbered here so life information
2311 doesn't get messed up for it. */
2312 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
2313
ba716ac9
BS
2314#ifdef HAVE_nonlocal_goto
2315 if (! HAVE_nonlocal_goto)
2316#endif
2317 /* First adjust our frame pointer to its actual value. It was
2318 previously set to the start of the virtual area corresponding to
2319 the stacked variables when we branched here and now needs to be
2320 adjusted to the actual hardware fp value.
2321
2322 Assignments are to virtual registers are converted by
2323 instantiate_virtual_regs into the corresponding assignment
2324 to the underlying register (fp in this case) that makes
2325 the original assignment true.
2326 So the following insn will actually be
2327 decrementing fp by STARTING_FRAME_OFFSET. */
2328 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
2329
2330#if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
2331 if (fixed_regs[ARG_POINTER_REGNUM])
2332 {
2333#ifdef ELIMINABLE_REGS
2334 /* If the argument pointer can be eliminated in favor of the
2335 frame pointer, we don't need to restore it. We assume here
2336 that if such an elimination is present, it can always be used.
2337 This is the case on all known machines; if we don't make this
2338 assumption, we do unnecessary saving on many machines. */
8b60264b 2339 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
ba716ac9
BS
2340 size_t i;
2341
b6a1cbae 2342 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
ba716ac9
BS
2343 if (elim_regs[i].from == ARG_POINTER_REGNUM
2344 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
2345 break;
2346
b6a1cbae 2347 if (i == ARRAY_SIZE (elim_regs))
ba716ac9
BS
2348#endif
2349 {
2350 /* Now restore our arg pointer from the address at which it
278ed218 2351 was saved in our stack frame. */
ba716ac9 2352 emit_move_insn (virtual_incoming_args_rtx,
278ed218 2353 copy_to_reg (get_arg_pointer_save_area (cfun)));
ba716ac9
BS
2354 }
2355 }
2356#endif
2357
2358#ifdef HAVE_nonlocal_goto_receiver
2359 if (HAVE_nonlocal_goto_receiver)
2360 emit_insn (gen_nonlocal_goto_receiver ());
2361#endif
e292dbb0
WH
2362
2363 /* @@@ This is a kludge. Not all machine descriptions define a blockage
2364 insn, but we must not allow the code we just generated to be reordered
2365 by scheduling. Specifically, the update of the frame pointer must
2366 happen immediately, not later. So emit an ASM_INPUT to act as blockage
2367 insn. */
2368 emit_insn (gen_rtx_ASM_INPUT (VOIDmode, ""));
ba716ac9
BS
2369}
2370
ba716677
MM
2371/* Warn about any unused VARS (which may contain nodes other than
2372 VAR_DECLs, but such nodes are ignored). The nodes are connected
2373 via the TREE_CHAIN field. */
2374
2375void
46c5ad27 2376warn_about_unused_variables (tree vars)
ba716677
MM
2377{
2378 tree decl;
2379
078721e1 2380 if (warn_unused_variable)
ba716677 2381 for (decl = vars; decl; decl = TREE_CHAIN (decl))
4381f7c2 2382 if (TREE_CODE (decl) == VAR_DECL
ba716677
MM
2383 && ! TREE_USED (decl)
2384 && ! DECL_IN_SYSTEM_HEADER (decl)
4381f7c2 2385 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
ddd2d57e 2386 warning ("%Junused variable '%D'", decl, decl);
ba716677
MM
2387}
2388
28d81abb 2389/* Generate RTL code to terminate a binding contour.
e97b5c12
MM
2390
2391 VARS is the chain of VAR_DECL nodes for the variables bound in this
2392 contour. There may actually be other nodes in this chain, but any
2393 nodes other than VAR_DECLS are ignored.
2394
28d81abb
RK
2395 MARK_ENDS is nonzero if we should put a note at the beginning
2396 and end of this binding contour.
2397
cda26058
RK
2398 DONT_JUMP_IN is positive if it is not valid to jump into this contour,
2399 zero if we can jump into this contour only if it does not have a saved
2400 stack level, and negative if we are not to check for invalid use of
2401 labels (because the front end does that). */
28d81abb
RK
2402
2403void
1ea463a2 2404expand_end_bindings (tree vars, int mark_ends ATTRIBUTE_UNUSED,
ac45df5d 2405 int dont_jump_in ATTRIBUTE_UNUSED)
28d81abb 2406{
b3694847 2407 struct nesting *thisblock = block_stack;
e976b8b2 2408
ba716677
MM
2409 /* If any of the variables in this scope were not used, warn the
2410 user. */
2411 warn_about_unused_variables (vars);
28d81abb 2412
28d81abb
RK
2413 if (thisblock->exit_label)
2414 {
2415 do_pending_stack_adjust ();
2416 emit_label (thisblock->exit_label);
2417 }
2418
ac45df5d 2419 /* Mark the beginning and end of the scope if requested. */
c7d2d61d 2420
1ea463a2
RH
2421 /* Get rid of the beginning-mark if we don't make an end-mark. */
2422 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
c7d2d61d 2423
e976b8b2 2424 /* Restore the temporary level of TARGET_EXPRs. */
3f1d071b 2425 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
e976b8b2 2426
28d81abb
RK
2427 /* Restore block_stack level for containing block. */
2428
28d81abb
RK
2429 POPSTACK (block_stack);
2430
2431 /* Pop the stack slot nesting and free any slots at this level. */
2432 pop_temp_slots ();
2433}
2434\f
2435/* Generate RTL for the automatic variable declaration DECL.
ec5cd386 2436 (Other kinds of declarations are simply ignored if seen here.) */
28d81abb
RK
2437
2438void
46c5ad27 2439expand_decl (tree decl)
28d81abb 2440{
ca695ac9
JB
2441 tree type;
2442
ca695ac9 2443 type = TREE_TYPE (decl);
28d81abb 2444
eabb9ed0
RK
2445 /* For a CONST_DECL, set mode, alignment, and sizes from those of the
2446 type in case this node is used in a reference. */
2447 if (TREE_CODE (decl) == CONST_DECL)
2448 {
2449 DECL_MODE (decl) = TYPE_MODE (type);
2450 DECL_ALIGN (decl) = TYPE_ALIGN (type);
2451 DECL_SIZE (decl) = TYPE_SIZE (type);
2452 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
2453 return;
2454 }
28d81abb 2455
eabb9ed0
RK
2456 /* Otherwise, only automatic variables need any expansion done. Static and
2457 external variables, and external functions, will be handled by
2458 `assemble_variable' (called from finish_decl). TYPE_DECL requires
2459 nothing. PARM_DECLs are handled in `assign_parms'. */
28d81abb
RK
2460 if (TREE_CODE (decl) != VAR_DECL)
2461 return;
eabb9ed0 2462
44fe2e80 2463 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
28d81abb
RK
2464 return;
2465
2466 /* Create the RTL representation for the variable. */
2467
2468 if (type == error_mark_node)
19e7881c 2469 SET_DECL_RTL (decl, gen_rtx_MEM (BLKmode, const0_rtx));
1da68f56 2470
28d81abb
RK
2471 else if (DECL_SIZE (decl) == 0)
2472 /* Variable with incomplete type. */
2473 {
abde42f7 2474 rtx x;
28d81abb
RK
2475 if (DECL_INITIAL (decl) == 0)
2476 /* Error message was already done; now avoid a crash. */
abde42f7 2477 x = gen_rtx_MEM (BLKmode, const0_rtx);
28d81abb
RK
2478 else
2479 /* An initializer is going to decide the size of this array.
2480 Until we know the size, represent its address with a reg. */
abde42f7 2481 x = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3bdf5ad1 2482
abde42f7
JH
2483 set_mem_attributes (x, decl, 1);
2484 SET_DECL_RTL (decl, x);
28d81abb 2485 }
8fff4fc1 2486 else if (use_register_for_decl (decl))
28d81abb
RK
2487 {
2488 /* Automatic variable that can go in a register. */
8df83eae 2489 int unsignedp = TYPE_UNSIGNED (type);
28612f9e
RK
2490 enum machine_mode reg_mode
2491 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
98f3b471 2492
19e7881c 2493 SET_DECL_RTL (decl, gen_reg_rtx (reg_mode));
0d4903b8 2494
0b068ee9 2495 /* Note if the object is a user variable. */
7dc8b126 2496 if (!DECL_ARTIFICIAL (decl))
0b068ee9
JL
2497 {
2498 mark_user_reg (DECL_RTL (decl));
2499
2500 /* Trust user variables which have a pointer type to really
2501 be pointers. Do not trust compiler generated temporaries
2502 as our type system is totally busted as it relates to
2503 pointer arithmetic which translates into lots of compiler
2504 generated objects with pointer types, but which are not really
2505 pointers. */
2506 if (POINTER_TYPE_P (type))
2507 mark_reg_pointer (DECL_RTL (decl),
2508 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
2509 }
258a120b
JM
2510
2511 maybe_set_unchanging (DECL_RTL (decl), decl);
28d81abb 2512 }
0df15c2c 2513
4559fd9e 2514 else if (TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
5e4ef18a 2515 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
05bccae2
RK
2516 && 0 < compare_tree_int (DECL_SIZE_UNIT (decl),
2517 STACK_CHECK_MAX_VAR_SIZE)))
28d81abb
RK
2518 {
2519 /* Variable of fixed size that goes on the stack. */
2520 rtx oldaddr = 0;
2521 rtx addr;
0d4903b8 2522 rtx x;
28d81abb
RK
2523
2524 /* If we previously made RTL for this decl, it must be an array
2525 whose size was determined by the initializer.
2526 The old address was a register; set that register now
2527 to the proper address. */
19e7881c 2528 if (DECL_RTL_SET_P (decl))
28d81abb 2529 {
3c0cb5de 2530 if (!MEM_P (DECL_RTL (decl))
f8cfc6aa 2531 || !REG_P (XEXP (DECL_RTL (decl), 0)))
28d81abb
RK
2532 abort ();
2533 oldaddr = XEXP (DECL_RTL (decl), 0);
2534 }
2535
28d81abb
RK
2536 /* Set alignment we actually gave this decl. */
2537 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
2538 : GET_MODE_BITSIZE (DECL_MODE (decl)));
11cf4d18 2539 DECL_USER_ALIGN (decl) = 0;
28d81abb 2540
9432c136 2541 x = assign_temp (decl, 1, 1, 1);
0d4903b8
RK
2542 set_mem_attributes (x, decl, 1);
2543 SET_DECL_RTL (decl, x);
2544
28d81abb
RK
2545 if (oldaddr)
2546 {
2547 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
2548 if (addr != oldaddr)
2549 emit_move_insn (oldaddr, addr);
2550 }
28d81abb
RK
2551 }
2552 else
2553 /* Dynamic-size object: must push space on the stack. */
2554 {
abde42f7 2555 rtx address, size, x;
28d81abb
RK
2556
2557 /* Record the stack pointer on entry to block, if have
2558 not already done so. */
7393c642 2559 do_pending_stack_adjust ();
28d81abb 2560
1c9766da
RK
2561 /* Compute the variable's size, in bytes. This will expand any
2562 needed SAVE_EXPRs for the first time. */
4559fd9e 2563 size = expand_expr (DECL_SIZE_UNIT (decl), NULL_RTX, VOIDmode, 0);
28d81abb
RK
2564 free_temp_slots ();
2565
ff91ad08 2566 /* Allocate space on the stack for the variable. Note that
4381f7c2 2567 DECL_ALIGN says how the variable is to be aligned and we
ff91ad08
RK
2568 cannot use it to conclude anything about the alignment of
2569 the size. */
37366632 2570 address = allocate_dynamic_stack_space (size, NULL_RTX,
ff91ad08 2571 TYPE_ALIGN (TREE_TYPE (decl)));
28d81abb 2572
28d81abb 2573 /* Reference the variable indirect through that rtx. */
abde42f7
JH
2574 x = gen_rtx_MEM (DECL_MODE (decl), address);
2575 set_mem_attributes (x, decl, 1);
2576 SET_DECL_RTL (decl, x);
28d81abb 2577
2207e295 2578
28d81abb
RK
2579 /* Indicate the alignment we actually gave this variable. */
2580#ifdef STACK_BOUNDARY
2581 DECL_ALIGN (decl) = STACK_BOUNDARY;
2582#else
2583 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
2584#endif
11cf4d18 2585 DECL_USER_ALIGN (decl) = 0;
28d81abb 2586 }
28d81abb
RK
2587}
2588\f
6de9cd9a
DN
2589/* Emit code to allocate T_SIZE bytes of dynamic stack space for ALLOC. */
2590void
2591expand_stack_alloc (tree alloc, tree t_size)
2592{
2593 rtx address, dest, size;
2594 tree var, type;
2595
2596 if (TREE_CODE (alloc) != ADDR_EXPR)
2597 abort ();
2598 var = TREE_OPERAND (alloc, 0);
2599 if (TREE_CODE (var) != VAR_DECL)
2600 abort ();
2601
2602 type = TREE_TYPE (var);
2603
6de9cd9a
DN
2604 /* Compute the variable's size, in bytes. */
2605 size = expand_expr (t_size, NULL_RTX, VOIDmode, 0);
2606 free_temp_slots ();
2607
2608 /* Allocate space on the stack for the variable. */
2609 address = XEXP (DECL_RTL (var), 0);
2610 dest = allocate_dynamic_stack_space (size, address, TYPE_ALIGN (type));
2611 if (dest != address)
2612 emit_move_insn (address, dest);
2613
2614 /* Indicate the alignment we actually gave this variable. */
2615#ifdef STACK_BOUNDARY
2616 DECL_ALIGN (var) = STACK_BOUNDARY;
2617#else
2618 DECL_ALIGN (var) = BIGGEST_ALIGNMENT;
2619#endif
2620 DECL_USER_ALIGN (var) = 0;
2621}
2622
2623/* Emit code to save the current value of stack. */
2624rtx
2625expand_stack_save (void)
2626{
2627 rtx ret = NULL_RTX;
2628
2629 do_pending_stack_adjust ();
2630 emit_stack_save (SAVE_BLOCK, &ret, NULL_RTX);
2631 return ret;
2632}
2633
2634/* Emit code to restore the current value of stack. */
2635void
2636expand_stack_restore (tree var)
2637{
2638 rtx sa = DECL_RTL (var);
2639
2640 emit_stack_restore (SAVE_BLOCK, sa, NULL_RTX);
2641}
2642\f
28d81abb
RK
2643/* Emit code to perform the initialization of a declaration DECL. */
2644
2645void
46c5ad27 2646expand_decl_init (tree decl)
28d81abb 2647{
b4ac57ab
RS
2648 int was_used = TREE_USED (decl);
2649
ac79cd5a
RK
2650 /* If this is a CONST_DECL, we don't have to generate any code. Likewise
2651 for static decls. */
2652 if (TREE_CODE (decl) == CONST_DECL
2653 || TREE_STATIC (decl))
28d81abb
RK
2654 return;
2655
2656 /* Compute and store the initial value now. */
2657
59a7f9bf
DJ
2658 push_temp_slots ();
2659
28d81abb
RK
2660 if (DECL_INITIAL (decl) == error_mark_node)
2661 {
2662 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
e5e809f4 2663
28d81abb 2664 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
e5e809f4 2665 || code == POINTER_TYPE || code == REFERENCE_TYPE)
28d81abb 2666 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
b90f141a 2667 0);
28d81abb
RK
2668 }
2669 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
2670 {
f31686a3 2671 emit_line_note (DECL_SOURCE_LOCATION (decl));
b90f141a 2672 expand_assignment (decl, DECL_INITIAL (decl), 0);
28d81abb
RK
2673 }
2674
b4ac57ab
RS
2675 /* Don't let the initialization count as "using" the variable. */
2676 TREE_USED (decl) = was_used;
2677
28d81abb 2678 /* Free any temporaries we made while initializing the decl. */
ae8c59c0 2679 preserve_temp_slots (NULL_RTX);
28d81abb 2680 free_temp_slots ();
59a7f9bf 2681 pop_temp_slots ();
28d81abb
RK
2682}
2683
28d81abb
RK
2684\f
2685/* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
2686 DECL_ELTS is the list of elements that belong to DECL's type.
2687 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
2688
2689void
ac45df5d
RH
2690expand_anon_union_decl (tree decl, tree cleanup ATTRIBUTE_UNUSED,
2691 tree decl_elts)
28d81abb 2692{
28d81abb 2693 rtx x;
8a693bd0 2694 tree t;
28d81abb 2695
8a693bd0
MM
2696 /* If any of the elements are addressable, so is the entire union. */
2697 for (t = decl_elts; t; t = TREE_CHAIN (t))
2698 if (TREE_ADDRESSABLE (TREE_VALUE (t)))
2699 {
2700 TREE_ADDRESSABLE (decl) = 1;
2701 break;
2702 }
4381f7c2 2703
ec5cd386 2704 expand_decl (decl);
28d81abb
RK
2705 x = DECL_RTL (decl);
2706
8a693bd0
MM
2707 /* Go through the elements, assigning RTL to each. */
2708 for (t = decl_elts; t; t = TREE_CHAIN (t))
28d81abb 2709 {
8a693bd0 2710 tree decl_elt = TREE_VALUE (t);
28d81abb
RK
2711 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
2712
3256b817
JJ
2713 /* If any of the elements are addressable, so is the entire
2714 union. */
2715 if (TREE_USED (decl_elt))
2716 TREE_USED (decl) = 1;
2717
7b9032dd
JM
2718 /* Propagate the union's alignment to the elements. */
2719 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
11cf4d18 2720 DECL_USER_ALIGN (decl_elt) = DECL_USER_ALIGN (decl);
7b9032dd
JM
2721
2722 /* If the element has BLKmode and the union doesn't, the union is
2723 aligned such that the element doesn't need to have BLKmode, so
2724 change the element's mode to the appropriate one for its size. */
2725 if (mode == BLKmode && DECL_MODE (decl) != BLKmode)
2726 DECL_MODE (decl_elt) = mode
05bccae2 2727 = mode_for_size_tree (DECL_SIZE (decl_elt), MODE_INT, 1);
7b9032dd 2728
28d81abb
RK
2729 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
2730 instead create a new MEM rtx with the proper mode. */
3c0cb5de 2731 if (MEM_P (x))
28d81abb
RK
2732 {
2733 if (mode == GET_MODE (x))
19e7881c 2734 SET_DECL_RTL (decl_elt, x);
28d81abb 2735 else
f1ec5147 2736 SET_DECL_RTL (decl_elt, adjust_address_nv (x, mode, 0));
28d81abb 2737 }
f8cfc6aa 2738 else if (REG_P (x))
28d81abb
RK
2739 {
2740 if (mode == GET_MODE (x))
19e7881c 2741 SET_DECL_RTL (decl_elt, x);
28d81abb 2742 else
ddef6bc7 2743 SET_DECL_RTL (decl_elt, gen_lowpart_SUBREG (mode, x));
28d81abb
RK
2744 }
2745 else
2746 abort ();
28d81abb
RK
2747 }
2748}
2749\f
28d81abb
RK
2750/* Enter a case (Pascal) or switch (C) statement.
2751 Push a block onto case_stack and nesting_stack
2752 to accumulate the case-labels that are seen
2753 and to record the labels generated for the statement.
2754
2755 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
2756 Otherwise, this construct is transparent for `exit_something'.
2757
2758 EXPR is the index-expression to be dispatched on.
2759 TYPE is its nominal type. We could simply convert EXPR to this type,
2760 but instead we take short cuts. */
2761
2762void
46c5ad27
AJ
2763expand_start_case (int exit_flag, tree expr, tree type,
2764 const char *printname)
28d81abb 2765{
b3694847 2766 struct nesting *thiscase = ALLOC_NESTING ();
28d81abb
RK
2767
2768 /* Make an entry on case_stack for the case we are entering. */
2769
e2500fed 2770 thiscase->desc = CASE_NESTING;
28d81abb
RK
2771 thiscase->next = case_stack;
2772 thiscase->all = nesting_stack;
2773 thiscase->depth = ++nesting_depth;
2774 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
2775 thiscase->data.case_stmt.case_list = 0;
2776 thiscase->data.case_stmt.index_expr = expr;
2777 thiscase->data.case_stmt.nominal_type = type;
2778 thiscase->data.case_stmt.default_label = 0;
28d81abb 2779 thiscase->data.case_stmt.printname = printname;
a11759a3 2780 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
28d81abb
RK
2781 case_stack = thiscase;
2782 nesting_stack = thiscase;
2783
2784 do_pending_stack_adjust ();
2785
2786 /* Make sure case_stmt.start points to something that won't
2787 need any transformation before expand_end_case. */
4b4bf941 2788 if (!NOTE_P (get_last_insn ()))
2e040219 2789 emit_note (NOTE_INSN_DELETED);
28d81abb
RK
2790
2791 thiscase->data.case_stmt.start = get_last_insn ();
2792}
a11759a3 2793
28d81abb
RK
2794/* Accumulate one case or default label inside a case or switch statement.
2795 VALUE is the value of the case (a null pointer, for a default label).
f52fba84
PE
2796 The function CONVERTER, when applied to arguments T and V,
2797 converts the value V to the type T.
28d81abb
RK
2798
2799 If not currently inside a case or switch statement, return 1 and do
2800 nothing. The caller will print a language-specific error message.
2801 If VALUE is a duplicate or overlaps, return 2 and do nothing
2802 except store the (first) duplicate node in *DUPLICATE.
2803 If VALUE is out of range, return 3 and do nothing.
28d81abb
RK
2804 Return 0 on success.
2805
2806 Extended to handle range statements. */
2807
2808int
46c5ad27
AJ
2809pushcase (tree value, tree (*converter) (tree, tree), tree label,
2810 tree *duplicate)
28d81abb 2811{
28d81abb
RK
2812 tree index_type;
2813 tree nominal_type;
2814
2815 /* Fail if not inside a real case statement. */
2816 if (! (case_stack && case_stack->data.case_stmt.start))
2817 return 1;
2818
28d81abb
RK
2819 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
2820 nominal_type = case_stack->data.case_stmt.nominal_type;
2821
2822 /* If the index is erroneous, avoid more problems: pretend to succeed. */
2823 if (index_type == error_mark_node)
2824 return 0;
2825
2f985ca6
JW
2826 /* Convert VALUE to the type in which the comparisons are nominally done. */
2827 if (value != 0)
2828 value = (*converter) (nominal_type, value);
2829
28d81abb
RK
2830 /* Fail if this value is out of range for the actual type of the index
2831 (which may be narrower than NOMINAL_TYPE). */
14a774a9
RK
2832 if (value != 0
2833 && (TREE_CONSTANT_OVERFLOW (value)
2834 || ! int_fits_type_p (value, index_type)))
28d81abb
RK
2835 return 3;
2836
6de9cd9a 2837 return add_case_node (value, value, label, duplicate, false);
28d81abb
RK
2838}
2839
956d6950
JL
2840/* Like pushcase but this case applies to all values between VALUE1 and
2841 VALUE2 (inclusive). If VALUE1 is NULL, the range starts at the lowest
2842 value of the index type and ends at VALUE2. If VALUE2 is NULL, the range
2843 starts at VALUE1 and ends at the highest value of the index type.
2844 If both are NULL, this case applies to all values.
2845
2846 The return value is the same as that of pushcase but there is one
2847 additional error code: 4 means the specified range was empty. */
28d81abb
RK
2848
2849int
46c5ad27
AJ
2850pushcase_range (tree value1, tree value2, tree (*converter) (tree, tree),
2851 tree label, tree *duplicate)
28d81abb 2852{
28d81abb
RK
2853 tree index_type;
2854 tree nominal_type;
2855
2856 /* Fail if not inside a real case statement. */
2857 if (! (case_stack && case_stack->data.case_stmt.start))
2858 return 1;
2859
28d81abb
RK
2860 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
2861 nominal_type = case_stack->data.case_stmt.nominal_type;
2862
2863 /* If the index is erroneous, avoid more problems: pretend to succeed. */
2864 if (index_type == error_mark_node)
2865 return 0;
2866
956d6950
JL
2867 /* Convert VALUEs to type in which the comparisons are nominally done
2868 and replace any unspecified value with the corresponding bound. */
2869 if (value1 == 0)
1974bfb1 2870 value1 = TYPE_MIN_VALUE (index_type);
956d6950 2871 if (value2 == 0)
1974bfb1 2872 value2 = TYPE_MAX_VALUE (index_type);
956d6950
JL
2873
2874 /* Fail if the range is empty. Do this before any conversion since
2875 we want to allow out-of-range empty ranges. */
14a774a9 2876 if (value2 != 0 && tree_int_cst_lt (value2, value1))
956d6950
JL
2877 return 4;
2878
4381f7c2 2879 /* If the max was unbounded, use the max of the nominal_type we are
e1ee5cdc
RH
2880 converting to. Do this after the < check above to suppress false
2881 positives. */
14a774a9 2882 if (value2 == 0)
e1ee5cdc 2883 value2 = TYPE_MAX_VALUE (nominal_type);
28d81abb 2884
2f985ca6
JW
2885 value1 = (*converter) (nominal_type, value1);
2886 value2 = (*converter) (nominal_type, value2);
2887
28d81abb 2888 /* Fail if these values are out of range. */
956d6950
JL
2889 if (TREE_CONSTANT_OVERFLOW (value1)
2890 || ! int_fits_type_p (value1, index_type))
28d81abb
RK
2891 return 3;
2892
956d6950
JL
2893 if (TREE_CONSTANT_OVERFLOW (value2)
2894 || ! int_fits_type_p (value2, index_type))
28d81abb
RK
2895 return 3;
2896
6de9cd9a 2897 return add_case_node (value1, value2, label, duplicate, false);
57641239
RK
2898}
2899
2900/* Do the actual insertion of a case label for pushcase and pushcase_range
2901 into case_stack->data.case_stmt.case_list. Use an AVL tree to avoid
2902 slowdown for large switch statements. */
2903
56cb9733 2904int
6de9cd9a
DN
2905add_case_node (tree low, tree high, tree label, tree *duplicate,
2906 bool dont_expand_label)
57641239
RK
2907{
2908 struct case_node *p, **q, *r;
2909
56cb9733
MM
2910 /* If there's no HIGH value, then this is not a case range; it's
2911 just a simple case label. But that's just a degenerate case
2912 range. */
2913 if (!high)
2914 high = low;
2915
2916 /* Handle default labels specially. */
2917 if (!high && !low)
2918 {
2919 if (case_stack->data.case_stmt.default_label != 0)
2920 {
2921 *duplicate = case_stack->data.case_stmt.default_label;
2922 return 2;
2923 }
2924 case_stack->data.case_stmt.default_label = label;
6de9cd9a
DN
2925 if (!dont_expand_label)
2926 expand_label (label);
56cb9733
MM
2927 return 0;
2928 }
2929
57641239
RK
2930 q = &case_stack->data.case_stmt.case_list;
2931 p = *q;
2932
69d4ca36 2933 while ((r = *q))
28d81abb 2934 {
57641239
RK
2935 p = r;
2936
2937 /* Keep going past elements distinctly greater than HIGH. */
2938 if (tree_int_cst_lt (high, p->low))
2939 q = &p->left;
2940
2941 /* or distinctly less than LOW. */
2942 else if (tree_int_cst_lt (p->high, low))
2943 q = &p->right;
2944
2945 else
28d81abb 2946 {
57641239
RK
2947 /* We have an overlap; this is an error. */
2948 *duplicate = p->code_label;
28d81abb
RK
2949 return 2;
2950 }
2951 }
2952
1da68f56 2953 /* Add this label to the chain, and succeed. */
28d81abb 2954
703ad42b 2955 r = ggc_alloc (sizeof (struct case_node));
1da68f56 2956 r->low = low;
28d81abb 2957
57641239 2958 /* If the bounds are equal, turn this into the one-value case. */
57641239
RK
2959 if (tree_int_cst_equal (low, high))
2960 r->high = r->low;
2961 else
1da68f56 2962 r->high = high;
57641239
RK
2963
2964 r->code_label = label;
6de9cd9a
DN
2965 if (!dont_expand_label)
2966 expand_label (label);
28d81abb 2967
57641239
RK
2968 *q = r;
2969 r->parent = p;
2970 r->left = 0;
2971 r->right = 0;
2972 r->balance = 0;
2973
2974 while (p)
2975 {
2976 struct case_node *s;
2977
2978 if (r == p->left)
2979 {
2980 int b;
2981
2982 if (! (b = p->balance))
2983 /* Growth propagation from left side. */
2984 p->balance = -1;
2985 else if (b < 0)
2986 {
2987 if (r->balance < 0)
2988 {
2989 /* R-Rotation */
51723711 2990 if ((p->left = s = r->right))
57641239
RK
2991 s->parent = p;
2992
2993 r->right = p;
2994 p->balance = 0;
2995 r->balance = 0;
2996 s = p->parent;
2997 p->parent = r;
2998
51723711 2999 if ((r->parent = s))
57641239
RK
3000 {
3001 if (s->left == p)
3002 s->left = r;
3003 else
3004 s->right = r;
3005 }
3006 else
3007 case_stack->data.case_stmt.case_list = r;
3008 }
3009 else
3010 /* r->balance == +1 */
3011 {
5720c7e7
RK
3012 /* LR-Rotation */
3013
57641239
RK
3014 int b2;
3015 struct case_node *t = r->right;
3016
51723711 3017 if ((p->left = s = t->right))
57641239
RK
3018 s->parent = p;
3019
3020 t->right = p;
51723711 3021 if ((r->right = s = t->left))
57641239
RK
3022 s->parent = r;
3023
3024 t->left = r;
3025 b = t->balance;
3026 b2 = b < 0;
3027 p->balance = b2;
3028 b2 = -b2 - b;
3029 r->balance = b2;
3030 t->balance = 0;
3031 s = p->parent;
3032 p->parent = t;
3033 r->parent = t;
3034
51723711 3035 if ((t->parent = s))
57641239
RK
3036 {
3037 if (s->left == p)
3038 s->left = t;
3039 else
3040 s->right = t;
3041 }
3042 else
3043 case_stack->data.case_stmt.case_list = t;
3044 }
3045 break;
3046 }
3047
3048 else
3049 {
3050 /* p->balance == +1; growth of left side balances the node. */
3051 p->balance = 0;
3052 break;
3053 }
3054 }
3055 else
3056 /* r == p->right */
3057 {
3058 int b;
3059
3060 if (! (b = p->balance))
3061 /* Growth propagation from right side. */
3062 p->balance++;
3063 else if (b > 0)
3064 {
3065 if (r->balance > 0)
3066 {
3067 /* L-Rotation */
3068
51723711 3069 if ((p->right = s = r->left))
57641239
RK
3070 s->parent = p;
3071
3072 r->left = p;
3073 p->balance = 0;
3074 r->balance = 0;
3075 s = p->parent;
3076 p->parent = r;
51723711 3077 if ((r->parent = s))
57641239
RK
3078 {
3079 if (s->left == p)
3080 s->left = r;
3081 else
3082 s->right = r;
3083 }
3084
3085 else
3086 case_stack->data.case_stmt.case_list = r;
3087 }
3088
3089 else
3090 /* r->balance == -1 */
3091 {
3092 /* RL-Rotation */
3093 int b2;
3094 struct case_node *t = r->left;
3095
51723711 3096 if ((p->right = s = t->left))
57641239
RK
3097 s->parent = p;
3098
3099 t->left = p;
3100
51723711 3101 if ((r->left = s = t->right))
57641239
RK
3102 s->parent = r;
3103
3104 t->right = r;
3105 b = t->balance;
3106 b2 = b < 0;
3107 r->balance = b2;
3108 b2 = -b2 - b;
3109 p->balance = b2;
3110 t->balance = 0;
3111 s = p->parent;
3112 p->parent = t;
3113 r->parent = t;
3114
51723711 3115 if ((t->parent = s))
57641239
RK
3116 {
3117 if (s->left == p)
3118 s->left = t;
3119 else
3120 s->right = t;
3121 }
3122
3123 else
3124 case_stack->data.case_stmt.case_list = t;
3125 }
3126 break;
3127 }
3128 else
3129 {
3130 /* p->balance == -1; growth of right side balances the node. */
3131 p->balance = 0;
3132 break;
3133 }
3134 }
3135
3136 r = p;
3137 p = p->parent;
3138 }
28d81abb
RK
3139
3140 return 0;
3141}
28d81abb 3142\f
9bb231fd
RS
3143/* Maximum number of case bit tests. */
3144#define MAX_CASE_BIT_TESTS 3
3145
3146/* By default, enable case bit tests on targets with ashlsi3. */
3147#ifndef CASE_USE_BIT_TESTS
3148#define CASE_USE_BIT_TESTS (ashl_optab->handlers[word_mode].insn_code \
3149 != CODE_FOR_nothing)
3150#endif
3151
3152
3153/* A case_bit_test represents a set of case nodes that may be
3154 selected from using a bit-wise comparison. HI and LO hold
3155 the integer to be tested against, LABEL contains the label
3156 to jump to upon success and BITS counts the number of case
3157 nodes handled by this test, typically the number of bits
3158 set in HI:LO. */
3159
3160struct case_bit_test
3161{
3162 HOST_WIDE_INT hi;
3163 HOST_WIDE_INT lo;
3164 rtx label;
3165 int bits;
3166};
3167
3168/* Determine whether "1 << x" is relatively cheap in word_mode. */
3169
7e51717c
AJ
3170static
3171bool lshift_cheap_p (void)
9bb231fd
RS
3172{
3173 static bool init = false;
3174 static bool cheap = true;
3175
3176 if (!init)
3177 {
3178 rtx reg = gen_rtx_REG (word_mode, 10000);
3179 int cost = rtx_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg), SET);
3180 cheap = cost < COSTS_N_INSNS (3);
3181 init = true;
3182 }
3183
3184 return cheap;
3185}
3186
3187/* Comparison function for qsort to order bit tests by decreasing
3188 number of case nodes, i.e. the node with the most cases gets
3189 tested first. */
3190
f667741c
SB
3191static int
3192case_bit_test_cmp (const void *p1, const void *p2)
9bb231fd
RS
3193{
3194 const struct case_bit_test *d1 = p1;
3195 const struct case_bit_test *d2 = p2;
3196
3197 return d2->bits - d1->bits;
3198}
3199
3200/* Expand a switch statement by a short sequence of bit-wise
3201 comparisons. "switch(x)" is effectively converted into
3202 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
3203 integer constants.
3204
3205 INDEX_EXPR is the value being switched on, which is of
3206 type INDEX_TYPE. MINVAL is the lowest case value of in
3207 the case nodes, of INDEX_TYPE type, and RANGE is highest
3208 value minus MINVAL, also of type INDEX_TYPE. NODES is
3209 the set of case nodes, and DEFAULT_LABEL is the label to
3210 branch to should none of the cases match.
3211
3212 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
3213 node targets. */
3214
3215static void
46c5ad27
AJ
3216emit_case_bit_tests (tree index_type, tree index_expr, tree minval,
3217 tree range, case_node_ptr nodes, rtx default_label)
9bb231fd
RS
3218{
3219 struct case_bit_test test[MAX_CASE_BIT_TESTS];
3220 enum machine_mode mode;
3221 rtx expr, index, label;
3222 unsigned int i,j,lo,hi;
3223 struct case_node *n;
3224 unsigned int count;
3225
3226 count = 0;
3227 for (n = nodes; n; n = n->right)
3228 {
3229 label = label_rtx (n->code_label);
3230 for (i = 0; i < count; i++)
3231 if (same_case_target_p (label, test[i].label))
3232 break;
3233
3234 if (i == count)
3235 {
3236 if (count >= MAX_CASE_BIT_TESTS)
3237 abort ();
3238 test[i].hi = 0;
3239 test[i].lo = 0;
3240 test[i].label = label;
3241 test[i].bits = 1;
3242 count++;
3243 }
3244 else
3245 test[i].bits++;
3246
3247 lo = tree_low_cst (fold (build (MINUS_EXPR, index_type,
3248 n->low, minval)), 1);
3249 hi = tree_low_cst (fold (build (MINUS_EXPR, index_type,
3250 n->high, minval)), 1);
3251 for (j = lo; j <= hi; j++)
3252 if (j >= HOST_BITS_PER_WIDE_INT)
3253 test[i].hi |= (HOST_WIDE_INT) 1 << (j - HOST_BITS_PER_INT);
3254 else
3255 test[i].lo |= (HOST_WIDE_INT) 1 << j;
3256 }
3257
3258 qsort (test, count, sizeof(*test), case_bit_test_cmp);
3259
3260 index_expr = fold (build (MINUS_EXPR, index_type,
3261 convert (index_type, index_expr),
3262 convert (index_type, minval)));
3263 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
9bb231fd
RS
3264 do_pending_stack_adjust ();
3265
3266 mode = TYPE_MODE (index_type);
3267 expr = expand_expr (range, NULL_RTX, VOIDmode, 0);
3268 emit_cmp_and_jump_insns (index, expr, GTU, NULL_RTX, mode, 1,
3269 default_label);
3270
3271 index = convert_to_mode (word_mode, index, 0);
3272 index = expand_binop (word_mode, ashl_optab, const1_rtx,
3273 index, NULL_RTX, 1, OPTAB_WIDEN);
3274
3275 for (i = 0; i < count; i++)
3276 {
3277 expr = immed_double_const (test[i].lo, test[i].hi, word_mode);
3278 expr = expand_binop (word_mode, and_optab, index, expr,
3279 NULL_RTX, 1, OPTAB_WIDEN);
3280 emit_cmp_and_jump_insns (expr, const0_rtx, NE, NULL_RTX,
3281 word_mode, 1, test[i].label);
3282 }
3283
3284 emit_jump (default_label);
3285}
ad82abb8 3286
41cbdcd0
KH
3287#ifndef HAVE_casesi
3288#define HAVE_casesi 0
3289#endif
3290
3291#ifndef HAVE_tablejump
3292#define HAVE_tablejump 0
3293#endif
3294
28d81abb 3295/* Terminate a case (Pascal) or switch (C) statement
9ab0ddd7 3296 in which ORIG_INDEX is the expression to be tested.
6f9fdf4d
JJ
3297 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
3298 type as given in the source before any compiler conversions.
28d81abb
RK
3299 Generate the code to test it and jump to the right place. */
3300
3301void
46c5ad27 3302expand_end_case_type (tree orig_index, tree orig_type)
28d81abb 3303{
9fb60a0d 3304 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
28d81abb 3305 rtx default_label = 0;
9bb231fd
RS
3306 struct case_node *n, *m;
3307 unsigned int count, uniq;
28d81abb 3308 rtx index;
ca695ac9 3309 rtx table_label;
28d81abb
RK
3310 int ncases;
3311 rtx *labelvec;
b3694847 3312 int i;
9bb231fd 3313 rtx before_case, end, lab;
b3694847 3314 struct nesting *thiscase = case_stack;
1b0cb6fc 3315 tree index_expr, index_type;
100e3acb 3316 bool exit_done = false;
ca695ac9
JB
3317 int unsignedp;
3318
03c03770
AS
3319 /* Don't crash due to previous errors. */
3320 if (thiscase == NULL)
3321 return;
3322
ca695ac9 3323 index_expr = thiscase->data.case_stmt.index_expr;
1b0cb6fc 3324 index_type = TREE_TYPE (index_expr);
8df83eae 3325 unsignedp = TYPE_UNSIGNED (index_type);
6f9fdf4d
JJ
3326 if (orig_type == NULL)
3327 orig_type = TREE_TYPE (orig_index);
28d81abb
RK
3328
3329 do_pending_stack_adjust ();
3330
3331 /* An ERROR_MARK occurs for various reasons including invalid data type. */
1b0cb6fc 3332 if (index_type != error_mark_node)
28d81abb 3333 {
28d81abb
RK
3334 /* If we don't have a default-label, create one here,
3335 after the body of the switch. */
3336 if (thiscase->data.case_stmt.default_label == 0)
3337 {
3338 thiscase->data.case_stmt.default_label
3339 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
100e3acb
RS
3340 /* Share the exit label if possible. */
3341 if (thiscase->exit_label)
3342 {
3343 SET_DECL_RTL (thiscase->data.case_stmt.default_label,
3344 thiscase->exit_label);
3345 exit_done = true;
3346 }
28d81abb
RK
3347 expand_label (thiscase->data.case_stmt.default_label);
3348 }
3349 default_label = label_rtx (thiscase->data.case_stmt.default_label);
3350
3351 before_case = get_last_insn ();
3352
5720c7e7
RK
3353 if (thiscase->data.case_stmt.case_list
3354 && thiscase->data.case_stmt.case_list->left)
b059139c 3355 thiscase->data.case_stmt.case_list
4381f7c2 3356 = case_tree2list (thiscase->data.case_stmt.case_list, 0);
b059139c 3357
28d81abb
RK
3358 /* Simplify the case-list before we count it. */
3359 group_case_nodes (thiscase->data.case_stmt.case_list);
100e3acb
RS
3360 strip_default_case_nodes (&thiscase->data.case_stmt.case_list,
3361 default_label);
28d81abb
RK
3362
3363 /* Get upper and lower bounds of case values.
3364 Also convert all the case values to the index expr's data type. */
3365
9bb231fd 3366 uniq = 0;
28d81abb
RK
3367 count = 0;
3368 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
3369 {
3370 /* Check low and high label values are integers. */
3371 if (TREE_CODE (n->low) != INTEGER_CST)
3372 abort ();
3373 if (TREE_CODE (n->high) != INTEGER_CST)
3374 abort ();
3375
1b0cb6fc
RK
3376 n->low = convert (index_type, n->low);
3377 n->high = convert (index_type, n->high);
28d81abb
RK
3378
3379 /* Count the elements and track the largest and smallest
3380 of them (treating them as signed even if they are not). */
3381 if (count++ == 0)
3382 {
3383 minval = n->low;
3384 maxval = n->high;
3385 }
3386 else
3387 {
3388 if (INT_CST_LT (n->low, minval))
3389 minval = n->low;
3390 if (INT_CST_LT (maxval, n->high))
3391 maxval = n->high;
3392 }
3393 /* A range counts double, since it requires two compares. */
3394 if (! tree_int_cst_equal (n->low, n->high))
3395 count++;
9bb231fd
RS
3396
3397 /* Count the number of unique case node targets. */
3398 uniq++;
3399 lab = label_rtx (n->code_label);
3400 for (m = thiscase->data.case_stmt.case_list; m != n; m = m->right)
3401 if (same_case_target_p (label_rtx (m->code_label), lab))
3402 {
3403 uniq--;
3404 break;
3405 }
28d81abb
RK
3406 }
3407
3408 /* Compute span of values. */
3409 if (count != 0)
1b0cb6fc 3410 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
28d81abb 3411
1b0cb6fc 3412 if (count == 0)
28d81abb
RK
3413 {
3414 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
28d81abb
RK
3415 emit_jump (default_label);
3416 }
3474db0e 3417
9bb231fd
RS
3418 /* Try implementing this switch statement by a short sequence of
3419 bit-wise comparisons. However, we let the binary-tree case
3420 below handle constant index expressions. */
3421 else if (CASE_USE_BIT_TESTS
3422 && ! TREE_CONSTANT (index_expr)
3423 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
766dec0e 3424 && compare_tree_int (range, 0) > 0
9bb231fd
RS
3425 && lshift_cheap_p ()
3426 && ((uniq == 1 && count >= 3)
3427 || (uniq == 2 && count >= 5)
3428 || (uniq == 3 && count >= 6)))
3429 {
3430 /* Optimize the case where all the case values fit in a
3431 word without having to subtract MINVAL. In this case,
3432 we can optimize away the subtraction. */
3433 if (compare_tree_int (minval, 0) > 0
3434 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
3435 {
3436 minval = integer_zero_node;
3437 range = maxval;
3438 }
3439 emit_case_bit_tests (index_type, index_expr, minval, range,
3440 thiscase->data.case_stmt.case_list,
3441 default_label);
3442 }
3443
28d81abb
RK
3444 /* If range of values is much bigger than number of values,
3445 make a sequence of conditional branches instead of a dispatch.
3446 If the switch-index is a constant, do it this way
3447 because we can optimize it. */
4f73c5dd 3448
ad82abb8 3449 else if (count < case_values_threshold ()
9e4b13a7
SB
3450 || compare_tree_int (range,
3451 (optimize_size ? 3 : 10) * count) > 0
f0c988c8
BS
3452 /* RANGE may be signed, and really large ranges will show up
3453 as negative numbers. */
3454 || compare_tree_int (range, 0) < 0
3f6fe18e
RK
3455#ifndef ASM_OUTPUT_ADDR_DIFF_ELT
3456 || flag_pic
3457#endif
41cbdcd0
KH
3458 || TREE_CONSTANT (index_expr)
3459 /* If neither casesi or tablejump is available, we can
3460 only go this way. */
3461 || (!HAVE_casesi && !HAVE_tablejump))
28d81abb 3462 {
37366632 3463 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
28d81abb
RK
3464
3465 /* If the index is a short or char that we do not have
3466 an insn to handle comparisons directly, convert it to
3467 a full integer now, rather than letting each comparison
3468 generate the conversion. */
3469
3470 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
ef89d648 3471 && ! have_insn_for (COMPARE, GET_MODE (index)))
28d81abb
RK
3472 {
3473 enum machine_mode wider_mode;
3474 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
3475 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
ef89d648 3476 if (have_insn_for (COMPARE, wider_mode))
28d81abb
RK
3477 {
3478 index = convert_to_mode (wider_mode, index, unsignedp);
3479 break;
3480 }
3481 }
3482
28d81abb
RK
3483 do_pending_stack_adjust ();
3484
3c0cb5de 3485 if (MEM_P (index))
28d81abb
RK
3486 index = copy_to_reg (index);
3487 if (GET_CODE (index) == CONST_INT
3488 || TREE_CODE (index_expr) == INTEGER_CST)
3489 {
3490 /* Make a tree node with the proper constant value
3491 if we don't already have one. */
3492 if (TREE_CODE (index_expr) != INTEGER_CST)
3493 {
3494 index_expr
3495 = build_int_2 (INTVAL (index),
e9a042b6 3496 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
1b0cb6fc 3497 index_expr = convert (index_type, index_expr);
28d81abb
RK
3498 }
3499
3500 /* For constant index expressions we need only
4fe9b91c 3501 issue an unconditional branch to the appropriate
28d81abb 3502 target code. The job of removing any unreachable
6356f892 3503 code is left to the optimization phase if the
28d81abb 3504 "-O" option is specified. */
1b0cb6fc
RK
3505 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
3506 if (! tree_int_cst_lt (index_expr, n->low)
3507 && ! tree_int_cst_lt (n->high, index_expr))
3508 break;
3509
28d81abb
RK
3510 if (n)
3511 emit_jump (label_rtx (n->code_label));
3512 else
3513 emit_jump (default_label);
3514 }
3515 else
3516 {
3517 /* If the index expression is not constant we generate
3518 a binary decision tree to select the appropriate
3519 target code. This is done as follows:
3520
3521 The list of cases is rearranged into a binary tree,
3522 nearly optimal assuming equal probability for each case.
3523
3524 The tree is transformed into RTL, eliminating
3525 redundant test conditions at the same time.
3526
3527 If program flow could reach the end of the
3528 decision tree an unconditional jump to the
3529 default code is emitted. */
3530
3531 use_cost_table
6f9fdf4d 3532 = (TREE_CODE (orig_type) != ENUMERAL_TYPE
28d81abb 3533 && estimate_case_costs (thiscase->data.case_stmt.case_list));
9714cf43 3534 balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
28d81abb 3535 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
1b0cb6fc 3536 default_label, index_type);
28d81abb
RK
3537 emit_jump_if_reachable (default_label);
3538 }
3539 }
3540 else
3541 {
100e3acb 3542 table_label = gen_label_rtx ();
ad82abb8
ZW
3543 if (! try_casesi (index_type, index_expr, minval, range,
3544 table_label, default_label))
28d81abb 3545 {
ecc9dd93 3546 index_type = thiscase->data.case_stmt.nominal_type;
1ff37128 3547
786de7eb 3548 /* Index jumptables from zero for suitable values of
1ff37128 3549 minval to avoid a subtraction. */
786de7eb
KH
3550 if (! optimize_size
3551 && compare_tree_int (minval, 0) > 0
3552 && compare_tree_int (minval, 3) < 0)
3553 {
3554 minval = integer_zero_node;
3555 range = maxval;
3556 }
1ff37128 3557
ad82abb8
ZW
3558 if (! try_tablejump (index_type, index_expr, minval, range,
3559 table_label, default_label))
3560 abort ();
28d81abb 3561 }
786de7eb 3562
28d81abb
RK
3563 /* Get table of labels to jump to, in order of case index. */
3564
1ff37128 3565 ncases = tree_low_cst (range, 0) + 1;
703ad42b
KG
3566 labelvec = alloca (ncases * sizeof (rtx));
3567 memset (labelvec, 0, ncases * sizeof (rtx));
28d81abb
RK
3568
3569 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
3570 {
2d9d49e4
OH
3571 /* Compute the low and high bounds relative to the minimum
3572 value since that should fit in a HOST_WIDE_INT while the
3573 actual values may not. */
3574 HOST_WIDE_INT i_low
786de7eb
KH
3575 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
3576 n->low, minval)), 1);
2d9d49e4 3577 HOST_WIDE_INT i_high
786de7eb
KH
3578 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
3579 n->high, minval)), 1);
2d9d49e4
OH
3580 HOST_WIDE_INT i;
3581
3582 for (i = i_low; i <= i_high; i ++)
3583 labelvec[i]
3584 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
28d81abb
RK
3585 }
3586
3587 /* Fill in the gaps with the default. */
3588 for (i = 0; i < ncases; i++)
3589 if (labelvec[i] == 0)
38a448ca 3590 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
28d81abb 3591
f9da5064 3592 /* Output the table. */
28d81abb
RK
3593 emit_label (table_label);
3594
18543a22 3595 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
38a448ca
RH
3596 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
3597 gen_rtx_LABEL_REF (Pmode, table_label),
33f7f353 3598 gen_rtvec_v (ncases, labelvec),
4381f7c2 3599 const0_rtx, const0_rtx));
28d81abb 3600 else
38a448ca
RH
3601 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
3602 gen_rtvec_v (ncases, labelvec)));
28d81abb
RK
3603
3604 /* If the case insn drops through the table,
3605 after the table we must jump to the default-label.
3606 Otherwise record no drop-through after the table. */
3607#ifdef CASE_DROPS_THROUGH
3608 emit_jump (default_label);
3609#else
3610 emit_barrier ();
3611#endif
3612 }
3613
2270623a
JM
3614 before_case = NEXT_INSN (before_case);
3615 end = get_last_insn ();
2b7d71b2
JJ
3616 if (squeeze_notes (&before_case, &end))
3617 abort ();
2270623a 3618 reorder_insns (before_case, end,
28d81abb
RK
3619 thiscase->data.case_stmt.start);
3620 }
1b0cb6fc 3621
100e3acb 3622 if (thiscase->exit_label && !exit_done)
28d81abb
RK
3623 emit_label (thiscase->exit_label);
3624
3625 POPSTACK (case_stack);
3626
3627 free_temp_slots ();
3628}
3629
57641239
RK
3630/* Convert the tree NODE into a list linked by the right field, with the left
3631 field zeroed. RIGHT is used for recursion; it is a list to be placed
3632 rightmost in the resulting list. */
3633
3634static struct case_node *
46c5ad27 3635case_tree2list (struct case_node *node, struct case_node *right)
57641239
RK
3636{
3637 struct case_node *left;
3638
3639 if (node->right)
3640 right = case_tree2list (node->right, right);
3641
3642 node->right = right;
51723711 3643 if ((left = node->left))
57641239
RK
3644 {
3645 node->left = 0;
3646 return case_tree2list (left, node);
3647 }
3648
3649 return node;
3650}
ca695ac9 3651
28d81abb
RK
3652/* Generate code to jump to LABEL if OP1 and OP2 are equal. */
3653
3654static void
46c5ad27 3655do_jump_if_equal (rtx op1, rtx op2, rtx label, int unsignedp)
28d81abb 3656{
d43e0b7d 3657 if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
28d81abb 3658 {
d51d146f 3659 if (op1 == op2)
28d81abb
RK
3660 emit_jump (label);
3661 }
3662 else
d43e0b7d
RK
3663 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX,
3664 (GET_MODE (op1) == VOIDmode
3665 ? GET_MODE (op2) : GET_MODE (op1)),
3666 unsignedp, label);
28d81abb
RK
3667}
3668\f
3669/* Not all case values are encountered equally. This function
3670 uses a heuristic to weight case labels, in cases where that
3671 looks like a reasonable thing to do.
3672
3673 Right now, all we try to guess is text, and we establish the
3674 following weights:
3675
3676 chars above space: 16
3677 digits: 16
3678 default: 12
3679 space, punct: 8
3680 tab: 4
3681 newline: 2
3682 other "\" chars: 1
3683 remaining chars: 0
3684
3685 If we find any cases in the switch that are not either -1 or in the range
3686 of valid ASCII characters, or are control characters other than those
3687 commonly used with "\", don't treat this switch scanning text.
3688
3689 Return 1 if these nodes are suitable for cost estimation, otherwise
3690 return 0. */
3691
3692static int
46c5ad27 3693estimate_case_costs (case_node_ptr node)
28d81abb 3694{
f2d1f0ba 3695 tree min_ascii = integer_minus_one_node;
28d81abb
RK
3696 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
3697 case_node_ptr n;
3698 int i;
3699
3700 /* If we haven't already made the cost table, make it now. Note that the
3701 lower bound of the table is -1, not zero. */
3702
2a2137c4 3703 if (! cost_table_initialized)
28d81abb 3704 {
2a2137c4 3705 cost_table_initialized = 1;
28d81abb
RK
3706
3707 for (i = 0; i < 128; i++)
3708 {
e9a780ec 3709 if (ISALNUM (i))
2a2137c4 3710 COST_TABLE (i) = 16;
e9a780ec 3711 else if (ISPUNCT (i))
2a2137c4 3712 COST_TABLE (i) = 8;
e9a780ec 3713 else if (ISCNTRL (i))
2a2137c4 3714 COST_TABLE (i) = -1;
28d81abb
RK
3715 }
3716
2a2137c4
RH
3717 COST_TABLE (' ') = 8;
3718 COST_TABLE ('\t') = 4;
3719 COST_TABLE ('\0') = 4;
3720 COST_TABLE ('\n') = 2;
3721 COST_TABLE ('\f') = 1;
3722 COST_TABLE ('\v') = 1;
3723 COST_TABLE ('\b') = 1;
28d81abb
RK
3724 }
3725
3726 /* See if all the case expressions look like text. It is text if the
3727 constant is >= -1 and the highest constant is <= 127. Do all comparisons
3728 as signed arithmetic since we don't want to ever access cost_table with a
3729 value less than -1. Also check that none of the constants in a range
3730 are strange control characters. */
3731
3732 for (n = node; n; n = n->right)
3733 {
3734 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
3735 return 0;
3736
05bccae2
RK
3737 for (i = (HOST_WIDE_INT) TREE_INT_CST_LOW (n->low);
3738 i <= (HOST_WIDE_INT) TREE_INT_CST_LOW (n->high); i++)
2a2137c4 3739 if (COST_TABLE (i) < 0)
28d81abb
RK
3740 return 0;
3741 }
3742
3743 /* All interesting values are within the range of interesting
3744 ASCII characters. */
3745 return 1;
3746}
3747
100e3acb
RS
3748/* Determine whether two case labels branch to the same target. */
3749
3750static bool
46c5ad27 3751same_case_target_p (rtx l1, rtx l2)
100e3acb 3752{
6de9cd9a 3753#if 0
100e3acb
RS
3754 rtx i1, i2;
3755
3756 if (l1 == l2)
3757 return true;
3758
3759 i1 = next_real_insn (l1);
3760 i2 = next_real_insn (l2);
3761 if (i1 == i2)
3762 return true;
3763
3764 if (i1 && simplejump_p (i1))
3765 {
3766 l1 = XEXP (SET_SRC (PATTERN (i1)), 0);
3767 }
3768
3769 if (i2 && simplejump_p (i2))
3770 {
3771 l2 = XEXP (SET_SRC (PATTERN (i2)), 0);
3772 }
6de9cd9a
DN
3773#endif
3774 /* When coming from gimple, we usually won't have emitted either
3775 the labels or the body of the switch statement. The job being
3776 done here should be done via jump threading at the tree level.
3777 Cases that go the same place should have the same label. */
100e3acb
RS
3778 return l1 == l2;
3779}
3780
3781/* Delete nodes that branch to the default label from a list of
3782 case nodes. Eg. case 5: default: becomes just default: */
3783
3784static void
46c5ad27 3785strip_default_case_nodes (case_node_ptr *prev, rtx deflab)
100e3acb
RS
3786{
3787 case_node_ptr ptr;
3788
3789 while (*prev)
3790 {
3791 ptr = *prev;
3792 if (same_case_target_p (label_rtx (ptr->code_label), deflab))
3793 *prev = ptr->right;
3794 else
3795 prev = &ptr->right;
3796 }
3797}
3798
28d81abb
RK
3799/* Scan an ordered list of case nodes
3800 combining those with consecutive values or ranges.
3801
3802 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
3803
3804static void
46c5ad27 3805group_case_nodes (case_node_ptr head)
28d81abb
RK
3806{
3807 case_node_ptr node = head;
3808
3809 while (node)
3810 {
6de9cd9a 3811 rtx lab;
28d81abb
RK
3812 case_node_ptr np = node;
3813
6de9cd9a
DN
3814 lab = label_rtx (node->code_label);
3815
28d81abb
RK
3816 /* Try to group the successors of NODE with NODE. */
3817 while (((np = np->right) != 0)
3818 /* Do they jump to the same place? */
100e3acb 3819 && same_case_target_p (label_rtx (np->code_label), lab)
28d81abb
RK
3820 /* Are their ranges consecutive? */
3821 && tree_int_cst_equal (np->low,
3822 fold (build (PLUS_EXPR,
3823 TREE_TYPE (node->high),
3824 node->high,
3825 integer_one_node)))
3826 /* An overflow is not consecutive. */
3827 && tree_int_cst_lt (node->high,
3828 fold (build (PLUS_EXPR,
3829 TREE_TYPE (node->high),
3830 node->high,
3831 integer_one_node))))
3832 {
3833 node->high = np->high;
3834 }
3835 /* NP is the first node after NODE which can't be grouped with it.
3836 Delete the nodes in between, and move on to that node. */
3837 node->right = np;
3838 node = np;
3839 }
3840}
3841
3842/* Take an ordered list of case nodes
3843 and transform them into a near optimal binary tree,
6dc42e49 3844 on the assumption that any target code selection value is as
28d81abb
RK
3845 likely as any other.
3846
3847 The transformation is performed by splitting the ordered
3848 list into two equal sections plus a pivot. The parts are
3849 then attached to the pivot as left and right branches. Each
38e01259 3850 branch is then transformed recursively. */
28d81abb
RK
3851
3852static void
46c5ad27 3853balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
28d81abb 3854{
b3694847 3855 case_node_ptr np;
28d81abb
RK
3856
3857 np = *head;
3858 if (np)
3859 {
3860 int cost = 0;
3861 int i = 0;
3862 int ranges = 0;
b3694847 3863 case_node_ptr *npp;
28d81abb
RK
3864 case_node_ptr left;
3865
3866 /* Count the number of entries on branch. Also count the ranges. */
3867
3868 while (np)
3869 {
3870 if (!tree_int_cst_equal (np->low, np->high))
3871 {
3872 ranges++;
3873 if (use_cost_table)
2a2137c4 3874 cost += COST_TABLE (TREE_INT_CST_LOW (np->high));
28d81abb
RK
3875 }
3876
3877 if (use_cost_table)
2a2137c4 3878 cost += COST_TABLE (TREE_INT_CST_LOW (np->low));
28d81abb
RK
3879
3880 i++;
3881 np = np->right;
3882 }
3883
3884 if (i > 2)
3885 {
3886 /* Split this list if it is long enough for that to help. */
3887 npp = head;
3888 left = *npp;
3889 if (use_cost_table)
3890 {
3891 /* Find the place in the list that bisects the list's total cost,
3892 Here I gets half the total cost. */
3893 int n_moved = 0;
3894 i = (cost + 1) / 2;
3895 while (1)
3896 {
3897 /* Skip nodes while their cost does not reach that amount. */
3898 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
2a2137c4
RH
3899 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->high));
3900 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->low));
28d81abb
RK
3901 if (i <= 0)
3902 break;
3903 npp = &(*npp)->right;
3904 n_moved += 1;
3905 }
3906 if (n_moved == 0)
3907 {
3908 /* Leave this branch lopsided, but optimize left-hand
3909 side and fill in `parent' fields for right-hand side. */
3910 np = *head;
3911 np->parent = parent;
3912 balance_case_nodes (&np->left, np);
3913 for (; np->right; np = np->right)
3914 np->right->parent = np;
3915 return;
3916 }
3917 }
3918 /* If there are just three nodes, split at the middle one. */
3919 else if (i == 3)
3920 npp = &(*npp)->right;
3921 else
3922 {
3923 /* Find the place in the list that bisects the list's total cost,
3924 where ranges count as 2.
3925 Here I gets half the total cost. */
3926 i = (i + ranges + 1) / 2;
3927 while (1)
3928 {
3929 /* Skip nodes while their cost does not reach that amount. */
3930 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
3931 i--;
3932 i--;
3933 if (i <= 0)
3934 break;
3935 npp = &(*npp)->right;
3936 }
3937 }
3938 *head = np = *npp;
3939 *npp = 0;
3940 np->parent = parent;
3941 np->left = left;
3942
3943 /* Optimize each of the two split parts. */
3944 balance_case_nodes (&np->left, np);
3945 balance_case_nodes (&np->right, np);
3946 }
3947 else
3948 {
3949 /* Else leave this branch as one level,
3950 but fill in `parent' fields. */
3951 np = *head;
3952 np->parent = parent;
3953 for (; np->right; np = np->right)
3954 np->right->parent = np;
3955 }
3956 }
3957}
3958\f
3959/* Search the parent sections of the case node tree
3960 to see if a test for the lower bound of NODE would be redundant.
3961 INDEX_TYPE is the type of the index expression.
3962
3963 The instructions to generate the case decision tree are
3964 output in the same order as nodes are processed so it is
3965 known that if a parent node checks the range of the current
3966 node minus one that the current node is bounded at its lower
3967 span. Thus the test would be redundant. */
3968
3969static int
46c5ad27 3970node_has_low_bound (case_node_ptr node, tree index_type)
28d81abb
RK
3971{
3972 tree low_minus_one;
3973 case_node_ptr pnode;
3974
3975 /* If the lower bound of this node is the lowest value in the index type,
3976 we need not test it. */
3977
3978 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
3979 return 1;
3980
3981 /* If this node has a left branch, the value at the left must be less
3982 than that at this node, so it cannot be bounded at the bottom and
3983 we need not bother testing any further. */
3984
3985 if (node->left)
3986 return 0;
3987
3988 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
3989 node->low, integer_one_node));
3990
3991 /* If the subtraction above overflowed, we can't verify anything.
3992 Otherwise, look for a parent that tests our value - 1. */
3993
3994 if (! tree_int_cst_lt (low_minus_one, node->low))
3995 return 0;
3996
3997 for (pnode = node->parent; pnode; pnode = pnode->parent)
3998 if (tree_int_cst_equal (low_minus_one, pnode->high))
3999 return 1;
4000
4001 return 0;
4002}
4003
4004/* Search the parent sections of the case node tree
4005 to see if a test for the upper bound of NODE would be redundant.
4006 INDEX_TYPE is the type of the index expression.
4007
4008 The instructions to generate the case decision tree are
4009 output in the same order as nodes are processed so it is
4010 known that if a parent node checks the range of the current
4011 node plus one that the current node is bounded at its upper
4012 span. Thus the test would be redundant. */
4013
4014static int
46c5ad27 4015node_has_high_bound (case_node_ptr node, tree index_type)
28d81abb
RK
4016{
4017 tree high_plus_one;
4018 case_node_ptr pnode;
4019
e1ee5cdc
RH
4020 /* If there is no upper bound, obviously no test is needed. */
4021
4022 if (TYPE_MAX_VALUE (index_type) == NULL)
4023 return 1;
4024
28d81abb
RK
4025 /* If the upper bound of this node is the highest value in the type
4026 of the index expression, we need not test against it. */
4027
4028 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
4029 return 1;
4030
4031 /* If this node has a right branch, the value at the right must be greater
4032 than that at this node, so it cannot be bounded at the top and
4033 we need not bother testing any further. */
4034
4035 if (node->right)
4036 return 0;
4037
4038 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
4039 node->high, integer_one_node));
4040
4041 /* If the addition above overflowed, we can't verify anything.
4042 Otherwise, look for a parent that tests our value + 1. */
4043
4044 if (! tree_int_cst_lt (node->high, high_plus_one))
4045 return 0;
4046
4047 for (pnode = node->parent; pnode; pnode = pnode->parent)
4048 if (tree_int_cst_equal (high_plus_one, pnode->low))
4049 return 1;
4050
4051 return 0;
4052}
4053
4054/* Search the parent sections of the
4055 case node tree to see if both tests for the upper and lower
4056 bounds of NODE would be redundant. */
4057
4058static int
46c5ad27 4059node_is_bounded (case_node_ptr node, tree index_type)
28d81abb
RK
4060{
4061 return (node_has_low_bound (node, index_type)
4062 && node_has_high_bound (node, index_type));
4063}
4064
4065/* Emit an unconditional jump to LABEL unless it would be dead code. */
4066
4067static void
46c5ad27 4068emit_jump_if_reachable (rtx label)
28d81abb 4069{
4b4bf941 4070 if (!BARRIER_P (get_last_insn ()))
28d81abb
RK
4071 emit_jump (label);
4072}
4073\f
4074/* Emit step-by-step code to select a case for the value of INDEX.
4075 The thus generated decision tree follows the form of the
4076 case-node binary tree NODE, whose nodes represent test conditions.
4077 INDEX_TYPE is the type of the index of the switch.
4078
4079 Care is taken to prune redundant tests from the decision tree
4080 by detecting any boundary conditions already checked by
4081 emitted rtx. (See node_has_high_bound, node_has_low_bound
4082 and node_is_bounded, above.)
4083
4084 Where the test conditions can be shown to be redundant we emit
4085 an unconditional jump to the target code. As a further
4086 optimization, the subordinates of a tree node are examined to
4087 check for bounded nodes. In this case conditional and/or
4088 unconditional jumps as a result of the boundary check for the
4089 current node are arranged to target the subordinates associated
38e01259 4090 code for out of bound conditions on the current node.
28d81abb 4091
f72aed24 4092 We can assume that when control reaches the code generated here,
28d81abb
RK
4093 the index value has already been compared with the parents
4094 of this node, and determined to be on the same side of each parent
4095 as this node is. Thus, if this node tests for the value 51,
4096 and a parent tested for 52, we don't need to consider
4097 the possibility of a value greater than 51. If another parent
4098 tests for the value 50, then this node need not test anything. */
4099
4100static void
46c5ad27
AJ
4101emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
4102 tree index_type)
28d81abb
RK
4103{
4104 /* If INDEX has an unsigned type, we must make unsigned branches. */
8df83eae 4105 int unsignedp = TYPE_UNSIGNED (index_type);
28d81abb 4106 enum machine_mode mode = GET_MODE (index);
69107307 4107 enum machine_mode imode = TYPE_MODE (index_type);
28d81abb
RK
4108
4109 /* See if our parents have already tested everything for us.
4110 If they have, emit an unconditional jump for this node. */
4111 if (node_is_bounded (node, index_type))
4112 emit_jump (label_rtx (node->code_label));
4113
4114 else if (tree_int_cst_equal (node->low, node->high))
4115 {
4116 /* Node is single valued. First see if the index expression matches
0f41302f 4117 this node and then check our children, if any. */
28d81abb 4118
69107307
AO
4119 do_jump_if_equal (index,
4120 convert_modes (mode, imode,
4121 expand_expr (node->low, NULL_RTX,
4122 VOIDmode, 0),
4123 unsignedp),
28d81abb
RK
4124 label_rtx (node->code_label), unsignedp);
4125
4126 if (node->right != 0 && node->left != 0)
4127 {
4128 /* This node has children on both sides.
4129 Dispatch to one side or the other
4130 by comparing the index value with this node's value.
4131 If one subtree is bounded, check that one first,
4132 so we can avoid real branches in the tree. */
4133
4134 if (node_is_bounded (node->right, index_type))
4135 {
4381f7c2 4136 emit_cmp_and_jump_insns (index,
69107307
AO
4137 convert_modes
4138 (mode, imode,
4139 expand_expr (node->high, NULL_RTX,
4140 VOIDmode, 0),
4141 unsignedp),
d43e0b7d 4142 GT, NULL_RTX, mode, unsignedp,
4381f7c2 4143 label_rtx (node->right->code_label));
28d81abb
RK
4144 emit_case_nodes (index, node->left, default_label, index_type);
4145 }
4146
4147 else if (node_is_bounded (node->left, index_type))
4148 {
4381f7c2 4149 emit_cmp_and_jump_insns (index,
69107307
AO
4150 convert_modes
4151 (mode, imode,
4152 expand_expr (node->high, NULL_RTX,
4153 VOIDmode, 0),
4154 unsignedp),
d43e0b7d 4155 LT, NULL_RTX, mode, unsignedp,
c5d5d461 4156 label_rtx (node->left->code_label));
28d81abb
RK
4157 emit_case_nodes (index, node->right, default_label, index_type);
4158 }
4159
43a21dfc
KH
4160 /* If both children are single-valued cases with no
4161 children, finish up all the work. This way, we can save
4162 one ordered comparison. */
4163 else if (tree_int_cst_equal (node->right->low, node->right->high)
4164 && node->right->left == 0
4165 && node->right->right == 0
4166 && tree_int_cst_equal (node->left->low, node->left->high)
4167 && node->left->left == 0
4168 && node->left->right == 0)
4169 {
4170 /* Neither node is bounded. First distinguish the two sides;
4171 then emit the code for one side at a time. */
4172
4173 /* See if the value matches what the right hand side
4174 wants. */
4175 do_jump_if_equal (index,
4176 convert_modes (mode, imode,
4177 expand_expr (node->right->low,
4178 NULL_RTX,
4179 VOIDmode, 0),
4180 unsignedp),
4181 label_rtx (node->right->code_label),
4182 unsignedp);
4183
4184 /* See if the value matches what the left hand side
4185 wants. */
4186 do_jump_if_equal (index,
4187 convert_modes (mode, imode,
4188 expand_expr (node->left->low,
4189 NULL_RTX,
4190 VOIDmode, 0),
4191 unsignedp),
4192 label_rtx (node->left->code_label),
4193 unsignedp);
4194 }
4195
28d81abb
RK
4196 else
4197 {
4198 /* Neither node is bounded. First distinguish the two sides;
4199 then emit the code for one side at a time. */
4200
4381f7c2 4201 tree test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
28d81abb
RK
4202
4203 /* See if the value is on the right. */
4381f7c2 4204 emit_cmp_and_jump_insns (index,
69107307
AO
4205 convert_modes
4206 (mode, imode,
4207 expand_expr (node->high, NULL_RTX,
4208 VOIDmode, 0),
4209 unsignedp),
d43e0b7d 4210 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4211 label_rtx (test_label));
28d81abb
RK
4212
4213 /* Value must be on the left.
4214 Handle the left-hand subtree. */
4215 emit_case_nodes (index, node->left, default_label, index_type);
4216 /* If left-hand subtree does nothing,
4217 go to default. */
4218 emit_jump_if_reachable (default_label);
4219
4220 /* Code branches here for the right-hand subtree. */
4221 expand_label (test_label);
4222 emit_case_nodes (index, node->right, default_label, index_type);
4223 }
4224 }
4225
4226 else if (node->right != 0 && node->left == 0)
4227 {
4228 /* Here we have a right child but no left so we issue conditional
4229 branch to default and process the right child.
4230
4231 Omit the conditional branch to default if we it avoid only one
4232 right child; it costs too much space to save so little time. */
4233
de14fd73 4234 if (node->right->right || node->right->left
28d81abb
RK
4235 || !tree_int_cst_equal (node->right->low, node->right->high))
4236 {
4237 if (!node_has_low_bound (node, index_type))
4238 {
4381f7c2 4239 emit_cmp_and_jump_insns (index,
69107307
AO
4240 convert_modes
4241 (mode, imode,
4242 expand_expr (node->high, NULL_RTX,
4243 VOIDmode, 0),
4244 unsignedp),
d43e0b7d 4245 LT, NULL_RTX, mode, unsignedp,
c5d5d461 4246 default_label);
28d81abb
RK
4247 }
4248
4249 emit_case_nodes (index, node->right, default_label, index_type);
4250 }
4251 else
4252 /* We cannot process node->right normally
4253 since we haven't ruled out the numbers less than
4254 this node's value. So handle node->right explicitly. */
4255 do_jump_if_equal (index,
69107307
AO
4256 convert_modes
4257 (mode, imode,
4258 expand_expr (node->right->low, NULL_RTX,
4259 VOIDmode, 0),
4260 unsignedp),
28d81abb
RK
4261 label_rtx (node->right->code_label), unsignedp);
4262 }
4263
4264 else if (node->right == 0 && node->left != 0)
4265 {
4266 /* Just one subtree, on the left. */
4381f7c2 4267 if (node->left->left || node->left->right
28d81abb
RK
4268 || !tree_int_cst_equal (node->left->low, node->left->high))
4269 {
4270 if (!node_has_high_bound (node, index_type))
4271 {
69107307
AO
4272 emit_cmp_and_jump_insns (index,
4273 convert_modes
4274 (mode, imode,
4275 expand_expr (node->high, NULL_RTX,
4276 VOIDmode, 0),
4277 unsignedp),
d43e0b7d 4278 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4279 default_label);
28d81abb
RK
4280 }
4281
4282 emit_case_nodes (index, node->left, default_label, index_type);
4283 }
4284 else
4285 /* We cannot process node->left normally
4286 since we haven't ruled out the numbers less than
4287 this node's value. So handle node->left explicitly. */
4288 do_jump_if_equal (index,
69107307
AO
4289 convert_modes
4290 (mode, imode,
4291 expand_expr (node->left->low, NULL_RTX,
4292 VOIDmode, 0),
4293 unsignedp),
28d81abb
RK
4294 label_rtx (node->left->code_label), unsignedp);
4295 }
4296 }
4297 else
4298 {
4299 /* Node is a range. These cases are very similar to those for a single
4300 value, except that we do not start by testing whether this node
4301 is the one to branch to. */
4302
4303 if (node->right != 0 && node->left != 0)
4304 {
4305 /* Node has subtrees on both sides.
4306 If the right-hand subtree is bounded,
4307 test for it first, since we can go straight there.
4308 Otherwise, we need to make a branch in the control structure,
4309 then handle the two subtrees. */
4310 tree test_label = 0;
4311
28d81abb
RK
4312 if (node_is_bounded (node->right, index_type))
4313 /* Right hand node is fully bounded so we can eliminate any
4314 testing and branch directly to the target code. */
69107307
AO
4315 emit_cmp_and_jump_insns (index,
4316 convert_modes
4317 (mode, imode,
4318 expand_expr (node->high, NULL_RTX,
4319 VOIDmode, 0),
4320 unsignedp),
d43e0b7d 4321 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4322 label_rtx (node->right->code_label));
28d81abb
RK
4323 else
4324 {
4325 /* Right hand node requires testing.
4326 Branch to a label where we will handle it later. */
4327
4328 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4381f7c2 4329 emit_cmp_and_jump_insns (index,
69107307
AO
4330 convert_modes
4331 (mode, imode,
4332 expand_expr (node->high, NULL_RTX,
4333 VOIDmode, 0),
4334 unsignedp),
d43e0b7d 4335 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4336 label_rtx (test_label));
28d81abb
RK
4337 }
4338
4339 /* Value belongs to this node or to the left-hand subtree. */
4340
69107307
AO
4341 emit_cmp_and_jump_insns (index,
4342 convert_modes
4343 (mode, imode,
4344 expand_expr (node->low, NULL_RTX,
4345 VOIDmode, 0),
4346 unsignedp),
d43e0b7d 4347 GE, NULL_RTX, mode, unsignedp,
c5d5d461 4348 label_rtx (node->code_label));
28d81abb
RK
4349
4350 /* Handle the left-hand subtree. */
4351 emit_case_nodes (index, node->left, default_label, index_type);
4352
4353 /* If right node had to be handled later, do that now. */
4354
4355 if (test_label)
4356 {
4357 /* If the left-hand subtree fell through,
4358 don't let it fall into the right-hand subtree. */
4359 emit_jump_if_reachable (default_label);
4360
4361 expand_label (test_label);
4362 emit_case_nodes (index, node->right, default_label, index_type);
4363 }
4364 }
4365
4366 else if (node->right != 0 && node->left == 0)
4367 {
4368 /* Deal with values to the left of this node,
4369 if they are possible. */
4370 if (!node_has_low_bound (node, index_type))
4371 {
4381f7c2 4372 emit_cmp_and_jump_insns (index,
69107307
AO
4373 convert_modes
4374 (mode, imode,
4375 expand_expr (node->low, NULL_RTX,
4376 VOIDmode, 0),
4377 unsignedp),
d43e0b7d 4378 LT, NULL_RTX, mode, unsignedp,
c5d5d461 4379 default_label);
28d81abb
RK
4380 }
4381
4382 /* Value belongs to this node or to the right-hand subtree. */
4383
69107307
AO
4384 emit_cmp_and_jump_insns (index,
4385 convert_modes
4386 (mode, imode,
4387 expand_expr (node->high, NULL_RTX,
4388 VOIDmode, 0),
4389 unsignedp),
d43e0b7d 4390 LE, NULL_RTX, mode, unsignedp,
c5d5d461 4391 label_rtx (node->code_label));
28d81abb
RK
4392
4393 emit_case_nodes (index, node->right, default_label, index_type);
4394 }
4395
4396 else if (node->right == 0 && node->left != 0)
4397 {
4398 /* Deal with values to the right of this node,
4399 if they are possible. */
4400 if (!node_has_high_bound (node, index_type))
4401 {
4381f7c2 4402 emit_cmp_and_jump_insns (index,
69107307
AO
4403 convert_modes
4404 (mode, imode,
4405 expand_expr (node->high, NULL_RTX,
4406 VOIDmode, 0),
4407 unsignedp),
d43e0b7d 4408 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4409 default_label);
28d81abb
RK
4410 }
4411
4412 /* Value belongs to this node or to the left-hand subtree. */
4413
4381f7c2 4414 emit_cmp_and_jump_insns (index,
69107307
AO
4415 convert_modes
4416 (mode, imode,
4417 expand_expr (node->low, NULL_RTX,
4418 VOIDmode, 0),
4419 unsignedp),
d43e0b7d 4420 GE, NULL_RTX, mode, unsignedp,
c5d5d461 4421 label_rtx (node->code_label));
28d81abb
RK
4422
4423 emit_case_nodes (index, node->left, default_label, index_type);
4424 }
4425
4426 else
4427 {
4428 /* Node has no children so we check low and high bounds to remove
4429 redundant tests. Only one of the bounds can exist,
4430 since otherwise this node is bounded--a case tested already. */
923cbdc3
JH
4431 int high_bound = node_has_high_bound (node, index_type);
4432 int low_bound = node_has_low_bound (node, index_type);
28d81abb 4433
923cbdc3 4434 if (!high_bound && low_bound)
28d81abb 4435 {
4381f7c2 4436 emit_cmp_and_jump_insns (index,
69107307
AO
4437 convert_modes
4438 (mode, imode,
4439 expand_expr (node->high, NULL_RTX,
4440 VOIDmode, 0),
4441 unsignedp),
d43e0b7d 4442 GT, NULL_RTX, mode, unsignedp,
c5d5d461 4443 default_label);
28d81abb
RK
4444 }
4445
923cbdc3 4446 else if (!low_bound && high_bound)
28d81abb 4447 {
4381f7c2 4448 emit_cmp_and_jump_insns (index,
69107307
AO
4449 convert_modes
4450 (mode, imode,
4451 expand_expr (node->low, NULL_RTX,
4452 VOIDmode, 0),
4453 unsignedp),
d43e0b7d 4454 LT, NULL_RTX, mode, unsignedp,
c5d5d461 4455 default_label);
28d81abb 4456 }
923cbdc3
JH
4457 else if (!low_bound && !high_bound)
4458 {
9312aecc 4459 /* Widen LOW and HIGH to the same width as INDEX. */
ae2bcd98 4460 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
9312aecc
JDA
4461 tree low = build1 (CONVERT_EXPR, type, node->low);
4462 tree high = build1 (CONVERT_EXPR, type, node->high);
ef89d648 4463 rtx low_rtx, new_index, new_bound;
9312aecc
JDA
4464
4465 /* Instead of doing two branches, emit one unsigned branch for
4466 (index-low) > (high-low). */
ef89d648
ZW
4467 low_rtx = expand_expr (low, NULL_RTX, mode, 0);
4468 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
4469 NULL_RTX, unsignedp,
4470 OPTAB_WIDEN);
9312aecc
JDA
4471 new_bound = expand_expr (fold (build (MINUS_EXPR, type,
4472 high, low)),
4473 NULL_RTX, mode, 0);
786de7eb 4474
9312aecc 4475 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
d43e0b7d 4476 mode, 1, default_label);
923cbdc3 4477 }
28d81abb
RK
4478
4479 emit_jump (label_rtx (node->code_label));
4480 }
4481 }
4482}
e2500fed
GK
4483
4484#include "gt-stmt.h"
This page took 3.456218 seconds and 5 git commands to generate.