* gcc_release (build_sources): Use two new variables EXPORTTAG and
[official-gcc.git] / gcc / stmt.c
blobdefbb70df21fa26ca37cd2dd9eda1311cbad3f89
1 /* Expands front end tree to back end RTL for GCC
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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.
27 The functions whose names start with `expand_' are called by the
28 parser to generate RTL instructions for various kinds of constructs.
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. */
36 #include "config.h"
37 #include "system.h"
38 #include "coretypes.h"
39 #include "tm.h"
41 #include "rtl.h"
42 #include "tree.h"
43 #include "tm_p.h"
44 #include "flags.h"
45 #include "except.h"
46 #include "function.h"
47 #include "insn-config.h"
48 #include "expr.h"
49 #include "libfuncs.h"
50 #include "hard-reg-set.h"
51 #include "loop.h"
52 #include "recog.h"
53 #include "machmode.h"
54 #include "toplev.h"
55 #include "output.h"
56 #include "ggc.h"
57 #include "langhooks.h"
58 #include "predict.h"
59 #include "optabs.h"
61 /* Assume that case vectors are not pc-relative. */
62 #ifndef CASE_VECTOR_PC_RELATIVE
63 #define CASE_VECTOR_PC_RELATIVE 0
64 #endif
66 /* Functions and data structures for expanding case statements. */
68 /* Case label structure, used to hold info on labels within case
69 statements. We handle "range" labels; for a single-value label
70 as in C, the high and low limits are the same.
72 An AVL tree of case nodes is initially created, and later transformed
73 to a list linked via the RIGHT fields in the nodes. Nodes with
74 higher case values are later in the list.
76 Switch statements can be output in one of two forms. A branch table
77 is used if there are more than a few labels and the labels are dense
78 within the range between the smallest and largest case value. If a
79 branch table is used, no further manipulations are done with the case
80 node chain.
82 The alternative to the use of a branch table is to generate a series
83 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
84 and PARENT fields to hold a binary tree. Initially the tree is
85 totally unbalanced, with everything on the right. We balance the tree
86 with nodes on the left having lower case values than the parent
87 and nodes on the right having higher values. We then output the tree
88 in order. */
90 struct case_node GTY(())
92 struct case_node *left; /* Left son in binary tree */
93 struct case_node *right; /* Right son in binary tree; also node chain */
94 struct case_node *parent; /* Parent of node in binary tree */
95 tree low; /* Lowest index value for this label */
96 tree high; /* Highest index value for this label */
97 tree code_label; /* Label to jump to when node matches */
98 int balance;
101 typedef struct case_node case_node;
102 typedef struct case_node *case_node_ptr;
104 /* These are used by estimate_case_costs and balance_case_nodes. */
106 /* This must be a signed type, and non-ANSI compilers lack signed char. */
107 static short cost_table_[129];
108 static int use_cost_table;
109 static int cost_table_initialized;
111 /* Special care is needed because we allow -1, but TREE_INT_CST_LOW
112 is unsigned. */
113 #define COST_TABLE(I) cost_table_[(unsigned HOST_WIDE_INT) ((I) + 1)]
115 /* Stack of control and binding constructs we are currently inside.
117 These constructs begin when you call `expand_start_WHATEVER'
118 and end when you call `expand_end_WHATEVER'. This stack records
119 info about how the construct began that tells the end-function
120 what to do. It also may provide information about the construct
121 to alter the behavior of other constructs within the body.
122 For example, they may affect the behavior of C `break' and `continue'.
124 Each construct gets one `struct nesting' object.
125 All of these objects are chained through the `all' field.
126 `nesting_stack' points to the first object (innermost construct).
127 The position of an entry on `nesting_stack' is in its `depth' field.
129 Each type of construct has its own individual stack.
130 For example, loops have `loop_stack'. Each object points to the
131 next object of the same type through the `next' field.
133 Some constructs are visible to `break' exit-statements and others
134 are not. Which constructs are visible depends on the language.
135 Therefore, the data structure allows each construct to be visible
136 or not, according to the args given when the construct is started.
137 The construct is visible if the `exit_label' field is non-null.
138 In that case, the value should be a CODE_LABEL rtx. */
140 struct nesting GTY(())
142 struct nesting *all;
143 struct nesting *next;
144 int depth;
145 rtx exit_label;
146 enum nesting_desc {
147 COND_NESTING,
148 LOOP_NESTING,
149 BLOCK_NESTING,
150 CASE_NESTING
151 } desc;
152 union nesting_u
154 /* For conds (if-then and if-then-else statements). */
155 struct nesting_cond
157 /* Label for the end of the if construct.
158 There is none if EXITFLAG was not set
159 and no `else' has been seen yet. */
160 rtx endif_label;
161 /* Label for the end of this alternative.
162 This may be the end of the if or the next else/elseif. */
163 rtx next_label;
164 } GTY ((tag ("COND_NESTING"))) cond;
165 /* For loops. */
166 struct nesting_loop
168 /* Label at the top of the loop; place to loop back to. */
169 rtx start_label;
170 /* Label at the end of the whole construct. */
171 rtx end_label;
172 /* Label for `continue' statement to jump to;
173 this is in front of the stepper of the loop. */
174 rtx continue_label;
175 } GTY ((tag ("LOOP_NESTING"))) loop;
176 /* For variable binding contours. */
177 struct nesting_block
179 /* Sequence number of this binding contour within the function,
180 in order of entry. */
181 int block_start_count;
182 /* Nonzero => value to restore stack to on exit. */
183 rtx stack_level;
184 /* The NOTE that starts this contour.
185 Used by expand_goto to check whether the destination
186 is within each contour or not. */
187 rtx first_insn;
188 /* Innermost containing binding contour that has a stack level. */
189 struct nesting *innermost_stack_block;
190 /* List of cleanups to be run on exit from this contour.
191 This is a list of expressions to be evaluated.
192 The TREE_PURPOSE of each link is the ..._DECL node
193 which the cleanup pertains to. */
194 tree cleanups;
195 /* List of cleanup-lists of blocks containing this block,
196 as they were at the locus where this block appears.
197 There is an element for each containing block,
198 ordered innermost containing block first.
199 The tail of this list can be 0,
200 if all remaining elements would be empty lists.
201 The element's TREE_VALUE is the cleanup-list of that block,
202 which may be null. */
203 tree outer_cleanups;
204 /* Chain of labels defined inside this binding contour.
205 For contours that have stack levels or cleanups. */
206 struct label_chain *label_chain;
207 /* Nonzero if this is associated with an EH region. */
208 int exception_region;
209 /* The saved target_temp_slot_level from our outer block.
210 We may reset target_temp_slot_level to be the level of
211 this block, if that is done, target_temp_slot_level
212 reverts to the saved target_temp_slot_level at the very
213 end of the block. */
214 int block_target_temp_slot_level;
215 /* True if we are currently emitting insns in an area of
216 output code that is controlled by a conditional
217 expression. This is used by the cleanup handling code to
218 generate conditional cleanup actions. */
219 int conditional_code;
220 /* A place to move the start of the exception region for any
221 of the conditional cleanups, must be at the end or after
222 the start of the last unconditional cleanup, and before any
223 conditional branch points. */
224 rtx last_unconditional_cleanup;
225 } GTY ((tag ("BLOCK_NESTING"))) block;
226 /* For switch (C) or case (Pascal) statements,
227 and also for dummies (see `expand_start_case_dummy'). */
228 struct nesting_case
230 /* The insn after which the case dispatch should finally
231 be emitted. Zero for a dummy. */
232 rtx start;
233 /* A list of case labels; it is first built as an AVL tree.
234 During expand_end_case, this is converted to a list, and may be
235 rearranged into a nearly balanced binary tree. */
236 struct case_node *case_list;
237 /* Label to jump to if no case matches. */
238 tree default_label;
239 /* The expression to be dispatched on. */
240 tree index_expr;
241 /* Type that INDEX_EXPR should be converted to. */
242 tree nominal_type;
243 /* Name of this kind of statement, for warnings. */
244 const char *printname;
245 /* Used to save no_line_numbers till we see the first case label.
246 We set this to -1 when we see the first case label in this
247 case statement. */
248 int line_number_status;
249 } GTY ((tag ("CASE_NESTING"))) case_stmt;
250 } GTY ((desc ("%1.desc"))) data;
253 /* Allocate and return a new `struct nesting'. */
255 #define ALLOC_NESTING() ggc_alloc (sizeof (struct nesting))
257 /* Pop the nesting stack element by element until we pop off
258 the element which is at the top of STACK.
259 Update all the other stacks, popping off elements from them
260 as we pop them from nesting_stack. */
262 #define POPSTACK(STACK) \
263 do { struct nesting *target = STACK; \
264 struct nesting *this; \
265 do { this = nesting_stack; \
266 if (loop_stack == this) \
267 loop_stack = loop_stack->next; \
268 if (cond_stack == this) \
269 cond_stack = cond_stack->next; \
270 if (block_stack == this) \
271 block_stack = block_stack->next; \
272 if (stack_block_stack == this) \
273 stack_block_stack = stack_block_stack->next; \
274 if (case_stack == this) \
275 case_stack = case_stack->next; \
276 nesting_depth = nesting_stack->depth - 1; \
277 nesting_stack = this->all; } \
278 while (this != target); } while (0)
280 /* In some cases it is impossible to generate code for a forward goto
281 until the label definition is seen. This happens when it may be necessary
282 for the goto to reset the stack pointer: we don't yet know how to do that.
283 So expand_goto puts an entry on this fixup list.
284 Each time a binding contour that resets the stack is exited,
285 we check each fixup.
286 If the target label has now been defined, we can insert the proper code. */
288 struct goto_fixup GTY(())
290 /* Points to following fixup. */
291 struct goto_fixup *next;
292 /* Points to the insn before the jump insn.
293 If more code must be inserted, it goes after this insn. */
294 rtx before_jump;
295 /* The LABEL_DECL that this jump is jumping to, or 0
296 for break, continue or return. */
297 tree target;
298 /* The BLOCK for the place where this goto was found. */
299 tree context;
300 /* The CODE_LABEL rtx that this is jumping to. */
301 rtx target_rtl;
302 /* Number of binding contours started in current function
303 before the label reference. */
304 int block_start_count;
305 /* The outermost stack level that should be restored for this jump.
306 Each time a binding contour that resets the stack is exited,
307 if the target label is *not* yet defined, this slot is updated. */
308 rtx stack_level;
309 /* List of lists of cleanup expressions to be run by this goto.
310 There is one element for each block that this goto is within.
311 The tail of this list can be 0,
312 if all remaining elements would be empty.
313 The TREE_VALUE contains the cleanup list of that block as of the
314 time this goto was seen.
315 The TREE_ADDRESSABLE flag is 1 for a block that has been exited. */
316 tree cleanup_list_list;
319 /* Within any binding contour that must restore a stack level,
320 all labels are recorded with a chain of these structures. */
322 struct label_chain GTY(())
324 /* Points to following fixup. */
325 struct label_chain *next;
326 tree label;
329 struct stmt_status GTY(())
331 /* Chain of all pending binding contours. */
332 struct nesting * x_block_stack;
334 /* If any new stacks are added here, add them to POPSTACKS too. */
336 /* Chain of all pending binding contours that restore stack levels
337 or have cleanups. */
338 struct nesting * x_stack_block_stack;
340 /* Chain of all pending conditional statements. */
341 struct nesting * x_cond_stack;
343 /* Chain of all pending loops. */
344 struct nesting * x_loop_stack;
346 /* Chain of all pending case or switch statements. */
347 struct nesting * x_case_stack;
349 /* Separate chain including all of the above,
350 chained through the `all' field. */
351 struct nesting * x_nesting_stack;
353 /* Number of entries on nesting_stack now. */
354 int x_nesting_depth;
356 /* Number of binding contours started so far in this function. */
357 int x_block_start_count;
359 /* Each time we expand an expression-statement,
360 record the expr's type and its RTL value here. */
361 tree x_last_expr_type;
362 rtx x_last_expr_value;
364 /* Nonzero if within a ({...}) grouping, in which case we must
365 always compute a value for each expr-stmt in case it is the last one. */
366 int x_expr_stmts_for_value;
368 /* Location of last line-number note, whether we actually
369 emitted it or not. */
370 location_t x_emit_locus;
372 struct goto_fixup *x_goto_fixup_chain;
375 #define block_stack (cfun->stmt->x_block_stack)
376 #define stack_block_stack (cfun->stmt->x_stack_block_stack)
377 #define cond_stack (cfun->stmt->x_cond_stack)
378 #define loop_stack (cfun->stmt->x_loop_stack)
379 #define case_stack (cfun->stmt->x_case_stack)
380 #define nesting_stack (cfun->stmt->x_nesting_stack)
381 #define nesting_depth (cfun->stmt->x_nesting_depth)
382 #define current_block_start_count (cfun->stmt->x_block_start_count)
383 #define last_expr_type (cfun->stmt->x_last_expr_type)
384 #define last_expr_value (cfun->stmt->x_last_expr_value)
385 #define expr_stmts_for_value (cfun->stmt->x_expr_stmts_for_value)
386 #define emit_locus (cfun->stmt->x_emit_locus)
387 #define goto_fixup_chain (cfun->stmt->x_goto_fixup_chain)
389 /* Nonzero if we are using EH to handle cleanups. */
390 static int using_eh_for_cleanups_p = 0;
392 static int n_occurrences (int, const char *);
393 static bool parse_input_constraint (const char **, int, int, int, int,
394 const char * const *, bool *, bool *);
395 static bool decl_conflicts_with_clobbers_p (tree, const HARD_REG_SET);
396 static void expand_goto_internal (tree, rtx, rtx);
397 static int expand_fixup (tree, rtx, rtx);
398 static rtx expand_nl_handler_label (rtx, rtx);
399 static void expand_nl_goto_receiver (void);
400 static void expand_nl_goto_receivers (struct nesting *);
401 static void fixup_gotos (struct nesting *, rtx, tree, rtx, int);
402 static bool check_operand_nalternatives (tree, tree);
403 static bool check_unique_operand_names (tree, tree);
404 static char *resolve_operand_name_1 (char *, tree, tree);
405 static void expand_null_return_1 (rtx);
406 static enum br_predictor return_prediction (rtx);
407 static void expand_value_return (rtx);
408 static int tail_recursion_args (tree, tree);
409 static void expand_cleanups (tree, int, int);
410 static void check_seenlabel (void);
411 static void do_jump_if_equal (rtx, rtx, rtx, int);
412 static int estimate_case_costs (case_node_ptr);
413 static bool same_case_target_p (rtx, rtx);
414 static void strip_default_case_nodes (case_node_ptr *, rtx);
415 static bool lshift_cheap_p (void);
416 static int case_bit_test_cmp (const void *, const void *);
417 static void emit_case_bit_tests (tree, tree, tree, tree, case_node_ptr, rtx);
418 static void group_case_nodes (case_node_ptr);
419 static void balance_case_nodes (case_node_ptr *, case_node_ptr);
420 static int node_has_low_bound (case_node_ptr, tree);
421 static int node_has_high_bound (case_node_ptr, tree);
422 static int node_is_bounded (case_node_ptr, tree);
423 static void emit_jump_if_reachable (rtx);
424 static void emit_case_nodes (rtx, case_node_ptr, rtx, tree);
425 static struct case_node *case_tree2list (case_node *, case_node *);
427 void
428 using_eh_for_cleanups (void)
430 using_eh_for_cleanups_p = 1;
433 void
434 init_stmt_for_function (void)
436 cfun->stmt =ggc_alloc (sizeof (struct stmt_status));
438 /* We are not currently within any block, conditional, loop or case. */
439 block_stack = 0;
440 stack_block_stack = 0;
441 loop_stack = 0;
442 case_stack = 0;
443 cond_stack = 0;
444 nesting_stack = 0;
445 nesting_depth = 0;
447 current_block_start_count = 0;
449 /* No gotos have been expanded yet. */
450 goto_fixup_chain = 0;
452 /* We are not processing a ({...}) grouping. */
453 expr_stmts_for_value = 0;
454 clear_last_expr ();
457 /* Record the current file and line. Called from emit_line_note. */
459 void
460 set_file_and_line_for_stmt (location_t location)
462 /* If we're outputting an inline function, and we add a line note,
463 there may be no CFUN->STMT information. So, there's no need to
464 update it. */
465 if (cfun->stmt)
466 emit_locus = location;
469 /* Emit a no-op instruction. */
471 void
472 emit_nop (void)
474 rtx last_insn;
476 last_insn = get_last_insn ();
477 if (!optimize
478 && (GET_CODE (last_insn) == CODE_LABEL
479 || (GET_CODE (last_insn) == NOTE
480 && prev_real_insn (last_insn) == 0)))
481 emit_insn (gen_nop ());
484 /* Return the rtx-label that corresponds to a LABEL_DECL,
485 creating it if necessary. */
488 label_rtx (tree label)
490 if (TREE_CODE (label) != LABEL_DECL)
491 abort ();
493 if (!DECL_RTL_SET_P (label))
494 SET_DECL_RTL (label, gen_label_rtx ());
496 return DECL_RTL (label);
499 /* As above, but also put it on the forced-reference list of the
500 function that contains it. */
502 force_label_rtx (tree label)
504 rtx ref = label_rtx (label);
505 tree function = decl_function_context (label);
506 struct function *p;
508 if (!function)
509 abort ();
511 if (function != current_function_decl
512 && function != inline_function_decl)
513 p = find_function_data (function);
514 else
515 p = cfun;
517 p->expr->x_forced_labels = gen_rtx_EXPR_LIST (VOIDmode, ref,
518 p->expr->x_forced_labels);
519 return ref;
522 /* Add an unconditional jump to LABEL as the next sequential instruction. */
524 void
525 emit_jump (rtx label)
527 do_pending_stack_adjust ();
528 emit_jump_insn (gen_jump (label));
529 emit_barrier ();
532 /* Emit code to jump to the address
533 specified by the pointer expression EXP. */
535 void
536 expand_computed_goto (tree exp)
538 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
540 #ifdef POINTERS_EXTEND_UNSIGNED
541 if (GET_MODE (x) != Pmode)
542 x = convert_memory_address (Pmode, x);
543 #endif
545 emit_queue ();
547 if (! cfun->computed_goto_common_label)
549 cfun->computed_goto_common_reg = copy_to_mode_reg (Pmode, x);
550 cfun->computed_goto_common_label = gen_label_rtx ();
551 emit_label (cfun->computed_goto_common_label);
553 do_pending_stack_adjust ();
554 emit_indirect_jump (cfun->computed_goto_common_reg);
556 current_function_has_computed_jump = 1;
558 else
560 emit_move_insn (cfun->computed_goto_common_reg, x);
561 emit_jump (cfun->computed_goto_common_label);
565 /* Handle goto statements and the labels that they can go to. */
567 /* Specify the location in the RTL code of a label LABEL,
568 which is a LABEL_DECL tree node.
570 This is used for the kind of label that the user can jump to with a
571 goto statement, and for alternatives of a switch or case statement.
572 RTL labels generated for loops and conditionals don't go through here;
573 they are generated directly at the RTL level, by other functions below.
575 Note that this has nothing to do with defining label *names*.
576 Languages vary in how they do that and what that even means. */
578 void
579 expand_label (tree label)
581 struct label_chain *p;
583 do_pending_stack_adjust ();
584 emit_label (label_rtx (label));
585 if (DECL_NAME (label))
586 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
588 if (stack_block_stack != 0)
590 p = ggc_alloc (sizeof (struct label_chain));
591 p->next = stack_block_stack->data.block.label_chain;
592 stack_block_stack->data.block.label_chain = p;
593 p->label = label;
597 /* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
598 from nested functions. */
600 void
601 declare_nonlocal_label (tree label)
603 rtx slot = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
605 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
606 LABEL_PRESERVE_P (label_rtx (label)) = 1;
607 if (nonlocal_goto_handler_slots == 0)
609 emit_stack_save (SAVE_NONLOCAL,
610 &nonlocal_goto_stack_level,
611 PREV_INSN (tail_recursion_reentry));
613 nonlocal_goto_handler_slots
614 = gen_rtx_EXPR_LIST (VOIDmode, slot, nonlocal_goto_handler_slots);
617 /* Generate RTL code for a `goto' statement with target label LABEL.
618 LABEL should be a LABEL_DECL tree node that was or will later be
619 defined with `expand_label'. */
621 void
622 expand_goto (tree label)
624 tree context;
626 /* Check for a nonlocal goto to a containing function. */
627 context = decl_function_context (label);
628 if (context != 0 && context != current_function_decl)
630 struct function *p = find_function_data (context);
631 rtx label_ref = gen_rtx_LABEL_REF (Pmode, label_rtx (label));
632 rtx handler_slot, static_chain, save_area, insn;
633 tree link;
635 /* Find the corresponding handler slot for this label. */
636 handler_slot = p->x_nonlocal_goto_handler_slots;
637 for (link = p->x_nonlocal_labels; TREE_VALUE (link) != label;
638 link = TREE_CHAIN (link))
639 handler_slot = XEXP (handler_slot, 1);
640 handler_slot = XEXP (handler_slot, 0);
642 p->has_nonlocal_label = 1;
643 current_function_has_nonlocal_goto = 1;
644 LABEL_REF_NONLOCAL_P (label_ref) = 1;
646 /* Copy the rtl for the slots so that they won't be shared in
647 case the virtual stack vars register gets instantiated differently
648 in the parent than in the child. */
650 static_chain = copy_to_reg (lookup_static_chain (label));
652 /* Get addr of containing function's current nonlocal goto handler,
653 which will do any cleanups and then jump to the label. */
654 handler_slot = copy_to_reg (replace_rtx (copy_rtx (handler_slot),
655 virtual_stack_vars_rtx,
656 static_chain));
658 /* Get addr of containing function's nonlocal save area. */
659 save_area = p->x_nonlocal_goto_stack_level;
660 if (save_area)
661 save_area = replace_rtx (copy_rtx (save_area),
662 virtual_stack_vars_rtx, static_chain);
664 #if HAVE_nonlocal_goto
665 if (HAVE_nonlocal_goto)
666 emit_insn (gen_nonlocal_goto (static_chain, handler_slot,
667 save_area, label_ref));
668 else
669 #endif
671 /* Restore frame pointer for containing function.
672 This sets the actual hard register used for the frame pointer
673 to the location of the function's incoming static chain info.
674 The non-local goto handler will then adjust it to contain the
675 proper value and reload the argument pointer, if needed. */
676 emit_move_insn (hard_frame_pointer_rtx, static_chain);
677 emit_stack_restore (SAVE_NONLOCAL, save_area, NULL_RTX);
679 /* USE of hard_frame_pointer_rtx added for consistency;
680 not clear if really needed. */
681 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
682 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
683 emit_indirect_jump (handler_slot);
686 /* Search backwards to the jump insn and mark it as a
687 non-local goto. */
688 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
690 if (GET_CODE (insn) == JUMP_INSN)
692 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
693 const0_rtx, REG_NOTES (insn));
694 break;
696 else if (GET_CODE (insn) == CALL_INSN)
697 break;
700 else
701 expand_goto_internal (label, label_rtx (label), NULL_RTX);
704 /* Generate RTL code for a `goto' statement with target label BODY.
705 LABEL should be a LABEL_REF.
706 LAST_INSN, if non-0, is the rtx we should consider as the last
707 insn emitted (for the purposes of cleaning up a return). */
709 static void
710 expand_goto_internal (tree body, rtx label, rtx last_insn)
712 struct nesting *block;
713 rtx stack_level = 0;
715 if (GET_CODE (label) != CODE_LABEL)
716 abort ();
718 /* If label has already been defined, we can tell now
719 whether and how we must alter the stack level. */
721 if (PREV_INSN (label) != 0)
723 /* Find the innermost pending block that contains the label.
724 (Check containment by comparing insn-uids.)
725 Then restore the outermost stack level within that block,
726 and do cleanups of all blocks contained in it. */
727 for (block = block_stack; block; block = block->next)
729 if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
730 break;
731 if (block->data.block.stack_level != 0)
732 stack_level = block->data.block.stack_level;
733 /* Execute the cleanups for blocks we are exiting. */
734 if (block->data.block.cleanups != 0)
736 expand_cleanups (block->data.block.cleanups, 1, 1);
737 do_pending_stack_adjust ();
741 if (stack_level)
743 /* Ensure stack adjust isn't done by emit_jump, as this
744 would clobber the stack pointer. This one should be
745 deleted as dead by flow. */
746 clear_pending_stack_adjust ();
747 do_pending_stack_adjust ();
749 /* Don't do this adjust if it's to the end label and this function
750 is to return with a depressed stack pointer. */
751 if (label == return_label
752 && (((TREE_CODE (TREE_TYPE (current_function_decl))
753 == FUNCTION_TYPE)
754 && (TYPE_RETURNS_STACK_DEPRESSED
755 (TREE_TYPE (current_function_decl))))))
757 else
758 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
761 if (body != 0 && DECL_TOO_LATE (body))
762 error ("jump to `%s' invalidly jumps into binding contour",
763 IDENTIFIER_POINTER (DECL_NAME (body)));
765 /* Label not yet defined: may need to put this goto
766 on the fixup list. */
767 else if (! expand_fixup (body, label, last_insn))
769 /* No fixup needed. Record that the label is the target
770 of at least one goto that has no fixup. */
771 if (body != 0)
772 TREE_ADDRESSABLE (body) = 1;
775 emit_jump (label);
778 /* Generate if necessary a fixup for a goto
779 whose target label in tree structure (if any) is TREE_LABEL
780 and whose target in rtl is RTL_LABEL.
782 If LAST_INSN is nonzero, we pretend that the jump appears
783 after insn LAST_INSN instead of at the current point in the insn stream.
785 The fixup will be used later to insert insns just before the goto.
786 Those insns will restore the stack level as appropriate for the
787 target label, and will (in the case of C++) also invoke any object
788 destructors which have to be invoked when we exit the scopes which
789 are exited by the goto.
791 Value is nonzero if a fixup is made. */
793 static int
794 expand_fixup (tree tree_label, rtx rtl_label, rtx last_insn)
796 struct nesting *block, *end_block;
798 /* See if we can recognize which block the label will be output in.
799 This is possible in some very common cases.
800 If we succeed, set END_BLOCK to that block.
801 Otherwise, set it to 0. */
803 if (cond_stack
804 && (rtl_label == cond_stack->data.cond.endif_label
805 || rtl_label == cond_stack->data.cond.next_label))
806 end_block = cond_stack;
807 /* If we are in a loop, recognize certain labels which
808 are likely targets. This reduces the number of fixups
809 we need to create. */
810 else if (loop_stack
811 && (rtl_label == loop_stack->data.loop.start_label
812 || rtl_label == loop_stack->data.loop.end_label
813 || rtl_label == loop_stack->data.loop.continue_label))
814 end_block = loop_stack;
815 else
816 end_block = 0;
818 /* Now set END_BLOCK to the binding level to which we will return. */
820 if (end_block)
822 struct nesting *next_block = end_block->all;
823 block = block_stack;
825 /* First see if the END_BLOCK is inside the innermost binding level.
826 If so, then no cleanups or stack levels are relevant. */
827 while (next_block && next_block != block)
828 next_block = next_block->all;
830 if (next_block)
831 return 0;
833 /* Otherwise, set END_BLOCK to the innermost binding level
834 which is outside the relevant control-structure nesting. */
835 next_block = block_stack->next;
836 for (block = block_stack; block != end_block; block = block->all)
837 if (block == next_block)
838 next_block = next_block->next;
839 end_block = next_block;
842 /* Does any containing block have a stack level or cleanups?
843 If not, no fixup is needed, and that is the normal case
844 (the only case, for standard C). */
845 for (block = block_stack; block != end_block; block = block->next)
846 if (block->data.block.stack_level != 0
847 || block->data.block.cleanups != 0)
848 break;
850 if (block != end_block)
852 /* Ok, a fixup is needed. Add a fixup to the list of such. */
853 struct goto_fixup *fixup = ggc_alloc (sizeof (struct goto_fixup));
854 /* In case an old stack level is restored, make sure that comes
855 after any pending stack adjust. */
856 /* ?? If the fixup isn't to come at the present position,
857 doing the stack adjust here isn't useful. Doing it with our
858 settings at that location isn't useful either. Let's hope
859 someone does it! */
860 if (last_insn == 0)
861 do_pending_stack_adjust ();
862 fixup->target = tree_label;
863 fixup->target_rtl = rtl_label;
865 /* Create a BLOCK node and a corresponding matched set of
866 NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes at
867 this point. The notes will encapsulate any and all fixup
868 code which we might later insert at this point in the insn
869 stream. Also, the BLOCK node will be the parent (i.e. the
870 `SUPERBLOCK') of any other BLOCK nodes which we might create
871 later on when we are expanding the fixup code.
873 Note that optimization passes (including expand_end_loop)
874 might move the *_BLOCK notes away, so we use a NOTE_INSN_DELETED
875 as a placeholder. */
878 rtx original_before_jump
879 = last_insn ? last_insn : get_last_insn ();
880 rtx start;
881 rtx end;
882 tree block;
884 block = make_node (BLOCK);
885 TREE_USED (block) = 1;
887 if (!cfun->x_whole_function_mode_p)
888 (*lang_hooks.decls.insert_block) (block);
889 else
891 BLOCK_CHAIN (block)
892 = BLOCK_CHAIN (DECL_INITIAL (current_function_decl));
893 BLOCK_CHAIN (DECL_INITIAL (current_function_decl))
894 = block;
897 start_sequence ();
898 start = emit_note (NOTE_INSN_BLOCK_BEG);
899 if (cfun->x_whole_function_mode_p)
900 NOTE_BLOCK (start) = block;
901 fixup->before_jump = emit_note (NOTE_INSN_DELETED);
902 end = emit_note (NOTE_INSN_BLOCK_END);
903 if (cfun->x_whole_function_mode_p)
904 NOTE_BLOCK (end) = block;
905 fixup->context = block;
906 end_sequence ();
907 emit_insn_after (start, original_before_jump);
910 fixup->block_start_count = current_block_start_count;
911 fixup->stack_level = 0;
912 fixup->cleanup_list_list
913 = ((block->data.block.outer_cleanups
914 || block->data.block.cleanups)
915 ? tree_cons (NULL_TREE, block->data.block.cleanups,
916 block->data.block.outer_cleanups)
917 : 0);
918 fixup->next = goto_fixup_chain;
919 goto_fixup_chain = fixup;
922 return block != 0;
925 /* Expand any needed fixups in the outputmost binding level of the
926 function. FIRST_INSN is the first insn in the function. */
928 void
929 expand_fixups (rtx first_insn)
931 fixup_gotos (NULL, NULL_RTX, NULL_TREE, first_insn, 0);
934 /* When exiting a binding contour, process all pending gotos requiring fixups.
935 THISBLOCK is the structure that describes the block being exited.
936 STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
937 CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
938 FIRST_INSN is the insn that began this contour.
940 Gotos that jump out of this contour must restore the
941 stack level and do the cleanups before actually jumping.
943 DONT_JUMP_IN positive means report error if there is a jump into this
944 contour from before the beginning of the contour. This is also done if
945 STACK_LEVEL is nonzero unless DONT_JUMP_IN is negative. */
947 static void
948 fixup_gotos (struct nesting *thisblock, rtx stack_level,
949 tree cleanup_list, rtx first_insn, int dont_jump_in)
951 struct goto_fixup *f, *prev;
953 /* F is the fixup we are considering; PREV is the previous one. */
954 /* We run this loop in two passes so that cleanups of exited blocks
955 are run first, and blocks that are exited are marked so
956 afterwards. */
958 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
960 /* Test for a fixup that is inactive because it is already handled. */
961 if (f->before_jump == 0)
963 /* Delete inactive fixup from the chain, if that is easy to do. */
964 if (prev != 0)
965 prev->next = f->next;
967 /* Has this fixup's target label been defined?
968 If so, we can finalize it. */
969 else if (PREV_INSN (f->target_rtl) != 0)
971 rtx cleanup_insns;
973 /* If this fixup jumped into this contour from before the beginning
974 of this contour, report an error. This code used to use
975 the first non-label insn after f->target_rtl, but that's
976 wrong since such can be added, by things like put_var_into_stack
977 and have INSN_UIDs that are out of the range of the block. */
978 /* ??? Bug: this does not detect jumping in through intermediate
979 blocks that have stack levels or cleanups.
980 It detects only a problem with the innermost block
981 around the label. */
982 if (f->target != 0
983 && (dont_jump_in > 0 || (dont_jump_in == 0 && stack_level)
984 || cleanup_list)
985 && INSN_UID (first_insn) < INSN_UID (f->target_rtl)
986 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
987 && ! DECL_ERROR_ISSUED (f->target))
989 error ("%Hlabel '%D' used before containing binding contour",
990 &DECL_SOURCE_LOCATION (f->target), f->target);
991 /* Prevent multiple errors for one label. */
992 DECL_ERROR_ISSUED (f->target) = 1;
995 /* We will expand the cleanups into a sequence of their own and
996 then later on we will attach this new sequence to the insn
997 stream just ahead of the actual jump insn. */
999 start_sequence ();
1001 /* Temporarily restore the lexical context where we will
1002 logically be inserting the fixup code. We do this for the
1003 sake of getting the debugging information right. */
1005 (*lang_hooks.decls.pushlevel) (0);
1006 (*lang_hooks.decls.set_block) (f->context);
1008 /* Expand the cleanups for blocks this jump exits. */
1009 if (f->cleanup_list_list)
1011 tree lists;
1012 for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
1013 /* Marked elements correspond to blocks that have been closed.
1014 Do their cleanups. */
1015 if (TREE_ADDRESSABLE (lists)
1016 && TREE_VALUE (lists) != 0)
1018 expand_cleanups (TREE_VALUE (lists), 1, 1);
1019 /* Pop any pushes done in the cleanups,
1020 in case function is about to return. */
1021 do_pending_stack_adjust ();
1025 /* Restore stack level for the biggest contour that this
1026 jump jumps out of. */
1027 if (f->stack_level
1028 && ! (f->target_rtl == return_label
1029 && ((TREE_CODE (TREE_TYPE (current_function_decl))
1030 == FUNCTION_TYPE)
1031 && (TYPE_RETURNS_STACK_DEPRESSED
1032 (TREE_TYPE (current_function_decl))))))
1033 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
1035 /* Finish up the sequence containing the insns which implement the
1036 necessary cleanups, and then attach that whole sequence to the
1037 insn stream just ahead of the actual jump insn. Attaching it
1038 at that point insures that any cleanups which are in fact
1039 implicit C++ object destructions (which must be executed upon
1040 leaving the block) appear (to the debugger) to be taking place
1041 in an area of the generated code where the object(s) being
1042 destructed are still "in scope". */
1044 cleanup_insns = get_insns ();
1045 (*lang_hooks.decls.poplevel) (1, 0, 0);
1047 end_sequence ();
1048 emit_insn_after (cleanup_insns, f->before_jump);
1050 f->before_jump = 0;
1054 /* For any still-undefined labels, do the cleanups for this block now.
1055 We must do this now since items in the cleanup list may go out
1056 of scope when the block ends. */
1057 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1058 if (f->before_jump != 0
1059 && PREV_INSN (f->target_rtl) == 0
1060 /* Label has still not appeared. If we are exiting a block with
1061 a stack level to restore, that started before the fixup,
1062 mark this stack level as needing restoration
1063 when the fixup is later finalized. */
1064 && thisblock != 0
1065 /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared, it
1066 means the label is undefined. That's erroneous, but possible. */
1067 && (thisblock->data.block.block_start_count
1068 <= f->block_start_count))
1070 tree lists = f->cleanup_list_list;
1071 rtx cleanup_insns;
1073 for (; lists; lists = TREE_CHAIN (lists))
1074 /* If the following elt. corresponds to our containing block
1075 then the elt. must be for this block. */
1076 if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
1078 start_sequence ();
1079 (*lang_hooks.decls.pushlevel) (0);
1080 (*lang_hooks.decls.set_block) (f->context);
1081 expand_cleanups (TREE_VALUE (lists), 1, 1);
1082 do_pending_stack_adjust ();
1083 cleanup_insns = get_insns ();
1084 (*lang_hooks.decls.poplevel) (1, 0, 0);
1085 end_sequence ();
1086 if (cleanup_insns != 0)
1087 f->before_jump
1088 = emit_insn_after (cleanup_insns, f->before_jump);
1090 f->cleanup_list_list = TREE_CHAIN (lists);
1093 if (stack_level)
1094 f->stack_level = stack_level;
1098 /* Return the number of times character C occurs in string S. */
1099 static int
1100 n_occurrences (int c, const char *s)
1102 int n = 0;
1103 while (*s)
1104 n += (*s++ == c);
1105 return n;
1108 /* Generate RTL for an asm statement (explicit assembler code).
1109 STRING is a STRING_CST node containing the assembler code text,
1110 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
1111 insn is volatile; don't optimize it. */
1113 void
1114 expand_asm (tree string, int vol)
1116 rtx body;
1118 if (TREE_CODE (string) == ADDR_EXPR)
1119 string = TREE_OPERAND (string, 0);
1121 body = gen_rtx_ASM_INPUT (VOIDmode, TREE_STRING_POINTER (string));
1123 MEM_VOLATILE_P (body) = vol;
1125 emit_insn (body);
1127 clear_last_expr ();
1130 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
1131 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
1132 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
1133 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
1134 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
1135 constraint allows the use of a register operand. And, *IS_INOUT
1136 will be true if the operand is read-write, i.e., if it is used as
1137 an input as well as an output. If *CONSTRAINT_P is not in
1138 canonical form, it will be made canonical. (Note that `+' will be
1139 replaced with `=' as part of this process.)
1141 Returns TRUE if all went well; FALSE if an error occurred. */
1143 bool
1144 parse_output_constraint (const char **constraint_p, int operand_num,
1145 int ninputs, int noutputs, bool *allows_mem,
1146 bool *allows_reg, bool *is_inout)
1148 const char *constraint = *constraint_p;
1149 const char *p;
1151 /* Assume the constraint doesn't allow the use of either a register
1152 or memory. */
1153 *allows_mem = false;
1154 *allows_reg = false;
1156 /* Allow the `=' or `+' to not be at the beginning of the string,
1157 since it wasn't explicitly documented that way, and there is a
1158 large body of code that puts it last. Swap the character to
1159 the front, so as not to uglify any place else. */
1160 p = strchr (constraint, '=');
1161 if (!p)
1162 p = strchr (constraint, '+');
1164 /* If the string doesn't contain an `=', issue an error
1165 message. */
1166 if (!p)
1168 error ("output operand constraint lacks `='");
1169 return false;
1172 /* If the constraint begins with `+', then the operand is both read
1173 from and written to. */
1174 *is_inout = (*p == '+');
1176 /* Canonicalize the output constraint so that it begins with `='. */
1177 if (p != constraint || is_inout)
1179 char *buf;
1180 size_t c_len = strlen (constraint);
1182 if (p != constraint)
1183 warning ("output constraint `%c' for operand %d is not at the beginning",
1184 *p, operand_num);
1186 /* Make a copy of the constraint. */
1187 buf = alloca (c_len + 1);
1188 strcpy (buf, constraint);
1189 /* Swap the first character and the `=' or `+'. */
1190 buf[p - constraint] = buf[0];
1191 /* Make sure the first character is an `='. (Until we do this,
1192 it might be a `+'.) */
1193 buf[0] = '=';
1194 /* Replace the constraint with the canonicalized string. */
1195 *constraint_p = ggc_alloc_string (buf, c_len);
1196 constraint = *constraint_p;
1199 /* Loop through the constraint string. */
1200 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
1201 switch (*p)
1203 case '+':
1204 case '=':
1205 error ("operand constraint contains incorrectly positioned '+' or '='");
1206 return false;
1208 case '%':
1209 if (operand_num + 1 == ninputs + noutputs)
1211 error ("`%%' constraint used with last operand");
1212 return false;
1214 break;
1216 case 'V': case 'm': case 'o':
1217 *allows_mem = true;
1218 break;
1220 case '?': case '!': case '*': case '&': case '#':
1221 case 'E': case 'F': case 'G': case 'H':
1222 case 's': case 'i': case 'n':
1223 case 'I': case 'J': case 'K': case 'L': case 'M':
1224 case 'N': case 'O': case 'P': case ',':
1225 break;
1227 case '0': case '1': case '2': case '3': case '4':
1228 case '5': case '6': case '7': case '8': case '9':
1229 case '[':
1230 error ("matching constraint not valid in output operand");
1231 return false;
1233 case '<': case '>':
1234 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
1235 excepting those that expand_call created. So match memory
1236 and hope. */
1237 *allows_mem = true;
1238 break;
1240 case 'g': case 'X':
1241 *allows_reg = true;
1242 *allows_mem = true;
1243 break;
1245 case 'p': case 'r':
1246 *allows_reg = true;
1247 break;
1249 default:
1250 if (!ISALPHA (*p))
1251 break;
1252 if (REG_CLASS_FROM_CONSTRAINT (*p, p) != NO_REGS)
1253 *allows_reg = true;
1254 #ifdef EXTRA_CONSTRAINT_STR
1255 else if (EXTRA_ADDRESS_CONSTRAINT (*p, p))
1256 *allows_reg = true;
1257 else if (EXTRA_MEMORY_CONSTRAINT (*p, p))
1258 *allows_mem = true;
1259 else
1261 /* Otherwise we can't assume anything about the nature of
1262 the constraint except that it isn't purely registers.
1263 Treat it like "g" and hope for the best. */
1264 *allows_reg = true;
1265 *allows_mem = true;
1267 #endif
1268 break;
1271 return true;
1274 /* Similar, but for input constraints. */
1276 static bool
1277 parse_input_constraint (const char **constraint_p, int input_num,
1278 int ninputs, int noutputs, int ninout,
1279 const char * const * constraints,
1280 bool *allows_mem, bool *allows_reg)
1282 const char *constraint = *constraint_p;
1283 const char *orig_constraint = constraint;
1284 size_t c_len = strlen (constraint);
1285 size_t j;
1287 /* Assume the constraint doesn't allow the use of either
1288 a register or memory. */
1289 *allows_mem = false;
1290 *allows_reg = false;
1292 /* Make sure constraint has neither `=', `+', nor '&'. */
1294 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
1295 switch (constraint[j])
1297 case '+': case '=': case '&':
1298 if (constraint == orig_constraint)
1300 error ("input operand constraint contains `%c'", constraint[j]);
1301 return false;
1303 break;
1305 case '%':
1306 if (constraint == orig_constraint
1307 && input_num + 1 == ninputs - ninout)
1309 error ("`%%' constraint used with last operand");
1310 return false;
1312 break;
1314 case 'V': case 'm': case 'o':
1315 *allows_mem = true;
1316 break;
1318 case '<': case '>':
1319 case '?': case '!': case '*': case '#':
1320 case 'E': case 'F': case 'G': case 'H':
1321 case 's': case 'i': case 'n':
1322 case 'I': case 'J': case 'K': case 'L': case 'M':
1323 case 'N': case 'O': case 'P': case ',':
1324 break;
1326 /* Whether or not a numeric constraint allows a register is
1327 decided by the matching constraint, and so there is no need
1328 to do anything special with them. We must handle them in
1329 the default case, so that we don't unnecessarily force
1330 operands to memory. */
1331 case '0': case '1': case '2': case '3': case '4':
1332 case '5': case '6': case '7': case '8': case '9':
1334 char *end;
1335 unsigned long match;
1337 match = strtoul (constraint + j, &end, 10);
1338 if (match >= (unsigned long) noutputs)
1340 error ("matching constraint references invalid operand number");
1341 return false;
1344 /* Try and find the real constraint for this dup. Only do this
1345 if the matching constraint is the only alternative. */
1346 if (*end == '\0'
1347 && (j == 0 || (j == 1 && constraint[0] == '%')))
1349 constraint = constraints[match];
1350 *constraint_p = constraint;
1351 c_len = strlen (constraint);
1352 j = 0;
1353 /* ??? At the end of the loop, we will skip the first part of
1354 the matched constraint. This assumes not only that the
1355 other constraint is an output constraint, but also that
1356 the '=' or '+' come first. */
1357 break;
1359 else
1360 j = end - constraint;
1361 /* Anticipate increment at end of loop. */
1362 j--;
1364 /* Fall through. */
1366 case 'p': case 'r':
1367 *allows_reg = true;
1368 break;
1370 case 'g': case 'X':
1371 *allows_reg = true;
1372 *allows_mem = true;
1373 break;
1375 default:
1376 if (! ISALPHA (constraint[j]))
1378 error ("invalid punctuation `%c' in constraint", constraint[j]);
1379 return false;
1381 if (REG_CLASS_FROM_CONSTRAINT (constraint[j], constraint + j)
1382 != NO_REGS)
1383 *allows_reg = true;
1384 #ifdef EXTRA_CONSTRAINT_STR
1385 else if (EXTRA_ADDRESS_CONSTRAINT (constraint[j], constraint + j))
1386 *allows_reg = true;
1387 else if (EXTRA_MEMORY_CONSTRAINT (constraint[j], constraint + j))
1388 *allows_mem = true;
1389 else
1391 /* Otherwise we can't assume anything about the nature of
1392 the constraint except that it isn't purely registers.
1393 Treat it like "g" and hope for the best. */
1394 *allows_reg = true;
1395 *allows_mem = true;
1397 #endif
1398 break;
1401 return true;
1404 /* Check for overlap between registers marked in CLOBBERED_REGS and
1405 anything inappropriate in DECL. Emit error and return TRUE for error,
1406 FALSE for ok. */
1408 static bool
1409 decl_conflicts_with_clobbers_p (tree decl, const HARD_REG_SET clobbered_regs)
1411 /* Conflicts between asm-declared register variables and the clobber
1412 list are not allowed. */
1413 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
1414 && DECL_REGISTER (decl)
1415 && REG_P (DECL_RTL (decl))
1416 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
1418 rtx reg = DECL_RTL (decl);
1419 unsigned int regno;
1421 for (regno = REGNO (reg);
1422 regno < (REGNO (reg)
1423 + HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
1424 regno++)
1425 if (TEST_HARD_REG_BIT (clobbered_regs, regno))
1427 error ("asm-specifier for variable `%s' conflicts with asm clobber list",
1428 IDENTIFIER_POINTER (DECL_NAME (decl)));
1430 /* Reset registerness to stop multiple errors emitted for a
1431 single variable. */
1432 DECL_REGISTER (decl) = 0;
1433 return true;
1436 return false;
1439 /* Generate RTL for an asm statement with arguments.
1440 STRING is the instruction template.
1441 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
1442 Each output or input has an expression in the TREE_VALUE and
1443 and a tree list in TREE_PURPOSE which in turn contains a constraint
1444 name in TREE_VALUE (or NULL_TREE) and a constraint string
1445 in TREE_PURPOSE.
1446 CLOBBERS is a list of STRING_CST nodes each naming a hard register
1447 that is clobbered by this insn.
1449 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
1450 Some elements of OUTPUTS may be replaced with trees representing temporary
1451 values. The caller should copy those temporary values to the originally
1452 specified lvalues.
1454 VOL nonzero means the insn is volatile; don't optimize it. */
1456 void
1457 expand_asm_operands (tree string, tree outputs, tree inputs,
1458 tree clobbers, int vol, const char *filename, int line)
1460 rtvec argvec, constraintvec;
1461 rtx body;
1462 int ninputs = list_length (inputs);
1463 int noutputs = list_length (outputs);
1464 int ninout;
1465 int nclobbers;
1466 HARD_REG_SET clobbered_regs;
1467 int clobber_conflict_found = 0;
1468 tree tail;
1469 tree t;
1470 int i;
1471 /* Vector of RTX's of evaluated output operands. */
1472 rtx *output_rtx = alloca (noutputs * sizeof (rtx));
1473 int *inout_opnum = alloca (noutputs * sizeof (int));
1474 rtx *real_output_rtx = alloca (noutputs * sizeof (rtx));
1475 enum machine_mode *inout_mode
1476 = alloca (noutputs * sizeof (enum machine_mode));
1477 const char **constraints
1478 = alloca ((noutputs + ninputs) * sizeof (const char *));
1479 int old_generating_concat_p = generating_concat_p;
1481 /* An ASM with no outputs needs to be treated as volatile, for now. */
1482 if (noutputs == 0)
1483 vol = 1;
1485 if (! check_operand_nalternatives (outputs, inputs))
1486 return;
1488 if (! check_unique_operand_names (outputs, inputs))
1489 return;
1491 string = resolve_asm_operand_names (string, outputs, inputs);
1493 /* Collect constraints. */
1494 i = 0;
1495 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
1496 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1497 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
1498 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1500 #ifdef MD_ASM_CLOBBERS
1501 /* Sometimes we wish to automatically clobber registers across an asm.
1502 Case in point is when the i386 backend moved from cc0 to a hard reg --
1503 maintaining source-level compatibility means automatically clobbering
1504 the flags register. */
1505 MD_ASM_CLOBBERS (clobbers);
1506 #endif
1508 /* Count the number of meaningful clobbered registers, ignoring what
1509 we would ignore later. */
1510 nclobbers = 0;
1511 CLEAR_HARD_REG_SET (clobbered_regs);
1512 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1514 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1516 i = decode_reg_name (regname);
1517 if (i >= 0 || i == -4)
1518 ++nclobbers;
1519 else if (i == -2)
1520 error ("unknown register name `%s' in `asm'", regname);
1522 /* Mark clobbered registers. */
1523 if (i >= 0)
1525 /* Clobbering the PIC register is an error */
1526 if (i == (int) PIC_OFFSET_TABLE_REGNUM)
1528 error ("PIC register `%s' clobbered in `asm'", regname);
1529 return;
1532 SET_HARD_REG_BIT (clobbered_regs, i);
1536 clear_last_expr ();
1538 /* First pass over inputs and outputs checks validity and sets
1539 mark_addressable if needed. */
1541 ninout = 0;
1542 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1544 tree val = TREE_VALUE (tail);
1545 tree type = TREE_TYPE (val);
1546 const char *constraint;
1547 bool is_inout;
1548 bool allows_reg;
1549 bool allows_mem;
1551 /* If there's an erroneous arg, emit no insn. */
1552 if (type == error_mark_node)
1553 return;
1555 /* Try to parse the output constraint. If that fails, there's
1556 no point in going further. */
1557 constraint = constraints[i];
1558 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
1559 &allows_mem, &allows_reg, &is_inout))
1560 return;
1562 if (! allows_reg
1563 && (allows_mem
1564 || is_inout
1565 || (DECL_P (val)
1566 && GET_CODE (DECL_RTL (val)) == REG
1567 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
1568 (*lang_hooks.mark_addressable) (val);
1570 if (is_inout)
1571 ninout++;
1574 ninputs += ninout;
1575 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1577 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1578 return;
1581 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
1583 bool allows_reg, allows_mem;
1584 const char *constraint;
1586 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
1587 would get VOIDmode and that could cause a crash in reload. */
1588 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1589 return;
1591 constraint = constraints[i + noutputs];
1592 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1593 constraints, &allows_mem, &allows_reg))
1594 return;
1596 if (! allows_reg && allows_mem)
1597 (*lang_hooks.mark_addressable) (TREE_VALUE (tail));
1600 /* Second pass evaluates arguments. */
1602 ninout = 0;
1603 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1605 tree val = TREE_VALUE (tail);
1606 tree type = TREE_TYPE (val);
1607 bool is_inout;
1608 bool allows_reg;
1609 bool allows_mem;
1610 rtx op;
1612 if (!parse_output_constraint (&constraints[i], i, ninputs,
1613 noutputs, &allows_mem, &allows_reg,
1614 &is_inout))
1615 abort ();
1617 /* If an output operand is not a decl or indirect ref and our constraint
1618 allows a register, make a temporary to act as an intermediate.
1619 Make the asm insn write into that, then our caller will copy it to
1620 the real output operand. Likewise for promoted variables. */
1622 generating_concat_p = 0;
1624 real_output_rtx[i] = NULL_RTX;
1625 if ((TREE_CODE (val) == INDIRECT_REF
1626 && allows_mem)
1627 || (DECL_P (val)
1628 && (allows_mem || GET_CODE (DECL_RTL (val)) == REG)
1629 && ! (GET_CODE (DECL_RTL (val)) == REG
1630 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
1631 || ! allows_reg
1632 || is_inout)
1634 op = expand_expr (val, NULL_RTX, VOIDmode, EXPAND_WRITE);
1635 if (GET_CODE (op) == MEM)
1636 op = validize_mem (op);
1638 if (! allows_reg && GET_CODE (op) != MEM)
1639 error ("output number %d not directly addressable", i);
1640 if ((! allows_mem && GET_CODE (op) == MEM)
1641 || GET_CODE (op) == CONCAT)
1643 real_output_rtx[i] = protect_from_queue (op, 1);
1644 op = gen_reg_rtx (GET_MODE (op));
1645 if (is_inout)
1646 emit_move_insn (op, real_output_rtx[i]);
1649 else
1651 op = assign_temp (type, 0, 0, 1);
1652 op = validize_mem (op);
1653 TREE_VALUE (tail) = make_tree (type, op);
1655 output_rtx[i] = op;
1657 generating_concat_p = old_generating_concat_p;
1659 if (is_inout)
1661 inout_mode[ninout] = TYPE_MODE (type);
1662 inout_opnum[ninout++] = i;
1665 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1666 clobber_conflict_found = 1;
1669 /* Make vectors for the expression-rtx, constraint strings,
1670 and named operands. */
1672 argvec = rtvec_alloc (ninputs);
1673 constraintvec = rtvec_alloc (ninputs);
1675 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
1676 : GET_MODE (output_rtx[0])),
1677 TREE_STRING_POINTER (string),
1678 empty_string, 0, argvec, constraintvec,
1679 filename, line);
1681 MEM_VOLATILE_P (body) = vol;
1683 /* Eval the inputs and put them into ARGVEC.
1684 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1686 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
1688 bool allows_reg, allows_mem;
1689 const char *constraint;
1690 tree val, type;
1691 rtx op;
1693 constraint = constraints[i + noutputs];
1694 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1695 constraints, &allows_mem, &allows_reg))
1696 abort ();
1698 generating_concat_p = 0;
1700 val = TREE_VALUE (tail);
1701 type = TREE_TYPE (val);
1702 op = expand_expr (val, NULL_RTX, VOIDmode,
1703 (allows_mem && !allows_reg
1704 ? EXPAND_MEMORY : EXPAND_NORMAL));
1706 /* Never pass a CONCAT to an ASM. */
1707 if (GET_CODE (op) == CONCAT)
1708 op = force_reg (GET_MODE (op), op);
1709 else if (GET_CODE (op) == MEM)
1710 op = validize_mem (op);
1712 if (asm_operand_ok (op, constraint) <= 0)
1714 if (allows_reg)
1715 op = force_reg (TYPE_MODE (type), op);
1716 else if (!allows_mem)
1717 warning ("asm operand %d probably doesn't match constraints",
1718 i + noutputs);
1719 else if (GET_CODE (op) == MEM)
1721 /* We won't recognize either volatile memory or memory
1722 with a queued address as available a memory_operand
1723 at this point. Ignore it: clearly this *is* a memory. */
1725 else
1727 warning ("use of memory input without lvalue in "
1728 "asm operand %d is deprecated", i + noutputs);
1730 if (CONSTANT_P (op))
1732 op = force_const_mem (TYPE_MODE (type), op);
1733 op = validize_mem (op);
1735 else if (GET_CODE (op) == REG
1736 || GET_CODE (op) == SUBREG
1737 || GET_CODE (op) == ADDRESSOF
1738 || GET_CODE (op) == CONCAT)
1740 tree qual_type = build_qualified_type (type,
1741 (TYPE_QUALS (type)
1742 | TYPE_QUAL_CONST));
1743 rtx memloc = assign_temp (qual_type, 1, 1, 1);
1744 memloc = validize_mem (memloc);
1745 emit_move_insn (memloc, op);
1746 op = memloc;
1751 generating_concat_p = old_generating_concat_p;
1752 ASM_OPERANDS_INPUT (body, i) = op;
1754 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
1755 = gen_rtx_ASM_INPUT (TYPE_MODE (type), constraints[i + noutputs]);
1757 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1758 clobber_conflict_found = 1;
1761 /* Protect all the operands from the queue now that they have all been
1762 evaluated. */
1764 generating_concat_p = 0;
1766 for (i = 0; i < ninputs - ninout; i++)
1767 ASM_OPERANDS_INPUT (body, i)
1768 = protect_from_queue (ASM_OPERANDS_INPUT (body, i), 0);
1770 for (i = 0; i < noutputs; i++)
1771 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1773 /* For in-out operands, copy output rtx to input rtx. */
1774 for (i = 0; i < ninout; i++)
1776 int j = inout_opnum[i];
1777 char buffer[16];
1779 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
1780 = output_rtx[j];
1782 sprintf (buffer, "%d", j);
1783 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
1784 = gen_rtx_ASM_INPUT (inout_mode[i], ggc_strdup (buffer));
1787 generating_concat_p = old_generating_concat_p;
1789 /* Now, for each output, construct an rtx
1790 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
1791 ARGVEC CONSTRAINTS OPNAMES))
1792 If there is more than one, put them inside a PARALLEL. */
1794 if (noutputs == 1 && nclobbers == 0)
1796 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
1797 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
1800 else if (noutputs == 0 && nclobbers == 0)
1802 /* No output operands: put in a raw ASM_OPERANDS rtx. */
1803 emit_insn (body);
1806 else
1808 rtx obody = body;
1809 int num = noutputs;
1811 if (num == 0)
1812 num = 1;
1814 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
1816 /* For each output operand, store a SET. */
1817 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1819 XVECEXP (body, 0, i)
1820 = gen_rtx_SET (VOIDmode,
1821 output_rtx[i],
1822 gen_rtx_ASM_OPERANDS
1823 (GET_MODE (output_rtx[i]),
1824 TREE_STRING_POINTER (string),
1825 constraints[i], i, argvec, constraintvec,
1826 filename, line));
1828 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1831 /* If there are no outputs (but there are some clobbers)
1832 store the bare ASM_OPERANDS into the PARALLEL. */
1834 if (i == 0)
1835 XVECEXP (body, 0, i++) = obody;
1837 /* Store (clobber REG) for each clobbered register specified. */
1839 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1841 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1842 int j = decode_reg_name (regname);
1843 rtx clobbered_reg;
1845 if (j < 0)
1847 if (j == -3) /* `cc', which is not a register */
1848 continue;
1850 if (j == -4) /* `memory', don't cache memory across asm */
1852 XVECEXP (body, 0, i++)
1853 = gen_rtx_CLOBBER (VOIDmode,
1854 gen_rtx_MEM
1855 (BLKmode,
1856 gen_rtx_SCRATCH (VOIDmode)));
1857 continue;
1860 /* Ignore unknown register, error already signaled. */
1861 continue;
1864 /* Use QImode since that's guaranteed to clobber just one reg. */
1865 clobbered_reg = gen_rtx_REG (QImode, j);
1867 /* Do sanity check for overlap between clobbers and respectively
1868 input and outputs that hasn't been handled. Such overlap
1869 should have been detected and reported above. */
1870 if (!clobber_conflict_found)
1872 int opno;
1874 /* We test the old body (obody) contents to avoid tripping
1875 over the under-construction body. */
1876 for (opno = 0; opno < noutputs; opno++)
1877 if (reg_overlap_mentioned_p (clobbered_reg, output_rtx[opno]))
1878 internal_error ("asm clobber conflict with output operand");
1880 for (opno = 0; opno < ninputs - ninout; opno++)
1881 if (reg_overlap_mentioned_p (clobbered_reg,
1882 ASM_OPERANDS_INPUT (obody, opno)))
1883 internal_error ("asm clobber conflict with input operand");
1886 XVECEXP (body, 0, i++)
1887 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
1890 emit_insn (body);
1893 /* For any outputs that needed reloading into registers, spill them
1894 back to where they belong. */
1895 for (i = 0; i < noutputs; ++i)
1896 if (real_output_rtx[i])
1897 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1899 free_temp_slots ();
1902 /* A subroutine of expand_asm_operands. Check that all operands have
1903 the same number of alternatives. Return true if so. */
1905 static bool
1906 check_operand_nalternatives (tree outputs, tree inputs)
1908 if (outputs || inputs)
1910 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1911 int nalternatives
1912 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
1913 tree next = inputs;
1915 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1917 error ("too many alternatives in `asm'");
1918 return false;
1921 tmp = outputs;
1922 while (tmp)
1924 const char *constraint
1925 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
1927 if (n_occurrences (',', constraint) != nalternatives)
1929 error ("operand constraints for `asm' differ in number of alternatives");
1930 return false;
1933 if (TREE_CHAIN (tmp))
1934 tmp = TREE_CHAIN (tmp);
1935 else
1936 tmp = next, next = 0;
1940 return true;
1943 /* A subroutine of expand_asm_operands. Check that all operand names
1944 are unique. Return true if so. We rely on the fact that these names
1945 are identifiers, and so have been canonicalized by get_identifier,
1946 so all we need are pointer comparisons. */
1948 static bool
1949 check_unique_operand_names (tree outputs, tree inputs)
1951 tree i, j;
1953 for (i = outputs; i ; i = TREE_CHAIN (i))
1955 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1956 if (! i_name)
1957 continue;
1959 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
1960 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1961 goto failure;
1964 for (i = inputs; i ; i = TREE_CHAIN (i))
1966 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
1967 if (! i_name)
1968 continue;
1970 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
1971 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1972 goto failure;
1973 for (j = outputs; j ; j = TREE_CHAIN (j))
1974 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
1975 goto failure;
1978 return true;
1980 failure:
1981 error ("duplicate asm operand name '%s'",
1982 TREE_STRING_POINTER (TREE_PURPOSE (TREE_PURPOSE (i))));
1983 return false;
1986 /* A subroutine of expand_asm_operands. Resolve the names of the operands
1987 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
1988 STRING and in the constraints to those numbers. */
1990 tree
1991 resolve_asm_operand_names (tree string, tree outputs, tree inputs)
1993 char *buffer;
1994 char *p;
1995 const char *c;
1996 tree t;
1998 /* Substitute [<name>] in input constraint strings. There should be no
1999 named operands in output constraints. */
2000 for (t = inputs; t ; t = TREE_CHAIN (t))
2002 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2003 if (strchr (c, '[') != NULL)
2005 p = buffer = xstrdup (c);
2006 while ((p = strchr (p, '[')) != NULL)
2007 p = resolve_operand_name_1 (p, outputs, inputs);
2008 TREE_VALUE (TREE_PURPOSE (t))
2009 = build_string (strlen (buffer), buffer);
2010 free (buffer);
2014 /* Now check for any needed substitutions in the template. */
2015 c = TREE_STRING_POINTER (string);
2016 while ((c = strchr (c, '%')) != NULL)
2018 if (c[1] == '[')
2019 break;
2020 else if (ISALPHA (c[1]) && c[2] == '[')
2021 break;
2022 else
2024 c += 1;
2025 continue;
2029 if (c)
2031 /* OK, we need to make a copy so we can perform the substitutions.
2032 Assume that we will not need extra space--we get to remove '['
2033 and ']', which means we cannot have a problem until we have more
2034 than 999 operands. */
2035 buffer = xstrdup (TREE_STRING_POINTER (string));
2036 p = buffer + (c - TREE_STRING_POINTER (string));
2038 while ((p = strchr (p, '%')) != NULL)
2040 if (p[1] == '[')
2041 p += 1;
2042 else if (ISALPHA (p[1]) && p[2] == '[')
2043 p += 2;
2044 else
2046 p += 1;
2047 continue;
2050 p = resolve_operand_name_1 (p, outputs, inputs);
2053 string = build_string (strlen (buffer), buffer);
2054 free (buffer);
2057 return string;
2060 /* A subroutine of resolve_operand_names. P points to the '[' for a
2061 potential named operand of the form [<name>]. In place, replace
2062 the name and brackets with a number. Return a pointer to the
2063 balance of the string after substitution. */
2065 static char *
2066 resolve_operand_name_1 (char *p, tree outputs, tree inputs)
2068 char *q;
2069 int op;
2070 tree t;
2071 size_t len;
2073 /* Collect the operand name. */
2074 q = strchr (p, ']');
2075 if (!q)
2077 error ("missing close brace for named operand");
2078 return strchr (p, '\0');
2080 len = q - p - 1;
2082 /* Resolve the name to a number. */
2083 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
2085 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
2086 if (name)
2088 const char *c = TREE_STRING_POINTER (name);
2089 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
2090 goto found;
2093 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
2095 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
2096 if (name)
2098 const char *c = TREE_STRING_POINTER (name);
2099 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
2100 goto found;
2104 *q = '\0';
2105 error ("undefined named operand '%s'", p + 1);
2106 op = 0;
2107 found:
2109 /* Replace the name with the number. Unfortunately, not all libraries
2110 get the return value of sprintf correct, so search for the end of the
2111 generated string by hand. */
2112 sprintf (p, "%d", op);
2113 p = strchr (p, '\0');
2115 /* Verify the no extra buffer space assumption. */
2116 if (p > q)
2117 abort ();
2119 /* Shift the rest of the buffer down to fill the gap. */
2120 memmove (p, q + 1, strlen (q + 1) + 1);
2122 return p;
2125 /* Generate RTL to evaluate the expression EXP
2126 and remember it in case this is the VALUE in a ({... VALUE; }) constr.
2127 Provided just for backward-compatibility. expand_expr_stmt_value()
2128 should be used for new code. */
2130 void
2131 expand_expr_stmt (tree exp)
2133 expand_expr_stmt_value (exp, -1, 1);
2136 /* Generate RTL to evaluate the expression EXP. WANT_VALUE tells
2137 whether to (1) save the value of the expression, (0) discard it or
2138 (-1) use expr_stmts_for_value to tell. The use of -1 is
2139 deprecated, and retained only for backward compatibility. */
2141 void
2142 expand_expr_stmt_value (tree exp, int want_value, int maybe_last)
2144 rtx value;
2145 tree type;
2147 if (want_value == -1)
2148 want_value = expr_stmts_for_value != 0;
2150 /* If -Wextra, warn about statements with no side effects,
2151 except for an explicit cast to void (e.g. for assert()), and
2152 except for last statement in ({...}) where they may be useful. */
2153 if (! want_value
2154 && (expr_stmts_for_value == 0 || ! maybe_last)
2155 && exp != error_mark_node
2156 && warn_unused_value)
2158 if (TREE_SIDE_EFFECTS (exp))
2159 warn_if_unused_value (exp);
2160 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
2161 warning ("%Hstatement with no effect", &emit_locus);
2164 /* If EXP is of function type and we are expanding statements for
2165 value, convert it to pointer-to-function. */
2166 if (want_value && TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE)
2167 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
2169 /* The call to `expand_expr' could cause last_expr_type and
2170 last_expr_value to get reset. Therefore, we set last_expr_value
2171 and last_expr_type *after* calling expand_expr. */
2172 value = expand_expr (exp, want_value ? NULL_RTX : const0_rtx,
2173 VOIDmode, 0);
2174 type = TREE_TYPE (exp);
2176 /* If all we do is reference a volatile value in memory,
2177 copy it to a register to be sure it is actually touched. */
2178 if (value && GET_CODE (value) == MEM && TREE_THIS_VOLATILE (exp))
2180 if (TYPE_MODE (type) == VOIDmode)
2182 else if (TYPE_MODE (type) != BLKmode)
2183 value = copy_to_reg (value);
2184 else
2186 rtx lab = gen_label_rtx ();
2188 /* Compare the value with itself to reference it. */
2189 emit_cmp_and_jump_insns (value, value, EQ,
2190 expand_expr (TYPE_SIZE (type),
2191 NULL_RTX, VOIDmode, 0),
2192 BLKmode, 0, lab);
2193 emit_label (lab);
2197 /* If this expression is part of a ({...}) and is in memory, we may have
2198 to preserve temporaries. */
2199 preserve_temp_slots (value);
2201 /* Free any temporaries used to evaluate this expression. Any temporary
2202 used as a result of this expression will already have been preserved
2203 above. */
2204 free_temp_slots ();
2206 if (want_value)
2208 last_expr_value = value;
2209 last_expr_type = type;
2212 emit_queue ();
2215 /* Warn if EXP contains any computations whose results are not used.
2216 Return 1 if a warning is printed; 0 otherwise. */
2219 warn_if_unused_value (tree exp)
2221 if (TREE_USED (exp))
2222 return 0;
2224 /* Don't warn about void constructs. This includes casting to void,
2225 void function calls, and statement expressions with a final cast
2226 to void. */
2227 if (VOID_TYPE_P (TREE_TYPE (exp)))
2228 return 0;
2230 switch (TREE_CODE (exp))
2232 case PREINCREMENT_EXPR:
2233 case POSTINCREMENT_EXPR:
2234 case PREDECREMENT_EXPR:
2235 case POSTDECREMENT_EXPR:
2236 case MODIFY_EXPR:
2237 case INIT_EXPR:
2238 case TARGET_EXPR:
2239 case CALL_EXPR:
2240 case RTL_EXPR:
2241 case TRY_CATCH_EXPR:
2242 case WITH_CLEANUP_EXPR:
2243 case EXIT_EXPR:
2244 return 0;
2246 case BIND_EXPR:
2247 /* For a binding, warn if no side effect within it. */
2248 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2250 case SAVE_EXPR:
2251 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2253 case TRUTH_ORIF_EXPR:
2254 case TRUTH_ANDIF_EXPR:
2255 /* In && or ||, warn if 2nd operand has no side effect. */
2256 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2258 case COMPOUND_EXPR:
2259 if (TREE_NO_UNUSED_WARNING (exp))
2260 return 0;
2261 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
2262 return 1;
2263 /* Let people do `(foo (), 0)' without a warning. */
2264 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2265 return 0;
2266 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2268 case NOP_EXPR:
2269 case CONVERT_EXPR:
2270 case NON_LVALUE_EXPR:
2271 /* Don't warn about conversions not explicit in the user's program. */
2272 if (TREE_NO_UNUSED_WARNING (exp))
2273 return 0;
2274 /* Assignment to a cast usually results in a cast of a modify.
2275 Don't complain about that. There can be an arbitrary number of
2276 casts before the modify, so we must loop until we find the first
2277 non-cast expression and then test to see if that is a modify. */
2279 tree tem = TREE_OPERAND (exp, 0);
2281 while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
2282 tem = TREE_OPERAND (tem, 0);
2284 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
2285 || TREE_CODE (tem) == CALL_EXPR)
2286 return 0;
2288 goto maybe_warn;
2290 case INDIRECT_REF:
2291 /* Don't warn about automatic dereferencing of references, since
2292 the user cannot control it. */
2293 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2294 return warn_if_unused_value (TREE_OPERAND (exp, 0));
2295 /* Fall through. */
2297 default:
2298 /* Referencing a volatile value is a side effect, so don't warn. */
2299 if ((DECL_P (exp)
2300 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
2301 && TREE_THIS_VOLATILE (exp))
2302 return 0;
2304 /* If this is an expression which has no operands, there is no value
2305 to be unused. There are no such language-independent codes,
2306 but front ends may define such. */
2307 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'e'
2308 && TREE_CODE_LENGTH (TREE_CODE (exp)) == 0)
2309 return 0;
2311 maybe_warn:
2312 /* If this is an expression with side effects, don't warn. */
2313 if (TREE_SIDE_EFFECTS (exp))
2314 return 0;
2316 warning ("%Hvalue computed is not used", &emit_locus);
2317 return 1;
2321 /* Clear out the memory of the last expression evaluated. */
2323 void
2324 clear_last_expr (void)
2326 last_expr_type = NULL_TREE;
2327 last_expr_value = NULL_RTX;
2330 /* Begin a statement-expression, i.e., a series of statements which
2331 may return a value. Return the RTL_EXPR for this statement expr.
2332 The caller must save that value and pass it to
2333 expand_end_stmt_expr. If HAS_SCOPE is nonzero, temporaries created
2334 in the statement-expression are deallocated at the end of the
2335 expression. */
2337 tree
2338 expand_start_stmt_expr (int has_scope)
2340 tree t;
2342 /* Make the RTL_EXPR node temporary, not momentary,
2343 so that rtl_expr_chain doesn't become garbage. */
2344 t = make_node (RTL_EXPR);
2345 do_pending_stack_adjust ();
2346 if (has_scope)
2347 start_sequence_for_rtl_expr (t);
2348 else
2349 start_sequence ();
2350 NO_DEFER_POP;
2351 expr_stmts_for_value++;
2352 return t;
2355 /* Restore the previous state at the end of a statement that returns a value.
2356 Returns a tree node representing the statement's value and the
2357 insns to compute the value.
2359 The nodes of that expression have been freed by now, so we cannot use them.
2360 But we don't want to do that anyway; the expression has already been
2361 evaluated and now we just want to use the value. So generate a RTL_EXPR
2362 with the proper type and RTL value.
2364 If the last substatement was not an expression,
2365 return something with type `void'. */
2367 tree
2368 expand_end_stmt_expr (tree t)
2370 OK_DEFER_POP;
2372 if (! last_expr_value || ! last_expr_type)
2374 last_expr_value = const0_rtx;
2375 last_expr_type = void_type_node;
2377 else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
2378 /* Remove any possible QUEUED. */
2379 last_expr_value = protect_from_queue (last_expr_value, 0);
2381 emit_queue ();
2383 TREE_TYPE (t) = last_expr_type;
2384 RTL_EXPR_RTL (t) = last_expr_value;
2385 RTL_EXPR_SEQUENCE (t) = get_insns ();
2387 rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
2389 end_sequence ();
2391 /* Don't consider deleting this expr or containing exprs at tree level. */
2392 TREE_SIDE_EFFECTS (t) = 1;
2393 /* Propagate volatility of the actual RTL expr. */
2394 TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
2396 clear_last_expr ();
2397 expr_stmts_for_value--;
2399 return t;
2402 /* Generate RTL for the start of an if-then. COND is the expression
2403 whose truth should be tested.
2405 If EXITFLAG is nonzero, this conditional is visible to
2406 `exit_something'. */
2408 void
2409 expand_start_cond (tree cond, int exitflag)
2411 struct nesting *thiscond = ALLOC_NESTING ();
2413 /* Make an entry on cond_stack for the cond we are entering. */
2415 thiscond->desc = COND_NESTING;
2416 thiscond->next = cond_stack;
2417 thiscond->all = nesting_stack;
2418 thiscond->depth = ++nesting_depth;
2419 thiscond->data.cond.next_label = gen_label_rtx ();
2420 /* Before we encounter an `else', we don't need a separate exit label
2421 unless there are supposed to be exit statements
2422 to exit this conditional. */
2423 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
2424 thiscond->data.cond.endif_label = thiscond->exit_label;
2425 cond_stack = thiscond;
2426 nesting_stack = thiscond;
2428 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
2431 /* Generate RTL between then-clause and the elseif-clause
2432 of an if-then-elseif-.... */
2434 void
2435 expand_start_elseif (tree cond)
2437 if (cond_stack->data.cond.endif_label == 0)
2438 cond_stack->data.cond.endif_label = gen_label_rtx ();
2439 emit_jump (cond_stack->data.cond.endif_label);
2440 emit_label (cond_stack->data.cond.next_label);
2441 cond_stack->data.cond.next_label = gen_label_rtx ();
2442 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2445 /* Generate RTL between the then-clause and the else-clause
2446 of an if-then-else. */
2448 void
2449 expand_start_else (void)
2451 if (cond_stack->data.cond.endif_label == 0)
2452 cond_stack->data.cond.endif_label = gen_label_rtx ();
2454 emit_jump (cond_stack->data.cond.endif_label);
2455 emit_label (cond_stack->data.cond.next_label);
2456 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
2459 /* After calling expand_start_else, turn this "else" into an "else if"
2460 by providing another condition. */
2462 void
2463 expand_elseif (tree cond)
2465 cond_stack->data.cond.next_label = gen_label_rtx ();
2466 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2469 /* Generate RTL for the end of an if-then.
2470 Pop the record for it off of cond_stack. */
2472 void
2473 expand_end_cond (void)
2475 struct nesting *thiscond = cond_stack;
2477 do_pending_stack_adjust ();
2478 if (thiscond->data.cond.next_label)
2479 emit_label (thiscond->data.cond.next_label);
2480 if (thiscond->data.cond.endif_label)
2481 emit_label (thiscond->data.cond.endif_label);
2483 POPSTACK (cond_stack);
2484 clear_last_expr ();
2487 /* Generate RTL for the start of a loop. EXIT_FLAG is nonzero if this
2488 loop should be exited by `exit_something'. This is a loop for which
2489 `expand_continue' will jump to the top of the loop.
2491 Make an entry on loop_stack to record the labels associated with
2492 this loop. */
2494 struct nesting *
2495 expand_start_loop (int exit_flag)
2497 struct nesting *thisloop = ALLOC_NESTING ();
2499 /* Make an entry on loop_stack for the loop we are entering. */
2501 thisloop->desc = LOOP_NESTING;
2502 thisloop->next = loop_stack;
2503 thisloop->all = nesting_stack;
2504 thisloop->depth = ++nesting_depth;
2505 thisloop->data.loop.start_label = gen_label_rtx ();
2506 thisloop->data.loop.end_label = gen_label_rtx ();
2507 thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
2508 thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
2509 loop_stack = thisloop;
2510 nesting_stack = thisloop;
2512 do_pending_stack_adjust ();
2513 emit_queue ();
2514 emit_note (NOTE_INSN_LOOP_BEG);
2515 emit_label (thisloop->data.loop.start_label);
2517 return thisloop;
2520 /* Like expand_start_loop but for a loop where the continuation point
2521 (for expand_continue_loop) will be specified explicitly. */
2523 struct nesting *
2524 expand_start_loop_continue_elsewhere (int exit_flag)
2526 struct nesting *thisloop = expand_start_loop (exit_flag);
2527 loop_stack->data.loop.continue_label = gen_label_rtx ();
2528 return thisloop;
2531 /* Begin a null, aka do { } while (0) "loop". But since the contents
2532 of said loop can still contain a break, we must frob the loop nest. */
2534 struct nesting *
2535 expand_start_null_loop (void)
2537 struct nesting *thisloop = ALLOC_NESTING ();
2539 /* Make an entry on loop_stack for the loop we are entering. */
2541 thisloop->desc = LOOP_NESTING;
2542 thisloop->next = loop_stack;
2543 thisloop->all = nesting_stack;
2544 thisloop->depth = ++nesting_depth;
2545 thisloop->data.loop.start_label = emit_note (NOTE_INSN_DELETED);
2546 thisloop->data.loop.end_label = gen_label_rtx ();
2547 thisloop->data.loop.continue_label = thisloop->data.loop.end_label;
2548 thisloop->exit_label = thisloop->data.loop.end_label;
2549 loop_stack = thisloop;
2550 nesting_stack = thisloop;
2552 return thisloop;
2555 /* Specify the continuation point for a loop started with
2556 expand_start_loop_continue_elsewhere.
2557 Use this at the point in the code to which a continue statement
2558 should jump. */
2560 void
2561 expand_loop_continue_here (void)
2563 do_pending_stack_adjust ();
2564 emit_note (NOTE_INSN_LOOP_CONT);
2565 emit_label (loop_stack->data.loop.continue_label);
2568 /* Finish a loop. Generate a jump back to the top and the loop-exit label.
2569 Pop the block off of loop_stack. */
2571 void
2572 expand_end_loop (void)
2574 rtx start_label = loop_stack->data.loop.start_label;
2575 rtx etc_note;
2576 int eh_regions, debug_blocks;
2577 bool empty_test;
2579 /* Mark the continue-point at the top of the loop if none elsewhere. */
2580 if (start_label == loop_stack->data.loop.continue_label)
2581 emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
2583 do_pending_stack_adjust ();
2585 /* If the loop starts with a loop exit, roll that to the end where
2586 it will optimize together with the jump back.
2588 If the loop presently looks like this (in pseudo-C):
2590 LOOP_BEG
2591 start_label:
2592 if (test) goto end_label;
2593 LOOP_END_TOP_COND
2594 body;
2595 goto start_label;
2596 end_label:
2598 transform it to look like:
2600 LOOP_BEG
2601 goto start_label;
2602 top_label:
2603 body;
2604 start_label:
2605 if (test) goto end_label;
2606 goto top_label;
2607 end_label:
2609 We rely on the presence of NOTE_INSN_LOOP_END_TOP_COND to mark
2610 the end of the entry conditional. Without this, our lexical scan
2611 can't tell the difference between an entry conditional and a
2612 body conditional that exits the loop. Mistaking the two means
2613 that we can misplace the NOTE_INSN_LOOP_CONT note, which can
2614 screw up loop unrolling.
2616 Things will be oh so much better when loop optimization is done
2617 off of a proper control flow graph... */
2619 /* Scan insns from the top of the loop looking for the END_TOP_COND note. */
2621 empty_test = true;
2622 eh_regions = debug_blocks = 0;
2623 for (etc_note = start_label; etc_note ; etc_note = NEXT_INSN (etc_note))
2624 if (GET_CODE (etc_note) == NOTE)
2626 if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_LOOP_END_TOP_COND)
2627 break;
2629 /* We must not walk into a nested loop. */
2630 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_LOOP_BEG)
2632 etc_note = NULL_RTX;
2633 break;
2636 /* At the same time, scan for EH region notes, as we don't want
2637 to scrog region nesting. This shouldn't happen, but... */
2638 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_EH_REGION_BEG)
2639 eh_regions++;
2640 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_EH_REGION_END)
2642 if (--eh_regions < 0)
2643 /* We've come to the end of an EH region, but never saw the
2644 beginning of that region. That means that an EH region
2645 begins before the top of the loop, and ends in the middle
2646 of it. The existence of such a situation violates a basic
2647 assumption in this code, since that would imply that even
2648 when EH_REGIONS is zero, we might move code out of an
2649 exception region. */
2650 abort ();
2653 /* Likewise for debug scopes. In this case we'll either (1) move
2654 all of the notes if they are properly nested or (2) leave the
2655 notes alone and only rotate the loop at high optimization
2656 levels when we expect to scrog debug info. */
2657 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_BLOCK_BEG)
2658 debug_blocks++;
2659 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_BLOCK_END)
2660 debug_blocks--;
2662 else if (INSN_P (etc_note))
2663 empty_test = false;
2665 if (etc_note
2666 && optimize
2667 && ! empty_test
2668 && eh_regions == 0
2669 && (debug_blocks == 0 || optimize >= 2)
2670 && NEXT_INSN (etc_note) != NULL_RTX
2671 && ! any_condjump_p (get_last_insn ()))
2673 /* We found one. Move everything from START to ETC to the end
2674 of the loop, and add a jump from the top of the loop. */
2675 rtx top_label = gen_label_rtx ();
2676 rtx start_move = start_label;
2678 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
2679 then we want to move this note also. */
2680 if (GET_CODE (PREV_INSN (start_move)) == NOTE
2681 && NOTE_LINE_NUMBER (PREV_INSN (start_move)) == NOTE_INSN_LOOP_CONT)
2682 start_move = PREV_INSN (start_move);
2684 emit_label_before (top_label, start_move);
2686 /* Actually move the insns. If the debug scopes are nested, we
2687 can move everything at once. Otherwise we have to move them
2688 one by one and squeeze out the block notes. */
2689 if (debug_blocks == 0)
2690 reorder_insns (start_move, etc_note, get_last_insn ());
2691 else
2693 rtx insn, next_insn;
2694 for (insn = start_move; insn; insn = next_insn)
2696 /* Figure out which insn comes after this one. We have
2697 to do this before we move INSN. */
2698 next_insn = (insn == etc_note ? NULL : NEXT_INSN (insn));
2700 if (GET_CODE (insn) == NOTE
2701 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2702 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2703 continue;
2705 reorder_insns (insn, insn, get_last_insn ());
2709 /* Add the jump from the top of the loop. */
2710 emit_jump_insn_before (gen_jump (start_label), top_label);
2711 emit_barrier_before (top_label);
2712 start_label = top_label;
2715 emit_jump (start_label);
2716 emit_note (NOTE_INSN_LOOP_END);
2717 emit_label (loop_stack->data.loop.end_label);
2719 POPSTACK (loop_stack);
2721 clear_last_expr ();
2724 /* Finish a null loop, aka do { } while (0). */
2726 void
2727 expand_end_null_loop (void)
2729 do_pending_stack_adjust ();
2730 emit_label (loop_stack->data.loop.end_label);
2732 POPSTACK (loop_stack);
2734 clear_last_expr ();
2737 /* Generate a jump to the current loop's continue-point.
2738 This is usually the top of the loop, but may be specified
2739 explicitly elsewhere. If not currently inside a loop,
2740 return 0 and do nothing; caller will print an error message. */
2743 expand_continue_loop (struct nesting *whichloop)
2745 /* Emit information for branch prediction. */
2746 rtx note;
2748 if (flag_guess_branch_prob)
2750 note = emit_note (NOTE_INSN_PREDICTION);
2751 NOTE_PREDICTION (note) = NOTE_PREDICT (PRED_CONTINUE, IS_TAKEN);
2753 clear_last_expr ();
2754 if (whichloop == 0)
2755 whichloop = loop_stack;
2756 if (whichloop == 0)
2757 return 0;
2758 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2759 NULL_RTX);
2760 return 1;
2763 /* Generate a jump to exit the current loop. If not currently inside a loop,
2764 return 0 and do nothing; caller will print an error message. */
2767 expand_exit_loop (struct nesting *whichloop)
2769 clear_last_expr ();
2770 if (whichloop == 0)
2771 whichloop = loop_stack;
2772 if (whichloop == 0)
2773 return 0;
2774 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
2775 return 1;
2778 /* Generate a conditional jump to exit the current loop if COND
2779 evaluates to zero. If not currently inside a loop,
2780 return 0 and do nothing; caller will print an error message. */
2783 expand_exit_loop_if_false (struct nesting *whichloop, tree cond)
2785 rtx label;
2786 clear_last_expr ();
2788 if (whichloop == 0)
2789 whichloop = loop_stack;
2790 if (whichloop == 0)
2791 return 0;
2793 if (integer_nonzerop (cond))
2794 return 1;
2795 if (integer_zerop (cond))
2796 return expand_exit_loop (whichloop);
2798 /* Check if we definitely won't need a fixup. */
2799 if (whichloop == nesting_stack)
2801 jumpifnot (cond, whichloop->data.loop.end_label);
2802 return 1;
2805 /* In order to handle fixups, we actually create a conditional jump
2806 around an unconditional branch to exit the loop. If fixups are
2807 necessary, they go before the unconditional branch. */
2809 label = gen_label_rtx ();
2810 jumpif (cond, label);
2811 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
2812 NULL_RTX);
2813 emit_label (label);
2815 return 1;
2818 /* Like expand_exit_loop_if_false except also emit a note marking
2819 the end of the conditional. Should only be used immediately
2820 after expand_loop_start. */
2823 expand_exit_loop_top_cond (struct nesting *whichloop, tree cond)
2825 if (! expand_exit_loop_if_false (whichloop, cond))
2826 return 0;
2828 emit_note (NOTE_INSN_LOOP_END_TOP_COND);
2829 return 1;
2832 /* Return nonzero if we should preserve sub-expressions as separate
2833 pseudos. We never do so if we aren't optimizing. We always do so
2834 if -fexpensive-optimizations.
2836 Otherwise, we only do so if we are in the "early" part of a loop. I.e.,
2837 the loop may still be a small one. */
2840 preserve_subexpressions_p (void)
2842 rtx insn;
2844 if (flag_expensive_optimizations)
2845 return 1;
2847 if (optimize == 0 || cfun == 0 || cfun->stmt == 0 || loop_stack == 0)
2848 return 0;
2850 insn = get_last_insn_anywhere ();
2852 return (insn
2853 && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2854 < n_non_fixed_regs * 3));
2858 /* Generate a jump to exit the current loop, conditional, binding contour
2859 or case statement. Not all such constructs are visible to this function,
2860 only those started with EXIT_FLAG nonzero. Individual languages use
2861 the EXIT_FLAG parameter to control which kinds of constructs you can
2862 exit this way.
2864 If not currently inside anything that can be exited,
2865 return 0 and do nothing; caller will print an error message. */
2868 expand_exit_something (void)
2870 struct nesting *n;
2871 clear_last_expr ();
2872 for (n = nesting_stack; n; n = n->all)
2873 if (n->exit_label != 0)
2875 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
2876 return 1;
2879 return 0;
2882 /* Generate RTL to return from the current function, with no value.
2883 (That is, we do not do anything about returning any value.) */
2885 void
2886 expand_null_return (void)
2888 rtx last_insn;
2890 last_insn = get_last_insn ();
2892 /* If this function was declared to return a value, but we
2893 didn't, clobber the return registers so that they are not
2894 propagated live to the rest of the function. */
2895 clobber_return_register ();
2897 expand_null_return_1 (last_insn);
2900 /* Try to guess whether the value of return means error code. */
2901 static enum br_predictor
2902 return_prediction (rtx val)
2904 /* Different heuristics for pointers and scalars. */
2905 if (POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
2907 /* NULL is usually not returned. */
2908 if (val == const0_rtx)
2909 return PRED_NULL_RETURN;
2911 else
2913 /* Negative return values are often used to indicate
2914 errors. */
2915 if (GET_CODE (val) == CONST_INT
2916 && INTVAL (val) < 0)
2917 return PRED_NEGATIVE_RETURN;
2918 /* Constant return values are also usually erors,
2919 zero/one often mean booleans so exclude them from the
2920 heuristics. */
2921 if (CONSTANT_P (val)
2922 && (val != const0_rtx && val != const1_rtx))
2923 return PRED_CONST_RETURN;
2925 return PRED_NO_PREDICTION;
2928 /* Generate RTL to return from the current function, with value VAL. */
2930 static void
2931 expand_value_return (rtx val)
2933 rtx last_insn;
2934 rtx return_reg;
2935 enum br_predictor pred;
2937 if (flag_guess_branch_prob
2938 && (pred = return_prediction (val)) != PRED_NO_PREDICTION)
2940 /* Emit information for branch prediction. */
2941 rtx note;
2943 note = emit_note (NOTE_INSN_PREDICTION);
2945 NOTE_PREDICTION (note) = NOTE_PREDICT (pred, NOT_TAKEN);
2949 last_insn = get_last_insn ();
2950 return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
2952 /* Copy the value to the return location
2953 unless it's already there. */
2955 if (return_reg != val)
2957 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
2958 #ifdef PROMOTE_FUNCTION_RETURN
2959 int unsignedp = TREE_UNSIGNED (type);
2960 enum machine_mode old_mode
2961 = DECL_MODE (DECL_RESULT (current_function_decl));
2962 enum machine_mode mode
2963 = promote_mode (type, old_mode, &unsignedp, 1);
2965 if (mode != old_mode)
2966 val = convert_modes (mode, old_mode, val, unsignedp);
2967 #endif
2968 if (GET_CODE (return_reg) == PARALLEL)
2969 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
2970 else
2971 emit_move_insn (return_reg, val);
2974 expand_null_return_1 (last_insn);
2977 /* Output a return with no value. If LAST_INSN is nonzero,
2978 pretend that the return takes place after LAST_INSN. */
2980 static void
2981 expand_null_return_1 (rtx last_insn)
2983 rtx end_label = cleanup_label ? cleanup_label : return_label;
2985 clear_pending_stack_adjust ();
2986 do_pending_stack_adjust ();
2987 clear_last_expr ();
2989 if (end_label == 0)
2990 end_label = return_label = gen_label_rtx ();
2991 expand_goto_internal (NULL_TREE, end_label, last_insn);
2994 /* Generate RTL to evaluate the expression RETVAL and return it
2995 from the current function. */
2997 void
2998 expand_return (tree retval)
3000 /* If there are any cleanups to be performed, then they will
3001 be inserted following LAST_INSN. It is desirable
3002 that the last_insn, for such purposes, should be the
3003 last insn before computing the return value. Otherwise, cleanups
3004 which call functions can clobber the return value. */
3005 /* ??? rms: I think that is erroneous, because in C++ it would
3006 run destructors on variables that might be used in the subsequent
3007 computation of the return value. */
3008 rtx last_insn = 0;
3009 rtx result_rtl;
3010 rtx val = 0;
3011 tree retval_rhs;
3013 /* If function wants no value, give it none. */
3014 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3016 expand_expr (retval, NULL_RTX, VOIDmode, 0);
3017 emit_queue ();
3018 expand_null_return ();
3019 return;
3022 if (retval == error_mark_node)
3024 /* Treat this like a return of no value from a function that
3025 returns a value. */
3026 expand_null_return ();
3027 return;
3029 else if (TREE_CODE (retval) == RESULT_DECL)
3030 retval_rhs = retval;
3031 else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
3032 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3033 retval_rhs = TREE_OPERAND (retval, 1);
3034 else if (VOID_TYPE_P (TREE_TYPE (retval)))
3035 /* Recognize tail-recursive call to void function. */
3036 retval_rhs = retval;
3037 else
3038 retval_rhs = NULL_TREE;
3040 last_insn = get_last_insn ();
3042 /* Distribute return down conditional expr if either of the sides
3043 may involve tail recursion (see test below). This enhances the number
3044 of tail recursions we see. Don't do this always since it can produce
3045 sub-optimal code in some cases and we distribute assignments into
3046 conditional expressions when it would help. */
3048 if (optimize && retval_rhs != 0
3049 && frame_offset == 0
3050 && TREE_CODE (retval_rhs) == COND_EXPR
3051 && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
3052 || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
3054 rtx label = gen_label_rtx ();
3055 tree expr;
3057 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
3058 start_cleanup_deferral ();
3059 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
3060 DECL_RESULT (current_function_decl),
3061 TREE_OPERAND (retval_rhs, 1));
3062 TREE_SIDE_EFFECTS (expr) = 1;
3063 expand_return (expr);
3064 emit_label (label);
3066 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
3067 DECL_RESULT (current_function_decl),
3068 TREE_OPERAND (retval_rhs, 2));
3069 TREE_SIDE_EFFECTS (expr) = 1;
3070 expand_return (expr);
3071 end_cleanup_deferral ();
3072 return;
3075 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3077 /* If the result is an aggregate that is being returned in one (or more)
3078 registers, load the registers here. The compiler currently can't handle
3079 copying a BLKmode value into registers. We could put this code in a
3080 more general area (for use by everyone instead of just function
3081 call/return), but until this feature is generally usable it is kept here
3082 (and in expand_call). The value must go into a pseudo in case there
3083 are cleanups that will clobber the real return register. */
3085 if (retval_rhs != 0
3086 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3087 && GET_CODE (result_rtl) == REG)
3089 int i;
3090 unsigned HOST_WIDE_INT bitpos, xbitpos;
3091 unsigned HOST_WIDE_INT big_endian_correction = 0;
3092 unsigned HOST_WIDE_INT bytes
3093 = int_size_in_bytes (TREE_TYPE (retval_rhs));
3094 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3095 unsigned int bitsize
3096 = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)), BITS_PER_WORD);
3097 rtx *result_pseudos = alloca (sizeof (rtx) * n_regs);
3098 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
3099 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
3100 enum machine_mode tmpmode, result_reg_mode;
3102 if (bytes == 0)
3104 expand_null_return ();
3105 return;
3108 /* Structures whose size is not a multiple of a word are aligned
3109 to the least significant byte (to the right). On a BYTES_BIG_ENDIAN
3110 machine, this means we must skip the empty high order bytes when
3111 calculating the bit offset. */
3112 if (BYTES_BIG_ENDIAN
3113 && bytes % UNITS_PER_WORD)
3114 big_endian_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3115 * BITS_PER_UNIT));
3117 /* Copy the structure BITSIZE bits at a time. */
3118 for (bitpos = 0, xbitpos = big_endian_correction;
3119 bitpos < bytes * BITS_PER_UNIT;
3120 bitpos += bitsize, xbitpos += bitsize)
3122 /* We need a new destination pseudo each time xbitpos is
3123 on a word boundary and when xbitpos == big_endian_correction
3124 (the first time through). */
3125 if (xbitpos % BITS_PER_WORD == 0
3126 || xbitpos == big_endian_correction)
3128 /* Generate an appropriate register. */
3129 dst = gen_reg_rtx (word_mode);
3130 result_pseudos[xbitpos / BITS_PER_WORD] = dst;
3132 /* Clear the destination before we move anything into it. */
3133 emit_move_insn (dst, CONST0_RTX (GET_MODE (dst)));
3136 /* We need a new source operand each time bitpos is on a word
3137 boundary. */
3138 if (bitpos % BITS_PER_WORD == 0)
3139 src = operand_subword_force (result_val,
3140 bitpos / BITS_PER_WORD,
3141 BLKmode);
3143 /* Use bitpos for the source extraction (left justified) and
3144 xbitpos for the destination store (right justified). */
3145 store_bit_field (dst, bitsize, xbitpos % BITS_PER_WORD, word_mode,
3146 extract_bit_field (src, bitsize,
3147 bitpos % BITS_PER_WORD, 1,
3148 NULL_RTX, word_mode, word_mode,
3149 BITS_PER_WORD),
3150 BITS_PER_WORD);
3153 /* Find the smallest integer mode large enough to hold the
3154 entire structure and use that mode instead of BLKmode
3155 on the USE insn for the return register. */
3156 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3157 tmpmode != VOIDmode;
3158 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
3159 /* Have we found a large enough mode? */
3160 if (GET_MODE_SIZE (tmpmode) >= bytes)
3161 break;
3163 /* No suitable mode found. */
3164 if (tmpmode == VOIDmode)
3165 abort ();
3167 PUT_MODE (result_rtl, tmpmode);
3169 if (GET_MODE_SIZE (tmpmode) < GET_MODE_SIZE (word_mode))
3170 result_reg_mode = word_mode;
3171 else
3172 result_reg_mode = tmpmode;
3173 result_reg = gen_reg_rtx (result_reg_mode);
3175 emit_queue ();
3176 for (i = 0; i < n_regs; i++)
3177 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3178 result_pseudos[i]);
3180 if (tmpmode != result_reg_mode)
3181 result_reg = gen_lowpart (tmpmode, result_reg);
3183 expand_value_return (result_reg);
3185 else if (retval_rhs != 0
3186 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3187 && (GET_CODE (result_rtl) == REG
3188 || (GET_CODE (result_rtl) == PARALLEL)))
3190 /* Calculate the return value into a temporary (usually a pseudo
3191 reg). */
3192 tree ot = TREE_TYPE (DECL_RESULT (current_function_decl));
3193 tree nt = build_qualified_type (ot, TYPE_QUALS (ot) | TYPE_QUAL_CONST);
3195 val = assign_temp (nt, 0, 0, 1);
3196 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
3197 val = force_not_mem (val);
3198 emit_queue ();
3199 /* Return the calculated value, doing cleanups first. */
3200 expand_value_return (val);
3202 else
3204 /* No cleanups or no hard reg used;
3205 calculate value into hard return reg. */
3206 expand_expr (retval, const0_rtx, VOIDmode, 0);
3207 emit_queue ();
3208 expand_value_return (result_rtl);
3212 /* Attempt to optimize a potential tail recursion call into a goto.
3213 ARGUMENTS are the arguments to a CALL_EXPR; LAST_INSN indicates
3214 where to place the jump to the tail recursion label.
3216 Return TRUE if the call was optimized into a goto. */
3219 optimize_tail_recursion (tree arguments, rtx last_insn)
3221 /* Finish checking validity, and if valid emit code to set the
3222 argument variables for the new call. */
3223 if (tail_recursion_args (arguments, DECL_ARGUMENTS (current_function_decl)))
3225 if (tail_recursion_label == 0)
3227 tail_recursion_label = gen_label_rtx ();
3228 emit_label_after (tail_recursion_label,
3229 tail_recursion_reentry);
3231 emit_queue ();
3232 expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
3233 emit_barrier ();
3234 return 1;
3236 return 0;
3239 /* Emit code to alter this function's formal parms for a tail-recursive call.
3240 ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
3241 FORMALS is the chain of decls of formals.
3242 Return 1 if this can be done;
3243 otherwise return 0 and do not emit any code. */
3245 static int
3246 tail_recursion_args (tree actuals, tree formals)
3248 tree a = actuals, f = formals;
3249 int i;
3250 rtx *argvec;
3252 /* Check that number and types of actuals are compatible
3253 with the formals. This is not always true in valid C code.
3254 Also check that no formal needs to be addressable
3255 and that all formals are scalars. */
3257 /* Also count the args. */
3259 for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
3261 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (a)))
3262 != TYPE_MAIN_VARIANT (TREE_TYPE (f)))
3263 return 0;
3264 if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
3265 return 0;
3267 if (a != 0 || f != 0)
3268 return 0;
3270 /* Compute all the actuals. */
3272 argvec = alloca (i * sizeof (rtx));
3274 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3275 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
3277 /* Find which actual values refer to current values of previous formals.
3278 Copy each of them now, before any formal is changed. */
3280 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3282 int copy = 0;
3283 int j;
3284 for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
3285 if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
3287 copy = 1;
3288 break;
3290 if (copy)
3291 argvec[i] = copy_to_reg (argvec[i]);
3294 /* Store the values of the actuals into the formals. */
3296 for (f = formals, a = actuals, i = 0; f;
3297 f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
3299 if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
3300 emit_move_insn (DECL_RTL (f), argvec[i]);
3301 else
3303 rtx tmp = argvec[i];
3304 int unsignedp = TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a)));
3305 promote_mode(TREE_TYPE (TREE_VALUE (a)), GET_MODE (tmp),
3306 &unsignedp, 0);
3307 if (DECL_MODE (f) != GET_MODE (DECL_RTL (f)))
3309 tmp = gen_reg_rtx (DECL_MODE (f));
3310 convert_move (tmp, argvec[i], unsignedp);
3312 convert_move (DECL_RTL (f), tmp, unsignedp);
3316 free_temp_slots ();
3317 return 1;
3320 /* Generate the RTL code for entering a binding contour.
3321 The variables are declared one by one, by calls to `expand_decl'.
3323 FLAGS is a bitwise or of the following flags:
3325 1 - Nonzero if this construct should be visible to
3326 `exit_something'.
3328 2 - Nonzero if this contour does not require a
3329 NOTE_INSN_BLOCK_BEG note. Virtually all calls from
3330 language-independent code should set this flag because they
3331 will not create corresponding BLOCK nodes. (There should be
3332 a one-to-one correspondence between NOTE_INSN_BLOCK_BEG notes
3333 and BLOCKs.) If this flag is set, MARK_ENDS should be zero
3334 when expand_end_bindings is called.
3336 If we are creating a NOTE_INSN_BLOCK_BEG note, a BLOCK may
3337 optionally be supplied. If so, it becomes the NOTE_BLOCK for the
3338 note. */
3340 void
3341 expand_start_bindings_and_block (int flags, tree block)
3343 struct nesting *thisblock = ALLOC_NESTING ();
3344 rtx note;
3345 int exit_flag = ((flags & 1) != 0);
3346 int block_flag = ((flags & 2) == 0);
3348 /* If a BLOCK is supplied, then the caller should be requesting a
3349 NOTE_INSN_BLOCK_BEG note. */
3350 if (!block_flag && block)
3351 abort ();
3353 /* Create a note to mark the beginning of the block. */
3354 if (block_flag)
3356 note = emit_note (NOTE_INSN_BLOCK_BEG);
3357 NOTE_BLOCK (note) = block;
3359 else
3360 note = emit_note (NOTE_INSN_DELETED);
3362 /* Make an entry on block_stack for the block we are entering. */
3364 thisblock->desc = BLOCK_NESTING;
3365 thisblock->next = block_stack;
3366 thisblock->all = nesting_stack;
3367 thisblock->depth = ++nesting_depth;
3368 thisblock->data.block.stack_level = 0;
3369 thisblock->data.block.cleanups = 0;
3370 thisblock->data.block.exception_region = 0;
3371 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
3373 thisblock->data.block.conditional_code = 0;
3374 thisblock->data.block.last_unconditional_cleanup = note;
3375 /* When we insert instructions after the last unconditional cleanup,
3376 we don't adjust last_insn. That means that a later add_insn will
3377 clobber the instructions we've just added. The easiest way to
3378 fix this is to just insert another instruction here, so that the
3379 instructions inserted after the last unconditional cleanup are
3380 never the last instruction. */
3381 emit_note (NOTE_INSN_DELETED);
3383 if (block_stack
3384 && !(block_stack->data.block.cleanups == NULL_TREE
3385 && block_stack->data.block.outer_cleanups == NULL_TREE))
3386 thisblock->data.block.outer_cleanups
3387 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3388 block_stack->data.block.outer_cleanups);
3389 else
3390 thisblock->data.block.outer_cleanups = 0;
3391 thisblock->data.block.label_chain = 0;
3392 thisblock->data.block.innermost_stack_block = stack_block_stack;
3393 thisblock->data.block.first_insn = note;
3394 thisblock->data.block.block_start_count = ++current_block_start_count;
3395 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3396 block_stack = thisblock;
3397 nesting_stack = thisblock;
3399 /* Make a new level for allocating stack slots. */
3400 push_temp_slots ();
3403 /* Specify the scope of temporaries created by TARGET_EXPRs. Similar
3404 to CLEANUP_POINT_EXPR, but handles cases when a series of calls to
3405 expand_expr are made. After we end the region, we know that all
3406 space for all temporaries that were created by TARGET_EXPRs will be
3407 destroyed and their space freed for reuse. */
3409 void
3410 expand_start_target_temps (void)
3412 /* This is so that even if the result is preserved, the space
3413 allocated will be freed, as we know that it is no longer in use. */
3414 push_temp_slots ();
3416 /* Start a new binding layer that will keep track of all cleanup
3417 actions to be performed. */
3418 expand_start_bindings (2);
3420 target_temp_slot_level = temp_slot_level;
3423 void
3424 expand_end_target_temps (void)
3426 expand_end_bindings (NULL_TREE, 0, 0);
3428 /* This is so that even if the result is preserved, the space
3429 allocated will be freed, as we know that it is no longer in use. */
3430 pop_temp_slots ();
3433 /* Given a pointer to a BLOCK node return nonzero if (and only if) the node
3434 in question represents the outermost pair of curly braces (i.e. the "body
3435 block") of a function or method.
3437 For any BLOCK node representing a "body block" of a function or method, the
3438 BLOCK_SUPERCONTEXT of the node will point to another BLOCK node which
3439 represents the outermost (function) scope for the function or method (i.e.
3440 the one which includes the formal parameters). The BLOCK_SUPERCONTEXT of
3441 *that* node in turn will point to the relevant FUNCTION_DECL node. */
3444 is_body_block (tree stmt)
3446 if (lang_hooks.no_body_blocks)
3447 return 0;
3449 if (TREE_CODE (stmt) == BLOCK)
3451 tree parent = BLOCK_SUPERCONTEXT (stmt);
3453 if (parent && TREE_CODE (parent) == BLOCK)
3455 tree grandparent = BLOCK_SUPERCONTEXT (parent);
3457 if (grandparent && TREE_CODE (grandparent) == FUNCTION_DECL)
3458 return 1;
3462 return 0;
3465 /* True if we are currently emitting insns in an area of output code
3466 that is controlled by a conditional expression. This is used by
3467 the cleanup handling code to generate conditional cleanup actions. */
3470 conditional_context (void)
3472 return block_stack && block_stack->data.block.conditional_code;
3475 /* Return an opaque pointer to the current nesting level, so frontend code
3476 can check its own sanity. */
3478 struct nesting *
3479 current_nesting_level (void)
3481 return cfun ? block_stack : 0;
3484 /* Emit a handler label for a nonlocal goto handler.
3485 Also emit code to store the handler label in SLOT before BEFORE_INSN. */
3487 static rtx
3488 expand_nl_handler_label (rtx slot, rtx before_insn)
3490 rtx insns;
3491 rtx handler_label = gen_label_rtx ();
3493 /* Don't let cleanup_cfg delete the handler. */
3494 LABEL_PRESERVE_P (handler_label) = 1;
3496 start_sequence ();
3497 emit_move_insn (slot, gen_rtx_LABEL_REF (Pmode, handler_label));
3498 insns = get_insns ();
3499 end_sequence ();
3500 emit_insn_before (insns, before_insn);
3502 emit_label (handler_label);
3504 return handler_label;
3507 /* Emit code to restore vital registers at the beginning of a nonlocal goto
3508 handler. */
3509 static void
3510 expand_nl_goto_receiver (void)
3512 #ifdef HAVE_nonlocal_goto
3513 if (! HAVE_nonlocal_goto)
3514 #endif
3515 /* First adjust our frame pointer to its actual value. It was
3516 previously set to the start of the virtual area corresponding to
3517 the stacked variables when we branched here and now needs to be
3518 adjusted to the actual hardware fp value.
3520 Assignments are to virtual registers are converted by
3521 instantiate_virtual_regs into the corresponding assignment
3522 to the underlying register (fp in this case) that makes
3523 the original assignment true.
3524 So the following insn will actually be
3525 decrementing fp by STARTING_FRAME_OFFSET. */
3526 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
3528 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3529 if (fixed_regs[ARG_POINTER_REGNUM])
3531 #ifdef ELIMINABLE_REGS
3532 /* If the argument pointer can be eliminated in favor of the
3533 frame pointer, we don't need to restore it. We assume here
3534 that if such an elimination is present, it can always be used.
3535 This is the case on all known machines; if we don't make this
3536 assumption, we do unnecessary saving on many machines. */
3537 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
3538 size_t i;
3540 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
3541 if (elim_regs[i].from == ARG_POINTER_REGNUM
3542 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3543 break;
3545 if (i == ARRAY_SIZE (elim_regs))
3546 #endif
3548 /* Now restore our arg pointer from the address at which it
3549 was saved in our stack frame. */
3550 emit_move_insn (virtual_incoming_args_rtx,
3551 copy_to_reg (get_arg_pointer_save_area (cfun)));
3554 #endif
3556 #ifdef HAVE_nonlocal_goto_receiver
3557 if (HAVE_nonlocal_goto_receiver)
3558 emit_insn (gen_nonlocal_goto_receiver ());
3559 #endif
3562 /* Make handlers for nonlocal gotos taking place in the function calls in
3563 block THISBLOCK. */
3565 static void
3566 expand_nl_goto_receivers (struct nesting *thisblock)
3568 tree link;
3569 rtx afterward = gen_label_rtx ();
3570 rtx insns, slot;
3571 rtx label_list;
3572 int any_invalid;
3574 /* Record the handler address in the stack slot for that purpose,
3575 during this block, saving and restoring the outer value. */
3576 if (thisblock->next != 0)
3577 for (slot = nonlocal_goto_handler_slots; slot; slot = XEXP (slot, 1))
3579 rtx save_receiver = gen_reg_rtx (Pmode);
3580 emit_move_insn (XEXP (slot, 0), save_receiver);
3582 start_sequence ();
3583 emit_move_insn (save_receiver, XEXP (slot, 0));
3584 insns = get_insns ();
3585 end_sequence ();
3586 emit_insn_before (insns, thisblock->data.block.first_insn);
3589 /* Jump around the handlers; they run only when specially invoked. */
3590 emit_jump (afterward);
3592 /* Make a separate handler for each label. */
3593 link = nonlocal_labels;
3594 slot = nonlocal_goto_handler_slots;
3595 label_list = NULL_RTX;
3596 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3597 /* Skip any labels we shouldn't be able to jump to from here,
3598 we generate one special handler for all of them below which just calls
3599 abort. */
3600 if (! DECL_TOO_LATE (TREE_VALUE (link)))
3602 rtx lab;
3603 lab = expand_nl_handler_label (XEXP (slot, 0),
3604 thisblock->data.block.first_insn);
3605 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3607 expand_nl_goto_receiver ();
3609 /* Jump to the "real" nonlocal label. */
3610 expand_goto (TREE_VALUE (link));
3613 /* A second pass over all nonlocal labels; this time we handle those
3614 we should not be able to jump to at this point. */
3615 link = nonlocal_labels;
3616 slot = nonlocal_goto_handler_slots;
3617 any_invalid = 0;
3618 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3619 if (DECL_TOO_LATE (TREE_VALUE (link)))
3621 rtx lab;
3622 lab = expand_nl_handler_label (XEXP (slot, 0),
3623 thisblock->data.block.first_insn);
3624 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3625 any_invalid = 1;
3628 if (any_invalid)
3630 expand_nl_goto_receiver ();
3631 expand_builtin_trap ();
3634 nonlocal_goto_handler_labels = label_list;
3635 emit_label (afterward);
3638 /* Warn about any unused VARS (which may contain nodes other than
3639 VAR_DECLs, but such nodes are ignored). The nodes are connected
3640 via the TREE_CHAIN field. */
3642 void
3643 warn_about_unused_variables (tree vars)
3645 tree decl;
3647 if (warn_unused_variable)
3648 for (decl = vars; decl; decl = TREE_CHAIN (decl))
3649 if (TREE_CODE (decl) == VAR_DECL
3650 && ! TREE_USED (decl)
3651 && ! DECL_IN_SYSTEM_HEADER (decl)
3652 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
3653 warning ("%Hunused variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
3656 /* Generate RTL code to terminate a binding contour.
3658 VARS is the chain of VAR_DECL nodes for the variables bound in this
3659 contour. There may actually be other nodes in this chain, but any
3660 nodes other than VAR_DECLS are ignored.
3662 MARK_ENDS is nonzero if we should put a note at the beginning
3663 and end of this binding contour.
3665 DONT_JUMP_IN is positive if it is not valid to jump into this contour,
3666 zero if we can jump into this contour only if it does not have a saved
3667 stack level, and negative if we are not to check for invalid use of
3668 labels (because the front end does that). */
3670 void
3671 expand_end_bindings (tree vars, int mark_ends, int dont_jump_in)
3673 struct nesting *thisblock = block_stack;
3675 /* If any of the variables in this scope were not used, warn the
3676 user. */
3677 warn_about_unused_variables (vars);
3679 if (thisblock->exit_label)
3681 do_pending_stack_adjust ();
3682 emit_label (thisblock->exit_label);
3685 /* If necessary, make handlers for nonlocal gotos taking
3686 place in the function calls in this block. */
3687 if (function_call_count != 0 && nonlocal_labels
3688 /* Make handler for outermost block
3689 if there were any nonlocal gotos to this function. */
3690 && (thisblock->next == 0 ? current_function_has_nonlocal_label
3691 /* Make handler for inner block if it has something
3692 special to do when you jump out of it. */
3693 : (thisblock->data.block.cleanups != 0
3694 || thisblock->data.block.stack_level != 0)))
3695 expand_nl_goto_receivers (thisblock);
3697 /* Don't allow jumping into a block that has a stack level.
3698 Cleanups are allowed, though. */
3699 if (dont_jump_in > 0
3700 || (dont_jump_in == 0 && thisblock->data.block.stack_level != 0))
3702 struct label_chain *chain;
3704 /* Any labels in this block are no longer valid to go to.
3705 Mark them to cause an error message. */
3706 for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
3708 DECL_TOO_LATE (chain->label) = 1;
3709 /* If any goto without a fixup came to this label,
3710 that must be an error, because gotos without fixups
3711 come from outside all saved stack-levels. */
3712 if (TREE_ADDRESSABLE (chain->label))
3713 error ("%Hlabel '%D' used before containing binding contour",
3714 &DECL_SOURCE_LOCATION (chain->label), chain->label);
3718 /* Restore stack level in effect before the block
3719 (only if variable-size objects allocated). */
3720 /* Perform any cleanups associated with the block. */
3722 if (thisblock->data.block.stack_level != 0
3723 || thisblock->data.block.cleanups != 0)
3725 int reachable;
3726 rtx insn;
3728 /* Don't let cleanups affect ({...}) constructs. */
3729 int old_expr_stmts_for_value = expr_stmts_for_value;
3730 rtx old_last_expr_value = last_expr_value;
3731 tree old_last_expr_type = last_expr_type;
3732 expr_stmts_for_value = 0;
3734 /* Only clean up here if this point can actually be reached. */
3735 insn = get_last_insn ();
3736 if (GET_CODE (insn) == NOTE)
3737 insn = prev_nonnote_insn (insn);
3738 reachable = (! insn || GET_CODE (insn) != BARRIER);
3740 /* Do the cleanups. */
3741 expand_cleanups (thisblock->data.block.cleanups, 0, reachable);
3742 if (reachable)
3743 do_pending_stack_adjust ();
3745 expr_stmts_for_value = old_expr_stmts_for_value;
3746 last_expr_value = old_last_expr_value;
3747 last_expr_type = old_last_expr_type;
3749 /* Restore the stack level. */
3751 if (reachable && thisblock->data.block.stack_level != 0)
3753 emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3754 thisblock->data.block.stack_level, NULL_RTX);
3755 if (nonlocal_goto_handler_slots != 0)
3756 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3757 NULL_RTX);
3760 /* Any gotos out of this block must also do these things.
3761 Also report any gotos with fixups that came to labels in this
3762 level. */
3763 fixup_gotos (thisblock,
3764 thisblock->data.block.stack_level,
3765 thisblock->data.block.cleanups,
3766 thisblock->data.block.first_insn,
3767 dont_jump_in);
3770 /* Mark the beginning and end of the scope if requested.
3771 We do this now, after running cleanups on the variables
3772 just going out of scope, so they are in scope for their cleanups. */
3774 if (mark_ends)
3776 rtx note = emit_note (NOTE_INSN_BLOCK_END);
3777 NOTE_BLOCK (note) = NOTE_BLOCK (thisblock->data.block.first_insn);
3779 else
3780 /* Get rid of the beginning-mark if we don't make an end-mark. */
3781 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
3783 /* Restore the temporary level of TARGET_EXPRs. */
3784 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
3786 /* Restore block_stack level for containing block. */
3788 stack_block_stack = thisblock->data.block.innermost_stack_block;
3789 POPSTACK (block_stack);
3791 /* Pop the stack slot nesting and free any slots at this level. */
3792 pop_temp_slots ();
3795 /* Generate code to save the stack pointer at the start of the current block
3796 and set up to restore it on exit. */
3798 void
3799 save_stack_pointer (void)
3801 struct nesting *thisblock = block_stack;
3803 if (thisblock->data.block.stack_level == 0)
3805 emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3806 &thisblock->data.block.stack_level,
3807 thisblock->data.block.first_insn);
3808 stack_block_stack = thisblock;
3812 /* Generate RTL for the automatic variable declaration DECL.
3813 (Other kinds of declarations are simply ignored if seen here.) */
3815 void
3816 expand_decl (tree decl)
3818 tree type;
3820 type = TREE_TYPE (decl);
3822 /* For a CONST_DECL, set mode, alignment, and sizes from those of the
3823 type in case this node is used in a reference. */
3824 if (TREE_CODE (decl) == CONST_DECL)
3826 DECL_MODE (decl) = TYPE_MODE (type);
3827 DECL_ALIGN (decl) = TYPE_ALIGN (type);
3828 DECL_SIZE (decl) = TYPE_SIZE (type);
3829 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
3830 return;
3833 /* Otherwise, only automatic variables need any expansion done. Static and
3834 external variables, and external functions, will be handled by
3835 `assemble_variable' (called from finish_decl). TYPE_DECL requires
3836 nothing. PARM_DECLs are handled in `assign_parms'. */
3837 if (TREE_CODE (decl) != VAR_DECL)
3838 return;
3840 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3841 return;
3843 /* Create the RTL representation for the variable. */
3845 if (type == error_mark_node)
3846 SET_DECL_RTL (decl, gen_rtx_MEM (BLKmode, const0_rtx));
3848 else if (DECL_SIZE (decl) == 0)
3849 /* Variable with incomplete type. */
3851 rtx x;
3852 if (DECL_INITIAL (decl) == 0)
3853 /* Error message was already done; now avoid a crash. */
3854 x = gen_rtx_MEM (BLKmode, const0_rtx);
3855 else
3856 /* An initializer is going to decide the size of this array.
3857 Until we know the size, represent its address with a reg. */
3858 x = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3860 set_mem_attributes (x, decl, 1);
3861 SET_DECL_RTL (decl, x);
3863 else if (DECL_MODE (decl) != BLKmode
3864 /* If -ffloat-store, don't put explicit float vars
3865 into regs. */
3866 && !(flag_float_store
3867 && TREE_CODE (type) == REAL_TYPE)
3868 && ! TREE_THIS_VOLATILE (decl)
3869 && ! DECL_NONLOCAL (decl)
3870 && (DECL_REGISTER (decl) || DECL_ARTIFICIAL (decl) || optimize))
3872 /* Automatic variable that can go in a register. */
3873 int unsignedp = TREE_UNSIGNED (type);
3874 enum machine_mode reg_mode
3875 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
3877 SET_DECL_RTL (decl, gen_reg_rtx (reg_mode));
3879 if (!DECL_ARTIFICIAL (decl))
3880 mark_user_reg (DECL_RTL (decl));
3882 if (POINTER_TYPE_P (type))
3883 mark_reg_pointer (DECL_RTL (decl),
3884 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
3886 maybe_set_unchanging (DECL_RTL (decl), decl);
3888 /* If something wants our address, try to use ADDRESSOF. */
3889 if (TREE_ADDRESSABLE (decl))
3890 put_var_into_stack (decl, /*rescan=*/false);
3893 else if (TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
3894 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
3895 && 0 < compare_tree_int (DECL_SIZE_UNIT (decl),
3896 STACK_CHECK_MAX_VAR_SIZE)))
3898 /* Variable of fixed size that goes on the stack. */
3899 rtx oldaddr = 0;
3900 rtx addr;
3901 rtx x;
3903 /* If we previously made RTL for this decl, it must be an array
3904 whose size was determined by the initializer.
3905 The old address was a register; set that register now
3906 to the proper address. */
3907 if (DECL_RTL_SET_P (decl))
3909 if (GET_CODE (DECL_RTL (decl)) != MEM
3910 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
3911 abort ();
3912 oldaddr = XEXP (DECL_RTL (decl), 0);
3915 /* Set alignment we actually gave this decl. */
3916 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
3917 : GET_MODE_BITSIZE (DECL_MODE (decl)));
3918 DECL_USER_ALIGN (decl) = 0;
3920 x = assign_temp (decl, 1, 1, 1);
3921 set_mem_attributes (x, decl, 1);
3922 SET_DECL_RTL (decl, x);
3924 if (oldaddr)
3926 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
3927 if (addr != oldaddr)
3928 emit_move_insn (oldaddr, addr);
3931 else
3932 /* Dynamic-size object: must push space on the stack. */
3934 rtx address, size, x;
3936 /* Record the stack pointer on entry to block, if have
3937 not already done so. */
3938 do_pending_stack_adjust ();
3939 save_stack_pointer ();
3941 /* In function-at-a-time mode, variable_size doesn't expand this,
3942 so do it now. */
3943 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
3944 expand_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
3945 const0_rtx, VOIDmode, 0);
3947 /* Compute the variable's size, in bytes. */
3948 size = expand_expr (DECL_SIZE_UNIT (decl), NULL_RTX, VOIDmode, 0);
3949 free_temp_slots ();
3951 /* Allocate space on the stack for the variable. Note that
3952 DECL_ALIGN says how the variable is to be aligned and we
3953 cannot use it to conclude anything about the alignment of
3954 the size. */
3955 address = allocate_dynamic_stack_space (size, NULL_RTX,
3956 TYPE_ALIGN (TREE_TYPE (decl)));
3958 /* Reference the variable indirect through that rtx. */
3959 x = gen_rtx_MEM (DECL_MODE (decl), address);
3960 set_mem_attributes (x, decl, 1);
3961 SET_DECL_RTL (decl, x);
3964 /* Indicate the alignment we actually gave this variable. */
3965 #ifdef STACK_BOUNDARY
3966 DECL_ALIGN (decl) = STACK_BOUNDARY;
3967 #else
3968 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
3969 #endif
3970 DECL_USER_ALIGN (decl) = 0;
3974 /* Emit code to perform the initialization of a declaration DECL. */
3976 void
3977 expand_decl_init (tree decl)
3979 int was_used = TREE_USED (decl);
3981 /* If this is a CONST_DECL, we don't have to generate any code. Likewise
3982 for static decls. */
3983 if (TREE_CODE (decl) == CONST_DECL
3984 || TREE_STATIC (decl))
3985 return;
3987 /* Compute and store the initial value now. */
3989 push_temp_slots ();
3991 if (DECL_INITIAL (decl) == error_mark_node)
3993 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
3995 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
3996 || code == POINTER_TYPE || code == REFERENCE_TYPE)
3997 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
3999 emit_queue ();
4001 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
4003 emit_line_note (DECL_SOURCE_LOCATION (decl));
4004 expand_assignment (decl, DECL_INITIAL (decl), 0);
4005 emit_queue ();
4008 /* Don't let the initialization count as "using" the variable. */
4009 TREE_USED (decl) = was_used;
4011 /* Free any temporaries we made while initializing the decl. */
4012 preserve_temp_slots (NULL_RTX);
4013 free_temp_slots ();
4014 pop_temp_slots ();
4017 /* CLEANUP is an expression to be executed at exit from this binding contour;
4018 for example, in C++, it might call the destructor for this variable.
4020 We wrap CLEANUP in an UNSAVE_EXPR node, so that we can expand the
4021 CLEANUP multiple times, and have the correct semantics. This
4022 happens in exception handling, for gotos, returns, breaks that
4023 leave the current scope.
4025 If CLEANUP is nonzero and DECL is zero, we record a cleanup
4026 that is not associated with any particular variable. */
4029 expand_decl_cleanup (tree decl, tree cleanup)
4031 struct nesting *thisblock;
4033 /* Error if we are not in any block. */
4034 if (cfun == 0 || block_stack == 0)
4035 return 0;
4037 thisblock = block_stack;
4039 /* Record the cleanup if there is one. */
4041 if (cleanup != 0)
4043 tree t;
4044 rtx seq;
4045 tree *cleanups = &thisblock->data.block.cleanups;
4046 int cond_context = conditional_context ();
4048 if (cond_context)
4050 rtx flag = gen_reg_rtx (word_mode);
4051 rtx set_flag_0;
4052 tree cond;
4054 start_sequence ();
4055 emit_move_insn (flag, const0_rtx);
4056 set_flag_0 = get_insns ();
4057 end_sequence ();
4059 thisblock->data.block.last_unconditional_cleanup
4060 = emit_insn_after (set_flag_0,
4061 thisblock->data.block.last_unconditional_cleanup);
4063 emit_move_insn (flag, const1_rtx);
4065 cond = build_decl (VAR_DECL, NULL_TREE,
4066 (*lang_hooks.types.type_for_mode) (word_mode, 1));
4067 SET_DECL_RTL (cond, flag);
4069 /* Conditionalize the cleanup. */
4070 cleanup = build (COND_EXPR, void_type_node,
4071 (*lang_hooks.truthvalue_conversion) (cond),
4072 cleanup, integer_zero_node);
4073 cleanup = fold (cleanup);
4075 cleanups = &thisblock->data.block.cleanups;
4078 cleanup = unsave_expr (cleanup);
4080 t = *cleanups = tree_cons (decl, cleanup, *cleanups);
4082 if (! cond_context)
4083 /* If this block has a cleanup, it belongs in stack_block_stack. */
4084 stack_block_stack = thisblock;
4086 if (cond_context)
4088 start_sequence ();
4091 if (! using_eh_for_cleanups_p)
4092 TREE_ADDRESSABLE (t) = 1;
4093 else
4094 expand_eh_region_start ();
4096 if (cond_context)
4098 seq = get_insns ();
4099 end_sequence ();
4100 if (seq)
4101 thisblock->data.block.last_unconditional_cleanup
4102 = emit_insn_after (seq,
4103 thisblock->data.block.last_unconditional_cleanup);
4105 else
4107 thisblock->data.block.last_unconditional_cleanup
4108 = get_last_insn ();
4109 /* When we insert instructions after the last unconditional cleanup,
4110 we don't adjust last_insn. That means that a later add_insn will
4111 clobber the instructions we've just added. The easiest way to
4112 fix this is to just insert another instruction here, so that the
4113 instructions inserted after the last unconditional cleanup are
4114 never the last instruction. */
4115 emit_note (NOTE_INSN_DELETED);
4118 return 1;
4121 /* Like expand_decl_cleanup, but maybe only run the cleanup if an exception
4122 is thrown. */
4125 expand_decl_cleanup_eh (tree decl, tree cleanup, int eh_only)
4127 int ret = expand_decl_cleanup (decl, cleanup);
4128 if (cleanup && ret)
4130 tree node = block_stack->data.block.cleanups;
4131 CLEANUP_EH_ONLY (node) = eh_only;
4133 return ret;
4136 /* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
4137 DECL_ELTS is the list of elements that belong to DECL's type.
4138 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
4140 void
4141 expand_anon_union_decl (tree decl, tree cleanup, tree decl_elts)
4143 struct nesting *thisblock = cfun == 0 ? 0 : block_stack;
4144 rtx x;
4145 tree t;
4147 /* If any of the elements are addressable, so is the entire union. */
4148 for (t = decl_elts; t; t = TREE_CHAIN (t))
4149 if (TREE_ADDRESSABLE (TREE_VALUE (t)))
4151 TREE_ADDRESSABLE (decl) = 1;
4152 break;
4155 expand_decl (decl);
4156 expand_decl_cleanup (decl, cleanup);
4157 x = DECL_RTL (decl);
4159 /* Go through the elements, assigning RTL to each. */
4160 for (t = decl_elts; t; t = TREE_CHAIN (t))
4162 tree decl_elt = TREE_VALUE (t);
4163 tree cleanup_elt = TREE_PURPOSE (t);
4164 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
4166 /* If any of the elements are addressable, so is the entire
4167 union. */
4168 if (TREE_USED (decl_elt))
4169 TREE_USED (decl) = 1;
4171 /* Propagate the union's alignment to the elements. */
4172 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
4173 DECL_USER_ALIGN (decl_elt) = DECL_USER_ALIGN (decl);
4175 /* If the element has BLKmode and the union doesn't, the union is
4176 aligned such that the element doesn't need to have BLKmode, so
4177 change the element's mode to the appropriate one for its size. */
4178 if (mode == BLKmode && DECL_MODE (decl) != BLKmode)
4179 DECL_MODE (decl_elt) = mode
4180 = mode_for_size_tree (DECL_SIZE (decl_elt), MODE_INT, 1);
4182 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
4183 instead create a new MEM rtx with the proper mode. */
4184 if (GET_CODE (x) == MEM)
4186 if (mode == GET_MODE (x))
4187 SET_DECL_RTL (decl_elt, x);
4188 else
4189 SET_DECL_RTL (decl_elt, adjust_address_nv (x, mode, 0));
4191 else if (GET_CODE (x) == REG)
4193 if (mode == GET_MODE (x))
4194 SET_DECL_RTL (decl_elt, x);
4195 else
4196 SET_DECL_RTL (decl_elt, gen_lowpart_SUBREG (mode, x));
4198 else
4199 abort ();
4201 /* Record the cleanup if there is one. */
4203 if (cleanup != 0)
4204 thisblock->data.block.cleanups
4205 = tree_cons (decl_elt, cleanup_elt,
4206 thisblock->data.block.cleanups);
4210 /* Expand a list of cleanups LIST.
4211 Elements may be expressions or may be nested lists.
4213 If IN_FIXUP is nonzero, we are generating this cleanup for a fixup
4214 goto and handle protection regions specially in that case.
4216 If REACHABLE, we emit code, otherwise just inform the exception handling
4217 code about this finalization. */
4219 static void
4220 expand_cleanups (tree list, int in_fixup, int reachable)
4222 tree tail;
4223 for (tail = list; tail; tail = TREE_CHAIN (tail))
4224 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
4225 expand_cleanups (TREE_VALUE (tail), in_fixup, reachable);
4226 else
4228 if (! in_fixup && using_eh_for_cleanups_p)
4229 expand_eh_region_end_cleanup (TREE_VALUE (tail));
4231 if (reachable && !CLEANUP_EH_ONLY (tail))
4233 /* Cleanups may be run multiple times. For example,
4234 when exiting a binding contour, we expand the
4235 cleanups associated with that contour. When a goto
4236 within that binding contour has a target outside that
4237 contour, it will expand all cleanups from its scope to
4238 the target. Though the cleanups are expanded multiple
4239 times, the control paths are non-overlapping so the
4240 cleanups will not be executed twice. */
4242 /* We may need to protect from outer cleanups. */
4243 if (in_fixup && using_eh_for_cleanups_p)
4245 expand_eh_region_start ();
4247 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4249 expand_eh_region_end_fixup (TREE_VALUE (tail));
4251 else
4252 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4254 free_temp_slots ();
4259 /* Mark when the context we are emitting RTL for as a conditional
4260 context, so that any cleanup actions we register with
4261 expand_decl_init will be properly conditionalized when those
4262 cleanup actions are later performed. Must be called before any
4263 expression (tree) is expanded that is within a conditional context. */
4265 void
4266 start_cleanup_deferral (void)
4268 /* block_stack can be NULL if we are inside the parameter list. It is
4269 OK to do nothing, because cleanups aren't possible here. */
4270 if (block_stack)
4271 ++block_stack->data.block.conditional_code;
4274 /* Mark the end of a conditional region of code. Because cleanup
4275 deferrals may be nested, we may still be in a conditional region
4276 after we end the currently deferred cleanups, only after we end all
4277 deferred cleanups, are we back in unconditional code. */
4279 void
4280 end_cleanup_deferral (void)
4282 /* block_stack can be NULL if we are inside the parameter list. It is
4283 OK to do nothing, because cleanups aren't possible here. */
4284 if (block_stack)
4285 --block_stack->data.block.conditional_code;
4288 tree
4289 last_cleanup_this_contour (void)
4291 if (block_stack == 0)
4292 return 0;
4294 return block_stack->data.block.cleanups;
4297 /* Return 1 if there are any pending cleanups at this point.
4298 Check the current contour as well as contours that enclose
4299 the current contour. */
4302 any_pending_cleanups (void)
4304 struct nesting *block;
4306 if (cfun == NULL || cfun->stmt == NULL || block_stack == 0)
4307 return 0;
4309 if (block_stack->data.block.cleanups != NULL)
4310 return 1;
4312 if (block_stack->data.block.outer_cleanups == 0)
4313 return 0;
4315 for (block = block_stack->next; block; block = block->next)
4316 if (block->data.block.cleanups != 0)
4317 return 1;
4319 return 0;
4322 /* Enter a case (Pascal) or switch (C) statement.
4323 Push a block onto case_stack and nesting_stack
4324 to accumulate the case-labels that are seen
4325 and to record the labels generated for the statement.
4327 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
4328 Otherwise, this construct is transparent for `exit_something'.
4330 EXPR is the index-expression to be dispatched on.
4331 TYPE is its nominal type. We could simply convert EXPR to this type,
4332 but instead we take short cuts. */
4334 void
4335 expand_start_case (int exit_flag, tree expr, tree type,
4336 const char *printname)
4338 struct nesting *thiscase = ALLOC_NESTING ();
4340 /* Make an entry on case_stack for the case we are entering. */
4342 thiscase->desc = CASE_NESTING;
4343 thiscase->next = case_stack;
4344 thiscase->all = nesting_stack;
4345 thiscase->depth = ++nesting_depth;
4346 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
4347 thiscase->data.case_stmt.case_list = 0;
4348 thiscase->data.case_stmt.index_expr = expr;
4349 thiscase->data.case_stmt.nominal_type = type;
4350 thiscase->data.case_stmt.default_label = 0;
4351 thiscase->data.case_stmt.printname = printname;
4352 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
4353 case_stack = thiscase;
4354 nesting_stack = thiscase;
4356 do_pending_stack_adjust ();
4357 emit_queue ();
4359 /* Make sure case_stmt.start points to something that won't
4360 need any transformation before expand_end_case. */
4361 if (GET_CODE (get_last_insn ()) != NOTE)
4362 emit_note (NOTE_INSN_DELETED);
4364 thiscase->data.case_stmt.start = get_last_insn ();
4366 start_cleanup_deferral ();
4369 /* Start a "dummy case statement" within which case labels are invalid
4370 and are not connected to any larger real case statement.
4371 This can be used if you don't want to let a case statement jump
4372 into the middle of certain kinds of constructs. */
4374 void
4375 expand_start_case_dummy (void)
4377 struct nesting *thiscase = ALLOC_NESTING ();
4379 /* Make an entry on case_stack for the dummy. */
4381 thiscase->desc = CASE_NESTING;
4382 thiscase->next = case_stack;
4383 thiscase->all = nesting_stack;
4384 thiscase->depth = ++nesting_depth;
4385 thiscase->exit_label = 0;
4386 thiscase->data.case_stmt.case_list = 0;
4387 thiscase->data.case_stmt.start = 0;
4388 thiscase->data.case_stmt.nominal_type = 0;
4389 thiscase->data.case_stmt.default_label = 0;
4390 case_stack = thiscase;
4391 nesting_stack = thiscase;
4392 start_cleanup_deferral ();
4395 static void
4396 check_seenlabel (void)
4398 /* If this is the first label, warn if any insns have been emitted. */
4399 if (case_stack->data.case_stmt.line_number_status >= 0)
4401 rtx insn;
4403 restore_line_number_status
4404 (case_stack->data.case_stmt.line_number_status);
4405 case_stack->data.case_stmt.line_number_status = -1;
4407 for (insn = case_stack->data.case_stmt.start;
4408 insn;
4409 insn = NEXT_INSN (insn))
4411 if (GET_CODE (insn) == CODE_LABEL)
4412 break;
4413 if (GET_CODE (insn) != NOTE
4414 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4417 insn = PREV_INSN (insn);
4418 while (insn && (GET_CODE (insn) != NOTE || NOTE_LINE_NUMBER (insn) < 0));
4420 /* If insn is zero, then there must have been a syntax error. */
4421 if (insn)
4423 location_t locus;
4424 locus.file = NOTE_SOURCE_FILE (insn);
4425 locus.line = NOTE_LINE_NUMBER (insn);
4426 warning ("%Hunreachable code at beginning of %s", &locus,
4427 case_stack->data.case_stmt.printname);
4429 break;
4435 /* Accumulate one case or default label inside a case or switch statement.
4436 VALUE is the value of the case (a null pointer, for a default label).
4437 The function CONVERTER, when applied to arguments T and V,
4438 converts the value V to the type T.
4440 If not currently inside a case or switch statement, return 1 and do
4441 nothing. The caller will print a language-specific error message.
4442 If VALUE is a duplicate or overlaps, return 2 and do nothing
4443 except store the (first) duplicate node in *DUPLICATE.
4444 If VALUE is out of range, return 3 and do nothing.
4445 If we are jumping into the scope of a cleanup or var-sized array, return 5.
4446 Return 0 on success.
4448 Extended to handle range statements. */
4451 pushcase (tree value, tree (*converter) (tree, tree), tree label,
4452 tree *duplicate)
4454 tree index_type;
4455 tree nominal_type;
4457 /* Fail if not inside a real case statement. */
4458 if (! (case_stack && case_stack->data.case_stmt.start))
4459 return 1;
4461 if (stack_block_stack
4462 && stack_block_stack->depth > case_stack->depth)
4463 return 5;
4465 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4466 nominal_type = case_stack->data.case_stmt.nominal_type;
4468 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4469 if (index_type == error_mark_node)
4470 return 0;
4472 /* Convert VALUE to the type in which the comparisons are nominally done. */
4473 if (value != 0)
4474 value = (*converter) (nominal_type, value);
4476 check_seenlabel ();
4478 /* Fail if this value is out of range for the actual type of the index
4479 (which may be narrower than NOMINAL_TYPE). */
4480 if (value != 0
4481 && (TREE_CONSTANT_OVERFLOW (value)
4482 || ! int_fits_type_p (value, index_type)))
4483 return 3;
4485 return add_case_node (value, value, label, duplicate);
4488 /* Like pushcase but this case applies to all values between VALUE1 and
4489 VALUE2 (inclusive). If VALUE1 is NULL, the range starts at the lowest
4490 value of the index type and ends at VALUE2. If VALUE2 is NULL, the range
4491 starts at VALUE1 and ends at the highest value of the index type.
4492 If both are NULL, this case applies to all values.
4494 The return value is the same as that of pushcase but there is one
4495 additional error code: 4 means the specified range was empty. */
4498 pushcase_range (tree value1, tree value2, tree (*converter) (tree, tree),
4499 tree label, tree *duplicate)
4501 tree index_type;
4502 tree nominal_type;
4504 /* Fail if not inside a real case statement. */
4505 if (! (case_stack && case_stack->data.case_stmt.start))
4506 return 1;
4508 if (stack_block_stack
4509 && stack_block_stack->depth > case_stack->depth)
4510 return 5;
4512 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4513 nominal_type = case_stack->data.case_stmt.nominal_type;
4515 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4516 if (index_type == error_mark_node)
4517 return 0;
4519 check_seenlabel ();
4521 /* Convert VALUEs to type in which the comparisons are nominally done
4522 and replace any unspecified value with the corresponding bound. */
4523 if (value1 == 0)
4524 value1 = TYPE_MIN_VALUE (index_type);
4525 if (value2 == 0)
4526 value2 = TYPE_MAX_VALUE (index_type);
4528 /* Fail if the range is empty. Do this before any conversion since
4529 we want to allow out-of-range empty ranges. */
4530 if (value2 != 0 && tree_int_cst_lt (value2, value1))
4531 return 4;
4533 /* If the max was unbounded, use the max of the nominal_type we are
4534 converting to. Do this after the < check above to suppress false
4535 positives. */
4536 if (value2 == 0)
4537 value2 = TYPE_MAX_VALUE (nominal_type);
4539 value1 = (*converter) (nominal_type, value1);
4540 value2 = (*converter) (nominal_type, value2);
4542 /* Fail if these values are out of range. */
4543 if (TREE_CONSTANT_OVERFLOW (value1)
4544 || ! int_fits_type_p (value1, index_type))
4545 return 3;
4547 if (TREE_CONSTANT_OVERFLOW (value2)
4548 || ! int_fits_type_p (value2, index_type))
4549 return 3;
4551 return add_case_node (value1, value2, label, duplicate);
4554 /* Do the actual insertion of a case label for pushcase and pushcase_range
4555 into case_stack->data.case_stmt.case_list. Use an AVL tree to avoid
4556 slowdown for large switch statements. */
4559 add_case_node (tree low, tree high, tree label, tree *duplicate)
4561 struct case_node *p, **q, *r;
4563 /* If there's no HIGH value, then this is not a case range; it's
4564 just a simple case label. But that's just a degenerate case
4565 range. */
4566 if (!high)
4567 high = low;
4569 /* Handle default labels specially. */
4570 if (!high && !low)
4572 if (case_stack->data.case_stmt.default_label != 0)
4574 *duplicate = case_stack->data.case_stmt.default_label;
4575 return 2;
4577 case_stack->data.case_stmt.default_label = label;
4578 expand_label (label);
4579 return 0;
4582 q = &case_stack->data.case_stmt.case_list;
4583 p = *q;
4585 while ((r = *q))
4587 p = r;
4589 /* Keep going past elements distinctly greater than HIGH. */
4590 if (tree_int_cst_lt (high, p->low))
4591 q = &p->left;
4593 /* or distinctly less than LOW. */
4594 else if (tree_int_cst_lt (p->high, low))
4595 q = &p->right;
4597 else
4599 /* We have an overlap; this is an error. */
4600 *duplicate = p->code_label;
4601 return 2;
4605 /* Add this label to the chain, and succeed. */
4607 r = ggc_alloc (sizeof (struct case_node));
4608 r->low = low;
4610 /* If the bounds are equal, turn this into the one-value case. */
4611 if (tree_int_cst_equal (low, high))
4612 r->high = r->low;
4613 else
4614 r->high = high;
4616 r->code_label = label;
4617 expand_label (label);
4619 *q = r;
4620 r->parent = p;
4621 r->left = 0;
4622 r->right = 0;
4623 r->balance = 0;
4625 while (p)
4627 struct case_node *s;
4629 if (r == p->left)
4631 int b;
4633 if (! (b = p->balance))
4634 /* Growth propagation from left side. */
4635 p->balance = -1;
4636 else if (b < 0)
4638 if (r->balance < 0)
4640 /* R-Rotation */
4641 if ((p->left = s = r->right))
4642 s->parent = p;
4644 r->right = p;
4645 p->balance = 0;
4646 r->balance = 0;
4647 s = p->parent;
4648 p->parent = r;
4650 if ((r->parent = s))
4652 if (s->left == p)
4653 s->left = r;
4654 else
4655 s->right = r;
4657 else
4658 case_stack->data.case_stmt.case_list = r;
4660 else
4661 /* r->balance == +1 */
4663 /* LR-Rotation */
4665 int b2;
4666 struct case_node *t = r->right;
4668 if ((p->left = s = t->right))
4669 s->parent = p;
4671 t->right = p;
4672 if ((r->right = s = t->left))
4673 s->parent = r;
4675 t->left = r;
4676 b = t->balance;
4677 b2 = b < 0;
4678 p->balance = b2;
4679 b2 = -b2 - b;
4680 r->balance = b2;
4681 t->balance = 0;
4682 s = p->parent;
4683 p->parent = t;
4684 r->parent = t;
4686 if ((t->parent = s))
4688 if (s->left == p)
4689 s->left = t;
4690 else
4691 s->right = t;
4693 else
4694 case_stack->data.case_stmt.case_list = t;
4696 break;
4699 else
4701 /* p->balance == +1; growth of left side balances the node. */
4702 p->balance = 0;
4703 break;
4706 else
4707 /* r == p->right */
4709 int b;
4711 if (! (b = p->balance))
4712 /* Growth propagation from right side. */
4713 p->balance++;
4714 else if (b > 0)
4716 if (r->balance > 0)
4718 /* L-Rotation */
4720 if ((p->right = s = r->left))
4721 s->parent = p;
4723 r->left = p;
4724 p->balance = 0;
4725 r->balance = 0;
4726 s = p->parent;
4727 p->parent = r;
4728 if ((r->parent = s))
4730 if (s->left == p)
4731 s->left = r;
4732 else
4733 s->right = r;
4736 else
4737 case_stack->data.case_stmt.case_list = r;
4740 else
4741 /* r->balance == -1 */
4743 /* RL-Rotation */
4744 int b2;
4745 struct case_node *t = r->left;
4747 if ((p->right = s = t->left))
4748 s->parent = p;
4750 t->left = p;
4752 if ((r->left = s = t->right))
4753 s->parent = r;
4755 t->right = r;
4756 b = t->balance;
4757 b2 = b < 0;
4758 r->balance = b2;
4759 b2 = -b2 - b;
4760 p->balance = b2;
4761 t->balance = 0;
4762 s = p->parent;
4763 p->parent = t;
4764 r->parent = t;
4766 if ((t->parent = s))
4768 if (s->left == p)
4769 s->left = t;
4770 else
4771 s->right = t;
4774 else
4775 case_stack->data.case_stmt.case_list = t;
4777 break;
4779 else
4781 /* p->balance == -1; growth of right side balances the node. */
4782 p->balance = 0;
4783 break;
4787 r = p;
4788 p = p->parent;
4791 return 0;
4794 /* Returns the number of possible values of TYPE.
4795 Returns -1 if the number is unknown, variable, or if the number does not
4796 fit in a HOST_WIDE_INT.
4797 Sets *SPARSENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
4798 do not increase monotonically (there may be duplicates);
4799 to 1 if the values increase monotonically, but not always by 1;
4800 otherwise sets it to 0. */
4802 HOST_WIDE_INT
4803 all_cases_count (tree type, int *sparseness)
4805 tree t;
4806 HOST_WIDE_INT count, minval, lastval;
4808 *sparseness = 0;
4810 switch (TREE_CODE (type))
4812 case BOOLEAN_TYPE:
4813 count = 2;
4814 break;
4816 case CHAR_TYPE:
4817 count = 1 << BITS_PER_UNIT;
4818 break;
4820 default:
4821 case INTEGER_TYPE:
4822 if (TYPE_MAX_VALUE (type) != 0
4823 && 0 != (t = fold (build (MINUS_EXPR, type, TYPE_MAX_VALUE (type),
4824 TYPE_MIN_VALUE (type))))
4825 && 0 != (t = fold (build (PLUS_EXPR, type, t,
4826 convert (type, integer_zero_node))))
4827 && host_integerp (t, 1))
4828 count = tree_low_cst (t, 1);
4829 else
4830 return -1;
4831 break;
4833 case ENUMERAL_TYPE:
4834 /* Don't waste time with enumeral types with huge values. */
4835 if (! host_integerp (TYPE_MIN_VALUE (type), 0)
4836 || TYPE_MAX_VALUE (type) == 0
4837 || ! host_integerp (TYPE_MAX_VALUE (type), 0))
4838 return -1;
4840 lastval = minval = tree_low_cst (TYPE_MIN_VALUE (type), 0);
4841 count = 0;
4843 for (t = TYPE_VALUES (type); t != NULL_TREE; t = TREE_CHAIN (t))
4845 HOST_WIDE_INT thisval = tree_low_cst (TREE_VALUE (t), 0);
4847 if (*sparseness == 2 || thisval <= lastval)
4848 *sparseness = 2;
4849 else if (thisval != minval + count)
4850 *sparseness = 1;
4852 lastval = thisval;
4853 count++;
4857 return count;
4860 #define BITARRAY_TEST(ARRAY, INDEX) \
4861 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4862 & (1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR)))
4863 #define BITARRAY_SET(ARRAY, INDEX) \
4864 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4865 |= 1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR))
4867 /* Set the elements of the bitstring CASES_SEEN (which has length COUNT),
4868 with the case values we have seen, assuming the case expression
4869 has the given TYPE.
4870 SPARSENESS is as determined by all_cases_count.
4872 The time needed is proportional to COUNT, unless
4873 SPARSENESS is 2, in which case quadratic time is needed. */
4875 void
4876 mark_seen_cases (tree type, unsigned char *cases_seen, HOST_WIDE_INT count,
4877 int sparseness)
4879 tree next_node_to_try = NULL_TREE;
4880 HOST_WIDE_INT next_node_offset = 0;
4882 struct case_node *n, *root = case_stack->data.case_stmt.case_list;
4883 tree val = make_node (INTEGER_CST);
4885 TREE_TYPE (val) = type;
4886 if (! root)
4887 /* Do nothing. */
4889 else if (sparseness == 2)
4891 tree t;
4892 unsigned HOST_WIDE_INT xlo;
4894 /* This less efficient loop is only needed to handle
4895 duplicate case values (multiple enum constants
4896 with the same value). */
4897 TREE_TYPE (val) = TREE_TYPE (root->low);
4898 for (t = TYPE_VALUES (type), xlo = 0; t != NULL_TREE;
4899 t = TREE_CHAIN (t), xlo++)
4901 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (TREE_VALUE (t));
4902 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (TREE_VALUE (t));
4903 n = root;
4906 /* Keep going past elements distinctly greater than VAL. */
4907 if (tree_int_cst_lt (val, n->low))
4908 n = n->left;
4910 /* or distinctly less than VAL. */
4911 else if (tree_int_cst_lt (n->high, val))
4912 n = n->right;
4914 else
4916 /* We have found a matching range. */
4917 BITARRAY_SET (cases_seen, xlo);
4918 break;
4921 while (n);
4924 else
4926 if (root->left)
4927 case_stack->data.case_stmt.case_list = root = case_tree2list (root, 0);
4929 for (n = root; n; n = n->right)
4931 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (n->low);
4932 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (n->low);
4933 while (! tree_int_cst_lt (n->high, val))
4935 /* Calculate (into xlo) the "offset" of the integer (val).
4936 The element with lowest value has offset 0, the next smallest
4937 element has offset 1, etc. */
4939 unsigned HOST_WIDE_INT xlo;
4940 HOST_WIDE_INT xhi;
4941 tree t;
4943 if (sparseness && TYPE_VALUES (type) != NULL_TREE)
4945 /* The TYPE_VALUES will be in increasing order, so
4946 starting searching where we last ended. */
4947 t = next_node_to_try;
4948 xlo = next_node_offset;
4949 xhi = 0;
4950 for (;;)
4952 if (t == NULL_TREE)
4954 t = TYPE_VALUES (type);
4955 xlo = 0;
4957 if (tree_int_cst_equal (val, TREE_VALUE (t)))
4959 next_node_to_try = TREE_CHAIN (t);
4960 next_node_offset = xlo + 1;
4961 break;
4963 xlo++;
4964 t = TREE_CHAIN (t);
4965 if (t == next_node_to_try)
4967 xlo = -1;
4968 break;
4972 else
4974 t = TYPE_MIN_VALUE (type);
4975 if (t)
4976 neg_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t),
4977 &xlo, &xhi);
4978 else
4979 xlo = xhi = 0;
4980 add_double (xlo, xhi,
4981 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
4982 &xlo, &xhi);
4985 if (xhi == 0 && xlo < (unsigned HOST_WIDE_INT) count)
4986 BITARRAY_SET (cases_seen, xlo);
4988 add_double (TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
4989 1, 0,
4990 &TREE_INT_CST_LOW (val), &TREE_INT_CST_HIGH (val));
4996 /* Given a switch statement with an expression that is an enumeration
4997 type, warn if any of the enumeration type's literals are not
4998 covered by the case expressions of the switch. Also, warn if there
4999 are any extra switch cases that are *not* elements of the
5000 enumerated type.
5002 Historical note:
5004 At one stage this function would: ``If all enumeration literals
5005 were covered by the case expressions, turn one of the expressions
5006 into the default expression since it should not be possible to fall
5007 through such a switch.''
5009 That code has since been removed as: ``This optimization is
5010 disabled because it causes valid programs to fail. ANSI C does not
5011 guarantee that an expression with enum type will have a value that
5012 is the same as one of the enumeration literals.'' */
5014 void
5015 check_for_full_enumeration_handling (tree type)
5017 struct case_node *n;
5018 tree chain;
5020 /* True iff the selector type is a numbered set mode. */
5021 int sparseness = 0;
5023 /* The number of possible selector values. */
5024 HOST_WIDE_INT size;
5026 /* For each possible selector value. a one iff it has been matched
5027 by a case value alternative. */
5028 unsigned char *cases_seen;
5030 /* The allocated size of cases_seen, in chars. */
5031 HOST_WIDE_INT bytes_needed;
5033 size = all_cases_count (type, &sparseness);
5034 bytes_needed = (size + HOST_BITS_PER_CHAR) / HOST_BITS_PER_CHAR;
5036 if (size > 0 && size < 600000
5037 /* We deliberately use calloc here, not cmalloc, so that we can suppress
5038 this optimization if we don't have enough memory rather than
5039 aborting, as xmalloc would do. */
5040 && (cases_seen = really_call_calloc (bytes_needed, 1)) != NULL)
5042 HOST_WIDE_INT i;
5043 tree v = TYPE_VALUES (type);
5045 /* The time complexity of this code is normally O(N), where
5046 N being the number of members in the enumerated type.
5047 However, if type is an ENUMERAL_TYPE whose values do not
5048 increase monotonically, O(N*log(N)) time may be needed. */
5050 mark_seen_cases (type, cases_seen, size, sparseness);
5052 for (i = 0; v != NULL_TREE && i < size; i++, v = TREE_CHAIN (v))
5053 if (BITARRAY_TEST (cases_seen, i) == 0)
5054 warning ("enumeration value `%s' not handled in switch",
5055 IDENTIFIER_POINTER (TREE_PURPOSE (v)));
5057 free (cases_seen);
5060 /* Now we go the other way around; we warn if there are case
5061 expressions that don't correspond to enumerators. This can
5062 occur since C and C++ don't enforce type-checking of
5063 assignments to enumeration variables. */
5065 if (case_stack->data.case_stmt.case_list
5066 && case_stack->data.case_stmt.case_list->left)
5067 case_stack->data.case_stmt.case_list
5068 = case_tree2list (case_stack->data.case_stmt.case_list, 0);
5069 for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
5071 for (chain = TYPE_VALUES (type);
5072 chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
5073 chain = TREE_CHAIN (chain))
5076 if (!chain)
5078 if (TYPE_NAME (type) == 0)
5079 warning ("case value `%ld' not in enumerated type",
5080 (long) TREE_INT_CST_LOW (n->low));
5081 else
5082 warning ("case value `%ld' not in enumerated type `%s'",
5083 (long) TREE_INT_CST_LOW (n->low),
5084 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5085 == IDENTIFIER_NODE)
5086 ? TYPE_NAME (type)
5087 : DECL_NAME (TYPE_NAME (type))));
5089 if (!tree_int_cst_equal (n->low, n->high))
5091 for (chain = TYPE_VALUES (type);
5092 chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
5093 chain = TREE_CHAIN (chain))
5096 if (!chain)
5098 if (TYPE_NAME (type) == 0)
5099 warning ("case value `%ld' not in enumerated type",
5100 (long) TREE_INT_CST_LOW (n->high));
5101 else
5102 warning ("case value `%ld' not in enumerated type `%s'",
5103 (long) TREE_INT_CST_LOW (n->high),
5104 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5105 == IDENTIFIER_NODE)
5106 ? TYPE_NAME (type)
5107 : DECL_NAME (TYPE_NAME (type))));
5114 /* Maximum number of case bit tests. */
5115 #define MAX_CASE_BIT_TESTS 3
5117 /* By default, enable case bit tests on targets with ashlsi3. */
5118 #ifndef CASE_USE_BIT_TESTS
5119 #define CASE_USE_BIT_TESTS (ashl_optab->handlers[word_mode].insn_code \
5120 != CODE_FOR_nothing)
5121 #endif
5124 /* A case_bit_test represents a set of case nodes that may be
5125 selected from using a bit-wise comparison. HI and LO hold
5126 the integer to be tested against, LABEL contains the label
5127 to jump to upon success and BITS counts the number of case
5128 nodes handled by this test, typically the number of bits
5129 set in HI:LO. */
5131 struct case_bit_test
5133 HOST_WIDE_INT hi;
5134 HOST_WIDE_INT lo;
5135 rtx label;
5136 int bits;
5139 /* Determine whether "1 << x" is relatively cheap in word_mode. */
5141 static
5142 bool lshift_cheap_p (void)
5144 static bool init = false;
5145 static bool cheap = true;
5147 if (!init)
5149 rtx reg = gen_rtx_REG (word_mode, 10000);
5150 int cost = rtx_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg), SET);
5151 cheap = cost < COSTS_N_INSNS (3);
5152 init = true;
5155 return cheap;
5158 /* Comparison function for qsort to order bit tests by decreasing
5159 number of case nodes, i.e. the node with the most cases gets
5160 tested first. */
5162 static
5163 int case_bit_test_cmp (const void *p1, const void *p2)
5165 const struct case_bit_test *d1 = p1;
5166 const struct case_bit_test *d2 = p2;
5168 return d2->bits - d1->bits;
5171 /* Expand a switch statement by a short sequence of bit-wise
5172 comparisons. "switch(x)" is effectively converted into
5173 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
5174 integer constants.
5176 INDEX_EXPR is the value being switched on, which is of
5177 type INDEX_TYPE. MINVAL is the lowest case value of in
5178 the case nodes, of INDEX_TYPE type, and RANGE is highest
5179 value minus MINVAL, also of type INDEX_TYPE. NODES is
5180 the set of case nodes, and DEFAULT_LABEL is the label to
5181 branch to should none of the cases match.
5183 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
5184 node targets. */
5186 static void
5187 emit_case_bit_tests (tree index_type, tree index_expr, tree minval,
5188 tree range, case_node_ptr nodes, rtx default_label)
5190 struct case_bit_test test[MAX_CASE_BIT_TESTS];
5191 enum machine_mode mode;
5192 rtx expr, index, label;
5193 unsigned int i,j,lo,hi;
5194 struct case_node *n;
5195 unsigned int count;
5197 count = 0;
5198 for (n = nodes; n; n = n->right)
5200 label = label_rtx (n->code_label);
5201 for (i = 0; i < count; i++)
5202 if (same_case_target_p (label, test[i].label))
5203 break;
5205 if (i == count)
5207 if (count >= MAX_CASE_BIT_TESTS)
5208 abort ();
5209 test[i].hi = 0;
5210 test[i].lo = 0;
5211 test[i].label = label;
5212 test[i].bits = 1;
5213 count++;
5215 else
5216 test[i].bits++;
5218 lo = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5219 n->low, minval)), 1);
5220 hi = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5221 n->high, minval)), 1);
5222 for (j = lo; j <= hi; j++)
5223 if (j >= HOST_BITS_PER_WIDE_INT)
5224 test[i].hi |= (HOST_WIDE_INT) 1 << (j - HOST_BITS_PER_INT);
5225 else
5226 test[i].lo |= (HOST_WIDE_INT) 1 << j;
5229 qsort (test, count, sizeof(*test), case_bit_test_cmp);
5231 index_expr = fold (build (MINUS_EXPR, index_type,
5232 convert (index_type, index_expr),
5233 convert (index_type, minval)));
5234 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5235 emit_queue ();
5236 index = protect_from_queue (index, 0);
5237 do_pending_stack_adjust ();
5239 mode = TYPE_MODE (index_type);
5240 expr = expand_expr (range, NULL_RTX, VOIDmode, 0);
5241 emit_cmp_and_jump_insns (index, expr, GTU, NULL_RTX, mode, 1,
5242 default_label);
5244 index = convert_to_mode (word_mode, index, 0);
5245 index = expand_binop (word_mode, ashl_optab, const1_rtx,
5246 index, NULL_RTX, 1, OPTAB_WIDEN);
5248 for (i = 0; i < count; i++)
5250 expr = immed_double_const (test[i].lo, test[i].hi, word_mode);
5251 expr = expand_binop (word_mode, and_optab, index, expr,
5252 NULL_RTX, 1, OPTAB_WIDEN);
5253 emit_cmp_and_jump_insns (expr, const0_rtx, NE, NULL_RTX,
5254 word_mode, 1, test[i].label);
5257 emit_jump (default_label);
5260 /* Terminate a case (Pascal) or switch (C) statement
5261 in which ORIG_INDEX is the expression to be tested.
5262 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
5263 type as given in the source before any compiler conversions.
5264 Generate the code to test it and jump to the right place. */
5266 void
5267 expand_end_case_type (tree orig_index, tree orig_type)
5269 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
5270 rtx default_label = 0;
5271 struct case_node *n, *m;
5272 unsigned int count, uniq;
5273 rtx index;
5274 rtx table_label;
5275 int ncases;
5276 rtx *labelvec;
5277 int i;
5278 rtx before_case, end, lab;
5279 struct nesting *thiscase = case_stack;
5280 tree index_expr, index_type;
5281 bool exit_done = false;
5282 int unsignedp;
5284 /* Don't crash due to previous errors. */
5285 if (thiscase == NULL)
5286 return;
5288 index_expr = thiscase->data.case_stmt.index_expr;
5289 index_type = TREE_TYPE (index_expr);
5290 unsignedp = TREE_UNSIGNED (index_type);
5291 if (orig_type == NULL)
5292 orig_type = TREE_TYPE (orig_index);
5294 do_pending_stack_adjust ();
5296 /* This might get a spurious warning in the presence of a syntax error;
5297 it could be fixed by moving the call to check_seenlabel after the
5298 check for error_mark_node, and copying the code of check_seenlabel that
5299 deals with case_stack->data.case_stmt.line_number_status /
5300 restore_line_number_status in front of the call to end_cleanup_deferral;
5301 However, this might miss some useful warnings in the presence of
5302 non-syntax errors. */
5303 check_seenlabel ();
5305 /* An ERROR_MARK occurs for various reasons including invalid data type. */
5306 if (index_type != error_mark_node)
5308 /* If the switch expression was an enumerated type, check that
5309 exactly all enumeration literals are covered by the cases.
5310 The check is made when -Wswitch was specified and there is no
5311 default case, or when -Wswitch-enum was specified. */
5312 if (((warn_switch && !thiscase->data.case_stmt.default_label)
5313 || warn_switch_enum)
5314 && TREE_CODE (orig_type) == ENUMERAL_TYPE
5315 && TREE_CODE (index_expr) != INTEGER_CST)
5316 check_for_full_enumeration_handling (orig_type);
5318 if (warn_switch_default && !thiscase->data.case_stmt.default_label)
5319 warning ("switch missing default case");
5321 /* If we don't have a default-label, create one here,
5322 after the body of the switch. */
5323 if (thiscase->data.case_stmt.default_label == 0)
5325 thiscase->data.case_stmt.default_label
5326 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5327 /* Share the exit label if possible. */
5328 if (thiscase->exit_label)
5330 SET_DECL_RTL (thiscase->data.case_stmt.default_label,
5331 thiscase->exit_label);
5332 exit_done = true;
5334 expand_label (thiscase->data.case_stmt.default_label);
5336 default_label = label_rtx (thiscase->data.case_stmt.default_label);
5338 before_case = get_last_insn ();
5340 if (thiscase->data.case_stmt.case_list
5341 && thiscase->data.case_stmt.case_list->left)
5342 thiscase->data.case_stmt.case_list
5343 = case_tree2list (thiscase->data.case_stmt.case_list, 0);
5345 /* Simplify the case-list before we count it. */
5346 group_case_nodes (thiscase->data.case_stmt.case_list);
5347 strip_default_case_nodes (&thiscase->data.case_stmt.case_list,
5348 default_label);
5350 /* Get upper and lower bounds of case values.
5351 Also convert all the case values to the index expr's data type. */
5353 uniq = 0;
5354 count = 0;
5355 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5357 /* Check low and high label values are integers. */
5358 if (TREE_CODE (n->low) != INTEGER_CST)
5359 abort ();
5360 if (TREE_CODE (n->high) != INTEGER_CST)
5361 abort ();
5363 n->low = convert (index_type, n->low);
5364 n->high = convert (index_type, n->high);
5366 /* Count the elements and track the largest and smallest
5367 of them (treating them as signed even if they are not). */
5368 if (count++ == 0)
5370 minval = n->low;
5371 maxval = n->high;
5373 else
5375 if (INT_CST_LT (n->low, minval))
5376 minval = n->low;
5377 if (INT_CST_LT (maxval, n->high))
5378 maxval = n->high;
5380 /* A range counts double, since it requires two compares. */
5381 if (! tree_int_cst_equal (n->low, n->high))
5382 count++;
5384 /* Count the number of unique case node targets. */
5385 uniq++;
5386 lab = label_rtx (n->code_label);
5387 for (m = thiscase->data.case_stmt.case_list; m != n; m = m->right)
5388 if (same_case_target_p (label_rtx (m->code_label), lab))
5390 uniq--;
5391 break;
5395 /* Compute span of values. */
5396 if (count != 0)
5397 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
5399 end_cleanup_deferral ();
5401 if (count == 0)
5403 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
5404 emit_queue ();
5405 emit_jump (default_label);
5408 /* Try implementing this switch statement by a short sequence of
5409 bit-wise comparisons. However, we let the binary-tree case
5410 below handle constant index expressions. */
5411 else if (CASE_USE_BIT_TESTS
5412 && ! TREE_CONSTANT (index_expr)
5413 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
5414 && compare_tree_int (range, 0) > 0
5415 && lshift_cheap_p ()
5416 && ((uniq == 1 && count >= 3)
5417 || (uniq == 2 && count >= 5)
5418 || (uniq == 3 && count >= 6)))
5420 /* Optimize the case where all the case values fit in a
5421 word without having to subtract MINVAL. In this case,
5422 we can optimize away the subtraction. */
5423 if (compare_tree_int (minval, 0) > 0
5424 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
5426 minval = integer_zero_node;
5427 range = maxval;
5429 emit_case_bit_tests (index_type, index_expr, minval, range,
5430 thiscase->data.case_stmt.case_list,
5431 default_label);
5434 /* If range of values is much bigger than number of values,
5435 make a sequence of conditional branches instead of a dispatch.
5436 If the switch-index is a constant, do it this way
5437 because we can optimize it. */
5439 else if (count < case_values_threshold ()
5440 || compare_tree_int (range, 10 * count) > 0
5441 /* RANGE may be signed, and really large ranges will show up
5442 as negative numbers. */
5443 || compare_tree_int (range, 0) < 0
5444 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
5445 || flag_pic
5446 #endif
5447 || TREE_CONSTANT (index_expr))
5449 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5451 /* If the index is a short or char that we do not have
5452 an insn to handle comparisons directly, convert it to
5453 a full integer now, rather than letting each comparison
5454 generate the conversion. */
5456 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
5457 && ! have_insn_for (COMPARE, GET_MODE (index)))
5459 enum machine_mode wider_mode;
5460 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
5461 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
5462 if (have_insn_for (COMPARE, wider_mode))
5464 index = convert_to_mode (wider_mode, index, unsignedp);
5465 break;
5469 emit_queue ();
5470 do_pending_stack_adjust ();
5472 index = protect_from_queue (index, 0);
5473 if (GET_CODE (index) == MEM)
5474 index = copy_to_reg (index);
5475 if (GET_CODE (index) == CONST_INT
5476 || TREE_CODE (index_expr) == INTEGER_CST)
5478 /* Make a tree node with the proper constant value
5479 if we don't already have one. */
5480 if (TREE_CODE (index_expr) != INTEGER_CST)
5482 index_expr
5483 = build_int_2 (INTVAL (index),
5484 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
5485 index_expr = convert (index_type, index_expr);
5488 /* For constant index expressions we need only
5489 issue an unconditional branch to the appropriate
5490 target code. The job of removing any unreachable
5491 code is left to the optimization phase if the
5492 "-O" option is specified. */
5493 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5494 if (! tree_int_cst_lt (index_expr, n->low)
5495 && ! tree_int_cst_lt (n->high, index_expr))
5496 break;
5498 if (n)
5499 emit_jump (label_rtx (n->code_label));
5500 else
5501 emit_jump (default_label);
5503 else
5505 /* If the index expression is not constant we generate
5506 a binary decision tree to select the appropriate
5507 target code. This is done as follows:
5509 The list of cases is rearranged into a binary tree,
5510 nearly optimal assuming equal probability for each case.
5512 The tree is transformed into RTL, eliminating
5513 redundant test conditions at the same time.
5515 If program flow could reach the end of the
5516 decision tree an unconditional jump to the
5517 default code is emitted. */
5519 use_cost_table
5520 = (TREE_CODE (orig_type) != ENUMERAL_TYPE
5521 && estimate_case_costs (thiscase->data.case_stmt.case_list));
5522 balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
5523 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
5524 default_label, index_type);
5525 emit_jump_if_reachable (default_label);
5528 else
5530 table_label = gen_label_rtx ();
5531 if (! try_casesi (index_type, index_expr, minval, range,
5532 table_label, default_label))
5534 index_type = thiscase->data.case_stmt.nominal_type;
5536 /* Index jumptables from zero for suitable values of
5537 minval to avoid a subtraction. */
5538 if (! optimize_size
5539 && compare_tree_int (minval, 0) > 0
5540 && compare_tree_int (minval, 3) < 0)
5542 minval = integer_zero_node;
5543 range = maxval;
5546 if (! try_tablejump (index_type, index_expr, minval, range,
5547 table_label, default_label))
5548 abort ();
5551 /* Get table of labels to jump to, in order of case index. */
5553 ncases = tree_low_cst (range, 0) + 1;
5554 labelvec = alloca (ncases * sizeof (rtx));
5555 memset (labelvec, 0, ncases * sizeof (rtx));
5557 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5559 /* Compute the low and high bounds relative to the minimum
5560 value since that should fit in a HOST_WIDE_INT while the
5561 actual values may not. */
5562 HOST_WIDE_INT i_low
5563 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5564 n->low, minval)), 1);
5565 HOST_WIDE_INT i_high
5566 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5567 n->high, minval)), 1);
5568 HOST_WIDE_INT i;
5570 for (i = i_low; i <= i_high; i ++)
5571 labelvec[i]
5572 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
5575 /* Fill in the gaps with the default. */
5576 for (i = 0; i < ncases; i++)
5577 if (labelvec[i] == 0)
5578 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
5580 /* Output the table. */
5581 emit_label (table_label);
5583 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
5584 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
5585 gen_rtx_LABEL_REF (Pmode, table_label),
5586 gen_rtvec_v (ncases, labelvec),
5587 const0_rtx, const0_rtx));
5588 else
5589 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
5590 gen_rtvec_v (ncases, labelvec)));
5592 /* If the case insn drops through the table,
5593 after the table we must jump to the default-label.
5594 Otherwise record no drop-through after the table. */
5595 #ifdef CASE_DROPS_THROUGH
5596 emit_jump (default_label);
5597 #else
5598 emit_barrier ();
5599 #endif
5602 before_case = NEXT_INSN (before_case);
5603 end = get_last_insn ();
5604 if (squeeze_notes (&before_case, &end))
5605 abort ();
5606 reorder_insns (before_case, end,
5607 thiscase->data.case_stmt.start);
5609 else
5610 end_cleanup_deferral ();
5612 if (thiscase->exit_label && !exit_done)
5613 emit_label (thiscase->exit_label);
5615 POPSTACK (case_stack);
5617 free_temp_slots ();
5620 /* Convert the tree NODE into a list linked by the right field, with the left
5621 field zeroed. RIGHT is used for recursion; it is a list to be placed
5622 rightmost in the resulting list. */
5624 static struct case_node *
5625 case_tree2list (struct case_node *node, struct case_node *right)
5627 struct case_node *left;
5629 if (node->right)
5630 right = case_tree2list (node->right, right);
5632 node->right = right;
5633 if ((left = node->left))
5635 node->left = 0;
5636 return case_tree2list (left, node);
5639 return node;
5642 /* Generate code to jump to LABEL if OP1 and OP2 are equal. */
5644 static void
5645 do_jump_if_equal (rtx op1, rtx op2, rtx label, int unsignedp)
5647 if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
5649 if (op1 == op2)
5650 emit_jump (label);
5652 else
5653 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX,
5654 (GET_MODE (op1) == VOIDmode
5655 ? GET_MODE (op2) : GET_MODE (op1)),
5656 unsignedp, label);
5659 /* Not all case values are encountered equally. This function
5660 uses a heuristic to weight case labels, in cases where that
5661 looks like a reasonable thing to do.
5663 Right now, all we try to guess is text, and we establish the
5664 following weights:
5666 chars above space: 16
5667 digits: 16
5668 default: 12
5669 space, punct: 8
5670 tab: 4
5671 newline: 2
5672 other "\" chars: 1
5673 remaining chars: 0
5675 If we find any cases in the switch that are not either -1 or in the range
5676 of valid ASCII characters, or are control characters other than those
5677 commonly used with "\", don't treat this switch scanning text.
5679 Return 1 if these nodes are suitable for cost estimation, otherwise
5680 return 0. */
5682 static int
5683 estimate_case_costs (case_node_ptr node)
5685 tree min_ascii = integer_minus_one_node;
5686 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5687 case_node_ptr n;
5688 int i;
5690 /* If we haven't already made the cost table, make it now. Note that the
5691 lower bound of the table is -1, not zero. */
5693 if (! cost_table_initialized)
5695 cost_table_initialized = 1;
5697 for (i = 0; i < 128; i++)
5699 if (ISALNUM (i))
5700 COST_TABLE (i) = 16;
5701 else if (ISPUNCT (i))
5702 COST_TABLE (i) = 8;
5703 else if (ISCNTRL (i))
5704 COST_TABLE (i) = -1;
5707 COST_TABLE (' ') = 8;
5708 COST_TABLE ('\t') = 4;
5709 COST_TABLE ('\0') = 4;
5710 COST_TABLE ('\n') = 2;
5711 COST_TABLE ('\f') = 1;
5712 COST_TABLE ('\v') = 1;
5713 COST_TABLE ('\b') = 1;
5716 /* See if all the case expressions look like text. It is text if the
5717 constant is >= -1 and the highest constant is <= 127. Do all comparisons
5718 as signed arithmetic since we don't want to ever access cost_table with a
5719 value less than -1. Also check that none of the constants in a range
5720 are strange control characters. */
5722 for (n = node; n; n = n->right)
5724 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5725 return 0;
5727 for (i = (HOST_WIDE_INT) TREE_INT_CST_LOW (n->low);
5728 i <= (HOST_WIDE_INT) TREE_INT_CST_LOW (n->high); i++)
5729 if (COST_TABLE (i) < 0)
5730 return 0;
5733 /* All interesting values are within the range of interesting
5734 ASCII characters. */
5735 return 1;
5738 /* Determine whether two case labels branch to the same target. */
5740 static bool
5741 same_case_target_p (rtx l1, rtx l2)
5743 rtx i1, i2;
5745 if (l1 == l2)
5746 return true;
5748 i1 = next_real_insn (l1);
5749 i2 = next_real_insn (l2);
5750 if (i1 == i2)
5751 return true;
5753 if (i1 && simplejump_p (i1))
5755 l1 = XEXP (SET_SRC (PATTERN (i1)), 0);
5758 if (i2 && simplejump_p (i2))
5760 l2 = XEXP (SET_SRC (PATTERN (i2)), 0);
5762 return l1 == l2;
5765 /* Delete nodes that branch to the default label from a list of
5766 case nodes. Eg. case 5: default: becomes just default: */
5768 static void
5769 strip_default_case_nodes (case_node_ptr *prev, rtx deflab)
5771 case_node_ptr ptr;
5773 while (*prev)
5775 ptr = *prev;
5776 if (same_case_target_p (label_rtx (ptr->code_label), deflab))
5777 *prev = ptr->right;
5778 else
5779 prev = &ptr->right;
5783 /* Scan an ordered list of case nodes
5784 combining those with consecutive values or ranges.
5786 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
5788 static void
5789 group_case_nodes (case_node_ptr head)
5791 case_node_ptr node = head;
5793 while (node)
5795 rtx lab = label_rtx (node->code_label);
5796 case_node_ptr np = node;
5798 /* Try to group the successors of NODE with NODE. */
5799 while (((np = np->right) != 0)
5800 /* Do they jump to the same place? */
5801 && same_case_target_p (label_rtx (np->code_label), lab)
5802 /* Are their ranges consecutive? */
5803 && tree_int_cst_equal (np->low,
5804 fold (build (PLUS_EXPR,
5805 TREE_TYPE (node->high),
5806 node->high,
5807 integer_one_node)))
5808 /* An overflow is not consecutive. */
5809 && tree_int_cst_lt (node->high,
5810 fold (build (PLUS_EXPR,
5811 TREE_TYPE (node->high),
5812 node->high,
5813 integer_one_node))))
5815 node->high = np->high;
5817 /* NP is the first node after NODE which can't be grouped with it.
5818 Delete the nodes in between, and move on to that node. */
5819 node->right = np;
5820 node = np;
5824 /* Take an ordered list of case nodes
5825 and transform them into a near optimal binary tree,
5826 on the assumption that any target code selection value is as
5827 likely as any other.
5829 The transformation is performed by splitting the ordered
5830 list into two equal sections plus a pivot. The parts are
5831 then attached to the pivot as left and right branches. Each
5832 branch is then transformed recursively. */
5834 static void
5835 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
5837 case_node_ptr np;
5839 np = *head;
5840 if (np)
5842 int cost = 0;
5843 int i = 0;
5844 int ranges = 0;
5845 case_node_ptr *npp;
5846 case_node_ptr left;
5848 /* Count the number of entries on branch. Also count the ranges. */
5850 while (np)
5852 if (!tree_int_cst_equal (np->low, np->high))
5854 ranges++;
5855 if (use_cost_table)
5856 cost += COST_TABLE (TREE_INT_CST_LOW (np->high));
5859 if (use_cost_table)
5860 cost += COST_TABLE (TREE_INT_CST_LOW (np->low));
5862 i++;
5863 np = np->right;
5866 if (i > 2)
5868 /* Split this list if it is long enough for that to help. */
5869 npp = head;
5870 left = *npp;
5871 if (use_cost_table)
5873 /* Find the place in the list that bisects the list's total cost,
5874 Here I gets half the total cost. */
5875 int n_moved = 0;
5876 i = (cost + 1) / 2;
5877 while (1)
5879 /* Skip nodes while their cost does not reach that amount. */
5880 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5881 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->high));
5882 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->low));
5883 if (i <= 0)
5884 break;
5885 npp = &(*npp)->right;
5886 n_moved += 1;
5888 if (n_moved == 0)
5890 /* Leave this branch lopsided, but optimize left-hand
5891 side and fill in `parent' fields for right-hand side. */
5892 np = *head;
5893 np->parent = parent;
5894 balance_case_nodes (&np->left, np);
5895 for (; np->right; np = np->right)
5896 np->right->parent = np;
5897 return;
5900 /* If there are just three nodes, split at the middle one. */
5901 else if (i == 3)
5902 npp = &(*npp)->right;
5903 else
5905 /* Find the place in the list that bisects the list's total cost,
5906 where ranges count as 2.
5907 Here I gets half the total cost. */
5908 i = (i + ranges + 1) / 2;
5909 while (1)
5911 /* Skip nodes while their cost does not reach that amount. */
5912 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5913 i--;
5914 i--;
5915 if (i <= 0)
5916 break;
5917 npp = &(*npp)->right;
5920 *head = np = *npp;
5921 *npp = 0;
5922 np->parent = parent;
5923 np->left = left;
5925 /* Optimize each of the two split parts. */
5926 balance_case_nodes (&np->left, np);
5927 balance_case_nodes (&np->right, np);
5929 else
5931 /* Else leave this branch as one level,
5932 but fill in `parent' fields. */
5933 np = *head;
5934 np->parent = parent;
5935 for (; np->right; np = np->right)
5936 np->right->parent = np;
5941 /* Search the parent sections of the case node tree
5942 to see if a test for the lower bound of NODE would be redundant.
5943 INDEX_TYPE is the type of the index expression.
5945 The instructions to generate the case decision tree are
5946 output in the same order as nodes are processed so it is
5947 known that if a parent node checks the range of the current
5948 node minus one that the current node is bounded at its lower
5949 span. Thus the test would be redundant. */
5951 static int
5952 node_has_low_bound (case_node_ptr node, tree index_type)
5954 tree low_minus_one;
5955 case_node_ptr pnode;
5957 /* If the lower bound of this node is the lowest value in the index type,
5958 we need not test it. */
5960 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
5961 return 1;
5963 /* If this node has a left branch, the value at the left must be less
5964 than that at this node, so it cannot be bounded at the bottom and
5965 we need not bother testing any further. */
5967 if (node->left)
5968 return 0;
5970 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
5971 node->low, integer_one_node));
5973 /* If the subtraction above overflowed, we can't verify anything.
5974 Otherwise, look for a parent that tests our value - 1. */
5976 if (! tree_int_cst_lt (low_minus_one, node->low))
5977 return 0;
5979 for (pnode = node->parent; pnode; pnode = pnode->parent)
5980 if (tree_int_cst_equal (low_minus_one, pnode->high))
5981 return 1;
5983 return 0;
5986 /* Search the parent sections of the case node tree
5987 to see if a test for the upper bound of NODE would be redundant.
5988 INDEX_TYPE is the type of the index expression.
5990 The instructions to generate the case decision tree are
5991 output in the same order as nodes are processed so it is
5992 known that if a parent node checks the range of the current
5993 node plus one that the current node is bounded at its upper
5994 span. Thus the test would be redundant. */
5996 static int
5997 node_has_high_bound (case_node_ptr node, tree index_type)
5999 tree high_plus_one;
6000 case_node_ptr pnode;
6002 /* If there is no upper bound, obviously no test is needed. */
6004 if (TYPE_MAX_VALUE (index_type) == NULL)
6005 return 1;
6007 /* If the upper bound of this node is the highest value in the type
6008 of the index expression, we need not test against it. */
6010 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
6011 return 1;
6013 /* If this node has a right branch, the value at the right must be greater
6014 than that at this node, so it cannot be bounded at the top and
6015 we need not bother testing any further. */
6017 if (node->right)
6018 return 0;
6020 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
6021 node->high, integer_one_node));
6023 /* If the addition above overflowed, we can't verify anything.
6024 Otherwise, look for a parent that tests our value + 1. */
6026 if (! tree_int_cst_lt (node->high, high_plus_one))
6027 return 0;
6029 for (pnode = node->parent; pnode; pnode = pnode->parent)
6030 if (tree_int_cst_equal (high_plus_one, pnode->low))
6031 return 1;
6033 return 0;
6036 /* Search the parent sections of the
6037 case node tree to see if both tests for the upper and lower
6038 bounds of NODE would be redundant. */
6040 static int
6041 node_is_bounded (case_node_ptr node, tree index_type)
6043 return (node_has_low_bound (node, index_type)
6044 && node_has_high_bound (node, index_type));
6047 /* Emit an unconditional jump to LABEL unless it would be dead code. */
6049 static void
6050 emit_jump_if_reachable (rtx label)
6052 if (GET_CODE (get_last_insn ()) != BARRIER)
6053 emit_jump (label);
6056 /* Emit step-by-step code to select a case for the value of INDEX.
6057 The thus generated decision tree follows the form of the
6058 case-node binary tree NODE, whose nodes represent test conditions.
6059 INDEX_TYPE is the type of the index of the switch.
6061 Care is taken to prune redundant tests from the decision tree
6062 by detecting any boundary conditions already checked by
6063 emitted rtx. (See node_has_high_bound, node_has_low_bound
6064 and node_is_bounded, above.)
6066 Where the test conditions can be shown to be redundant we emit
6067 an unconditional jump to the target code. As a further
6068 optimization, the subordinates of a tree node are examined to
6069 check for bounded nodes. In this case conditional and/or
6070 unconditional jumps as a result of the boundary check for the
6071 current node are arranged to target the subordinates associated
6072 code for out of bound conditions on the current node.
6074 We can assume that when control reaches the code generated here,
6075 the index value has already been compared with the parents
6076 of this node, and determined to be on the same side of each parent
6077 as this node is. Thus, if this node tests for the value 51,
6078 and a parent tested for 52, we don't need to consider
6079 the possibility of a value greater than 51. If another parent
6080 tests for the value 50, then this node need not test anything. */
6082 static void
6083 emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
6084 tree index_type)
6086 /* If INDEX has an unsigned type, we must make unsigned branches. */
6087 int unsignedp = TREE_UNSIGNED (index_type);
6088 enum machine_mode mode = GET_MODE (index);
6089 enum machine_mode imode = TYPE_MODE (index_type);
6091 /* See if our parents have already tested everything for us.
6092 If they have, emit an unconditional jump for this node. */
6093 if (node_is_bounded (node, index_type))
6094 emit_jump (label_rtx (node->code_label));
6096 else if (tree_int_cst_equal (node->low, node->high))
6098 /* Node is single valued. First see if the index expression matches
6099 this node and then check our children, if any. */
6101 do_jump_if_equal (index,
6102 convert_modes (mode, imode,
6103 expand_expr (node->low, NULL_RTX,
6104 VOIDmode, 0),
6105 unsignedp),
6106 label_rtx (node->code_label), unsignedp);
6108 if (node->right != 0 && node->left != 0)
6110 /* This node has children on both sides.
6111 Dispatch to one side or the other
6112 by comparing the index value with this node's value.
6113 If one subtree is bounded, check that one first,
6114 so we can avoid real branches in the tree. */
6116 if (node_is_bounded (node->right, index_type))
6118 emit_cmp_and_jump_insns (index,
6119 convert_modes
6120 (mode, imode,
6121 expand_expr (node->high, NULL_RTX,
6122 VOIDmode, 0),
6123 unsignedp),
6124 GT, NULL_RTX, mode, unsignedp,
6125 label_rtx (node->right->code_label));
6126 emit_case_nodes (index, node->left, default_label, index_type);
6129 else if (node_is_bounded (node->left, index_type))
6131 emit_cmp_and_jump_insns (index,
6132 convert_modes
6133 (mode, imode,
6134 expand_expr (node->high, NULL_RTX,
6135 VOIDmode, 0),
6136 unsignedp),
6137 LT, NULL_RTX, mode, unsignedp,
6138 label_rtx (node->left->code_label));
6139 emit_case_nodes (index, node->right, default_label, index_type);
6142 else
6144 /* Neither node is bounded. First distinguish the two sides;
6145 then emit the code for one side at a time. */
6147 tree test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6149 /* See if the value is on the right. */
6150 emit_cmp_and_jump_insns (index,
6151 convert_modes
6152 (mode, imode,
6153 expand_expr (node->high, NULL_RTX,
6154 VOIDmode, 0),
6155 unsignedp),
6156 GT, NULL_RTX, mode, unsignedp,
6157 label_rtx (test_label));
6159 /* Value must be on the left.
6160 Handle the left-hand subtree. */
6161 emit_case_nodes (index, node->left, default_label, index_type);
6162 /* If left-hand subtree does nothing,
6163 go to default. */
6164 emit_jump_if_reachable (default_label);
6166 /* Code branches here for the right-hand subtree. */
6167 expand_label (test_label);
6168 emit_case_nodes (index, node->right, default_label, index_type);
6172 else if (node->right != 0 && node->left == 0)
6174 /* Here we have a right child but no left so we issue conditional
6175 branch to default and process the right child.
6177 Omit the conditional branch to default if we it avoid only one
6178 right child; it costs too much space to save so little time. */
6180 if (node->right->right || node->right->left
6181 || !tree_int_cst_equal (node->right->low, node->right->high))
6183 if (!node_has_low_bound (node, index_type))
6185 emit_cmp_and_jump_insns (index,
6186 convert_modes
6187 (mode, imode,
6188 expand_expr (node->high, NULL_RTX,
6189 VOIDmode, 0),
6190 unsignedp),
6191 LT, NULL_RTX, mode, unsignedp,
6192 default_label);
6195 emit_case_nodes (index, node->right, default_label, index_type);
6197 else
6198 /* We cannot process node->right normally
6199 since we haven't ruled out the numbers less than
6200 this node's value. So handle node->right explicitly. */
6201 do_jump_if_equal (index,
6202 convert_modes
6203 (mode, imode,
6204 expand_expr (node->right->low, NULL_RTX,
6205 VOIDmode, 0),
6206 unsignedp),
6207 label_rtx (node->right->code_label), unsignedp);
6210 else if (node->right == 0 && node->left != 0)
6212 /* Just one subtree, on the left. */
6213 if (node->left->left || node->left->right
6214 || !tree_int_cst_equal (node->left->low, node->left->high))
6216 if (!node_has_high_bound (node, index_type))
6218 emit_cmp_and_jump_insns (index,
6219 convert_modes
6220 (mode, imode,
6221 expand_expr (node->high, NULL_RTX,
6222 VOIDmode, 0),
6223 unsignedp),
6224 GT, NULL_RTX, mode, unsignedp,
6225 default_label);
6228 emit_case_nodes (index, node->left, default_label, index_type);
6230 else
6231 /* We cannot process node->left normally
6232 since we haven't ruled out the numbers less than
6233 this node's value. So handle node->left explicitly. */
6234 do_jump_if_equal (index,
6235 convert_modes
6236 (mode, imode,
6237 expand_expr (node->left->low, NULL_RTX,
6238 VOIDmode, 0),
6239 unsignedp),
6240 label_rtx (node->left->code_label), unsignedp);
6243 else
6245 /* Node is a range. These cases are very similar to those for a single
6246 value, except that we do not start by testing whether this node
6247 is the one to branch to. */
6249 if (node->right != 0 && node->left != 0)
6251 /* Node has subtrees on both sides.
6252 If the right-hand subtree is bounded,
6253 test for it first, since we can go straight there.
6254 Otherwise, we need to make a branch in the control structure,
6255 then handle the two subtrees. */
6256 tree test_label = 0;
6258 if (node_is_bounded (node->right, index_type))
6259 /* Right hand node is fully bounded so we can eliminate any
6260 testing and branch directly to the target code. */
6261 emit_cmp_and_jump_insns (index,
6262 convert_modes
6263 (mode, imode,
6264 expand_expr (node->high, NULL_RTX,
6265 VOIDmode, 0),
6266 unsignedp),
6267 GT, NULL_RTX, mode, unsignedp,
6268 label_rtx (node->right->code_label));
6269 else
6271 /* Right hand node requires testing.
6272 Branch to a label where we will handle it later. */
6274 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6275 emit_cmp_and_jump_insns (index,
6276 convert_modes
6277 (mode, imode,
6278 expand_expr (node->high, NULL_RTX,
6279 VOIDmode, 0),
6280 unsignedp),
6281 GT, NULL_RTX, mode, unsignedp,
6282 label_rtx (test_label));
6285 /* Value belongs to this node or to the left-hand subtree. */
6287 emit_cmp_and_jump_insns (index,
6288 convert_modes
6289 (mode, imode,
6290 expand_expr (node->low, NULL_RTX,
6291 VOIDmode, 0),
6292 unsignedp),
6293 GE, NULL_RTX, mode, unsignedp,
6294 label_rtx (node->code_label));
6296 /* Handle the left-hand subtree. */
6297 emit_case_nodes (index, node->left, default_label, index_type);
6299 /* If right node had to be handled later, do that now. */
6301 if (test_label)
6303 /* If the left-hand subtree fell through,
6304 don't let it fall into the right-hand subtree. */
6305 emit_jump_if_reachable (default_label);
6307 expand_label (test_label);
6308 emit_case_nodes (index, node->right, default_label, index_type);
6312 else if (node->right != 0 && node->left == 0)
6314 /* Deal with values to the left of this node,
6315 if they are possible. */
6316 if (!node_has_low_bound (node, index_type))
6318 emit_cmp_and_jump_insns (index,
6319 convert_modes
6320 (mode, imode,
6321 expand_expr (node->low, NULL_RTX,
6322 VOIDmode, 0),
6323 unsignedp),
6324 LT, NULL_RTX, mode, unsignedp,
6325 default_label);
6328 /* Value belongs to this node or to the right-hand subtree. */
6330 emit_cmp_and_jump_insns (index,
6331 convert_modes
6332 (mode, imode,
6333 expand_expr (node->high, NULL_RTX,
6334 VOIDmode, 0),
6335 unsignedp),
6336 LE, NULL_RTX, mode, unsignedp,
6337 label_rtx (node->code_label));
6339 emit_case_nodes (index, node->right, default_label, index_type);
6342 else if (node->right == 0 && node->left != 0)
6344 /* Deal with values to the right of this node,
6345 if they are possible. */
6346 if (!node_has_high_bound (node, index_type))
6348 emit_cmp_and_jump_insns (index,
6349 convert_modes
6350 (mode, imode,
6351 expand_expr (node->high, NULL_RTX,
6352 VOIDmode, 0),
6353 unsignedp),
6354 GT, NULL_RTX, mode, unsignedp,
6355 default_label);
6358 /* Value belongs to this node or to the left-hand subtree. */
6360 emit_cmp_and_jump_insns (index,
6361 convert_modes
6362 (mode, imode,
6363 expand_expr (node->low, NULL_RTX,
6364 VOIDmode, 0),
6365 unsignedp),
6366 GE, NULL_RTX, mode, unsignedp,
6367 label_rtx (node->code_label));
6369 emit_case_nodes (index, node->left, default_label, index_type);
6372 else
6374 /* Node has no children so we check low and high bounds to remove
6375 redundant tests. Only one of the bounds can exist,
6376 since otherwise this node is bounded--a case tested already. */
6377 int high_bound = node_has_high_bound (node, index_type);
6378 int low_bound = node_has_low_bound (node, index_type);
6380 if (!high_bound && low_bound)
6382 emit_cmp_and_jump_insns (index,
6383 convert_modes
6384 (mode, imode,
6385 expand_expr (node->high, NULL_RTX,
6386 VOIDmode, 0),
6387 unsignedp),
6388 GT, NULL_RTX, mode, unsignedp,
6389 default_label);
6392 else if (!low_bound && high_bound)
6394 emit_cmp_and_jump_insns (index,
6395 convert_modes
6396 (mode, imode,
6397 expand_expr (node->low, NULL_RTX,
6398 VOIDmode, 0),
6399 unsignedp),
6400 LT, NULL_RTX, mode, unsignedp,
6401 default_label);
6403 else if (!low_bound && !high_bound)
6405 /* Widen LOW and HIGH to the same width as INDEX. */
6406 tree type = (*lang_hooks.types.type_for_mode) (mode, unsignedp);
6407 tree low = build1 (CONVERT_EXPR, type, node->low);
6408 tree high = build1 (CONVERT_EXPR, type, node->high);
6409 rtx low_rtx, new_index, new_bound;
6411 /* Instead of doing two branches, emit one unsigned branch for
6412 (index-low) > (high-low). */
6413 low_rtx = expand_expr (low, NULL_RTX, mode, 0);
6414 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
6415 NULL_RTX, unsignedp,
6416 OPTAB_WIDEN);
6417 new_bound = expand_expr (fold (build (MINUS_EXPR, type,
6418 high, low)),
6419 NULL_RTX, mode, 0);
6421 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
6422 mode, 1, default_label);
6425 emit_jump (label_rtx (node->code_label));
6430 #include "gt-stmt.h"