Add a directory forgotten in merge.
[official-gcc.git] / gcc / stmt.c
blob403a13ad64b2275e9c4c62aa549ed394b93fcdc0
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"
60 #include "target.h"
62 /* Assume that case vectors are not pc-relative. */
63 #ifndef CASE_VECTOR_PC_RELATIVE
64 #define CASE_VECTOR_PC_RELATIVE 0
65 #endif
67 /* Functions and data structures for expanding case statements. */
69 /* Case label structure, used to hold info on labels within case
70 statements. We handle "range" labels; for a single-value label
71 as in C, the high and low limits are the same.
73 An AVL tree of case nodes is initially created, and later transformed
74 to a list linked via the RIGHT fields in the nodes. Nodes with
75 higher case values are later in the list.
77 Switch statements can be output in one of two forms. A branch table
78 is used if there are more than a few labels and the labels are dense
79 within the range between the smallest and largest case value. If a
80 branch table is used, no further manipulations are done with the case
81 node chain.
83 The alternative to the use of a branch table is to generate a series
84 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
85 and PARENT fields to hold a binary tree. Initially the tree is
86 totally unbalanced, with everything on the right. We balance the tree
87 with nodes on the left having lower case values than the parent
88 and nodes on the right having higher values. We then output the tree
89 in order. */
91 struct case_node GTY(())
93 struct case_node *left; /* Left son in binary tree */
94 struct case_node *right; /* Right son in binary tree; also node chain */
95 struct case_node *parent; /* Parent of node in binary tree */
96 tree low; /* Lowest index value for this label */
97 tree high; /* Highest index value for this label */
98 tree code_label; /* Label to jump to when node matches */
99 int balance;
102 typedef struct case_node case_node;
103 typedef struct case_node *case_node_ptr;
105 /* These are used by estimate_case_costs and balance_case_nodes. */
107 /* This must be a signed type, and non-ANSI compilers lack signed char. */
108 static short cost_table_[129];
109 static int use_cost_table;
110 static int cost_table_initialized;
112 /* Special care is needed because we allow -1, but TREE_INT_CST_LOW
113 is unsigned. */
114 #define COST_TABLE(I) cost_table_[(unsigned HOST_WIDE_INT) ((I) + 1)]
116 /* Stack of control and binding constructs we are currently inside.
118 These constructs begin when you call `expand_start_WHATEVER'
119 and end when you call `expand_end_WHATEVER'. This stack records
120 info about how the construct began that tells the end-function
121 what to do. It also may provide information about the construct
122 to alter the behavior of other constructs within the body.
123 For example, they may affect the behavior of C `break' and `continue'.
125 Each construct gets one `struct nesting' object.
126 All of these objects are chained through the `all' field.
127 `nesting_stack' points to the first object (innermost construct).
128 The position of an entry on `nesting_stack' is in its `depth' field.
130 Each type of construct has its own individual stack.
131 For example, loops have `loop_stack'. Each object points to the
132 next object of the same type through the `next' field.
134 Some constructs are visible to `break' exit-statements and others
135 are not. Which constructs are visible depends on the language.
136 Therefore, the data structure allows each construct to be visible
137 or not, according to the args given when the construct is started.
138 The construct is visible if the `exit_label' field is non-null.
139 In that case, the value should be a CODE_LABEL rtx. */
141 struct nesting GTY(())
143 struct nesting *all;
144 struct nesting *next;
145 int depth;
146 rtx exit_label;
147 enum nesting_desc {
148 COND_NESTING,
149 LOOP_NESTING,
150 BLOCK_NESTING,
151 CASE_NESTING
152 } desc;
153 union nesting_u
155 /* For conds (if-then and if-then-else statements). */
156 struct nesting_cond
158 /* Label for the end of the if construct.
159 There is none if EXITFLAG was not set
160 and no `else' has been seen yet. */
161 rtx endif_label;
162 /* Label for the end of this alternative.
163 This may be the end of the if or the next else/elseif. */
164 rtx next_label;
165 } GTY ((tag ("COND_NESTING"))) cond;
166 /* For loops. */
167 struct nesting_loop
169 /* Label at the top of the loop; place to loop back to. */
170 rtx start_label;
171 /* Label at the end of the whole construct. */
172 rtx end_label;
173 /* Label for `continue' statement to jump to;
174 this is in front of the stepper of the loop. */
175 rtx continue_label;
176 } GTY ((tag ("LOOP_NESTING"))) loop;
177 /* For variable binding contours. */
178 struct nesting_block
180 /* Sequence number of this binding contour within the function,
181 in order of entry. */
182 int block_start_count;
183 /* Nonzero => value to restore stack to on exit. */
184 rtx stack_level;
185 /* The NOTE that starts this contour.
186 Used by expand_goto to check whether the destination
187 is within each contour or not. */
188 rtx first_insn;
189 /* Innermost containing binding contour that has a stack level. */
190 struct nesting *innermost_stack_block;
191 /* List of cleanups to be run on exit from this contour.
192 This is a list of expressions to be evaluated.
193 The TREE_PURPOSE of each link is the ..._DECL node
194 which the cleanup pertains to. */
195 tree cleanups;
196 /* List of cleanup-lists of blocks containing this block,
197 as they were at the locus where this block appears.
198 There is an element for each containing block,
199 ordered innermost containing block first.
200 The tail of this list can be 0,
201 if all remaining elements would be empty lists.
202 The element's TREE_VALUE is the cleanup-list of that block,
203 which may be null. */
204 tree outer_cleanups;
205 /* Chain of labels defined inside this binding contour.
206 For contours that have stack levels or cleanups. */
207 struct label_chain *label_chain;
208 /* Nonzero if this is associated with an EH region. */
209 int exception_region;
210 /* The saved target_temp_slot_level from our outer block.
211 We may reset target_temp_slot_level to be the level of
212 this block, if that is done, target_temp_slot_level
213 reverts to the saved target_temp_slot_level at the very
214 end of the block. */
215 int block_target_temp_slot_level;
216 /* True if we are currently emitting insns in an area of
217 output code that is controlled by a conditional
218 expression. This is used by the cleanup handling code to
219 generate conditional cleanup actions. */
220 int conditional_code;
221 /* A place to move the start of the exception region for any
222 of the conditional cleanups, must be at the end or after
223 the start of the last unconditional cleanup, and before any
224 conditional branch points. */
225 rtx last_unconditional_cleanup;
226 } GTY ((tag ("BLOCK_NESTING"))) block;
227 /* For switch (C) or case (Pascal) statements,
228 and also for dummies (see `expand_start_case_dummy'). */
229 struct nesting_case
231 /* The insn after which the case dispatch should finally
232 be emitted. Zero for a dummy. */
233 rtx start;
234 /* A list of case labels; it is first built as an AVL tree.
235 During expand_end_case, this is converted to a list, and may be
236 rearranged into a nearly balanced binary tree. */
237 struct case_node *case_list;
238 /* Label to jump to if no case matches. */
239 tree default_label;
240 /* The expression to be dispatched on. */
241 tree index_expr;
242 /* Type that INDEX_EXPR should be converted to. */
243 tree nominal_type;
244 /* Name of this kind of statement, for warnings. */
245 const char *printname;
246 /* Used to save no_line_numbers till we see the first case label.
247 We set this to -1 when we see the first case label in this
248 case statement. */
249 int line_number_status;
250 } GTY ((tag ("CASE_NESTING"))) case_stmt;
251 } GTY ((desc ("%1.desc"))) data;
254 /* Allocate and return a new `struct nesting'. */
256 #define ALLOC_NESTING() ggc_alloc (sizeof (struct nesting))
258 /* Pop the nesting stack element by element until we pop off
259 the element which is at the top of STACK.
260 Update all the other stacks, popping off elements from them
261 as we pop them from nesting_stack. */
263 #define POPSTACK(STACK) \
264 do { struct nesting *target = STACK; \
265 struct nesting *this; \
266 do { this = nesting_stack; \
267 if (loop_stack == this) \
268 loop_stack = loop_stack->next; \
269 if (cond_stack == this) \
270 cond_stack = cond_stack->next; \
271 if (block_stack == this) \
272 block_stack = block_stack->next; \
273 if (stack_block_stack == this) \
274 stack_block_stack = stack_block_stack->next; \
275 if (case_stack == this) \
276 case_stack = case_stack->next; \
277 nesting_depth = nesting_stack->depth - 1; \
278 nesting_stack = this->all; } \
279 while (this != target); } while (0)
281 /* In some cases it is impossible to generate code for a forward goto
282 until the label definition is seen. This happens when it may be necessary
283 for the goto to reset the stack pointer: we don't yet know how to do that.
284 So expand_goto puts an entry on this fixup list.
285 Each time a binding contour that resets the stack is exited,
286 we check each fixup.
287 If the target label has now been defined, we can insert the proper code. */
289 struct goto_fixup GTY(())
291 /* Points to following fixup. */
292 struct goto_fixup *next;
293 /* Points to the insn before the jump insn.
294 If more code must be inserted, it goes after this insn. */
295 rtx before_jump;
296 /* The LABEL_DECL that this jump is jumping to, or 0
297 for break, continue or return. */
298 tree target;
299 /* The BLOCK for the place where this goto was found. */
300 tree context;
301 /* The CODE_LABEL rtx that this is jumping to. */
302 rtx target_rtl;
303 /* Number of binding contours started in current function
304 before the label reference. */
305 int block_start_count;
306 /* The outermost stack level that should be restored for this jump.
307 Each time a binding contour that resets the stack is exited,
308 if the target label is *not* yet defined, this slot is updated. */
309 rtx stack_level;
310 /* List of lists of cleanup expressions to be run by this goto.
311 There is one element for each block that this goto is within.
312 The tail of this list can be 0,
313 if all remaining elements would be empty.
314 The TREE_VALUE contains the cleanup list of that block as of the
315 time this goto was seen.
316 The TREE_ADDRESSABLE flag is 1 for a block that has been exited. */
317 tree cleanup_list_list;
320 /* Within any binding contour that must restore a stack level,
321 all labels are recorded with a chain of these structures. */
323 struct label_chain GTY(())
325 /* Points to following fixup. */
326 struct label_chain *next;
327 tree label;
330 struct stmt_status GTY(())
332 /* Chain of all pending binding contours. */
333 struct nesting * x_block_stack;
335 /* If any new stacks are added here, add them to POPSTACKS too. */
337 /* Chain of all pending binding contours that restore stack levels
338 or have cleanups. */
339 struct nesting * x_stack_block_stack;
341 /* Chain of all pending conditional statements. */
342 struct nesting * x_cond_stack;
344 /* Chain of all pending loops. */
345 struct nesting * x_loop_stack;
347 /* Chain of all pending case or switch statements. */
348 struct nesting * x_case_stack;
350 /* Separate chain including all of the above,
351 chained through the `all' field. */
352 struct nesting * x_nesting_stack;
354 /* Number of entries on nesting_stack now. */
355 int x_nesting_depth;
357 /* Number of binding contours started so far in this function. */
358 int x_block_start_count;
360 /* Each time we expand an expression-statement,
361 record the expr's type and its RTL value here. */
362 tree x_last_expr_type;
363 rtx x_last_expr_value;
365 /* Nonzero if within a ({...}) grouping, in which case we must
366 always compute a value for each expr-stmt in case it is the last one. */
367 int x_expr_stmts_for_value;
369 /* Location of last line-number note, whether we actually
370 emitted it or not. */
371 location_t x_emit_locus;
373 struct goto_fixup *x_goto_fixup_chain;
376 #define block_stack (cfun->stmt->x_block_stack)
377 #define stack_block_stack (cfun->stmt->x_stack_block_stack)
378 #define cond_stack (cfun->stmt->x_cond_stack)
379 #define loop_stack (cfun->stmt->x_loop_stack)
380 #define case_stack (cfun->stmt->x_case_stack)
381 #define nesting_stack (cfun->stmt->x_nesting_stack)
382 #define nesting_depth (cfun->stmt->x_nesting_depth)
383 #define current_block_start_count (cfun->stmt->x_block_start_count)
384 #define last_expr_type (cfun->stmt->x_last_expr_type)
385 #define last_expr_value (cfun->stmt->x_last_expr_value)
386 #define expr_stmts_for_value (cfun->stmt->x_expr_stmts_for_value)
387 #define emit_locus (cfun->stmt->x_emit_locus)
388 #define goto_fixup_chain (cfun->stmt->x_goto_fixup_chain)
390 /* Nonzero if we are using EH to handle cleanups. */
391 int using_eh_for_cleanups_p = 0;
393 static int n_occurrences (int, const char *);
394 static bool parse_input_constraint (const char **, int, int, int, int,
395 const char * const *, bool *, bool *);
396 static bool decl_conflicts_with_clobbers_p (tree, const HARD_REG_SET);
397 static void expand_goto_internal (tree, rtx, rtx);
398 static int expand_fixup (tree, rtx, rtx);
399 static rtx expand_nl_handler_label (rtx, rtx);
400 static void expand_nl_goto_receiver (void);
401 static void expand_nl_goto_receivers (struct nesting *);
402 static void fixup_gotos (struct nesting *, rtx, tree, rtx, int);
403 static bool check_operand_nalternatives (tree, tree);
404 static bool check_unique_operand_names (tree, tree);
405 static char *resolve_operand_name_1 (char *, tree, tree);
406 static void expand_null_return_1 (rtx);
407 static enum br_predictor return_prediction (rtx);
408 static void expand_value_return (rtx);
409 static int tail_recursion_args (tree, tree);
410 static void expand_cleanups (tree, int, int);
411 static void check_seenlabel (void);
412 static void do_jump_if_equal (rtx, rtx, rtx, int);
413 static int estimate_case_costs (case_node_ptr);
414 static bool same_case_target_p (rtx, rtx);
415 static void strip_default_case_nodes (case_node_ptr *, rtx);
416 static bool lshift_cheap_p (void);
417 static int case_bit_test_cmp (const void *, const void *);
418 static void emit_case_bit_tests (tree, tree, tree, tree, case_node_ptr, rtx);
419 static void group_case_nodes (case_node_ptr);
420 static void balance_case_nodes (case_node_ptr *, case_node_ptr);
421 static int node_has_low_bound (case_node_ptr, tree);
422 static int node_has_high_bound (case_node_ptr, tree);
423 static int node_is_bounded (case_node_ptr, tree);
424 static void emit_jump_if_reachable (rtx);
425 static void emit_case_nodes (rtx, case_node_ptr, rtx, tree);
426 static struct case_node *case_tree2list (case_node *, case_node *);
428 void
429 using_eh_for_cleanups (void)
431 using_eh_for_cleanups_p = 1;
434 void
435 init_stmt_for_function (void)
437 cfun->stmt = ggc_alloc_cleared (sizeof (struct stmt_status));
440 /* Record the current file and line. Called from emit_line_note. */
442 void
443 set_file_and_line_for_stmt (location_t location)
445 /* If we're outputting an inline function, and we add a line note,
446 there may be no CFUN->STMT information. So, there's no need to
447 update it. */
448 if (cfun->stmt)
449 emit_locus = location;
452 /* Emit a no-op instruction. */
454 void
455 emit_nop (void)
457 rtx last_insn;
459 last_insn = get_last_insn ();
460 if (!optimize
461 && (GET_CODE (last_insn) == CODE_LABEL
462 || (GET_CODE (last_insn) == NOTE
463 && prev_real_insn (last_insn) == 0)))
464 emit_insn (gen_nop ());
467 /* Return the rtx-label that corresponds to a LABEL_DECL,
468 creating it if necessary. */
471 label_rtx (tree label)
473 if (TREE_CODE (label) != LABEL_DECL)
474 abort ();
476 if (!DECL_RTL_SET_P (label))
477 SET_DECL_RTL (label, gen_label_rtx ());
479 return DECL_RTL (label);
482 /* As above, but also put it on the forced-reference list of the
483 function that contains it. */
485 force_label_rtx (tree label)
487 rtx ref = label_rtx (label);
488 tree function = decl_function_context (label);
489 struct function *p;
491 if (!function)
492 abort ();
494 if (function != current_function_decl
495 && function != inline_function_decl)
496 p = find_function_data (function);
497 else
498 p = cfun;
500 p->expr->x_forced_labels = gen_rtx_EXPR_LIST (VOIDmode, ref,
501 p->expr->x_forced_labels);
502 return ref;
505 /* Add an unconditional jump to LABEL as the next sequential instruction. */
507 void
508 emit_jump (rtx label)
510 do_pending_stack_adjust ();
511 emit_jump_insn (gen_jump (label));
512 emit_barrier ();
515 /* Emit code to jump to the address
516 specified by the pointer expression EXP. */
518 void
519 expand_computed_goto (tree exp)
521 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
523 x = convert_memory_address (Pmode, x);
525 emit_queue ();
527 if (! cfun->computed_goto_common_label)
529 cfun->computed_goto_common_reg = copy_to_mode_reg (Pmode, x);
530 cfun->computed_goto_common_label = gen_label_rtx ();
531 emit_label (cfun->computed_goto_common_label);
533 do_pending_stack_adjust ();
534 emit_indirect_jump (cfun->computed_goto_common_reg);
536 current_function_has_computed_jump = 1;
538 else
540 emit_move_insn (cfun->computed_goto_common_reg, x);
541 emit_jump (cfun->computed_goto_common_label);
545 /* Handle goto statements and the labels that they can go to. */
547 /* Specify the location in the RTL code of a label LABEL,
548 which is a LABEL_DECL tree node.
550 This is used for the kind of label that the user can jump to with a
551 goto statement, and for alternatives of a switch or case statement.
552 RTL labels generated for loops and conditionals don't go through here;
553 they are generated directly at the RTL level, by other functions below.
555 Note that this has nothing to do with defining label *names*.
556 Languages vary in how they do that and what that even means. */
558 void
559 expand_label (tree label)
561 struct label_chain *p;
563 do_pending_stack_adjust ();
564 emit_label (label_rtx (label));
565 if (DECL_NAME (label))
566 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
568 if (stack_block_stack != 0)
570 p = ggc_alloc (sizeof (struct label_chain));
571 p->next = stack_block_stack->data.block.label_chain;
572 stack_block_stack->data.block.label_chain = p;
573 p->label = label;
577 /* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
578 from nested functions. */
580 void
581 declare_nonlocal_label (tree label)
583 rtx slot = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
585 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
586 LABEL_PRESERVE_P (label_rtx (label)) = 1;
587 if (nonlocal_goto_handler_slots == 0)
589 emit_stack_save (SAVE_NONLOCAL,
590 &nonlocal_goto_stack_level,
591 PREV_INSN (tail_recursion_reentry));
593 nonlocal_goto_handler_slots
594 = gen_rtx_EXPR_LIST (VOIDmode, slot, nonlocal_goto_handler_slots);
597 /* Generate RTL code for a `goto' statement with target label LABEL.
598 LABEL should be a LABEL_DECL tree node that was or will later be
599 defined with `expand_label'. */
601 void
602 expand_goto (tree label)
604 tree context;
606 /* Check for a nonlocal goto to a containing function. */
607 context = decl_function_context (label);
608 if (context != 0 && context != current_function_decl)
610 struct function *p = find_function_data (context);
611 rtx label_ref = gen_rtx_LABEL_REF (Pmode, label_rtx (label));
612 rtx handler_slot, static_chain, save_area, insn;
613 tree link;
615 /* Find the corresponding handler slot for this label. */
616 handler_slot = p->x_nonlocal_goto_handler_slots;
617 for (link = p->x_nonlocal_labels; TREE_VALUE (link) != label;
618 link = TREE_CHAIN (link))
619 handler_slot = XEXP (handler_slot, 1);
620 handler_slot = XEXP (handler_slot, 0);
622 p->has_nonlocal_label = 1;
623 current_function_has_nonlocal_goto = 1;
624 LABEL_REF_NONLOCAL_P (label_ref) = 1;
626 /* Copy the rtl for the slots so that they won't be shared in
627 case the virtual stack vars register gets instantiated differently
628 in the parent than in the child. */
630 static_chain = copy_to_reg (lookup_static_chain (label));
632 /* Get addr of containing function's current nonlocal goto handler,
633 which will do any cleanups and then jump to the label. */
634 handler_slot = copy_to_reg (replace_rtx (copy_rtx (handler_slot),
635 virtual_stack_vars_rtx,
636 static_chain));
638 /* Get addr of containing function's nonlocal save area. */
639 save_area = p->x_nonlocal_goto_stack_level;
640 if (save_area)
641 save_area = replace_rtx (copy_rtx (save_area),
642 virtual_stack_vars_rtx, static_chain);
644 #if HAVE_nonlocal_goto
645 if (HAVE_nonlocal_goto)
646 emit_insn (gen_nonlocal_goto (static_chain, handler_slot,
647 save_area, label_ref));
648 else
649 #endif
651 /* Restore frame pointer for containing function.
652 This sets the actual hard register used for the frame pointer
653 to the location of the function's incoming static chain info.
654 The non-local goto handler will then adjust it to contain the
655 proper value and reload the argument pointer, if needed. */
656 emit_move_insn (hard_frame_pointer_rtx, static_chain);
657 emit_stack_restore (SAVE_NONLOCAL, save_area, NULL_RTX);
659 /* USE of hard_frame_pointer_rtx added for consistency;
660 not clear if really needed. */
661 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
662 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
663 emit_indirect_jump (handler_slot);
666 /* Search backwards to the jump insn and mark it as a
667 non-local goto. */
668 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
670 if (GET_CODE (insn) == JUMP_INSN)
672 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
673 const0_rtx, REG_NOTES (insn));
674 break;
676 else if (GET_CODE (insn) == CALL_INSN)
677 break;
680 else
681 expand_goto_internal (label, label_rtx (label), NULL_RTX);
684 /* Generate RTL code for a `goto' statement with target label BODY.
685 LABEL should be a LABEL_REF.
686 LAST_INSN, if non-0, is the rtx we should consider as the last
687 insn emitted (for the purposes of cleaning up a return). */
689 static void
690 expand_goto_internal (tree body, rtx label, rtx last_insn)
692 struct nesting *block;
693 rtx stack_level = 0;
695 if (GET_CODE (label) != CODE_LABEL)
696 abort ();
698 /* If label has already been defined, we can tell now
699 whether and how we must alter the stack level. */
701 if (PREV_INSN (label) != 0)
703 /* Find the innermost pending block that contains the label.
704 (Check containment by comparing insn-uids.)
705 Then restore the outermost stack level within that block,
706 and do cleanups of all blocks contained in it. */
707 for (block = block_stack; block; block = block->next)
709 if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
710 break;
711 if (block->data.block.stack_level != 0)
712 stack_level = block->data.block.stack_level;
713 /* Execute the cleanups for blocks we are exiting. */
714 if (block->data.block.cleanups != 0)
716 expand_cleanups (block->data.block.cleanups, 1, 1);
717 do_pending_stack_adjust ();
721 if (stack_level)
723 /* Ensure stack adjust isn't done by emit_jump, as this
724 would clobber the stack pointer. This one should be
725 deleted as dead by flow. */
726 clear_pending_stack_adjust ();
727 do_pending_stack_adjust ();
729 /* Don't do this adjust if it's to the end label and this function
730 is to return with a depressed stack pointer. */
731 if (label == return_label
732 && (((TREE_CODE (TREE_TYPE (current_function_decl))
733 == FUNCTION_TYPE)
734 && (TYPE_RETURNS_STACK_DEPRESSED
735 (TREE_TYPE (current_function_decl))))))
737 else
738 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
741 if (body != 0 && DECL_TOO_LATE (body))
742 error ("jump to `%s' invalidly jumps into binding contour",
743 IDENTIFIER_POINTER (DECL_NAME (body)));
745 /* Label not yet defined: may need to put this goto
746 on the fixup list. */
747 else if (! expand_fixup (body, label, last_insn))
749 /* No fixup needed. Record that the label is the target
750 of at least one goto that has no fixup. */
751 if (body != 0)
752 TREE_ADDRESSABLE (body) = 1;
755 emit_jump (label);
758 /* Generate if necessary a fixup for a goto
759 whose target label in tree structure (if any) is TREE_LABEL
760 and whose target in rtl is RTL_LABEL.
762 If LAST_INSN is nonzero, we pretend that the jump appears
763 after insn LAST_INSN instead of at the current point in the insn stream.
765 The fixup will be used later to insert insns just before the goto.
766 Those insns will restore the stack level as appropriate for the
767 target label, and will (in the case of C++) also invoke any object
768 destructors which have to be invoked when we exit the scopes which
769 are exited by the goto.
771 Value is nonzero if a fixup is made. */
773 static int
774 expand_fixup (tree tree_label, rtx rtl_label, rtx last_insn)
776 struct nesting *block, *end_block;
778 /* See if we can recognize which block the label will be output in.
779 This is possible in some very common cases.
780 If we succeed, set END_BLOCK to that block.
781 Otherwise, set it to 0. */
783 if (cond_stack
784 && (rtl_label == cond_stack->data.cond.endif_label
785 || rtl_label == cond_stack->data.cond.next_label))
786 end_block = cond_stack;
787 /* If we are in a loop, recognize certain labels which
788 are likely targets. This reduces the number of fixups
789 we need to create. */
790 else if (loop_stack
791 && (rtl_label == loop_stack->data.loop.start_label
792 || rtl_label == loop_stack->data.loop.end_label
793 || rtl_label == loop_stack->data.loop.continue_label))
794 end_block = loop_stack;
795 else
796 end_block = 0;
798 /* Now set END_BLOCK to the binding level to which we will return. */
800 if (end_block)
802 struct nesting *next_block = end_block->all;
803 block = block_stack;
805 /* First see if the END_BLOCK is inside the innermost binding level.
806 If so, then no cleanups or stack levels are relevant. */
807 while (next_block && next_block != block)
808 next_block = next_block->all;
810 if (next_block)
811 return 0;
813 /* Otherwise, set END_BLOCK to the innermost binding level
814 which is outside the relevant control-structure nesting. */
815 next_block = block_stack->next;
816 for (block = block_stack; block != end_block; block = block->all)
817 if (block == next_block)
818 next_block = next_block->next;
819 end_block = next_block;
822 /* Does any containing block have a stack level or cleanups?
823 If not, no fixup is needed, and that is the normal case
824 (the only case, for standard C). */
825 for (block = block_stack; block != end_block; block = block->next)
826 if (block->data.block.stack_level != 0
827 || block->data.block.cleanups != 0)
828 break;
830 if (block != end_block)
832 /* Ok, a fixup is needed. Add a fixup to the list of such. */
833 struct goto_fixup *fixup = ggc_alloc (sizeof (struct goto_fixup));
834 /* In case an old stack level is restored, make sure that comes
835 after any pending stack adjust. */
836 /* ?? If the fixup isn't to come at the present position,
837 doing the stack adjust here isn't useful. Doing it with our
838 settings at that location isn't useful either. Let's hope
839 someone does it! */
840 if (last_insn == 0)
841 do_pending_stack_adjust ();
842 fixup->target = tree_label;
843 fixup->target_rtl = rtl_label;
845 /* Create a BLOCK node and a corresponding matched set of
846 NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes at
847 this point. The notes will encapsulate any and all fixup
848 code which we might later insert at this point in the insn
849 stream. Also, the BLOCK node will be the parent (i.e. the
850 `SUPERBLOCK') of any other BLOCK nodes which we might create
851 later on when we are expanding the fixup code.
853 Note that optimization passes (including expand_end_loop)
854 might move the *_BLOCK notes away, so we use a NOTE_INSN_DELETED
855 as a placeholder. */
858 rtx original_before_jump
859 = last_insn ? last_insn : get_last_insn ();
860 rtx start;
861 rtx end;
862 tree block;
864 block = make_node (BLOCK);
865 TREE_USED (block) = 1;
867 if (!cfun->x_whole_function_mode_p)
868 (*lang_hooks.decls.insert_block) (block);
869 else
871 BLOCK_CHAIN (block)
872 = BLOCK_CHAIN (DECL_INITIAL (current_function_decl));
873 BLOCK_CHAIN (DECL_INITIAL (current_function_decl))
874 = block;
877 start_sequence ();
878 start = emit_note (NOTE_INSN_BLOCK_BEG);
879 if (cfun->x_whole_function_mode_p)
880 NOTE_BLOCK (start) = block;
881 fixup->before_jump = emit_note (NOTE_INSN_DELETED);
882 end = emit_note (NOTE_INSN_BLOCK_END);
883 if (cfun->x_whole_function_mode_p)
884 NOTE_BLOCK (end) = block;
885 fixup->context = block;
886 end_sequence ();
887 emit_insn_after (start, original_before_jump);
890 fixup->block_start_count = current_block_start_count;
891 fixup->stack_level = 0;
892 fixup->cleanup_list_list
893 = ((block->data.block.outer_cleanups
894 || block->data.block.cleanups)
895 ? tree_cons (NULL_TREE, block->data.block.cleanups,
896 block->data.block.outer_cleanups)
897 : 0);
898 fixup->next = goto_fixup_chain;
899 goto_fixup_chain = fixup;
902 return block != 0;
905 /* Expand any needed fixups in the outputmost binding level of the
906 function. FIRST_INSN is the first insn in the function. */
908 void
909 expand_fixups (rtx first_insn)
911 fixup_gotos (NULL, NULL_RTX, NULL_TREE, first_insn, 0);
914 /* When exiting a binding contour, process all pending gotos requiring fixups.
915 THISBLOCK is the structure that describes the block being exited.
916 STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
917 CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
918 FIRST_INSN is the insn that began this contour.
920 Gotos that jump out of this contour must restore the
921 stack level and do the cleanups before actually jumping.
923 DONT_JUMP_IN positive means report error if there is a jump into this
924 contour from before the beginning of the contour. This is also done if
925 STACK_LEVEL is nonzero unless DONT_JUMP_IN is negative. */
927 static void
928 fixup_gotos (struct nesting *thisblock, rtx stack_level,
929 tree cleanup_list, rtx first_insn, int dont_jump_in)
931 struct goto_fixup *f, *prev;
933 /* F is the fixup we are considering; PREV is the previous one. */
934 /* We run this loop in two passes so that cleanups of exited blocks
935 are run first, and blocks that are exited are marked so
936 afterwards. */
938 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
940 /* Test for a fixup that is inactive because it is already handled. */
941 if (f->before_jump == 0)
943 /* Delete inactive fixup from the chain, if that is easy to do. */
944 if (prev != 0)
945 prev->next = f->next;
947 /* Has this fixup's target label been defined?
948 If so, we can finalize it. */
949 else if (PREV_INSN (f->target_rtl) != 0)
951 rtx cleanup_insns;
953 /* If this fixup jumped into this contour from before the beginning
954 of this contour, report an error. This code used to use
955 the first non-label insn after f->target_rtl, but that's
956 wrong since such can be added, by things like put_var_into_stack
957 and have INSN_UIDs that are out of the range of the block. */
958 /* ??? Bug: this does not detect jumping in through intermediate
959 blocks that have stack levels or cleanups.
960 It detects only a problem with the innermost block
961 around the label. */
962 if (f->target != 0
963 && (dont_jump_in > 0 || (dont_jump_in == 0 && stack_level)
964 || cleanup_list)
965 && INSN_UID (first_insn) < INSN_UID (f->target_rtl)
966 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
967 && ! DECL_ERROR_ISSUED (f->target))
969 error ("%Jlabel '%D' used before containing binding contour",
970 f->target, f->target);
971 /* Prevent multiple errors for one label. */
972 DECL_ERROR_ISSUED (f->target) = 1;
975 /* We will expand the cleanups into a sequence of their own and
976 then later on we will attach this new sequence to the insn
977 stream just ahead of the actual jump insn. */
979 start_sequence ();
981 /* Temporarily restore the lexical context where we will
982 logically be inserting the fixup code. We do this for the
983 sake of getting the debugging information right. */
985 (*lang_hooks.decls.pushlevel) (0);
986 (*lang_hooks.decls.set_block) (f->context);
988 /* Expand the cleanups for blocks this jump exits. */
989 if (f->cleanup_list_list)
991 tree lists;
992 for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
993 /* Marked elements correspond to blocks that have been closed.
994 Do their cleanups. */
995 if (TREE_ADDRESSABLE (lists)
996 && TREE_VALUE (lists) != 0)
998 expand_cleanups (TREE_VALUE (lists), 1, 1);
999 /* Pop any pushes done in the cleanups,
1000 in case function is about to return. */
1001 do_pending_stack_adjust ();
1005 /* Restore stack level for the biggest contour that this
1006 jump jumps out of. */
1007 if (f->stack_level
1008 && ! (f->target_rtl == return_label
1009 && ((TREE_CODE (TREE_TYPE (current_function_decl))
1010 == FUNCTION_TYPE)
1011 && (TYPE_RETURNS_STACK_DEPRESSED
1012 (TREE_TYPE (current_function_decl))))))
1013 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
1015 /* Finish up the sequence containing the insns which implement the
1016 necessary cleanups, and then attach that whole sequence to the
1017 insn stream just ahead of the actual jump insn. Attaching it
1018 at that point insures that any cleanups which are in fact
1019 implicit C++ object destructions (which must be executed upon
1020 leaving the block) appear (to the debugger) to be taking place
1021 in an area of the generated code where the object(s) being
1022 destructed are still "in scope". */
1024 cleanup_insns = get_insns ();
1025 (*lang_hooks.decls.poplevel) (1, 0, 0);
1027 end_sequence ();
1028 emit_insn_after (cleanup_insns, f->before_jump);
1030 f->before_jump = 0;
1034 /* For any still-undefined labels, do the cleanups for this block now.
1035 We must do this now since items in the cleanup list may go out
1036 of scope when the block ends. */
1037 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1038 if (f->before_jump != 0
1039 && PREV_INSN (f->target_rtl) == 0
1040 /* Label has still not appeared. If we are exiting a block with
1041 a stack level to restore, that started before the fixup,
1042 mark this stack level as needing restoration
1043 when the fixup is later finalized. */
1044 && thisblock != 0
1045 /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared, it
1046 means the label is undefined. That's erroneous, but possible. */
1047 && (thisblock->data.block.block_start_count
1048 <= f->block_start_count))
1050 tree lists = f->cleanup_list_list;
1051 rtx cleanup_insns;
1053 for (; lists; lists = TREE_CHAIN (lists))
1054 /* If the following elt. corresponds to our containing block
1055 then the elt. must be for this block. */
1056 if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
1058 start_sequence ();
1059 (*lang_hooks.decls.pushlevel) (0);
1060 (*lang_hooks.decls.set_block) (f->context);
1061 expand_cleanups (TREE_VALUE (lists), 1, 1);
1062 do_pending_stack_adjust ();
1063 cleanup_insns = get_insns ();
1064 (*lang_hooks.decls.poplevel) (1, 0, 0);
1065 end_sequence ();
1066 if (cleanup_insns != 0)
1067 f->before_jump
1068 = emit_insn_after (cleanup_insns, f->before_jump);
1070 f->cleanup_list_list = TREE_CHAIN (lists);
1073 if (stack_level)
1074 f->stack_level = stack_level;
1078 /* Return the number of times character C occurs in string S. */
1079 static int
1080 n_occurrences (int c, const char *s)
1082 int n = 0;
1083 while (*s)
1084 n += (*s++ == c);
1085 return n;
1088 /* Generate RTL for an asm statement (explicit assembler code).
1089 STRING is a STRING_CST node containing the assembler code text,
1090 or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
1091 insn is volatile; don't optimize it. */
1093 void
1094 expand_asm (tree string, int vol)
1096 rtx body;
1098 if (TREE_CODE (string) == ADDR_EXPR)
1099 string = TREE_OPERAND (string, 0);
1101 body = gen_rtx_ASM_INPUT (VOIDmode, TREE_STRING_POINTER (string));
1103 MEM_VOLATILE_P (body) = vol;
1105 emit_insn (body);
1107 clear_last_expr ();
1110 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
1111 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
1112 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
1113 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
1114 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
1115 constraint allows the use of a register operand. And, *IS_INOUT
1116 will be true if the operand is read-write, i.e., if it is used as
1117 an input as well as an output. If *CONSTRAINT_P is not in
1118 canonical form, it will be made canonical. (Note that `+' will be
1119 replaced with `=' as part of this process.)
1121 Returns TRUE if all went well; FALSE if an error occurred. */
1123 bool
1124 parse_output_constraint (const char **constraint_p, int operand_num,
1125 int ninputs, int noutputs, bool *allows_mem,
1126 bool *allows_reg, bool *is_inout)
1128 const char *constraint = *constraint_p;
1129 const char *p;
1131 /* Assume the constraint doesn't allow the use of either a register
1132 or memory. */
1133 *allows_mem = false;
1134 *allows_reg = false;
1136 /* Allow the `=' or `+' to not be at the beginning of the string,
1137 since it wasn't explicitly documented that way, and there is a
1138 large body of code that puts it last. Swap the character to
1139 the front, so as not to uglify any place else. */
1140 p = strchr (constraint, '=');
1141 if (!p)
1142 p = strchr (constraint, '+');
1144 /* If the string doesn't contain an `=', issue an error
1145 message. */
1146 if (!p)
1148 error ("output operand constraint lacks `='");
1149 return false;
1152 /* If the constraint begins with `+', then the operand is both read
1153 from and written to. */
1154 *is_inout = (*p == '+');
1156 /* Canonicalize the output constraint so that it begins with `='. */
1157 if (p != constraint || is_inout)
1159 char *buf;
1160 size_t c_len = strlen (constraint);
1162 if (p != constraint)
1163 warning ("output constraint `%c' for operand %d is not at the beginning",
1164 *p, operand_num);
1166 /* Make a copy of the constraint. */
1167 buf = alloca (c_len + 1);
1168 strcpy (buf, constraint);
1169 /* Swap the first character and the `=' or `+'. */
1170 buf[p - constraint] = buf[0];
1171 /* Make sure the first character is an `='. (Until we do this,
1172 it might be a `+'.) */
1173 buf[0] = '=';
1174 /* Replace the constraint with the canonicalized string. */
1175 *constraint_p = ggc_alloc_string (buf, c_len);
1176 constraint = *constraint_p;
1179 /* Loop through the constraint string. */
1180 for (p = constraint + 1; *p; p += CONSTRAINT_LEN (*p, p))
1181 switch (*p)
1183 case '+':
1184 case '=':
1185 error ("operand constraint contains incorrectly positioned '+' or '='");
1186 return false;
1188 case '%':
1189 if (operand_num + 1 == ninputs + noutputs)
1191 error ("`%%' constraint used with last operand");
1192 return false;
1194 break;
1196 case 'V': case 'm': case 'o':
1197 *allows_mem = true;
1198 break;
1200 case '?': case '!': case '*': case '&': case '#':
1201 case 'E': case 'F': case 'G': case 'H':
1202 case 's': case 'i': case 'n':
1203 case 'I': case 'J': case 'K': case 'L': case 'M':
1204 case 'N': case 'O': case 'P': case ',':
1205 break;
1207 case '0': case '1': case '2': case '3': case '4':
1208 case '5': case '6': case '7': case '8': case '9':
1209 case '[':
1210 error ("matching constraint not valid in output operand");
1211 return false;
1213 case '<': case '>':
1214 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
1215 excepting those that expand_call created. So match memory
1216 and hope. */
1217 *allows_mem = true;
1218 break;
1220 case 'g': case 'X':
1221 *allows_reg = true;
1222 *allows_mem = true;
1223 break;
1225 case 'p': case 'r':
1226 *allows_reg = true;
1227 break;
1229 default:
1230 if (!ISALPHA (*p))
1231 break;
1232 if (REG_CLASS_FROM_CONSTRAINT (*p, p) != NO_REGS)
1233 *allows_reg = true;
1234 #ifdef EXTRA_CONSTRAINT_STR
1235 else if (EXTRA_ADDRESS_CONSTRAINT (*p, p))
1236 *allows_reg = true;
1237 else if (EXTRA_MEMORY_CONSTRAINT (*p, p))
1238 *allows_mem = true;
1239 else
1241 /* Otherwise we can't assume anything about the nature of
1242 the constraint except that it isn't purely registers.
1243 Treat it like "g" and hope for the best. */
1244 *allows_reg = true;
1245 *allows_mem = true;
1247 #endif
1248 break;
1251 return true;
1254 /* Similar, but for input constraints. */
1256 static bool
1257 parse_input_constraint (const char **constraint_p, int input_num,
1258 int ninputs, int noutputs, int ninout,
1259 const char * const * constraints,
1260 bool *allows_mem, bool *allows_reg)
1262 const char *constraint = *constraint_p;
1263 const char *orig_constraint = constraint;
1264 size_t c_len = strlen (constraint);
1265 size_t j;
1267 /* Assume the constraint doesn't allow the use of either
1268 a register or memory. */
1269 *allows_mem = false;
1270 *allows_reg = false;
1272 /* Make sure constraint has neither `=', `+', nor '&'. */
1274 for (j = 0; j < c_len; j += CONSTRAINT_LEN (constraint[j], constraint+j))
1275 switch (constraint[j])
1277 case '+': case '=': case '&':
1278 if (constraint == orig_constraint)
1280 error ("input operand constraint contains `%c'", constraint[j]);
1281 return false;
1283 break;
1285 case '%':
1286 if (constraint == orig_constraint
1287 && input_num + 1 == ninputs - ninout)
1289 error ("`%%' constraint used with last operand");
1290 return false;
1292 break;
1294 case 'V': case 'm': case 'o':
1295 *allows_mem = true;
1296 break;
1298 case '<': case '>':
1299 case '?': case '!': case '*': case '#':
1300 case 'E': case 'F': case 'G': case 'H':
1301 case 's': case 'i': case 'n':
1302 case 'I': case 'J': case 'K': case 'L': case 'M':
1303 case 'N': case 'O': case 'P': case ',':
1304 break;
1306 /* Whether or not a numeric constraint allows a register is
1307 decided by the matching constraint, and so there is no need
1308 to do anything special with them. We must handle them in
1309 the default case, so that we don't unnecessarily force
1310 operands to memory. */
1311 case '0': case '1': case '2': case '3': case '4':
1312 case '5': case '6': case '7': case '8': case '9':
1314 char *end;
1315 unsigned long match;
1317 match = strtoul (constraint + j, &end, 10);
1318 if (match >= (unsigned long) noutputs)
1320 error ("matching constraint references invalid operand number");
1321 return false;
1324 /* Try and find the real constraint for this dup. Only do this
1325 if the matching constraint is the only alternative. */
1326 if (*end == '\0'
1327 && (j == 0 || (j == 1 && constraint[0] == '%')))
1329 constraint = constraints[match];
1330 *constraint_p = constraint;
1331 c_len = strlen (constraint);
1332 j = 0;
1333 /* ??? At the end of the loop, we will skip the first part of
1334 the matched constraint. This assumes not only that the
1335 other constraint is an output constraint, but also that
1336 the '=' or '+' come first. */
1337 break;
1339 else
1340 j = end - constraint;
1341 /* Anticipate increment at end of loop. */
1342 j--;
1344 /* Fall through. */
1346 case 'p': case 'r':
1347 *allows_reg = true;
1348 break;
1350 case 'g': case 'X':
1351 *allows_reg = true;
1352 *allows_mem = true;
1353 break;
1355 default:
1356 if (! ISALPHA (constraint[j]))
1358 error ("invalid punctuation `%c' in constraint", constraint[j]);
1359 return false;
1361 if (REG_CLASS_FROM_CONSTRAINT (constraint[j], constraint + j)
1362 != NO_REGS)
1363 *allows_reg = true;
1364 #ifdef EXTRA_CONSTRAINT_STR
1365 else if (EXTRA_ADDRESS_CONSTRAINT (constraint[j], constraint + j))
1366 *allows_reg = true;
1367 else if (EXTRA_MEMORY_CONSTRAINT (constraint[j], constraint + j))
1368 *allows_mem = true;
1369 else
1371 /* Otherwise we can't assume anything about the nature of
1372 the constraint except that it isn't purely registers.
1373 Treat it like "g" and hope for the best. */
1374 *allows_reg = true;
1375 *allows_mem = true;
1377 #endif
1378 break;
1381 return true;
1384 /* INPUT is one of the input operands from EXPR, an ASM_EXPR. Returns true
1385 if it is an operand which must be passed in memory (i.e. an "m"
1386 constraint), false otherwise. */
1388 bool
1389 asm_op_is_mem_input (tree input, tree expr)
1391 const char *constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (input)));
1392 tree outputs = ASM_OUTPUTS (expr);
1393 int noutputs = list_length (outputs);
1394 const char **constraints
1395 = (const char **) alloca ((noutputs) * sizeof (const char *));
1396 int i = 0;
1397 bool allows_mem, allows_reg;
1398 tree t;
1400 /* Collect output constraints. */
1401 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
1402 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1404 /* We pass 0 for input_num, ninputs and ninout; they are only used for
1405 error checking which will be done at expand time. */
1406 parse_input_constraint (&constraint, 0, 0, noutputs, 0, constraints,
1407 &allows_mem, &allows_reg);
1408 return (!allows_reg && allows_mem);
1411 /* Check for overlap between registers marked in CLOBBERED_REGS and
1412 anything inappropriate in DECL. Emit error and return TRUE for error,
1413 FALSE for ok. */
1415 static bool
1416 decl_conflicts_with_clobbers_p (tree decl, const HARD_REG_SET clobbered_regs)
1418 /* Conflicts between asm-declared register variables and the clobber
1419 list are not allowed. */
1420 if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
1421 && DECL_REGISTER (decl)
1422 && REG_P (DECL_RTL (decl))
1423 && REGNO (DECL_RTL (decl)) < FIRST_PSEUDO_REGISTER)
1425 rtx reg = DECL_RTL (decl);
1426 unsigned int regno;
1428 for (regno = REGNO (reg);
1429 regno < (REGNO (reg)
1430 + HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
1431 regno++)
1432 if (TEST_HARD_REG_BIT (clobbered_regs, regno))
1434 error ("asm-specifier for variable `%s' conflicts with asm clobber list",
1435 IDENTIFIER_POINTER (DECL_NAME (decl)));
1437 /* Reset registerness to stop multiple errors emitted for a
1438 single variable. */
1439 DECL_REGISTER (decl) = 0;
1440 return true;
1443 return false;
1446 /* Generate RTL for an asm statement with arguments.
1447 STRING is the instruction template.
1448 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
1449 Each output or input has an expression in the TREE_VALUE and
1450 and a tree list in TREE_PURPOSE which in turn contains a constraint
1451 name in TREE_VALUE (or NULL_TREE) and a constraint string
1452 in TREE_PURPOSE.
1453 CLOBBERS is a list of STRING_CST nodes each naming a hard register
1454 that is clobbered by this insn.
1456 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
1457 Some elements of OUTPUTS may be replaced with trees representing temporary
1458 values. The caller should copy those temporary values to the originally
1459 specified lvalues.
1461 VOL nonzero means the insn is volatile; don't optimize it. */
1463 void
1464 expand_asm_operands (tree string, tree outputs, tree inputs,
1465 tree clobbers, int vol, const char *filename, int line)
1467 rtvec argvec, constraintvec;
1468 rtx body;
1469 int ninputs = list_length (inputs);
1470 int noutputs = list_length (outputs);
1471 int ninout;
1472 int nclobbers;
1473 HARD_REG_SET clobbered_regs;
1474 int clobber_conflict_found = 0;
1475 tree tail;
1476 tree t;
1477 int i;
1478 /* Vector of RTX's of evaluated output operands. */
1479 rtx *output_rtx = alloca (noutputs * sizeof (rtx));
1480 int *inout_opnum = alloca (noutputs * sizeof (int));
1481 rtx *real_output_rtx = alloca (noutputs * sizeof (rtx));
1482 enum machine_mode *inout_mode
1483 = alloca (noutputs * sizeof (enum machine_mode));
1484 const char **constraints
1485 = alloca ((noutputs + ninputs) * sizeof (const char *));
1486 int old_generating_concat_p = generating_concat_p;
1488 /* An ASM with no outputs needs to be treated as volatile, for now. */
1489 if (noutputs == 0)
1490 vol = 1;
1492 if (! check_operand_nalternatives (outputs, inputs))
1493 return;
1495 if (! check_unique_operand_names (outputs, inputs))
1496 return;
1498 string = resolve_asm_operand_names (string, outputs, inputs);
1500 /* Collect constraints. */
1501 i = 0;
1502 for (t = outputs; t ; t = TREE_CHAIN (t), i++)
1503 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1504 for (t = inputs; t ; t = TREE_CHAIN (t), i++)
1505 constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1507 #ifdef MD_ASM_CLOBBERS
1508 /* Sometimes we wish to automatically clobber registers across an asm.
1509 Case in point is when the i386 backend moved from cc0 to a hard reg --
1510 maintaining source-level compatibility means automatically clobbering
1511 the flags register. */
1512 MD_ASM_CLOBBERS (clobbers);
1513 #endif
1515 /* Count the number of meaningful clobbered registers, ignoring what
1516 we would ignore later. */
1517 nclobbers = 0;
1518 CLEAR_HARD_REG_SET (clobbered_regs);
1519 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1521 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1523 i = decode_reg_name (regname);
1524 if (i >= 0 || i == -4)
1525 ++nclobbers;
1526 else if (i == -2)
1527 error ("unknown register name `%s' in `asm'", regname);
1529 /* Mark clobbered registers. */
1530 if (i >= 0)
1532 /* Clobbering the PIC register is an error */
1533 if (i == (int) PIC_OFFSET_TABLE_REGNUM)
1535 error ("PIC register `%s' clobbered in `asm'", regname);
1536 return;
1539 SET_HARD_REG_BIT (clobbered_regs, i);
1543 clear_last_expr ();
1545 /* First pass over inputs and outputs checks validity and sets
1546 mark_addressable if needed. */
1548 ninout = 0;
1549 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1551 tree val = TREE_VALUE (tail);
1552 tree type = TREE_TYPE (val);
1553 const char *constraint;
1554 bool is_inout;
1555 bool allows_reg;
1556 bool allows_mem;
1558 /* If there's an erroneous arg, emit no insn. */
1559 if (type == error_mark_node)
1560 return;
1562 /* Try to parse the output constraint. If that fails, there's
1563 no point in going further. */
1564 constraint = constraints[i];
1565 if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
1566 &allows_mem, &allows_reg, &is_inout))
1567 return;
1569 if (! allows_reg
1570 && (allows_mem
1571 || is_inout
1572 || (DECL_P (val)
1573 && GET_CODE (DECL_RTL (val)) == REG
1574 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
1575 (*lang_hooks.mark_addressable) (val);
1577 if (is_inout)
1578 ninout++;
1581 ninputs += ninout;
1582 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1584 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1585 return;
1588 for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
1590 bool allows_reg, allows_mem;
1591 const char *constraint;
1593 /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
1594 would get VOIDmode and that could cause a crash in reload. */
1595 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1596 return;
1598 constraint = constraints[i + noutputs];
1599 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1600 constraints, &allows_mem, &allows_reg))
1601 return;
1603 if (! allows_reg && allows_mem)
1604 (*lang_hooks.mark_addressable) (TREE_VALUE (tail));
1607 /* Second pass evaluates arguments. */
1609 ninout = 0;
1610 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1612 tree val = TREE_VALUE (tail);
1613 tree type = TREE_TYPE (val);
1614 bool is_inout;
1615 bool allows_reg;
1616 bool allows_mem;
1617 rtx op;
1619 if (!parse_output_constraint (&constraints[i], i, ninputs,
1620 noutputs, &allows_mem, &allows_reg,
1621 &is_inout))
1622 abort ();
1624 /* If an output operand is not a decl or indirect ref and our constraint
1625 allows a register, make a temporary to act as an intermediate.
1626 Make the asm insn write into that, then our caller will copy it to
1627 the real output operand. Likewise for promoted variables. */
1629 generating_concat_p = 0;
1631 real_output_rtx[i] = NULL_RTX;
1632 if ((TREE_CODE (val) == INDIRECT_REF
1633 && allows_mem)
1634 || (DECL_P (val)
1635 && (allows_mem || GET_CODE (DECL_RTL (val)) == REG)
1636 && ! (GET_CODE (DECL_RTL (val)) == REG
1637 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
1638 || ! allows_reg
1639 || is_inout)
1641 op = expand_expr (val, NULL_RTX, VOIDmode, EXPAND_WRITE);
1642 if (GET_CODE (op) == MEM)
1643 op = validize_mem (op);
1645 if (! allows_reg && GET_CODE (op) != MEM)
1646 error ("output number %d not directly addressable", i);
1647 if ((! allows_mem && GET_CODE (op) == MEM)
1648 || GET_CODE (op) == CONCAT)
1650 real_output_rtx[i] = protect_from_queue (op, 1);
1651 op = gen_reg_rtx (GET_MODE (op));
1652 if (is_inout)
1653 emit_move_insn (op, real_output_rtx[i]);
1656 else
1658 op = assign_temp (type, 0, 0, 1);
1659 op = validize_mem (op);
1660 TREE_VALUE (tail) = make_tree (type, op);
1662 output_rtx[i] = op;
1664 generating_concat_p = old_generating_concat_p;
1666 if (is_inout)
1668 inout_mode[ninout] = TYPE_MODE (type);
1669 inout_opnum[ninout++] = i;
1672 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1673 clobber_conflict_found = 1;
1676 /* Make vectors for the expression-rtx, constraint strings,
1677 and named operands. */
1679 argvec = rtvec_alloc (ninputs);
1680 constraintvec = rtvec_alloc (ninputs);
1682 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
1683 : GET_MODE (output_rtx[0])),
1684 TREE_STRING_POINTER (string),
1685 empty_string, 0, argvec, constraintvec,
1686 filename, line);
1688 MEM_VOLATILE_P (body) = vol;
1690 /* Eval the inputs and put them into ARGVEC.
1691 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1693 for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
1695 bool allows_reg, allows_mem;
1696 const char *constraint;
1697 tree val, type;
1698 rtx op;
1700 constraint = constraints[i + noutputs];
1701 if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
1702 constraints, &allows_mem, &allows_reg))
1703 abort ();
1705 generating_concat_p = 0;
1707 val = TREE_VALUE (tail);
1708 type = TREE_TYPE (val);
1709 op = expand_expr (val, NULL_RTX, VOIDmode,
1710 (allows_mem && !allows_reg
1711 ? EXPAND_MEMORY : EXPAND_NORMAL));
1713 /* Never pass a CONCAT to an ASM. */
1714 if (GET_CODE (op) == CONCAT)
1715 op = force_reg (GET_MODE (op), op);
1716 else if (GET_CODE (op) == MEM)
1717 op = validize_mem (op);
1719 if (asm_operand_ok (op, constraint) <= 0)
1721 if (allows_reg)
1722 op = force_reg (TYPE_MODE (type), op);
1723 else if (!allows_mem)
1724 warning ("asm operand %d probably doesn't match constraints",
1725 i + noutputs);
1726 else if (GET_CODE (op) == MEM)
1728 /* We won't recognize either volatile memory or memory
1729 with a queued address as available a memory_operand
1730 at this point. Ignore it: clearly this *is* a memory. */
1732 else
1734 warning ("use of memory input without lvalue in "
1735 "asm operand %d is deprecated", i + noutputs);
1737 if (CONSTANT_P (op))
1739 op = force_const_mem (TYPE_MODE (type), op);
1740 op = validize_mem (op);
1742 else if (GET_CODE (op) == REG
1743 || GET_CODE (op) == SUBREG
1744 || GET_CODE (op) == ADDRESSOF
1745 || GET_CODE (op) == CONCAT)
1747 tree qual_type = build_qualified_type (type,
1748 (TYPE_QUALS (type)
1749 | TYPE_QUAL_CONST));
1750 rtx memloc = assign_temp (qual_type, 1, 1, 1);
1751 memloc = validize_mem (memloc);
1752 emit_move_insn (memloc, op);
1753 op = memloc;
1758 generating_concat_p = old_generating_concat_p;
1759 ASM_OPERANDS_INPUT (body, i) = op;
1761 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
1762 = gen_rtx_ASM_INPUT (TYPE_MODE (type), constraints[i + noutputs]);
1764 if (decl_conflicts_with_clobbers_p (val, clobbered_regs))
1765 clobber_conflict_found = 1;
1768 /* Protect all the operands from the queue now that they have all been
1769 evaluated. */
1771 generating_concat_p = 0;
1773 for (i = 0; i < ninputs - ninout; i++)
1774 ASM_OPERANDS_INPUT (body, i)
1775 = protect_from_queue (ASM_OPERANDS_INPUT (body, i), 0);
1777 for (i = 0; i < noutputs; i++)
1778 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1780 /* For in-out operands, copy output rtx to input rtx. */
1781 for (i = 0; i < ninout; i++)
1783 int j = inout_opnum[i];
1784 char buffer[16];
1786 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
1787 = output_rtx[j];
1789 sprintf (buffer, "%d", j);
1790 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
1791 = gen_rtx_ASM_INPUT (inout_mode[i], ggc_strdup (buffer));
1794 generating_concat_p = old_generating_concat_p;
1796 /* Now, for each output, construct an rtx
1797 (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
1798 ARGVEC CONSTRAINTS OPNAMES))
1799 If there is more than one, put them inside a PARALLEL. */
1801 if (noutputs == 1 && nclobbers == 0)
1803 ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = constraints[0];
1804 emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
1807 else if (noutputs == 0 && nclobbers == 0)
1809 /* No output operands: put in a raw ASM_OPERANDS rtx. */
1810 emit_insn (body);
1813 else
1815 rtx obody = body;
1816 int num = noutputs;
1818 if (num == 0)
1819 num = 1;
1821 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
1823 /* For each output operand, store a SET. */
1824 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1826 XVECEXP (body, 0, i)
1827 = gen_rtx_SET (VOIDmode,
1828 output_rtx[i],
1829 gen_rtx_ASM_OPERANDS
1830 (GET_MODE (output_rtx[i]),
1831 TREE_STRING_POINTER (string),
1832 constraints[i], i, argvec, constraintvec,
1833 filename, line));
1835 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1838 /* If there are no outputs (but there are some clobbers)
1839 store the bare ASM_OPERANDS into the PARALLEL. */
1841 if (i == 0)
1842 XVECEXP (body, 0, i++) = obody;
1844 /* Store (clobber REG) for each clobbered register specified. */
1846 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1848 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1849 int j = decode_reg_name (regname);
1850 rtx clobbered_reg;
1852 if (j < 0)
1854 if (j == -3) /* `cc', which is not a register */
1855 continue;
1857 if (j == -4) /* `memory', don't cache memory across asm */
1859 XVECEXP (body, 0, i++)
1860 = gen_rtx_CLOBBER (VOIDmode,
1861 gen_rtx_MEM
1862 (BLKmode,
1863 gen_rtx_SCRATCH (VOIDmode)));
1864 continue;
1867 /* Ignore unknown register, error already signaled. */
1868 continue;
1871 /* Use QImode since that's guaranteed to clobber just one reg. */
1872 clobbered_reg = gen_rtx_REG (QImode, j);
1874 /* Do sanity check for overlap between clobbers and respectively
1875 input and outputs that hasn't been handled. Such overlap
1876 should have been detected and reported above. */
1877 if (!clobber_conflict_found)
1879 int opno;
1881 /* We test the old body (obody) contents to avoid tripping
1882 over the under-construction body. */
1883 for (opno = 0; opno < noutputs; opno++)
1884 if (reg_overlap_mentioned_p (clobbered_reg, output_rtx[opno]))
1885 internal_error ("asm clobber conflict with output operand");
1887 for (opno = 0; opno < ninputs - ninout; opno++)
1888 if (reg_overlap_mentioned_p (clobbered_reg,
1889 ASM_OPERANDS_INPUT (obody, opno)))
1890 internal_error ("asm clobber conflict with input operand");
1893 XVECEXP (body, 0, i++)
1894 = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
1897 emit_insn (body);
1900 /* For any outputs that needed reloading into registers, spill them
1901 back to where they belong. */
1902 for (i = 0; i < noutputs; ++i)
1903 if (real_output_rtx[i])
1904 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1906 free_temp_slots ();
1909 void
1910 expand_asm_expr (tree exp)
1912 int noutputs, i;
1913 tree outputs, tail;
1914 tree *o;
1916 if (ASM_INPUT_P (exp))
1918 expand_asm (ASM_STRING (exp), ASM_VOLATILE_P (exp));
1919 return;
1922 outputs = ASM_OUTPUTS (exp);
1923 noutputs = list_length (outputs);
1924 /* o[I] is the place that output number I should be written. */
1925 o = (tree *) alloca (noutputs * sizeof (tree));
1927 /* Record the contents of OUTPUTS before it is modified. */
1928 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1929 o[i] = TREE_VALUE (tail);
1931 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
1932 OUTPUTS some trees for where the values were actually stored. */
1933 expand_asm_operands (ASM_STRING (exp), outputs, ASM_INPUTS (exp),
1934 ASM_CLOBBERS (exp), ASM_VOLATILE_P (exp),
1935 input_filename, input_line);
1937 /* Copy all the intermediate outputs into the specified outputs. */
1938 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1940 if (o[i] != TREE_VALUE (tail))
1942 expand_assignment (o[i], TREE_VALUE (tail), 0);
1943 free_temp_slots ();
1945 /* Restore the original value so that it's correct the next
1946 time we expand this function. */
1947 TREE_VALUE (tail) = o[i];
1951 /* Those MODIFY_EXPRs could do autoincrements. */
1952 emit_queue ();
1955 /* A subroutine of expand_asm_operands. Check that all operands have
1956 the same number of alternatives. Return true if so. */
1958 static bool
1959 check_operand_nalternatives (tree outputs, tree inputs)
1961 if (outputs || inputs)
1963 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1964 int nalternatives
1965 = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
1966 tree next = inputs;
1968 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1970 error ("too many alternatives in `asm'");
1971 return false;
1974 tmp = outputs;
1975 while (tmp)
1977 const char *constraint
1978 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
1980 if (n_occurrences (',', constraint) != nalternatives)
1982 error ("operand constraints for `asm' differ in number of alternatives");
1983 return false;
1986 if (TREE_CHAIN (tmp))
1987 tmp = TREE_CHAIN (tmp);
1988 else
1989 tmp = next, next = 0;
1993 return true;
1996 /* A subroutine of expand_asm_operands. Check that all operand names
1997 are unique. Return true if so. We rely on the fact that these names
1998 are identifiers, and so have been canonicalized by get_identifier,
1999 so all we need are pointer comparisons. */
2001 static bool
2002 check_unique_operand_names (tree outputs, tree inputs)
2004 tree i, j;
2006 for (i = outputs; i ; i = TREE_CHAIN (i))
2008 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
2009 if (! i_name)
2010 continue;
2012 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
2013 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2014 goto failure;
2017 for (i = inputs; i ; i = TREE_CHAIN (i))
2019 tree i_name = TREE_PURPOSE (TREE_PURPOSE (i));
2020 if (! i_name)
2021 continue;
2023 for (j = TREE_CHAIN (i); j ; j = TREE_CHAIN (j))
2024 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2025 goto failure;
2026 for (j = outputs; j ; j = TREE_CHAIN (j))
2027 if (simple_cst_equal (i_name, TREE_PURPOSE (TREE_PURPOSE (j))))
2028 goto failure;
2031 return true;
2033 failure:
2034 error ("duplicate asm operand name '%s'",
2035 TREE_STRING_POINTER (TREE_PURPOSE (TREE_PURPOSE (i))));
2036 return false;
2039 /* A subroutine of expand_asm_operands. Resolve the names of the operands
2040 in *POUTPUTS and *PINPUTS to numbers, and replace the name expansions in
2041 STRING and in the constraints to those numbers. */
2043 tree
2044 resolve_asm_operand_names (tree string, tree outputs, tree inputs)
2046 char *buffer;
2047 char *p;
2048 const char *c;
2049 tree t;
2051 /* Substitute [<name>] in input constraint strings. There should be no
2052 named operands in output constraints. */
2053 for (t = inputs; t ; t = TREE_CHAIN (t))
2055 c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2056 if (strchr (c, '[') != NULL)
2058 p = buffer = xstrdup (c);
2059 while ((p = strchr (p, '[')) != NULL)
2060 p = resolve_operand_name_1 (p, outputs, inputs);
2061 TREE_VALUE (TREE_PURPOSE (t))
2062 = build_string (strlen (buffer), buffer);
2063 free (buffer);
2067 /* Now check for any needed substitutions in the template. */
2068 c = TREE_STRING_POINTER (string);
2069 while ((c = strchr (c, '%')) != NULL)
2071 if (c[1] == '[')
2072 break;
2073 else if (ISALPHA (c[1]) && c[2] == '[')
2074 break;
2075 else
2077 c += 1;
2078 continue;
2082 if (c)
2084 /* OK, we need to make a copy so we can perform the substitutions.
2085 Assume that we will not need extra space--we get to remove '['
2086 and ']', which means we cannot have a problem until we have more
2087 than 999 operands. */
2088 buffer = xstrdup (TREE_STRING_POINTER (string));
2089 p = buffer + (c - TREE_STRING_POINTER (string));
2091 while ((p = strchr (p, '%')) != NULL)
2093 if (p[1] == '[')
2094 p += 1;
2095 else if (ISALPHA (p[1]) && p[2] == '[')
2096 p += 2;
2097 else
2099 p += 1;
2100 continue;
2103 p = resolve_operand_name_1 (p, outputs, inputs);
2106 string = build_string (strlen (buffer), buffer);
2107 free (buffer);
2110 return string;
2113 /* A subroutine of resolve_operand_names. P points to the '[' for a
2114 potential named operand of the form [<name>]. In place, replace
2115 the name and brackets with a number. Return a pointer to the
2116 balance of the string after substitution. */
2118 static char *
2119 resolve_operand_name_1 (char *p, tree outputs, tree inputs)
2121 char *q;
2122 int op;
2123 tree t;
2124 size_t len;
2126 /* Collect the operand name. */
2127 q = strchr (p, ']');
2128 if (!q)
2130 error ("missing close brace for named operand");
2131 return strchr (p, '\0');
2133 len = q - p - 1;
2135 /* Resolve the name to a number. */
2136 for (op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
2138 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
2139 if (name)
2141 const char *c = TREE_STRING_POINTER (name);
2142 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
2143 goto found;
2146 for (t = inputs; t ; t = TREE_CHAIN (t), op++)
2148 tree name = TREE_PURPOSE (TREE_PURPOSE (t));
2149 if (name)
2151 const char *c = TREE_STRING_POINTER (name);
2152 if (strncmp (c, p + 1, len) == 0 && c[len] == '\0')
2153 goto found;
2157 *q = '\0';
2158 error ("undefined named operand '%s'", p + 1);
2159 op = 0;
2160 found:
2162 /* Replace the name with the number. Unfortunately, not all libraries
2163 get the return value of sprintf correct, so search for the end of the
2164 generated string by hand. */
2165 sprintf (p, "%d", op);
2166 p = strchr (p, '\0');
2168 /* Verify the no extra buffer space assumption. */
2169 if (p > q)
2170 abort ();
2172 /* Shift the rest of the buffer down to fill the gap. */
2173 memmove (p, q + 1, strlen (q + 1) + 1);
2175 return p;
2178 /* Generate RTL to evaluate the expression EXP
2179 and remember it in case this is the VALUE in a ({... VALUE; }) constr.
2180 Provided just for backward-compatibility. expand_expr_stmt_value()
2181 should be used for new code. */
2183 void
2184 expand_expr_stmt (tree exp)
2186 expand_expr_stmt_value (exp, -1, 1);
2189 /* Generate RTL to evaluate the expression EXP. WANT_VALUE tells
2190 whether to (1) save the value of the expression, (0) discard it or
2191 (-1) use expr_stmts_for_value to tell. The use of -1 is
2192 deprecated, and retained only for backward compatibility. */
2194 void
2195 expand_expr_stmt_value (tree exp, int want_value, int maybe_last)
2197 rtx value;
2198 tree type;
2200 if (want_value == -1)
2201 want_value = expr_stmts_for_value != 0;
2203 /* If -Wextra, warn about statements with no side effects,
2204 except for an explicit cast to void (e.g. for assert()), and
2205 except for last statement in ({...}) where they may be useful. */
2206 if (! want_value
2207 && (expr_stmts_for_value == 0 || ! maybe_last)
2208 && exp != error_mark_node
2209 && warn_unused_value)
2211 if (TREE_SIDE_EFFECTS (exp))
2212 warn_if_unused_value (exp);
2213 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
2214 warning ("%Hstatement with no effect", &emit_locus);
2217 /* If EXP is of function type and we are expanding statements for
2218 value, convert it to pointer-to-function. */
2219 if (want_value && TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE)
2220 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
2222 /* The call to `expand_expr' could cause last_expr_type and
2223 last_expr_value to get reset. Therefore, we set last_expr_value
2224 and last_expr_type *after* calling expand_expr. */
2225 value = expand_expr (exp, want_value ? NULL_RTX : const0_rtx,
2226 VOIDmode, 0);
2227 type = TREE_TYPE (exp);
2229 /* If all we do is reference a volatile value in memory,
2230 copy it to a register to be sure it is actually touched. */
2231 if (value && GET_CODE (value) == MEM && TREE_THIS_VOLATILE (exp))
2233 if (TYPE_MODE (type) == VOIDmode)
2235 else if (TYPE_MODE (type) != BLKmode)
2236 value = copy_to_reg (value);
2237 else
2239 rtx lab = gen_label_rtx ();
2241 /* Compare the value with itself to reference it. */
2242 emit_cmp_and_jump_insns (value, value, EQ,
2243 expand_expr (TYPE_SIZE (type),
2244 NULL_RTX, VOIDmode, 0),
2245 BLKmode, 0, lab);
2246 emit_label (lab);
2250 /* If this expression is part of a ({...}) and is in memory, we may have
2251 to preserve temporaries. */
2252 preserve_temp_slots (value);
2254 /* Free any temporaries used to evaluate this expression. Any temporary
2255 used as a result of this expression will already have been preserved
2256 above. */
2257 free_temp_slots ();
2259 if (want_value)
2261 last_expr_value = value;
2262 last_expr_type = type;
2265 emit_queue ();
2268 /* Warn if EXP contains any computations whose results are not used.
2269 Return 1 if a warning is printed; 0 otherwise. */
2272 warn_if_unused_value (tree exp)
2274 if (TREE_USED (exp))
2275 return 0;
2277 /* Don't warn about void constructs. This includes casting to void,
2278 void function calls, and statement expressions with a final cast
2279 to void. */
2280 if (VOID_TYPE_P (TREE_TYPE (exp)))
2281 return 0;
2283 switch (TREE_CODE (exp))
2285 case PREINCREMENT_EXPR:
2286 case POSTINCREMENT_EXPR:
2287 case PREDECREMENT_EXPR:
2288 case POSTDECREMENT_EXPR:
2289 case MODIFY_EXPR:
2290 case INIT_EXPR:
2291 case TARGET_EXPR:
2292 case CALL_EXPR:
2293 case RTL_EXPR:
2294 case TRY_CATCH_EXPR:
2295 case WITH_CLEANUP_EXPR:
2296 case EXIT_EXPR:
2297 return 0;
2299 case BIND_EXPR:
2300 /* For a binding, warn if no side effect within it. */
2301 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2303 case SAVE_EXPR:
2304 return warn_if_unused_value (TREE_OPERAND (exp, 0));
2306 case TRUTH_ORIF_EXPR:
2307 case TRUTH_ANDIF_EXPR:
2308 /* In && or ||, warn if 2nd operand has no side effect. */
2309 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2311 case COMPOUND_EXPR:
2312 if (TREE_NO_UNUSED_WARNING (exp))
2313 return 0;
2314 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
2315 return 1;
2316 /* Let people do `(foo (), 0)' without a warning. */
2317 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2318 return 0;
2319 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2321 case NOP_EXPR:
2322 case CONVERT_EXPR:
2323 case NON_LVALUE_EXPR:
2324 /* Don't warn about conversions not explicit in the user's program. */
2325 if (TREE_NO_UNUSED_WARNING (exp))
2326 return 0;
2327 /* Assignment to a cast usually results in a cast of a modify.
2328 Don't complain about that. There can be an arbitrary number of
2329 casts before the modify, so we must loop until we find the first
2330 non-cast expression and then test to see if that is a modify. */
2332 tree tem = TREE_OPERAND (exp, 0);
2334 while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
2335 tem = TREE_OPERAND (tem, 0);
2337 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
2338 || TREE_CODE (tem) == CALL_EXPR)
2339 return 0;
2341 goto maybe_warn;
2343 case INDIRECT_REF:
2344 /* Don't warn about automatic dereferencing of references, since
2345 the user cannot control it. */
2346 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2347 return warn_if_unused_value (TREE_OPERAND (exp, 0));
2348 /* Fall through. */
2350 default:
2351 /* Referencing a volatile value is a side effect, so don't warn. */
2352 if ((DECL_P (exp)
2353 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
2354 && TREE_THIS_VOLATILE (exp))
2355 return 0;
2357 /* If this is an expression which has no operands, there is no value
2358 to be unused. There are no such language-independent codes,
2359 but front ends may define such. */
2360 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'e'
2361 && TREE_CODE_LENGTH (TREE_CODE (exp)) == 0)
2362 return 0;
2364 maybe_warn:
2365 /* If this is an expression with side effects, don't warn. */
2366 if (TREE_SIDE_EFFECTS (exp))
2367 return 0;
2369 warning ("%Hvalue computed is not used", &emit_locus);
2370 return 1;
2374 /* Clear out the memory of the last expression evaluated. */
2376 void
2377 clear_last_expr (void)
2379 last_expr_type = NULL_TREE;
2380 last_expr_value = NULL_RTX;
2383 /* Begin a statement-expression, i.e., a series of statements which
2384 may return a value. Return the RTL_EXPR for this statement expr.
2385 The caller must save that value and pass it to
2386 expand_end_stmt_expr. If HAS_SCOPE is nonzero, temporaries created
2387 in the statement-expression are deallocated at the end of the
2388 expression. */
2390 tree
2391 expand_start_stmt_expr (int has_scope)
2393 tree t;
2395 /* Make the RTL_EXPR node temporary, not momentary,
2396 so that rtl_expr_chain doesn't become garbage. */
2397 t = make_node (RTL_EXPR);
2398 do_pending_stack_adjust ();
2399 if (has_scope)
2400 start_sequence_for_rtl_expr (t);
2401 else
2402 start_sequence ();
2403 NO_DEFER_POP;
2404 expr_stmts_for_value++;
2405 return t;
2408 /* Restore the previous state at the end of a statement that returns a value.
2409 Returns a tree node representing the statement's value and the
2410 insns to compute the value.
2412 The nodes of that expression have been freed by now, so we cannot use them.
2413 But we don't want to do that anyway; the expression has already been
2414 evaluated and now we just want to use the value. So generate a RTL_EXPR
2415 with the proper type and RTL value.
2417 If the last substatement was not an expression,
2418 return something with type `void'. */
2420 tree
2421 expand_end_stmt_expr (tree t)
2423 OK_DEFER_POP;
2425 if (! last_expr_value || ! last_expr_type)
2427 last_expr_value = const0_rtx;
2428 last_expr_type = void_type_node;
2430 else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
2431 /* Remove any possible QUEUED. */
2432 last_expr_value = protect_from_queue (last_expr_value, 0);
2434 emit_queue ();
2436 TREE_TYPE (t) = last_expr_type;
2437 RTL_EXPR_RTL (t) = last_expr_value;
2438 RTL_EXPR_SEQUENCE (t) = get_insns ();
2440 rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
2442 end_sequence ();
2444 /* Don't consider deleting this expr or containing exprs at tree level. */
2445 TREE_SIDE_EFFECTS (t) = 1;
2446 /* Propagate volatility of the actual RTL expr. */
2447 TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
2449 clear_last_expr ();
2450 expr_stmts_for_value--;
2452 return t;
2455 /* Generate RTL for the start of an if-then. COND is the expression
2456 whose truth should be tested.
2458 If EXITFLAG is nonzero, this conditional is visible to
2459 `exit_something'. */
2461 void
2462 expand_start_cond (tree cond, int exitflag)
2464 struct nesting *thiscond = ALLOC_NESTING ();
2466 /* Make an entry on cond_stack for the cond we are entering. */
2468 thiscond->desc = COND_NESTING;
2469 thiscond->next = cond_stack;
2470 thiscond->all = nesting_stack;
2471 thiscond->depth = ++nesting_depth;
2472 thiscond->data.cond.next_label = gen_label_rtx ();
2473 /* Before we encounter an `else', we don't need a separate exit label
2474 unless there are supposed to be exit statements
2475 to exit this conditional. */
2476 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
2477 thiscond->data.cond.endif_label = thiscond->exit_label;
2478 cond_stack = thiscond;
2479 nesting_stack = thiscond;
2481 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
2484 /* Generate RTL between then-clause and the elseif-clause
2485 of an if-then-elseif-.... */
2487 void
2488 expand_start_elseif (tree cond)
2490 if (cond_stack->data.cond.endif_label == 0)
2491 cond_stack->data.cond.endif_label = gen_label_rtx ();
2492 emit_jump (cond_stack->data.cond.endif_label);
2493 emit_label (cond_stack->data.cond.next_label);
2494 cond_stack->data.cond.next_label = gen_label_rtx ();
2495 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2498 /* Generate RTL between the then-clause and the else-clause
2499 of an if-then-else. */
2501 void
2502 expand_start_else (void)
2504 if (cond_stack->data.cond.endif_label == 0)
2505 cond_stack->data.cond.endif_label = gen_label_rtx ();
2507 emit_jump (cond_stack->data.cond.endif_label);
2508 emit_label (cond_stack->data.cond.next_label);
2509 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
2512 /* After calling expand_start_else, turn this "else" into an "else if"
2513 by providing another condition. */
2515 void
2516 expand_elseif (tree cond)
2518 cond_stack->data.cond.next_label = gen_label_rtx ();
2519 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2522 /* Generate RTL for the end of an if-then.
2523 Pop the record for it off of cond_stack. */
2525 void
2526 expand_end_cond (void)
2528 struct nesting *thiscond = cond_stack;
2530 do_pending_stack_adjust ();
2531 if (thiscond->data.cond.next_label)
2532 emit_label (thiscond->data.cond.next_label);
2533 if (thiscond->data.cond.endif_label)
2534 emit_label (thiscond->data.cond.endif_label);
2536 POPSTACK (cond_stack);
2537 clear_last_expr ();
2540 /* Generate RTL for the start of a loop. EXIT_FLAG is nonzero if this
2541 loop should be exited by `exit_something'. This is a loop for which
2542 `expand_continue' will jump to the top of the loop.
2544 Make an entry on loop_stack to record the labels associated with
2545 this loop. */
2547 struct nesting *
2548 expand_start_loop (int exit_flag)
2550 struct nesting *thisloop = ALLOC_NESTING ();
2552 /* Make an entry on loop_stack for the loop we are entering. */
2554 thisloop->desc = LOOP_NESTING;
2555 thisloop->next = loop_stack;
2556 thisloop->all = nesting_stack;
2557 thisloop->depth = ++nesting_depth;
2558 thisloop->data.loop.start_label = gen_label_rtx ();
2559 thisloop->data.loop.end_label = gen_label_rtx ();
2560 thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
2561 thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
2562 loop_stack = thisloop;
2563 nesting_stack = thisloop;
2565 do_pending_stack_adjust ();
2566 emit_queue ();
2567 emit_note (NOTE_INSN_LOOP_BEG);
2568 emit_label (thisloop->data.loop.start_label);
2570 return thisloop;
2573 /* Like expand_start_loop but for a loop where the continuation point
2574 (for expand_continue_loop) will be specified explicitly. */
2576 struct nesting *
2577 expand_start_loop_continue_elsewhere (int exit_flag)
2579 struct nesting *thisloop = expand_start_loop (exit_flag);
2580 loop_stack->data.loop.continue_label = gen_label_rtx ();
2581 return thisloop;
2584 /* Begin a null, aka do { } while (0) "loop". But since the contents
2585 of said loop can still contain a break, we must frob the loop nest. */
2587 struct nesting *
2588 expand_start_null_loop (void)
2590 struct nesting *thisloop = ALLOC_NESTING ();
2592 /* Make an entry on loop_stack for the loop we are entering. */
2594 thisloop->desc = LOOP_NESTING;
2595 thisloop->next = loop_stack;
2596 thisloop->all = nesting_stack;
2597 thisloop->depth = ++nesting_depth;
2598 thisloop->data.loop.start_label = emit_note (NOTE_INSN_DELETED);
2599 thisloop->data.loop.end_label = gen_label_rtx ();
2600 thisloop->data.loop.continue_label = thisloop->data.loop.end_label;
2601 thisloop->exit_label = thisloop->data.loop.end_label;
2602 loop_stack = thisloop;
2603 nesting_stack = thisloop;
2605 return thisloop;
2608 /* Specify the continuation point for a loop started with
2609 expand_start_loop_continue_elsewhere.
2610 Use this at the point in the code to which a continue statement
2611 should jump. */
2613 void
2614 expand_loop_continue_here (void)
2616 do_pending_stack_adjust ();
2617 emit_note (NOTE_INSN_LOOP_CONT);
2618 emit_label (loop_stack->data.loop.continue_label);
2621 /* Finish a loop. Generate a jump back to the top and the loop-exit label.
2622 Pop the block off of loop_stack. */
2624 void
2625 expand_end_loop (void)
2627 rtx start_label = loop_stack->data.loop.start_label;
2628 rtx etc_note;
2629 int eh_regions, debug_blocks;
2630 bool empty_test;
2632 /* Mark the continue-point at the top of the loop if none elsewhere. */
2633 if (start_label == loop_stack->data.loop.continue_label)
2634 emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
2636 do_pending_stack_adjust ();
2638 /* If the loop starts with a loop exit, roll that to the end where
2639 it will optimize together with the jump back.
2641 If the loop presently looks like this (in pseudo-C):
2643 LOOP_BEG
2644 start_label:
2645 if (test) goto end_label;
2646 LOOP_END_TOP_COND
2647 body;
2648 goto start_label;
2649 end_label:
2651 transform it to look like:
2653 LOOP_BEG
2654 goto start_label;
2655 top_label:
2656 body;
2657 start_label:
2658 if (test) goto end_label;
2659 goto top_label;
2660 end_label:
2662 We rely on the presence of NOTE_INSN_LOOP_END_TOP_COND to mark
2663 the end of the entry conditional. Without this, our lexical scan
2664 can't tell the difference between an entry conditional and a
2665 body conditional that exits the loop. Mistaking the two means
2666 that we can misplace the NOTE_INSN_LOOP_CONT note, which can
2667 screw up loop unrolling.
2669 Things will be oh so much better when loop optimization is done
2670 off of a proper control flow graph... */
2672 /* Scan insns from the top of the loop looking for the END_TOP_COND note. */
2674 empty_test = true;
2675 eh_regions = debug_blocks = 0;
2676 for (etc_note = start_label; etc_note ; etc_note = NEXT_INSN (etc_note))
2677 if (GET_CODE (etc_note) == NOTE)
2679 if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_LOOP_END_TOP_COND)
2680 break;
2682 /* We must not walk into a nested loop. */
2683 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_LOOP_BEG)
2685 etc_note = NULL_RTX;
2686 break;
2689 /* At the same time, scan for EH region notes, as we don't want
2690 to scrog region nesting. This shouldn't happen, but... */
2691 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_EH_REGION_BEG)
2692 eh_regions++;
2693 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_EH_REGION_END)
2695 if (--eh_regions < 0)
2696 /* We've come to the end of an EH region, but never saw the
2697 beginning of that region. That means that an EH region
2698 begins before the top of the loop, and ends in the middle
2699 of it. The existence of such a situation violates a basic
2700 assumption in this code, since that would imply that even
2701 when EH_REGIONS is zero, we might move code out of an
2702 exception region. */
2703 abort ();
2706 /* Likewise for debug scopes. In this case we'll either (1) move
2707 all of the notes if they are properly nested or (2) leave the
2708 notes alone and only rotate the loop at high optimization
2709 levels when we expect to scrog debug info. */
2710 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_BLOCK_BEG)
2711 debug_blocks++;
2712 else if (NOTE_LINE_NUMBER (etc_note) == NOTE_INSN_BLOCK_END)
2713 debug_blocks--;
2715 else if (INSN_P (etc_note))
2716 empty_test = false;
2718 if (etc_note
2719 && optimize
2720 && ! empty_test
2721 && eh_regions == 0
2722 && (debug_blocks == 0 || optimize >= 2)
2723 && NEXT_INSN (etc_note) != NULL_RTX
2724 && ! any_condjump_p (get_last_insn ()))
2726 /* We found one. Move everything from START to ETC to the end
2727 of the loop, and add a jump from the top of the loop. */
2728 rtx top_label = gen_label_rtx ();
2729 rtx start_move = start_label;
2731 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
2732 then we want to move this note also. */
2733 if (GET_CODE (PREV_INSN (start_move)) == NOTE
2734 && NOTE_LINE_NUMBER (PREV_INSN (start_move)) == NOTE_INSN_LOOP_CONT)
2735 start_move = PREV_INSN (start_move);
2737 emit_label_before (top_label, start_move);
2739 /* Actually move the insns. If the debug scopes are nested, we
2740 can move everything at once. Otherwise we have to move them
2741 one by one and squeeze out the block notes. */
2742 if (debug_blocks == 0)
2743 reorder_insns (start_move, etc_note, get_last_insn ());
2744 else
2746 rtx insn, next_insn;
2747 for (insn = start_move; insn; insn = next_insn)
2749 /* Figure out which insn comes after this one. We have
2750 to do this before we move INSN. */
2751 next_insn = (insn == etc_note ? NULL : NEXT_INSN (insn));
2753 if (GET_CODE (insn) == NOTE
2754 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2755 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2756 continue;
2758 reorder_insns (insn, insn, get_last_insn ());
2762 /* Add the jump from the top of the loop. */
2763 emit_jump_insn_before (gen_jump (start_label), top_label);
2764 emit_barrier_before (top_label);
2765 start_label = top_label;
2768 emit_jump (start_label);
2769 emit_note (NOTE_INSN_LOOP_END);
2770 emit_label (loop_stack->data.loop.end_label);
2772 POPSTACK (loop_stack);
2774 clear_last_expr ();
2777 /* Finish a null loop, aka do { } while (0). */
2779 void
2780 expand_end_null_loop (void)
2782 do_pending_stack_adjust ();
2783 emit_label (loop_stack->data.loop.end_label);
2785 POPSTACK (loop_stack);
2787 clear_last_expr ();
2790 /* Generate a jump to the current loop's continue-point.
2791 This is usually the top of the loop, but may be specified
2792 explicitly elsewhere. If not currently inside a loop,
2793 return 0 and do nothing; caller will print an error message. */
2796 expand_continue_loop (struct nesting *whichloop)
2798 /* Emit information for branch prediction. */
2799 rtx note;
2801 if (flag_guess_branch_prob)
2803 note = emit_note (NOTE_INSN_PREDICTION);
2804 NOTE_PREDICTION (note) = NOTE_PREDICT (PRED_CONTINUE, IS_TAKEN);
2806 clear_last_expr ();
2807 if (whichloop == 0)
2808 whichloop = loop_stack;
2809 if (whichloop == 0)
2810 return 0;
2811 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2812 NULL_RTX);
2813 return 1;
2816 /* Generate a jump to exit the current loop. If not currently inside a loop,
2817 return 0 and do nothing; caller will print an error message. */
2820 expand_exit_loop (struct nesting *whichloop)
2822 clear_last_expr ();
2823 if (whichloop == 0)
2824 whichloop = loop_stack;
2825 if (whichloop == 0)
2826 return 0;
2827 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
2828 return 1;
2831 /* Generate a conditional jump to exit the current loop if COND
2832 evaluates to zero. If not currently inside a loop,
2833 return 0 and do nothing; caller will print an error message. */
2836 expand_exit_loop_if_false (struct nesting *whichloop, tree cond)
2838 rtx label;
2839 clear_last_expr ();
2841 if (whichloop == 0)
2842 whichloop = loop_stack;
2843 if (whichloop == 0)
2844 return 0;
2846 if (integer_nonzerop (cond))
2847 return 1;
2848 if (integer_zerop (cond))
2849 return expand_exit_loop (whichloop);
2851 /* Check if we definitely won't need a fixup. */
2852 if (whichloop == nesting_stack)
2854 jumpifnot (cond, whichloop->data.loop.end_label);
2855 return 1;
2858 /* In order to handle fixups, we actually create a conditional jump
2859 around an unconditional branch to exit the loop. If fixups are
2860 necessary, they go before the unconditional branch. */
2862 label = gen_label_rtx ();
2863 jumpif (cond, label);
2864 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
2865 NULL_RTX);
2866 emit_label (label);
2868 return 1;
2871 /* Like expand_exit_loop_if_false except also emit a note marking
2872 the end of the conditional. Should only be used immediately
2873 after expand_loop_start. */
2876 expand_exit_loop_top_cond (struct nesting *whichloop, tree cond)
2878 if (! expand_exit_loop_if_false (whichloop, cond))
2879 return 0;
2881 emit_note (NOTE_INSN_LOOP_END_TOP_COND);
2882 return 1;
2885 /* Return nonzero if we should preserve sub-expressions as separate
2886 pseudos. We never do so if we aren't optimizing. We always do so
2887 if -fexpensive-optimizations.
2889 Otherwise, we only do so if we are in the "early" part of a loop. I.e.,
2890 the loop may still be a small one. */
2893 preserve_subexpressions_p (void)
2895 rtx insn;
2897 if (flag_expensive_optimizations)
2898 return 1;
2900 if (optimize == 0 || cfun == 0 || cfun->stmt == 0 || loop_stack == 0)
2901 return 0;
2903 insn = get_last_insn_anywhere ();
2905 return (insn
2906 && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2907 < n_non_fixed_regs * 3));
2911 /* Generate a jump to exit the current loop, conditional, binding contour
2912 or case statement. Not all such constructs are visible to this function,
2913 only those started with EXIT_FLAG nonzero. Individual languages use
2914 the EXIT_FLAG parameter to control which kinds of constructs you can
2915 exit this way.
2917 If not currently inside anything that can be exited,
2918 return 0 and do nothing; caller will print an error message. */
2921 expand_exit_something (void)
2923 struct nesting *n;
2924 clear_last_expr ();
2925 for (n = nesting_stack; n; n = n->all)
2926 if (n->exit_label != 0)
2928 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
2929 return 1;
2932 return 0;
2935 /* Generate RTL to return from the current function, with no value.
2936 (That is, we do not do anything about returning any value.) */
2938 void
2939 expand_null_return (void)
2941 rtx last_insn;
2943 last_insn = get_last_insn ();
2945 /* If this function was declared to return a value, but we
2946 didn't, clobber the return registers so that they are not
2947 propagated live to the rest of the function. */
2948 clobber_return_register ();
2950 expand_null_return_1 (last_insn);
2953 /* Try to guess whether the value of return means error code. */
2954 static enum br_predictor
2955 return_prediction (rtx val)
2957 /* Different heuristics for pointers and scalars. */
2958 if (POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl))))
2960 /* NULL is usually not returned. */
2961 if (val == const0_rtx)
2962 return PRED_NULL_RETURN;
2964 else
2966 /* Negative return values are often used to indicate
2967 errors. */
2968 if (GET_CODE (val) == CONST_INT
2969 && INTVAL (val) < 0)
2970 return PRED_NEGATIVE_RETURN;
2971 /* Constant return values are also usually erors,
2972 zero/one often mean booleans so exclude them from the
2973 heuristics. */
2974 if (CONSTANT_P (val)
2975 && (val != const0_rtx && val != const1_rtx))
2976 return PRED_CONST_RETURN;
2978 return PRED_NO_PREDICTION;
2981 /* Generate RTL to return from the current function, with value VAL. */
2983 static void
2984 expand_value_return (rtx val)
2986 rtx last_insn;
2987 rtx return_reg;
2988 enum br_predictor pred;
2990 if (flag_guess_branch_prob
2991 && (pred = return_prediction (val)) != PRED_NO_PREDICTION)
2993 /* Emit information for branch prediction. */
2994 rtx note;
2996 note = emit_note (NOTE_INSN_PREDICTION);
2998 NOTE_PREDICTION (note) = NOTE_PREDICT (pred, NOT_TAKEN);
3002 last_insn = get_last_insn ();
3003 return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
3005 /* Copy the value to the return location
3006 unless it's already there. */
3008 if (return_reg != val)
3010 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
3011 if (targetm.calls.promote_function_return (TREE_TYPE (current_function_decl)))
3013 int unsignedp = TREE_UNSIGNED (type);
3014 enum machine_mode old_mode
3015 = DECL_MODE (DECL_RESULT (current_function_decl));
3016 enum machine_mode mode
3017 = promote_mode (type, old_mode, &unsignedp, 1);
3019 if (mode != old_mode)
3020 val = convert_modes (mode, old_mode, val, unsignedp);
3022 if (GET_CODE (return_reg) == PARALLEL)
3023 emit_group_load (return_reg, val, type, int_size_in_bytes (type));
3024 else
3025 emit_move_insn (return_reg, val);
3028 expand_null_return_1 (last_insn);
3031 /* Output a return with no value. If LAST_INSN is nonzero,
3032 pretend that the return takes place after LAST_INSN. */
3034 static void
3035 expand_null_return_1 (rtx last_insn)
3037 rtx end_label = cleanup_label ? cleanup_label : return_label;
3039 clear_pending_stack_adjust ();
3040 do_pending_stack_adjust ();
3041 clear_last_expr ();
3043 if (end_label == 0)
3044 end_label = return_label = gen_label_rtx ();
3045 expand_goto_internal (NULL_TREE, end_label, last_insn);
3048 /* Generate RTL to evaluate the expression RETVAL and return it
3049 from the current function. */
3051 void
3052 expand_return (tree retval)
3054 /* If there are any cleanups to be performed, then they will
3055 be inserted following LAST_INSN. It is desirable
3056 that the last_insn, for such purposes, should be the
3057 last insn before computing the return value. Otherwise, cleanups
3058 which call functions can clobber the return value. */
3059 /* ??? rms: I think that is erroneous, because in C++ it would
3060 run destructors on variables that might be used in the subsequent
3061 computation of the return value. */
3062 rtx last_insn = 0;
3063 rtx result_rtl;
3064 rtx val = 0;
3065 tree retval_rhs;
3067 /* If function wants no value, give it none. */
3068 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
3070 expand_expr (retval, NULL_RTX, VOIDmode, 0);
3071 emit_queue ();
3072 expand_null_return ();
3073 return;
3076 if (retval == error_mark_node)
3078 /* Treat this like a return of no value from a function that
3079 returns a value. */
3080 expand_null_return ();
3081 return;
3083 else if (TREE_CODE (retval) == RESULT_DECL)
3084 retval_rhs = retval;
3085 else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
3086 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
3087 retval_rhs = TREE_OPERAND (retval, 1);
3088 else
3089 retval_rhs = retval;
3091 last_insn = get_last_insn ();
3093 /* Distribute return down conditional expr if either of the sides
3094 may involve tail recursion (see test below). This enhances the number
3095 of tail recursions we see. Don't do this always since it can produce
3096 sub-optimal code in some cases and we distribute assignments into
3097 conditional expressions when it would help. */
3099 if (optimize && retval_rhs != 0
3100 && frame_offset == 0
3101 && TREE_CODE (retval_rhs) == COND_EXPR
3102 && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
3103 || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
3105 rtx label = gen_label_rtx ();
3106 tree expr;
3108 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
3109 start_cleanup_deferral ();
3110 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
3111 DECL_RESULT (current_function_decl),
3112 TREE_OPERAND (retval_rhs, 1));
3113 TREE_SIDE_EFFECTS (expr) = 1;
3114 expand_return (expr);
3115 emit_label (label);
3117 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
3118 DECL_RESULT (current_function_decl),
3119 TREE_OPERAND (retval_rhs, 2));
3120 TREE_SIDE_EFFECTS (expr) = 1;
3121 expand_return (expr);
3122 end_cleanup_deferral ();
3123 return;
3126 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3128 /* If the result is an aggregate that is being returned in one (or more)
3129 registers, load the registers here. The compiler currently can't handle
3130 copying a BLKmode value into registers. We could put this code in a
3131 more general area (for use by everyone instead of just function
3132 call/return), but until this feature is generally usable it is kept here
3133 (and in expand_call). The value must go into a pseudo in case there
3134 are cleanups that will clobber the real return register. */
3136 if (retval_rhs != 0
3137 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3138 && GET_CODE (result_rtl) == REG)
3140 int i;
3141 unsigned HOST_WIDE_INT bitpos, xbitpos;
3142 unsigned HOST_WIDE_INT big_endian_correction = 0;
3143 unsigned HOST_WIDE_INT bytes
3144 = int_size_in_bytes (TREE_TYPE (retval_rhs));
3145 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3146 unsigned int bitsize
3147 = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)), BITS_PER_WORD);
3148 rtx *result_pseudos = alloca (sizeof (rtx) * n_regs);
3149 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
3150 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
3151 enum machine_mode tmpmode, result_reg_mode;
3153 if (bytes == 0)
3155 expand_null_return ();
3156 return;
3159 /* Structures whose size is not a multiple of a word are aligned
3160 to the least significant byte (to the right). On a BYTES_BIG_ENDIAN
3161 machine, this means we must skip the empty high order bytes when
3162 calculating the bit offset. */
3163 if (BYTES_BIG_ENDIAN
3164 && bytes % UNITS_PER_WORD)
3165 big_endian_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3166 * BITS_PER_UNIT));
3168 /* Copy the structure BITSIZE bits at a time. */
3169 for (bitpos = 0, xbitpos = big_endian_correction;
3170 bitpos < bytes * BITS_PER_UNIT;
3171 bitpos += bitsize, xbitpos += bitsize)
3173 /* We need a new destination pseudo each time xbitpos is
3174 on a word boundary and when xbitpos == big_endian_correction
3175 (the first time through). */
3176 if (xbitpos % BITS_PER_WORD == 0
3177 || xbitpos == big_endian_correction)
3179 /* Generate an appropriate register. */
3180 dst = gen_reg_rtx (word_mode);
3181 result_pseudos[xbitpos / BITS_PER_WORD] = dst;
3183 /* Clear the destination before we move anything into it. */
3184 emit_move_insn (dst, CONST0_RTX (GET_MODE (dst)));
3187 /* We need a new source operand each time bitpos is on a word
3188 boundary. */
3189 if (bitpos % BITS_PER_WORD == 0)
3190 src = operand_subword_force (result_val,
3191 bitpos / BITS_PER_WORD,
3192 BLKmode);
3194 /* Use bitpos for the source extraction (left justified) and
3195 xbitpos for the destination store (right justified). */
3196 store_bit_field (dst, bitsize, xbitpos % BITS_PER_WORD, word_mode,
3197 extract_bit_field (src, bitsize,
3198 bitpos % BITS_PER_WORD, 1,
3199 NULL_RTX, word_mode, word_mode,
3200 BITS_PER_WORD),
3201 BITS_PER_WORD);
3204 /* Find the smallest integer mode large enough to hold the
3205 entire structure and use that mode instead of BLKmode
3206 on the USE insn for the return register. */
3207 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3208 tmpmode != VOIDmode;
3209 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
3210 /* Have we found a large enough mode? */
3211 if (GET_MODE_SIZE (tmpmode) >= bytes)
3212 break;
3214 /* No suitable mode found. */
3215 if (tmpmode == VOIDmode)
3216 abort ();
3218 PUT_MODE (result_rtl, tmpmode);
3220 if (GET_MODE_SIZE (tmpmode) < GET_MODE_SIZE (word_mode))
3221 result_reg_mode = word_mode;
3222 else
3223 result_reg_mode = tmpmode;
3224 result_reg = gen_reg_rtx (result_reg_mode);
3226 emit_queue ();
3227 for (i = 0; i < n_regs; i++)
3228 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3229 result_pseudos[i]);
3231 if (tmpmode != result_reg_mode)
3232 result_reg = gen_lowpart (tmpmode, result_reg);
3234 expand_value_return (result_reg);
3236 else if (retval_rhs != 0
3237 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3238 && (GET_CODE (result_rtl) == REG
3239 || (GET_CODE (result_rtl) == PARALLEL)))
3241 /* Calculate the return value into a temporary (usually a pseudo
3242 reg). */
3243 tree ot = TREE_TYPE (DECL_RESULT (current_function_decl));
3244 tree nt = build_qualified_type (ot, TYPE_QUALS (ot) | TYPE_QUAL_CONST);
3246 val = assign_temp (nt, 0, 0, 1);
3247 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
3248 val = force_not_mem (val);
3249 emit_queue ();
3250 /* Return the calculated value, doing cleanups first. */
3251 expand_value_return (val);
3253 else
3255 /* No cleanups or no hard reg used;
3256 calculate value into hard return reg. */
3257 expand_expr (retval, const0_rtx, VOIDmode, 0);
3258 emit_queue ();
3259 expand_value_return (result_rtl);
3263 /* Attempt to optimize a potential tail recursion call into a goto.
3264 ARGUMENTS are the arguments to a CALL_EXPR; LAST_INSN indicates
3265 where to place the jump to the tail recursion label.
3267 Return TRUE if the call was optimized into a goto. */
3270 optimize_tail_recursion (tree arguments, rtx last_insn)
3272 /* Finish checking validity, and if valid emit code to set the
3273 argument variables for the new call. */
3274 if (tail_recursion_args (arguments, DECL_ARGUMENTS (current_function_decl)))
3276 if (tail_recursion_label == 0)
3278 tail_recursion_label = gen_label_rtx ();
3279 emit_label_after (tail_recursion_label,
3280 tail_recursion_reentry);
3282 emit_queue ();
3283 expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
3284 emit_barrier ();
3285 return 1;
3287 return 0;
3290 /* Emit code to alter this function's formal parms for a tail-recursive call.
3291 ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
3292 FORMALS is the chain of decls of formals.
3293 Return 1 if this can be done;
3294 otherwise return 0 and do not emit any code. */
3296 static int
3297 tail_recursion_args (tree actuals, tree formals)
3299 tree a = actuals, f = formals;
3300 int i;
3301 rtx *argvec;
3303 /* Check that number and types of actuals are compatible
3304 with the formals. This is not always true in valid C code.
3305 Also check that no formal needs to be addressable
3306 and that all formals are scalars. */
3308 /* Also count the args. */
3310 for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
3312 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (a)))
3313 != TYPE_MAIN_VARIANT (TREE_TYPE (f)))
3314 return 0;
3315 if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
3316 return 0;
3318 if (a != 0 || f != 0)
3319 return 0;
3321 /* Compute all the actuals. */
3323 argvec = alloca (i * sizeof (rtx));
3325 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3326 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
3328 /* Find which actual values refer to current values of previous formals.
3329 Copy each of them now, before any formal is changed. */
3331 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3333 int copy = 0;
3334 int j;
3335 for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
3336 if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
3338 copy = 1;
3339 break;
3341 if (copy)
3342 argvec[i] = copy_to_reg (argvec[i]);
3345 /* Store the values of the actuals into the formals. */
3347 for (f = formals, a = actuals, i = 0; f;
3348 f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
3350 if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
3351 emit_move_insn (DECL_RTL (f), argvec[i]);
3352 else
3354 rtx tmp = argvec[i];
3355 int unsignedp = TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a)));
3356 promote_mode(TREE_TYPE (TREE_VALUE (a)), GET_MODE (tmp),
3357 &unsignedp, 0);
3358 if (DECL_MODE (f) != GET_MODE (DECL_RTL (f)))
3360 tmp = gen_reg_rtx (DECL_MODE (f));
3361 convert_move (tmp, argvec[i], unsignedp);
3363 convert_move (DECL_RTL (f), tmp, unsignedp);
3367 free_temp_slots ();
3368 return 1;
3371 /* Generate the RTL code for entering a binding contour.
3372 The variables are declared one by one, by calls to `expand_decl'.
3374 FLAGS is a bitwise or of the following flags:
3376 1 - Nonzero if this construct should be visible to
3377 `exit_something'.
3379 2 - Nonzero if this contour does not require a
3380 NOTE_INSN_BLOCK_BEG note. Virtually all calls from
3381 language-independent code should set this flag because they
3382 will not create corresponding BLOCK nodes. (There should be
3383 a one-to-one correspondence between NOTE_INSN_BLOCK_BEG notes
3384 and BLOCKs.) If this flag is set, MARK_ENDS should be zero
3385 when expand_end_bindings is called.
3387 If we are creating a NOTE_INSN_BLOCK_BEG note, a BLOCK may
3388 optionally be supplied. If so, it becomes the NOTE_BLOCK for the
3389 note. */
3391 void
3392 expand_start_bindings_and_block (int flags, tree block)
3394 struct nesting *thisblock = ALLOC_NESTING ();
3395 rtx note;
3396 int exit_flag = ((flags & 1) != 0);
3397 int block_flag = ((flags & 2) == 0);
3399 /* If a BLOCK is supplied, then the caller should be requesting a
3400 NOTE_INSN_BLOCK_BEG note. */
3401 if (!block_flag && block)
3402 abort ();
3404 /* Create a note to mark the beginning of the block. */
3405 if (block_flag && !cfun->dont_emit_block_notes)
3407 note = emit_note (NOTE_INSN_BLOCK_BEG);
3408 NOTE_BLOCK (note) = block;
3410 else
3411 note = emit_note (NOTE_INSN_DELETED);
3413 /* Make an entry on block_stack for the block we are entering. */
3415 thisblock->desc = BLOCK_NESTING;
3416 thisblock->next = block_stack;
3417 thisblock->all = nesting_stack;
3418 thisblock->depth = ++nesting_depth;
3419 thisblock->data.block.stack_level = 0;
3420 thisblock->data.block.cleanups = 0;
3421 thisblock->data.block.exception_region = 0;
3422 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
3424 thisblock->data.block.conditional_code = 0;
3425 thisblock->data.block.last_unconditional_cleanup = note;
3426 /* When we insert instructions after the last unconditional cleanup,
3427 we don't adjust last_insn. That means that a later add_insn will
3428 clobber the instructions we've just added. The easiest way to
3429 fix this is to just insert another instruction here, so that the
3430 instructions inserted after the last unconditional cleanup are
3431 never the last instruction. */
3432 emit_note (NOTE_INSN_DELETED);
3434 if (block_stack
3435 && !(block_stack->data.block.cleanups == NULL_TREE
3436 && block_stack->data.block.outer_cleanups == NULL_TREE))
3437 thisblock->data.block.outer_cleanups
3438 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3439 block_stack->data.block.outer_cleanups);
3440 else
3441 thisblock->data.block.outer_cleanups = 0;
3442 thisblock->data.block.label_chain = 0;
3443 thisblock->data.block.innermost_stack_block = stack_block_stack;
3444 thisblock->data.block.first_insn = note;
3445 thisblock->data.block.block_start_count = ++current_block_start_count;
3446 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3447 block_stack = thisblock;
3448 nesting_stack = thisblock;
3450 /* Make a new level for allocating stack slots. */
3451 push_temp_slots ();
3454 /* Specify the scope of temporaries created by TARGET_EXPRs. Similar
3455 to CLEANUP_POINT_EXPR, but handles cases when a series of calls to
3456 expand_expr are made. After we end the region, we know that all
3457 space for all temporaries that were created by TARGET_EXPRs will be
3458 destroyed and their space freed for reuse. */
3460 void
3461 expand_start_target_temps (void)
3463 /* This is so that even if the result is preserved, the space
3464 allocated will be freed, as we know that it is no longer in use. */
3465 push_temp_slots ();
3467 /* Start a new binding layer that will keep track of all cleanup
3468 actions to be performed. */
3469 expand_start_bindings (2);
3471 target_temp_slot_level = temp_slot_level;
3474 void
3475 expand_end_target_temps (void)
3477 expand_end_bindings (NULL_TREE, 0, 0);
3479 /* This is so that even if the result is preserved, the space
3480 allocated will be freed, as we know that it is no longer in use. */
3481 pop_temp_slots ();
3484 /* Given a pointer to a BLOCK node return nonzero if (and only if) the node
3485 in question represents the outermost pair of curly braces (i.e. the "body
3486 block") of a function or method.
3488 For any BLOCK node representing a "body block" of a function or method, the
3489 BLOCK_SUPERCONTEXT of the node will point to another BLOCK node which
3490 represents the outermost (function) scope for the function or method (i.e.
3491 the one which includes the formal parameters). The BLOCK_SUPERCONTEXT of
3492 *that* node in turn will point to the relevant FUNCTION_DECL node. */
3495 is_body_block (tree stmt)
3497 if (lang_hooks.no_body_blocks)
3498 return 0;
3500 if (TREE_CODE (stmt) == BLOCK)
3502 tree parent = BLOCK_SUPERCONTEXT (stmt);
3504 if (parent && TREE_CODE (parent) == BLOCK)
3506 tree grandparent = BLOCK_SUPERCONTEXT (parent);
3508 if (grandparent && TREE_CODE (grandparent) == FUNCTION_DECL)
3509 return 1;
3513 return 0;
3516 /* True if we are currently emitting insns in an area of output code
3517 that is controlled by a conditional expression. This is used by
3518 the cleanup handling code to generate conditional cleanup actions. */
3521 conditional_context (void)
3523 return block_stack && block_stack->data.block.conditional_code;
3526 /* Return an opaque pointer to the current nesting level, so frontend code
3527 can check its own sanity. */
3529 struct nesting *
3530 current_nesting_level (void)
3532 return cfun ? block_stack : 0;
3535 /* Emit a handler label for a nonlocal goto handler.
3536 Also emit code to store the handler label in SLOT before BEFORE_INSN. */
3538 static rtx
3539 expand_nl_handler_label (rtx slot, rtx before_insn)
3541 rtx insns;
3542 rtx handler_label = gen_label_rtx ();
3544 /* Don't let cleanup_cfg delete the handler. */
3545 LABEL_PRESERVE_P (handler_label) = 1;
3547 start_sequence ();
3548 emit_move_insn (slot, gen_rtx_LABEL_REF (Pmode, handler_label));
3549 insns = get_insns ();
3550 end_sequence ();
3551 emit_insn_before (insns, before_insn);
3553 emit_label (handler_label);
3555 return handler_label;
3558 /* Emit code to restore vital registers at the beginning of a nonlocal goto
3559 handler. */
3560 static void
3561 expand_nl_goto_receiver (void)
3563 #ifdef HAVE_nonlocal_goto
3564 if (! HAVE_nonlocal_goto)
3565 #endif
3566 /* First adjust our frame pointer to its actual value. It was
3567 previously set to the start of the virtual area corresponding to
3568 the stacked variables when we branched here and now needs to be
3569 adjusted to the actual hardware fp value.
3571 Assignments are to virtual registers are converted by
3572 instantiate_virtual_regs into the corresponding assignment
3573 to the underlying register (fp in this case) that makes
3574 the original assignment true.
3575 So the following insn will actually be
3576 decrementing fp by STARTING_FRAME_OFFSET. */
3577 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
3579 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3580 if (fixed_regs[ARG_POINTER_REGNUM])
3582 #ifdef ELIMINABLE_REGS
3583 /* If the argument pointer can be eliminated in favor of the
3584 frame pointer, we don't need to restore it. We assume here
3585 that if such an elimination is present, it can always be used.
3586 This is the case on all known machines; if we don't make this
3587 assumption, we do unnecessary saving on many machines. */
3588 static const struct elims {const int from, to;} elim_regs[] = ELIMINABLE_REGS;
3589 size_t i;
3591 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
3592 if (elim_regs[i].from == ARG_POINTER_REGNUM
3593 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3594 break;
3596 if (i == ARRAY_SIZE (elim_regs))
3597 #endif
3599 /* Now restore our arg pointer from the address at which it
3600 was saved in our stack frame. */
3601 emit_move_insn (virtual_incoming_args_rtx,
3602 copy_to_reg (get_arg_pointer_save_area (cfun)));
3605 #endif
3607 #ifdef HAVE_nonlocal_goto_receiver
3608 if (HAVE_nonlocal_goto_receiver)
3609 emit_insn (gen_nonlocal_goto_receiver ());
3610 #endif
3613 /* Make handlers for nonlocal gotos taking place in the function calls in
3614 block THISBLOCK. */
3616 static void
3617 expand_nl_goto_receivers (struct nesting *thisblock)
3619 tree link;
3620 rtx afterward = gen_label_rtx ();
3621 rtx insns, slot;
3622 rtx label_list;
3623 int any_invalid;
3625 /* Record the handler address in the stack slot for that purpose,
3626 during this block, saving and restoring the outer value. */
3627 if (thisblock->next != 0)
3628 for (slot = nonlocal_goto_handler_slots; slot; slot = XEXP (slot, 1))
3630 rtx save_receiver = gen_reg_rtx (Pmode);
3631 emit_move_insn (XEXP (slot, 0), save_receiver);
3633 start_sequence ();
3634 emit_move_insn (save_receiver, XEXP (slot, 0));
3635 insns = get_insns ();
3636 end_sequence ();
3637 emit_insn_before (insns, thisblock->data.block.first_insn);
3640 /* Jump around the handlers; they run only when specially invoked. */
3641 emit_jump (afterward);
3643 /* Make a separate handler for each label. */
3644 link = nonlocal_labels;
3645 slot = nonlocal_goto_handler_slots;
3646 label_list = NULL_RTX;
3647 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3648 /* Skip any labels we shouldn't be able to jump to from here,
3649 we generate one special handler for all of them below which just calls
3650 abort. */
3651 if (! DECL_TOO_LATE (TREE_VALUE (link)))
3653 rtx lab;
3654 lab = expand_nl_handler_label (XEXP (slot, 0),
3655 thisblock->data.block.first_insn);
3656 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3658 expand_nl_goto_receiver ();
3660 /* Jump to the "real" nonlocal label. */
3661 expand_goto (TREE_VALUE (link));
3664 /* A second pass over all nonlocal labels; this time we handle those
3665 we should not be able to jump to at this point. */
3666 link = nonlocal_labels;
3667 slot = nonlocal_goto_handler_slots;
3668 any_invalid = 0;
3669 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3670 if (DECL_TOO_LATE (TREE_VALUE (link)))
3672 rtx lab;
3673 lab = expand_nl_handler_label (XEXP (slot, 0),
3674 thisblock->data.block.first_insn);
3675 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3676 any_invalid = 1;
3679 if (any_invalid)
3681 expand_nl_goto_receiver ();
3682 expand_builtin_trap ();
3685 nonlocal_goto_handler_labels = label_list;
3686 emit_label (afterward);
3689 /* Warn about any unused VARS (which may contain nodes other than
3690 VAR_DECLs, but such nodes are ignored). The nodes are connected
3691 via the TREE_CHAIN field. */
3693 void
3694 warn_about_unused_variables (tree vars)
3696 tree decl;
3698 if (warn_unused_variable)
3699 for (decl = vars; decl; decl = TREE_CHAIN (decl))
3700 if (TREE_CODE (decl) == VAR_DECL
3701 && ! TREE_USED (decl)
3702 && ! DECL_IN_SYSTEM_HEADER (decl)
3703 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
3704 warning ("%Junused variable '%D'", decl, decl);
3707 /* Generate RTL code to terminate a binding contour.
3709 VARS is the chain of VAR_DECL nodes for the variables bound in this
3710 contour. There may actually be other nodes in this chain, but any
3711 nodes other than VAR_DECLS are ignored.
3713 MARK_ENDS is nonzero if we should put a note at the beginning
3714 and end of this binding contour.
3716 DONT_JUMP_IN is positive if it is not valid to jump into this contour,
3717 zero if we can jump into this contour only if it does not have a saved
3718 stack level, and negative if we are not to check for invalid use of
3719 labels (because the front end does that). */
3721 void
3722 expand_end_bindings (tree vars, int mark_ends, int dont_jump_in)
3724 struct nesting *thisblock = block_stack;
3726 /* If any of the variables in this scope were not used, warn the
3727 user. */
3728 warn_about_unused_variables (vars);
3730 if (thisblock->exit_label)
3732 do_pending_stack_adjust ();
3733 emit_label (thisblock->exit_label);
3736 /* If necessary, make handlers for nonlocal gotos taking
3737 place in the function calls in this block. */
3738 if (function_call_count != 0 && nonlocal_labels
3739 /* Make handler for outermost block
3740 if there were any nonlocal gotos to this function. */
3741 && (thisblock->next == 0 ? current_function_has_nonlocal_label
3742 /* Make handler for inner block if it has something
3743 special to do when you jump out of it. */
3744 : (thisblock->data.block.cleanups != 0
3745 || thisblock->data.block.stack_level != 0)))
3746 expand_nl_goto_receivers (thisblock);
3748 /* Don't allow jumping into a block that has a stack level.
3749 Cleanups are allowed, though. */
3750 if (dont_jump_in > 0
3751 || (dont_jump_in == 0 && thisblock->data.block.stack_level != 0))
3753 struct label_chain *chain;
3755 /* Any labels in this block are no longer valid to go to.
3756 Mark them to cause an error message. */
3757 for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
3759 DECL_TOO_LATE (chain->label) = 1;
3760 /* If any goto without a fixup came to this label,
3761 that must be an error, because gotos without fixups
3762 come from outside all saved stack-levels. */
3763 if (TREE_ADDRESSABLE (chain->label))
3764 error ("%Jlabel '%D' used before containing binding contour",
3765 chain->label, chain->label);
3769 /* Restore stack level in effect before the block
3770 (only if variable-size objects allocated). */
3771 /* Perform any cleanups associated with the block. */
3773 if (thisblock->data.block.stack_level != 0
3774 || thisblock->data.block.cleanups != 0)
3776 int reachable;
3777 rtx insn;
3779 /* Don't let cleanups affect ({...}) constructs. */
3780 int old_expr_stmts_for_value = expr_stmts_for_value;
3781 rtx old_last_expr_value = last_expr_value;
3782 tree old_last_expr_type = last_expr_type;
3783 expr_stmts_for_value = 0;
3785 /* Only clean up here if this point can actually be reached. */
3786 insn = get_last_insn ();
3787 if (GET_CODE (insn) == NOTE)
3788 insn = prev_nonnote_insn (insn);
3789 reachable = (! insn || GET_CODE (insn) != BARRIER);
3791 /* Do the cleanups. */
3792 expand_cleanups (thisblock->data.block.cleanups, 0, reachable);
3793 if (reachable)
3794 do_pending_stack_adjust ();
3796 expr_stmts_for_value = old_expr_stmts_for_value;
3797 last_expr_value = old_last_expr_value;
3798 last_expr_type = old_last_expr_type;
3800 /* Restore the stack level. */
3802 if (reachable && thisblock->data.block.stack_level != 0)
3804 emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3805 thisblock->data.block.stack_level, NULL_RTX);
3806 if (nonlocal_goto_handler_slots != 0)
3807 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3808 NULL_RTX);
3811 /* Any gotos out of this block must also do these things.
3812 Also report any gotos with fixups that came to labels in this
3813 level. */
3814 fixup_gotos (thisblock,
3815 thisblock->data.block.stack_level,
3816 thisblock->data.block.cleanups,
3817 thisblock->data.block.first_insn,
3818 dont_jump_in);
3821 /* Mark the beginning and end of the scope if requested.
3822 We do this now, after running cleanups on the variables
3823 just going out of scope, so they are in scope for their cleanups. */
3825 if (mark_ends && !cfun->dont_emit_block_notes)
3827 rtx note = emit_note (NOTE_INSN_BLOCK_END);
3828 NOTE_BLOCK (note) = NOTE_BLOCK (thisblock->data.block.first_insn);
3830 else
3831 /* Get rid of the beginning-mark if we don't make an end-mark. */
3832 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
3834 /* Restore the temporary level of TARGET_EXPRs. */
3835 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
3837 /* Restore block_stack level for containing block. */
3839 stack_block_stack = thisblock->data.block.innermost_stack_block;
3840 POPSTACK (block_stack);
3842 /* Pop the stack slot nesting and free any slots at this level. */
3843 pop_temp_slots ();
3846 /* Generate code to save the stack pointer at the start of the current block
3847 and set up to restore it on exit. */
3849 void
3850 save_stack_pointer (void)
3852 struct nesting *thisblock = block_stack;
3854 if (thisblock->data.block.stack_level == 0)
3856 emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3857 &thisblock->data.block.stack_level,
3858 thisblock->data.block.first_insn);
3859 stack_block_stack = thisblock;
3863 /* Generate RTL for the automatic variable declaration DECL.
3864 (Other kinds of declarations are simply ignored if seen here.) */
3866 void
3867 expand_decl (tree decl)
3869 tree type;
3871 type = TREE_TYPE (decl);
3873 /* For a CONST_DECL, set mode, alignment, and sizes from those of the
3874 type in case this node is used in a reference. */
3875 if (TREE_CODE (decl) == CONST_DECL)
3877 DECL_MODE (decl) = TYPE_MODE (type);
3878 DECL_ALIGN (decl) = TYPE_ALIGN (type);
3879 DECL_SIZE (decl) = TYPE_SIZE (type);
3880 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
3881 return;
3884 /* Otherwise, only automatic variables need any expansion done. Static and
3885 external variables, and external functions, will be handled by
3886 `assemble_variable' (called from finish_decl). TYPE_DECL requires
3887 nothing. PARM_DECLs are handled in `assign_parms'. */
3888 if (TREE_CODE (decl) != VAR_DECL)
3889 return;
3891 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3892 return;
3894 /* Create the RTL representation for the variable. */
3896 if (type == error_mark_node)
3897 SET_DECL_RTL (decl, gen_rtx_MEM (BLKmode, const0_rtx));
3899 else if (DECL_SIZE (decl) == 0)
3900 /* Variable with incomplete type. */
3902 rtx x;
3903 if (DECL_INITIAL (decl) == 0)
3904 /* Error message was already done; now avoid a crash. */
3905 x = gen_rtx_MEM (BLKmode, const0_rtx);
3906 else
3907 /* An initializer is going to decide the size of this array.
3908 Until we know the size, represent its address with a reg. */
3909 x = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3911 set_mem_attributes (x, decl, 1);
3912 SET_DECL_RTL (decl, x);
3914 else if (DECL_MODE (decl) != BLKmode
3915 /* If -ffloat-store, don't put explicit float vars
3916 into regs. */
3917 && !(flag_float_store
3918 && TREE_CODE (type) == REAL_TYPE)
3919 && ! TREE_THIS_VOLATILE (decl)
3920 && ! DECL_NONLOCAL (decl)
3921 && (DECL_REGISTER (decl) || DECL_ARTIFICIAL (decl) || optimize))
3923 /* Automatic variable that can go in a register. */
3924 int unsignedp = TREE_UNSIGNED (type);
3925 enum machine_mode reg_mode
3926 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
3928 SET_DECL_RTL (decl, gen_reg_rtx (reg_mode));
3930 if (!DECL_ARTIFICIAL (decl))
3931 mark_user_reg (DECL_RTL (decl));
3933 if (POINTER_TYPE_P (type))
3934 mark_reg_pointer (DECL_RTL (decl),
3935 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
3937 maybe_set_unchanging (DECL_RTL (decl), decl);
3939 /* If something wants our address, try to use ADDRESSOF. */
3940 if (TREE_ADDRESSABLE (decl))
3941 put_var_into_stack (decl, /*rescan=*/false);
3944 else if (TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
3945 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
3946 && 0 < compare_tree_int (DECL_SIZE_UNIT (decl),
3947 STACK_CHECK_MAX_VAR_SIZE)))
3949 /* Variable of fixed size that goes on the stack. */
3950 rtx oldaddr = 0;
3951 rtx addr;
3952 rtx x;
3954 /* If we previously made RTL for this decl, it must be an array
3955 whose size was determined by the initializer.
3956 The old address was a register; set that register now
3957 to the proper address. */
3958 if (DECL_RTL_SET_P (decl))
3960 if (GET_CODE (DECL_RTL (decl)) != MEM
3961 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
3962 abort ();
3963 oldaddr = XEXP (DECL_RTL (decl), 0);
3966 /* Set alignment we actually gave this decl. */
3967 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
3968 : GET_MODE_BITSIZE (DECL_MODE (decl)));
3969 DECL_USER_ALIGN (decl) = 0;
3971 x = assign_temp (decl, 1, 1, 1);
3972 set_mem_attributes (x, decl, 1);
3973 SET_DECL_RTL (decl, x);
3975 if (oldaddr)
3977 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
3978 if (addr != oldaddr)
3979 emit_move_insn (oldaddr, addr);
3982 else
3983 /* Dynamic-size object: must push space on the stack. */
3985 rtx address, size, x;
3987 /* Record the stack pointer on entry to block, if have
3988 not already done so. */
3989 do_pending_stack_adjust ();
3990 save_stack_pointer ();
3992 /* In function-at-a-time mode, variable_size doesn't expand this,
3993 so do it now. */
3994 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
3995 expand_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
3996 const0_rtx, VOIDmode, 0);
3998 /* Compute the variable's size, in bytes. */
3999 size = expand_expr (DECL_SIZE_UNIT (decl), NULL_RTX, VOIDmode, 0);
4000 free_temp_slots ();
4002 /* Allocate space on the stack for the variable. Note that
4003 DECL_ALIGN says how the variable is to be aligned and we
4004 cannot use it to conclude anything about the alignment of
4005 the size. */
4006 address = allocate_dynamic_stack_space (size, NULL_RTX,
4007 TYPE_ALIGN (TREE_TYPE (decl)));
4009 /* Reference the variable indirect through that rtx. */
4010 x = gen_rtx_MEM (DECL_MODE (decl), address);
4011 set_mem_attributes (x, decl, 1);
4012 SET_DECL_RTL (decl, x);
4015 /* Indicate the alignment we actually gave this variable. */
4016 #ifdef STACK_BOUNDARY
4017 DECL_ALIGN (decl) = STACK_BOUNDARY;
4018 #else
4019 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
4020 #endif
4021 DECL_USER_ALIGN (decl) = 0;
4025 /* Emit code to allocate T_SIZE bytes of dynamic stack space for ALLOC. */
4026 void
4027 expand_stack_alloc (tree alloc, tree t_size)
4029 rtx address, dest, size;
4030 tree var, type;
4032 if (TREE_CODE (alloc) != ADDR_EXPR)
4033 abort ();
4034 var = TREE_OPERAND (alloc, 0);
4035 if (TREE_CODE (var) != VAR_DECL)
4036 abort ();
4038 type = TREE_TYPE (var);
4040 /* In function-at-a-time mode, variable_size doesn't expand this,
4041 so do it now. */
4042 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
4043 expand_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
4044 const0_rtx, VOIDmode, 0);
4046 /* Compute the variable's size, in bytes. */
4047 size = expand_expr (t_size, NULL_RTX, VOIDmode, 0);
4048 free_temp_slots ();
4050 /* Allocate space on the stack for the variable. */
4051 address = XEXP (DECL_RTL (var), 0);
4052 dest = allocate_dynamic_stack_space (size, address, TYPE_ALIGN (type));
4053 if (dest != address)
4054 emit_move_insn (address, dest);
4056 /* Indicate the alignment we actually gave this variable. */
4057 #ifdef STACK_BOUNDARY
4058 DECL_ALIGN (var) = STACK_BOUNDARY;
4059 #else
4060 DECL_ALIGN (var) = BIGGEST_ALIGNMENT;
4061 #endif
4062 DECL_USER_ALIGN (var) = 0;
4065 /* Emit code to save the current value of stack. */
4067 expand_stack_save ()
4069 rtx ret = NULL_RTX;
4071 do_pending_stack_adjust ();
4072 emit_stack_save (SAVE_BLOCK, &ret, NULL_RTX);
4073 return ret;
4076 /* Emit code to restore the current value of stack. */
4077 void
4078 expand_stack_restore (tree var)
4080 rtx sa = DECL_RTL (var);
4082 emit_stack_restore (SAVE_BLOCK, sa, NULL_RTX);
4085 /* Emit code to perform the initialization of a declaration DECL. */
4087 void
4088 expand_decl_init (tree decl)
4090 int was_used = TREE_USED (decl);
4092 /* If this is a CONST_DECL, we don't have to generate any code. Likewise
4093 for static decls. */
4094 if (TREE_CODE (decl) == CONST_DECL
4095 || TREE_STATIC (decl))
4096 return;
4098 /* Compute and store the initial value now. */
4100 push_temp_slots ();
4102 if (DECL_INITIAL (decl) == error_mark_node)
4104 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
4106 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
4107 || code == POINTER_TYPE || code == REFERENCE_TYPE)
4108 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
4110 emit_queue ();
4112 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
4114 emit_line_note (DECL_SOURCE_LOCATION (decl));
4115 expand_assignment (decl, DECL_INITIAL (decl), 0);
4116 emit_queue ();
4119 /* Don't let the initialization count as "using" the variable. */
4120 TREE_USED (decl) = was_used;
4122 /* Free any temporaries we made while initializing the decl. */
4123 preserve_temp_slots (NULL_RTX);
4124 free_temp_slots ();
4125 pop_temp_slots ();
4128 /* CLEANUP is an expression to be executed at exit from this binding contour;
4129 for example, in C++, it might call the destructor for this variable.
4131 We wrap CLEANUP in an UNSAVE_EXPR node, so that we can expand the
4132 CLEANUP multiple times, and have the correct semantics. This
4133 happens in exception handling, for gotos, returns, breaks that
4134 leave the current scope.
4136 If CLEANUP is nonzero and DECL is zero, we record a cleanup
4137 that is not associated with any particular variable. */
4140 expand_decl_cleanup (tree decl, tree cleanup)
4142 struct nesting *thisblock;
4144 /* Error if we are not in any block. */
4145 if (cfun == 0 || block_stack == 0)
4146 return 0;
4148 thisblock = block_stack;
4150 /* Record the cleanup if there is one. */
4152 if (cleanup != 0)
4154 tree t;
4155 rtx seq;
4156 tree *cleanups = &thisblock->data.block.cleanups;
4157 int cond_context = conditional_context ();
4159 if (cond_context)
4161 rtx flag = gen_reg_rtx (word_mode);
4162 rtx set_flag_0;
4163 tree cond;
4165 start_sequence ();
4166 emit_move_insn (flag, const0_rtx);
4167 set_flag_0 = get_insns ();
4168 end_sequence ();
4170 thisblock->data.block.last_unconditional_cleanup
4171 = emit_insn_after (set_flag_0,
4172 thisblock->data.block.last_unconditional_cleanup);
4174 emit_move_insn (flag, const1_rtx);
4176 cond = build_decl (VAR_DECL, NULL_TREE,
4177 (*lang_hooks.types.type_for_mode) (word_mode, 1));
4178 SET_DECL_RTL (cond, flag);
4180 /* Conditionalize the cleanup. */
4181 cleanup = build (COND_EXPR, void_type_node,
4182 (*lang_hooks.truthvalue_conversion) (cond),
4183 cleanup, integer_zero_node);
4184 cleanup = fold (cleanup);
4186 cleanups = &thisblock->data.block.cleanups;
4189 cleanup = unsave_expr (cleanup);
4191 t = *cleanups = tree_cons (decl, cleanup, *cleanups);
4193 if (! cond_context)
4194 /* If this block has a cleanup, it belongs in stack_block_stack. */
4195 stack_block_stack = thisblock;
4197 if (cond_context)
4199 start_sequence ();
4202 if (! using_eh_for_cleanups_p)
4203 TREE_ADDRESSABLE (t) = 1;
4204 else
4205 expand_eh_region_start ();
4207 if (cond_context)
4209 seq = get_insns ();
4210 end_sequence ();
4211 if (seq)
4212 thisblock->data.block.last_unconditional_cleanup
4213 = emit_insn_after (seq,
4214 thisblock->data.block.last_unconditional_cleanup);
4216 else
4218 thisblock->data.block.last_unconditional_cleanup
4219 = get_last_insn ();
4220 /* When we insert instructions after the last unconditional cleanup,
4221 we don't adjust last_insn. That means that a later add_insn will
4222 clobber the instructions we've just added. The easiest way to
4223 fix this is to just insert another instruction here, so that the
4224 instructions inserted after the last unconditional cleanup are
4225 never the last instruction. */
4226 emit_note (NOTE_INSN_DELETED);
4229 return 1;
4232 /* Like expand_decl_cleanup, but maybe only run the cleanup if an exception
4233 is thrown. */
4236 expand_decl_cleanup_eh (tree decl, tree cleanup, int eh_only)
4238 int ret = expand_decl_cleanup (decl, cleanup);
4239 if (cleanup && ret)
4241 tree node = block_stack->data.block.cleanups;
4242 CLEANUP_EH_ONLY (node) = eh_only;
4244 return ret;
4247 /* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
4248 DECL_ELTS is the list of elements that belong to DECL's type.
4249 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
4251 void
4252 expand_anon_union_decl (tree decl, tree cleanup, tree decl_elts)
4254 struct nesting *thisblock = cfun == 0 ? 0 : block_stack;
4255 rtx x;
4256 tree t;
4258 /* If any of the elements are addressable, so is the entire union. */
4259 for (t = decl_elts; t; t = TREE_CHAIN (t))
4260 if (TREE_ADDRESSABLE (TREE_VALUE (t)))
4262 TREE_ADDRESSABLE (decl) = 1;
4263 break;
4266 expand_decl (decl);
4267 expand_decl_cleanup (decl, cleanup);
4268 x = DECL_RTL (decl);
4270 /* Go through the elements, assigning RTL to each. */
4271 for (t = decl_elts; t; t = TREE_CHAIN (t))
4273 tree decl_elt = TREE_VALUE (t);
4274 tree cleanup_elt = TREE_PURPOSE (t);
4275 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
4277 /* If any of the elements are addressable, so is the entire
4278 union. */
4279 if (TREE_USED (decl_elt))
4280 TREE_USED (decl) = 1;
4282 /* Propagate the union's alignment to the elements. */
4283 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
4284 DECL_USER_ALIGN (decl_elt) = DECL_USER_ALIGN (decl);
4286 /* If the element has BLKmode and the union doesn't, the union is
4287 aligned such that the element doesn't need to have BLKmode, so
4288 change the element's mode to the appropriate one for its size. */
4289 if (mode == BLKmode && DECL_MODE (decl) != BLKmode)
4290 DECL_MODE (decl_elt) = mode
4291 = mode_for_size_tree (DECL_SIZE (decl_elt), MODE_INT, 1);
4293 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
4294 instead create a new MEM rtx with the proper mode. */
4295 if (GET_CODE (x) == MEM)
4297 if (mode == GET_MODE (x))
4298 SET_DECL_RTL (decl_elt, x);
4299 else
4300 SET_DECL_RTL (decl_elt, adjust_address_nv (x, mode, 0));
4302 else if (GET_CODE (x) == REG)
4304 if (mode == GET_MODE (x))
4305 SET_DECL_RTL (decl_elt, x);
4306 else
4307 SET_DECL_RTL (decl_elt, gen_lowpart_SUBREG (mode, x));
4309 else
4310 abort ();
4312 /* Record the cleanup if there is one. */
4314 if (cleanup != 0)
4315 thisblock->data.block.cleanups
4316 = tree_cons (decl_elt, cleanup_elt,
4317 thisblock->data.block.cleanups);
4321 /* Expand a list of cleanups LIST.
4322 Elements may be expressions or may be nested lists.
4324 If IN_FIXUP is nonzero, we are generating this cleanup for a fixup
4325 goto and handle protection regions specially in that case.
4327 If REACHABLE, we emit code, otherwise just inform the exception handling
4328 code about this finalization. */
4330 static void
4331 expand_cleanups (tree list, int in_fixup, int reachable)
4333 tree tail;
4334 for (tail = list; tail; tail = TREE_CHAIN (tail))
4335 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
4336 expand_cleanups (TREE_VALUE (tail), in_fixup, reachable);
4337 else
4339 if (! in_fixup && using_eh_for_cleanups_p)
4340 expand_eh_region_end_cleanup (TREE_VALUE (tail));
4342 if (reachable && !CLEANUP_EH_ONLY (tail))
4344 /* Cleanups may be run multiple times. For example,
4345 when exiting a binding contour, we expand the
4346 cleanups associated with that contour. When a goto
4347 within that binding contour has a target outside that
4348 contour, it will expand all cleanups from its scope to
4349 the target. Though the cleanups are expanded multiple
4350 times, the control paths are non-overlapping so the
4351 cleanups will not be executed twice. */
4353 /* We may need to protect from outer cleanups. */
4354 if (in_fixup && using_eh_for_cleanups_p)
4356 expand_eh_region_start ();
4358 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4360 expand_eh_region_end_fixup (TREE_VALUE (tail));
4362 else
4363 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4365 free_temp_slots ();
4370 /* Mark when the context we are emitting RTL for as a conditional
4371 context, so that any cleanup actions we register with
4372 expand_decl_init will be properly conditionalized when those
4373 cleanup actions are later performed. Must be called before any
4374 expression (tree) is expanded that is within a conditional context. */
4376 void
4377 start_cleanup_deferral (void)
4379 /* block_stack can be NULL if we are inside the parameter list. It is
4380 OK to do nothing, because cleanups aren't possible here. */
4381 if (block_stack)
4382 ++block_stack->data.block.conditional_code;
4385 /* Mark the end of a conditional region of code. Because cleanup
4386 deferrals may be nested, we may still be in a conditional region
4387 after we end the currently deferred cleanups, only after we end all
4388 deferred cleanups, are we back in unconditional code. */
4390 void
4391 end_cleanup_deferral (void)
4393 /* block_stack can be NULL if we are inside the parameter list. It is
4394 OK to do nothing, because cleanups aren't possible here. */
4395 if (block_stack)
4396 --block_stack->data.block.conditional_code;
4399 tree
4400 last_cleanup_this_contour (void)
4402 if (block_stack == 0)
4403 return 0;
4405 return block_stack->data.block.cleanups;
4409 /* Return nonzero if any containing block has a stack level or
4410 cleanups. */
4413 containing_blocks_have_cleanups_or_stack_level ()
4415 struct nesting *block;
4417 for (block = block_stack; block; block = block->next)
4418 if (block->data.block.stack_level != 0
4419 || block->data.block.cleanups != 0)
4420 return 1;
4422 return 0;
4425 /* Return 1 if there are any pending cleanups at this point.
4426 Check the current contour as well as contours that enclose
4427 the current contour. */
4430 any_pending_cleanups (void)
4432 struct nesting *block;
4434 if (cfun == NULL || cfun->stmt == NULL || block_stack == 0)
4435 return 0;
4437 if (block_stack->data.block.cleanups != NULL)
4438 return 1;
4440 if (block_stack->data.block.outer_cleanups == 0)
4441 return 0;
4443 for (block = block_stack->next; block; block = block->next)
4444 if (block->data.block.cleanups != 0)
4445 return 1;
4447 return 0;
4450 /* Enter a case (Pascal) or switch (C) statement.
4451 Push a block onto case_stack and nesting_stack
4452 to accumulate the case-labels that are seen
4453 and to record the labels generated for the statement.
4455 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
4456 Otherwise, this construct is transparent for `exit_something'.
4458 EXPR is the index-expression to be dispatched on.
4459 TYPE is its nominal type. We could simply convert EXPR to this type,
4460 but instead we take short cuts. */
4462 void
4463 expand_start_case (int exit_flag, tree expr, tree type,
4464 const char *printname)
4466 struct nesting *thiscase = ALLOC_NESTING ();
4468 /* Make an entry on case_stack for the case we are entering. */
4470 thiscase->desc = CASE_NESTING;
4471 thiscase->next = case_stack;
4472 thiscase->all = nesting_stack;
4473 thiscase->depth = ++nesting_depth;
4474 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
4475 thiscase->data.case_stmt.case_list = 0;
4476 thiscase->data.case_stmt.index_expr = expr;
4477 thiscase->data.case_stmt.nominal_type = type;
4478 thiscase->data.case_stmt.default_label = 0;
4479 thiscase->data.case_stmt.printname = printname;
4480 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
4481 case_stack = thiscase;
4482 nesting_stack = thiscase;
4484 do_pending_stack_adjust ();
4485 emit_queue ();
4487 /* Make sure case_stmt.start points to something that won't
4488 need any transformation before expand_end_case. */
4489 if (GET_CODE (get_last_insn ()) != NOTE)
4490 emit_note (NOTE_INSN_DELETED);
4492 thiscase->data.case_stmt.start = get_last_insn ();
4494 start_cleanup_deferral ();
4497 /* Start a "dummy case statement" within which case labels are invalid
4498 and are not connected to any larger real case statement.
4499 This can be used if you don't want to let a case statement jump
4500 into the middle of certain kinds of constructs. */
4502 void
4503 expand_start_case_dummy (void)
4505 struct nesting *thiscase = ALLOC_NESTING ();
4507 /* Make an entry on case_stack for the dummy. */
4509 thiscase->desc = CASE_NESTING;
4510 thiscase->next = case_stack;
4511 thiscase->all = nesting_stack;
4512 thiscase->depth = ++nesting_depth;
4513 thiscase->exit_label = 0;
4514 thiscase->data.case_stmt.case_list = 0;
4515 thiscase->data.case_stmt.start = 0;
4516 thiscase->data.case_stmt.nominal_type = 0;
4517 thiscase->data.case_stmt.default_label = 0;
4518 case_stack = thiscase;
4519 nesting_stack = thiscase;
4520 start_cleanup_deferral ();
4523 static void
4524 check_seenlabel (void)
4526 /* If this is the first label, warn if any insns have been emitted. */
4527 if (case_stack->data.case_stmt.line_number_status >= 0)
4529 rtx insn;
4531 restore_line_number_status
4532 (case_stack->data.case_stmt.line_number_status);
4533 case_stack->data.case_stmt.line_number_status = -1;
4535 for (insn = case_stack->data.case_stmt.start;
4536 insn;
4537 insn = NEXT_INSN (insn))
4539 if (GET_CODE (insn) == CODE_LABEL)
4540 break;
4541 if (GET_CODE (insn) != NOTE
4542 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4545 insn = PREV_INSN (insn);
4546 while (insn && (GET_CODE (insn) != NOTE || NOTE_LINE_NUMBER (insn) < 0));
4548 /* If insn is zero, then there must have been a syntax error. */
4549 if (insn)
4551 location_t locus;
4552 locus.file = NOTE_SOURCE_FILE (insn);
4553 locus.line = NOTE_LINE_NUMBER (insn);
4554 warning ("%Hunreachable code at beginning of %s", &locus,
4555 case_stack->data.case_stmt.printname);
4557 break;
4563 /* Accumulate one case or default label inside a case or switch statement.
4564 VALUE is the value of the case (a null pointer, for a default label).
4565 The function CONVERTER, when applied to arguments T and V,
4566 converts the value V to the type T.
4568 If not currently inside a case or switch statement, return 1 and do
4569 nothing. The caller will print a language-specific error message.
4570 If VALUE is a duplicate or overlaps, return 2 and do nothing
4571 except store the (first) duplicate node in *DUPLICATE.
4572 If VALUE is out of range, return 3 and do nothing.
4573 If we are jumping into the scope of a cleanup or var-sized array, return 5.
4574 Return 0 on success.
4576 Extended to handle range statements. */
4579 pushcase (tree value, tree (*converter) (tree, tree), tree label,
4580 tree *duplicate)
4582 tree index_type;
4583 tree nominal_type;
4585 /* Fail if not inside a real case statement. */
4586 if (! (case_stack && case_stack->data.case_stmt.start))
4587 return 1;
4589 if (stack_block_stack
4590 && stack_block_stack->depth > case_stack->depth)
4591 return 5;
4593 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4594 nominal_type = case_stack->data.case_stmt.nominal_type;
4596 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4597 if (index_type == error_mark_node)
4598 return 0;
4600 /* Convert VALUE to the type in which the comparisons are nominally done. */
4601 if (value != 0)
4602 value = (*converter) (nominal_type, value);
4604 check_seenlabel ();
4606 /* Fail if this value is out of range for the actual type of the index
4607 (which may be narrower than NOMINAL_TYPE). */
4608 if (value != 0
4609 && (TREE_CONSTANT_OVERFLOW (value)
4610 || ! int_fits_type_p (value, index_type)))
4611 return 3;
4613 return add_case_node (value, value, label, duplicate);
4616 /* Like pushcase but this case applies to all values between VALUE1 and
4617 VALUE2 (inclusive). If VALUE1 is NULL, the range starts at the lowest
4618 value of the index type and ends at VALUE2. If VALUE2 is NULL, the range
4619 starts at VALUE1 and ends at the highest value of the index type.
4620 If both are NULL, this case applies to all values.
4622 The return value is the same as that of pushcase but there is one
4623 additional error code: 4 means the specified range was empty. */
4626 pushcase_range (tree value1, tree value2, tree (*converter) (tree, tree),
4627 tree label, tree *duplicate)
4629 tree index_type;
4630 tree nominal_type;
4632 /* Fail if not inside a real case statement. */
4633 if (! (case_stack && case_stack->data.case_stmt.start))
4634 return 1;
4636 if (stack_block_stack
4637 && stack_block_stack->depth > case_stack->depth)
4638 return 5;
4640 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4641 nominal_type = case_stack->data.case_stmt.nominal_type;
4643 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4644 if (index_type == error_mark_node)
4645 return 0;
4647 check_seenlabel ();
4649 /* Convert VALUEs to type in which the comparisons are nominally done
4650 and replace any unspecified value with the corresponding bound. */
4651 if (value1 == 0)
4652 value1 = TYPE_MIN_VALUE (index_type);
4653 if (value2 == 0)
4654 value2 = TYPE_MAX_VALUE (index_type);
4656 /* Fail if the range is empty. Do this before any conversion since
4657 we want to allow out-of-range empty ranges. */
4658 if (value2 != 0 && tree_int_cst_lt (value2, value1))
4659 return 4;
4661 /* If the max was unbounded, use the max of the nominal_type we are
4662 converting to. Do this after the < check above to suppress false
4663 positives. */
4664 if (value2 == 0)
4665 value2 = TYPE_MAX_VALUE (nominal_type);
4667 value1 = (*converter) (nominal_type, value1);
4668 value2 = (*converter) (nominal_type, value2);
4670 /* Fail if these values are out of range. */
4671 if (TREE_CONSTANT_OVERFLOW (value1)
4672 || ! int_fits_type_p (value1, index_type))
4673 return 3;
4675 if (TREE_CONSTANT_OVERFLOW (value2)
4676 || ! int_fits_type_p (value2, index_type))
4677 return 3;
4679 return add_case_node (value1, value2, label, duplicate);
4682 /* Do the actual insertion of a case label for pushcase and pushcase_range
4683 into case_stack->data.case_stmt.case_list. Use an AVL tree to avoid
4684 slowdown for large switch statements. */
4687 add_case_node (tree low, tree high, tree label, tree *duplicate)
4689 struct case_node *p, **q, *r;
4691 /* If there's no HIGH value, then this is not a case range; it's
4692 just a simple case label. But that's just a degenerate case
4693 range. */
4694 if (!high)
4695 high = low;
4697 /* Handle default labels specially. */
4698 if (!high && !low)
4700 if (case_stack->data.case_stmt.default_label != 0)
4702 *duplicate = case_stack->data.case_stmt.default_label;
4703 return 2;
4705 case_stack->data.case_stmt.default_label = label;
4706 expand_label (label);
4707 return 0;
4710 q = &case_stack->data.case_stmt.case_list;
4711 p = *q;
4713 while ((r = *q))
4715 p = r;
4717 /* Keep going past elements distinctly greater than HIGH. */
4718 if (tree_int_cst_lt (high, p->low))
4719 q = &p->left;
4721 /* or distinctly less than LOW. */
4722 else if (tree_int_cst_lt (p->high, low))
4723 q = &p->right;
4725 else
4727 /* We have an overlap; this is an error. */
4728 *duplicate = p->code_label;
4729 return 2;
4733 /* Add this label to the chain, and succeed. */
4735 r = ggc_alloc (sizeof (struct case_node));
4736 r->low = low;
4738 /* If the bounds are equal, turn this into the one-value case. */
4739 if (tree_int_cst_equal (low, high))
4740 r->high = r->low;
4741 else
4742 r->high = high;
4744 r->code_label = label;
4745 expand_label (label);
4747 *q = r;
4748 r->parent = p;
4749 r->left = 0;
4750 r->right = 0;
4751 r->balance = 0;
4753 while (p)
4755 struct case_node *s;
4757 if (r == p->left)
4759 int b;
4761 if (! (b = p->balance))
4762 /* Growth propagation from left side. */
4763 p->balance = -1;
4764 else if (b < 0)
4766 if (r->balance < 0)
4768 /* R-Rotation */
4769 if ((p->left = s = r->right))
4770 s->parent = p;
4772 r->right = p;
4773 p->balance = 0;
4774 r->balance = 0;
4775 s = p->parent;
4776 p->parent = r;
4778 if ((r->parent = s))
4780 if (s->left == p)
4781 s->left = r;
4782 else
4783 s->right = r;
4785 else
4786 case_stack->data.case_stmt.case_list = r;
4788 else
4789 /* r->balance == +1 */
4791 /* LR-Rotation */
4793 int b2;
4794 struct case_node *t = r->right;
4796 if ((p->left = s = t->right))
4797 s->parent = p;
4799 t->right = p;
4800 if ((r->right = s = t->left))
4801 s->parent = r;
4803 t->left = r;
4804 b = t->balance;
4805 b2 = b < 0;
4806 p->balance = b2;
4807 b2 = -b2 - b;
4808 r->balance = b2;
4809 t->balance = 0;
4810 s = p->parent;
4811 p->parent = t;
4812 r->parent = t;
4814 if ((t->parent = s))
4816 if (s->left == p)
4817 s->left = t;
4818 else
4819 s->right = t;
4821 else
4822 case_stack->data.case_stmt.case_list = t;
4824 break;
4827 else
4829 /* p->balance == +1; growth of left side balances the node. */
4830 p->balance = 0;
4831 break;
4834 else
4835 /* r == p->right */
4837 int b;
4839 if (! (b = p->balance))
4840 /* Growth propagation from right side. */
4841 p->balance++;
4842 else if (b > 0)
4844 if (r->balance > 0)
4846 /* L-Rotation */
4848 if ((p->right = s = r->left))
4849 s->parent = p;
4851 r->left = p;
4852 p->balance = 0;
4853 r->balance = 0;
4854 s = p->parent;
4855 p->parent = r;
4856 if ((r->parent = s))
4858 if (s->left == p)
4859 s->left = r;
4860 else
4861 s->right = r;
4864 else
4865 case_stack->data.case_stmt.case_list = r;
4868 else
4869 /* r->balance == -1 */
4871 /* RL-Rotation */
4872 int b2;
4873 struct case_node *t = r->left;
4875 if ((p->right = s = t->left))
4876 s->parent = p;
4878 t->left = p;
4880 if ((r->left = s = t->right))
4881 s->parent = r;
4883 t->right = r;
4884 b = t->balance;
4885 b2 = b < 0;
4886 r->balance = b2;
4887 b2 = -b2 - b;
4888 p->balance = b2;
4889 t->balance = 0;
4890 s = p->parent;
4891 p->parent = t;
4892 r->parent = t;
4894 if ((t->parent = s))
4896 if (s->left == p)
4897 s->left = t;
4898 else
4899 s->right = t;
4902 else
4903 case_stack->data.case_stmt.case_list = t;
4905 break;
4907 else
4909 /* p->balance == -1; growth of right side balances the node. */
4910 p->balance = 0;
4911 break;
4915 r = p;
4916 p = p->parent;
4919 return 0;
4922 /* Returns the number of possible values of TYPE.
4923 Returns -1 if the number is unknown, variable, or if the number does not
4924 fit in a HOST_WIDE_INT.
4925 Sets *SPARSENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
4926 do not increase monotonically (there may be duplicates);
4927 to 1 if the values increase monotonically, but not always by 1;
4928 otherwise sets it to 0. */
4930 HOST_WIDE_INT
4931 all_cases_count (tree type, int *sparseness)
4933 tree t;
4934 HOST_WIDE_INT count, minval, lastval;
4936 *sparseness = 0;
4938 switch (TREE_CODE (type))
4940 case BOOLEAN_TYPE:
4941 count = 2;
4942 break;
4944 case CHAR_TYPE:
4945 count = 1 << BITS_PER_UNIT;
4946 break;
4948 default:
4949 case INTEGER_TYPE:
4950 if (TYPE_MAX_VALUE (type) != 0
4951 && 0 != (t = fold (build (MINUS_EXPR, type, TYPE_MAX_VALUE (type),
4952 TYPE_MIN_VALUE (type))))
4953 && 0 != (t = fold (build (PLUS_EXPR, type, t,
4954 convert (type, integer_zero_node))))
4955 && host_integerp (t, 1))
4956 count = tree_low_cst (t, 1);
4957 else
4958 return -1;
4959 break;
4961 case ENUMERAL_TYPE:
4962 /* Don't waste time with enumeral types with huge values. */
4963 if (! host_integerp (TYPE_MIN_VALUE (type), 0)
4964 || TYPE_MAX_VALUE (type) == 0
4965 || ! host_integerp (TYPE_MAX_VALUE (type), 0))
4966 return -1;
4968 lastval = minval = tree_low_cst (TYPE_MIN_VALUE (type), 0);
4969 count = 0;
4971 for (t = TYPE_VALUES (type); t != NULL_TREE; t = TREE_CHAIN (t))
4973 HOST_WIDE_INT thisval = tree_low_cst (TREE_VALUE (t), 0);
4975 if (*sparseness == 2 || thisval <= lastval)
4976 *sparseness = 2;
4977 else if (thisval != minval + count)
4978 *sparseness = 1;
4980 lastval = thisval;
4981 count++;
4985 return count;
4988 #define BITARRAY_TEST(ARRAY, INDEX) \
4989 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4990 & (1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR)))
4991 #define BITARRAY_SET(ARRAY, INDEX) \
4992 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4993 |= 1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR))
4995 /* Set the elements of the bitstring CASES_SEEN (which has length COUNT),
4996 with the case values we have seen, assuming the case expression
4997 has the given TYPE.
4998 SPARSENESS is as determined by all_cases_count.
5000 The time needed is proportional to COUNT, unless
5001 SPARSENESS is 2, in which case quadratic time is needed. */
5003 void
5004 mark_seen_cases (tree type, unsigned char *cases_seen, HOST_WIDE_INT count,
5005 int sparseness)
5007 tree next_node_to_try = NULL_TREE;
5008 HOST_WIDE_INT next_node_offset = 0;
5010 struct case_node *n, *root = case_stack->data.case_stmt.case_list;
5011 tree val = make_node (INTEGER_CST);
5013 TREE_TYPE (val) = type;
5014 if (! root)
5015 /* Do nothing. */
5017 else if (sparseness == 2)
5019 tree t;
5020 unsigned HOST_WIDE_INT xlo;
5022 /* This less efficient loop is only needed to handle
5023 duplicate case values (multiple enum constants
5024 with the same value). */
5025 TREE_TYPE (val) = TREE_TYPE (root->low);
5026 for (t = TYPE_VALUES (type), xlo = 0; t != NULL_TREE;
5027 t = TREE_CHAIN (t), xlo++)
5029 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (TREE_VALUE (t));
5030 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (TREE_VALUE (t));
5031 n = root;
5034 /* Keep going past elements distinctly greater than VAL. */
5035 if (tree_int_cst_lt (val, n->low))
5036 n = n->left;
5038 /* or distinctly less than VAL. */
5039 else if (tree_int_cst_lt (n->high, val))
5040 n = n->right;
5042 else
5044 /* We have found a matching range. */
5045 BITARRAY_SET (cases_seen, xlo);
5046 break;
5049 while (n);
5052 else
5054 if (root->left)
5055 case_stack->data.case_stmt.case_list = root = case_tree2list (root, 0);
5057 for (n = root; n; n = n->right)
5059 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (n->low);
5060 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (n->low);
5061 while (! tree_int_cst_lt (n->high, val))
5063 /* Calculate (into xlo) the "offset" of the integer (val).
5064 The element with lowest value has offset 0, the next smallest
5065 element has offset 1, etc. */
5067 unsigned HOST_WIDE_INT xlo;
5068 HOST_WIDE_INT xhi;
5069 tree t;
5071 if (sparseness && TYPE_VALUES (type) != NULL_TREE)
5073 /* The TYPE_VALUES will be in increasing order, so
5074 starting searching where we last ended. */
5075 t = next_node_to_try;
5076 xlo = next_node_offset;
5077 xhi = 0;
5078 for (;;)
5080 if (t == NULL_TREE)
5082 t = TYPE_VALUES (type);
5083 xlo = 0;
5085 if (tree_int_cst_equal (val, TREE_VALUE (t)))
5087 next_node_to_try = TREE_CHAIN (t);
5088 next_node_offset = xlo + 1;
5089 break;
5091 xlo++;
5092 t = TREE_CHAIN (t);
5093 if (t == next_node_to_try)
5095 xlo = -1;
5096 break;
5100 else
5102 t = TYPE_MIN_VALUE (type);
5103 if (t)
5104 neg_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t),
5105 &xlo, &xhi);
5106 else
5107 xlo = xhi = 0;
5108 add_double (xlo, xhi,
5109 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
5110 &xlo, &xhi);
5113 if (xhi == 0 && xlo < (unsigned HOST_WIDE_INT) count)
5114 BITARRAY_SET (cases_seen, xlo);
5116 add_double (TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
5117 1, 0,
5118 &TREE_INT_CST_LOW (val), &TREE_INT_CST_HIGH (val));
5124 /* Given a switch statement with an expression that is an enumeration
5125 type, warn if any of the enumeration type's literals are not
5126 covered by the case expressions of the switch. Also, warn if there
5127 are any extra switch cases that are *not* elements of the
5128 enumerated type.
5130 Historical note:
5132 At one stage this function would: ``If all enumeration literals
5133 were covered by the case expressions, turn one of the expressions
5134 into the default expression since it should not be possible to fall
5135 through such a switch.''
5137 That code has since been removed as: ``This optimization is
5138 disabled because it causes valid programs to fail. ANSI C does not
5139 guarantee that an expression with enum type will have a value that
5140 is the same as one of the enumeration literals.'' */
5142 void
5143 check_for_full_enumeration_handling (tree type)
5145 struct case_node *n;
5146 tree chain;
5148 /* True iff the selector type is a numbered set mode. */
5149 int sparseness = 0;
5151 /* The number of possible selector values. */
5152 HOST_WIDE_INT size;
5154 /* For each possible selector value. a one iff it has been matched
5155 by a case value alternative. */
5156 unsigned char *cases_seen;
5158 /* The allocated size of cases_seen, in chars. */
5159 HOST_WIDE_INT bytes_needed;
5161 size = all_cases_count (type, &sparseness);
5162 bytes_needed = (size + HOST_BITS_PER_CHAR) / HOST_BITS_PER_CHAR;
5164 if (size > 0 && size < 600000
5165 /* We deliberately use calloc here, not cmalloc, so that we can suppress
5166 this optimization if we don't have enough memory rather than
5167 aborting, as xmalloc would do. */
5168 && (cases_seen = really_call_calloc (bytes_needed, 1)) != NULL)
5170 HOST_WIDE_INT i;
5171 tree v = TYPE_VALUES (type);
5173 /* The time complexity of this code is normally O(N), where
5174 N being the number of members in the enumerated type.
5175 However, if type is an ENUMERAL_TYPE whose values do not
5176 increase monotonically, O(N*log(N)) time may be needed. */
5178 mark_seen_cases (type, cases_seen, size, sparseness);
5180 for (i = 0; v != NULL_TREE && i < size; i++, v = TREE_CHAIN (v))
5181 if (BITARRAY_TEST (cases_seen, i) == 0)
5182 warning ("enumeration value `%s' not handled in switch",
5183 IDENTIFIER_POINTER (TREE_PURPOSE (v)));
5185 free (cases_seen);
5188 /* Now we go the other way around; we warn if there are case
5189 expressions that don't correspond to enumerators. This can
5190 occur since C and C++ don't enforce type-checking of
5191 assignments to enumeration variables. */
5193 if (case_stack->data.case_stmt.case_list
5194 && case_stack->data.case_stmt.case_list->left)
5195 case_stack->data.case_stmt.case_list
5196 = case_tree2list (case_stack->data.case_stmt.case_list, 0);
5197 for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
5199 for (chain = TYPE_VALUES (type);
5200 chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
5201 chain = TREE_CHAIN (chain))
5204 if (!chain)
5206 if (TYPE_NAME (type) == 0)
5207 warning ("case value `%ld' not in enumerated type",
5208 (long) TREE_INT_CST_LOW (n->low));
5209 else
5210 warning ("case value `%ld' not in enumerated type `%s'",
5211 (long) TREE_INT_CST_LOW (n->low),
5212 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5213 == IDENTIFIER_NODE)
5214 ? TYPE_NAME (type)
5215 : DECL_NAME (TYPE_NAME (type))));
5217 if (!tree_int_cst_equal (n->low, n->high))
5219 for (chain = TYPE_VALUES (type);
5220 chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
5221 chain = TREE_CHAIN (chain))
5224 if (!chain)
5226 if (TYPE_NAME (type) == 0)
5227 warning ("case value `%ld' not in enumerated type",
5228 (long) TREE_INT_CST_LOW (n->high));
5229 else
5230 warning ("case value `%ld' not in enumerated type `%s'",
5231 (long) TREE_INT_CST_LOW (n->high),
5232 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5233 == IDENTIFIER_NODE)
5234 ? TYPE_NAME (type)
5235 : DECL_NAME (TYPE_NAME (type))));
5242 /* Maximum number of case bit tests. */
5243 #define MAX_CASE_BIT_TESTS 3
5245 /* By default, enable case bit tests on targets with ashlsi3. */
5246 #ifndef CASE_USE_BIT_TESTS
5247 #define CASE_USE_BIT_TESTS (ashl_optab->handlers[word_mode].insn_code \
5248 != CODE_FOR_nothing)
5249 #endif
5252 /* A case_bit_test represents a set of case nodes that may be
5253 selected from using a bit-wise comparison. HI and LO hold
5254 the integer to be tested against, LABEL contains the label
5255 to jump to upon success and BITS counts the number of case
5256 nodes handled by this test, typically the number of bits
5257 set in HI:LO. */
5259 struct case_bit_test
5261 HOST_WIDE_INT hi;
5262 HOST_WIDE_INT lo;
5263 rtx label;
5264 int bits;
5267 /* Determine whether "1 << x" is relatively cheap in word_mode. */
5269 static
5270 bool lshift_cheap_p (void)
5272 static bool init = false;
5273 static bool cheap = true;
5275 if (!init)
5277 rtx reg = gen_rtx_REG (word_mode, 10000);
5278 int cost = rtx_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg), SET);
5279 cheap = cost < COSTS_N_INSNS (3);
5280 init = true;
5283 return cheap;
5286 /* Comparison function for qsort to order bit tests by decreasing
5287 number of case nodes, i.e. the node with the most cases gets
5288 tested first. */
5290 static
5291 int case_bit_test_cmp (const void *p1, const void *p2)
5293 const struct case_bit_test *d1 = p1;
5294 const struct case_bit_test *d2 = p2;
5296 return d2->bits - d1->bits;
5299 /* Expand a switch statement by a short sequence of bit-wise
5300 comparisons. "switch(x)" is effectively converted into
5301 "if ((1 << (x-MINVAL)) & CST)" where CST and MINVAL are
5302 integer constants.
5304 INDEX_EXPR is the value being switched on, which is of
5305 type INDEX_TYPE. MINVAL is the lowest case value of in
5306 the case nodes, of INDEX_TYPE type, and RANGE is highest
5307 value minus MINVAL, also of type INDEX_TYPE. NODES is
5308 the set of case nodes, and DEFAULT_LABEL is the label to
5309 branch to should none of the cases match.
5311 There *MUST* be MAX_CASE_BIT_TESTS or less unique case
5312 node targets. */
5314 static void
5315 emit_case_bit_tests (tree index_type, tree index_expr, tree minval,
5316 tree range, case_node_ptr nodes, rtx default_label)
5318 struct case_bit_test test[MAX_CASE_BIT_TESTS];
5319 enum machine_mode mode;
5320 rtx expr, index, label;
5321 unsigned int i,j,lo,hi;
5322 struct case_node *n;
5323 unsigned int count;
5325 count = 0;
5326 for (n = nodes; n; n = n->right)
5328 label = label_rtx (n->code_label);
5329 for (i = 0; i < count; i++)
5330 if (same_case_target_p (label, test[i].label))
5331 break;
5333 if (i == count)
5335 if (count >= MAX_CASE_BIT_TESTS)
5336 abort ();
5337 test[i].hi = 0;
5338 test[i].lo = 0;
5339 test[i].label = label;
5340 test[i].bits = 1;
5341 count++;
5343 else
5344 test[i].bits++;
5346 lo = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5347 n->low, minval)), 1);
5348 hi = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5349 n->high, minval)), 1);
5350 for (j = lo; j <= hi; j++)
5351 if (j >= HOST_BITS_PER_WIDE_INT)
5352 test[i].hi |= (HOST_WIDE_INT) 1 << (j - HOST_BITS_PER_INT);
5353 else
5354 test[i].lo |= (HOST_WIDE_INT) 1 << j;
5357 qsort (test, count, sizeof(*test), case_bit_test_cmp);
5359 index_expr = fold (build (MINUS_EXPR, index_type,
5360 convert (index_type, index_expr),
5361 convert (index_type, minval)));
5362 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5363 emit_queue ();
5364 index = protect_from_queue (index, 0);
5365 do_pending_stack_adjust ();
5367 mode = TYPE_MODE (index_type);
5368 expr = expand_expr (range, NULL_RTX, VOIDmode, 0);
5369 emit_cmp_and_jump_insns (index, expr, GTU, NULL_RTX, mode, 1,
5370 default_label);
5372 index = convert_to_mode (word_mode, index, 0);
5373 index = expand_binop (word_mode, ashl_optab, const1_rtx,
5374 index, NULL_RTX, 1, OPTAB_WIDEN);
5376 for (i = 0; i < count; i++)
5378 expr = immed_double_const (test[i].lo, test[i].hi, word_mode);
5379 expr = expand_binop (word_mode, and_optab, index, expr,
5380 NULL_RTX, 1, OPTAB_WIDEN);
5381 emit_cmp_and_jump_insns (expr, const0_rtx, NE, NULL_RTX,
5382 word_mode, 1, test[i].label);
5385 emit_jump (default_label);
5388 /* Terminate a case (Pascal) or switch (C) statement
5389 in which ORIG_INDEX is the expression to be tested.
5390 If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
5391 type as given in the source before any compiler conversions.
5392 Generate the code to test it and jump to the right place. */
5394 void
5395 expand_end_case_type (tree orig_index, tree orig_type)
5397 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE;
5398 rtx default_label = 0;
5399 struct case_node *n, *m;
5400 unsigned int count, uniq;
5401 rtx index;
5402 rtx table_label;
5403 int ncases;
5404 rtx *labelvec;
5405 int i;
5406 rtx before_case, end, lab;
5407 struct nesting *thiscase = case_stack;
5408 tree index_expr, index_type;
5409 bool exit_done = false;
5410 int unsignedp;
5412 /* Don't crash due to previous errors. */
5413 if (thiscase == NULL)
5414 return;
5416 index_expr = thiscase->data.case_stmt.index_expr;
5417 index_type = TREE_TYPE (index_expr);
5418 unsignedp = TREE_UNSIGNED (index_type);
5419 if (orig_type == NULL)
5420 orig_type = TREE_TYPE (orig_index);
5422 do_pending_stack_adjust ();
5424 /* This might get a spurious warning in the presence of a syntax error;
5425 it could be fixed by moving the call to check_seenlabel after the
5426 check for error_mark_node, and copying the code of check_seenlabel that
5427 deals with case_stack->data.case_stmt.line_number_status /
5428 restore_line_number_status in front of the call to end_cleanup_deferral;
5429 However, this might miss some useful warnings in the presence of
5430 non-syntax errors. */
5431 check_seenlabel ();
5433 /* An ERROR_MARK occurs for various reasons including invalid data type. */
5434 if (index_type != error_mark_node)
5436 /* If the switch expression was an enumerated type, check that
5437 exactly all enumeration literals are covered by the cases.
5438 The check is made when -Wswitch was specified and there is no
5439 default case, or when -Wswitch-enum was specified. */
5440 if (((warn_switch && !thiscase->data.case_stmt.default_label)
5441 || warn_switch_enum)
5442 && TREE_CODE (orig_type) == ENUMERAL_TYPE
5443 && TREE_CODE (index_expr) != INTEGER_CST)
5444 check_for_full_enumeration_handling (orig_type);
5446 if (warn_switch_default && !thiscase->data.case_stmt.default_label)
5447 warning ("switch missing default case");
5449 /* If we don't have a default-label, create one here,
5450 after the body of the switch. */
5451 if (thiscase->data.case_stmt.default_label == 0)
5453 thiscase->data.case_stmt.default_label
5454 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5455 /* Share the exit label if possible. */
5456 if (thiscase->exit_label)
5458 SET_DECL_RTL (thiscase->data.case_stmt.default_label,
5459 thiscase->exit_label);
5460 exit_done = true;
5462 expand_label (thiscase->data.case_stmt.default_label);
5464 default_label = label_rtx (thiscase->data.case_stmt.default_label);
5466 before_case = get_last_insn ();
5468 if (thiscase->data.case_stmt.case_list
5469 && thiscase->data.case_stmt.case_list->left)
5470 thiscase->data.case_stmt.case_list
5471 = case_tree2list (thiscase->data.case_stmt.case_list, 0);
5473 /* Simplify the case-list before we count it. */
5474 group_case_nodes (thiscase->data.case_stmt.case_list);
5475 strip_default_case_nodes (&thiscase->data.case_stmt.case_list,
5476 default_label);
5478 /* Get upper and lower bounds of case values.
5479 Also convert all the case values to the index expr's data type. */
5481 uniq = 0;
5482 count = 0;
5483 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5485 /* Check low and high label values are integers. */
5486 if (TREE_CODE (n->low) != INTEGER_CST)
5487 abort ();
5488 if (TREE_CODE (n->high) != INTEGER_CST)
5489 abort ();
5491 n->low = convert (index_type, n->low);
5492 n->high = convert (index_type, n->high);
5494 /* Count the elements and track the largest and smallest
5495 of them (treating them as signed even if they are not). */
5496 if (count++ == 0)
5498 minval = n->low;
5499 maxval = n->high;
5501 else
5503 if (INT_CST_LT (n->low, minval))
5504 minval = n->low;
5505 if (INT_CST_LT (maxval, n->high))
5506 maxval = n->high;
5508 /* A range counts double, since it requires two compares. */
5509 if (! tree_int_cst_equal (n->low, n->high))
5510 count++;
5512 /* Count the number of unique case node targets. */
5513 uniq++;
5514 lab = label_rtx (n->code_label);
5515 for (m = thiscase->data.case_stmt.case_list; m != n; m = m->right)
5516 if (same_case_target_p (label_rtx (m->code_label), lab))
5518 uniq--;
5519 break;
5523 /* Compute span of values. */
5524 if (count != 0)
5525 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
5527 end_cleanup_deferral ();
5529 if (count == 0)
5531 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
5532 emit_queue ();
5533 emit_jump (default_label);
5536 /* Try implementing this switch statement by a short sequence of
5537 bit-wise comparisons. However, we let the binary-tree case
5538 below handle constant index expressions. */
5539 else if (CASE_USE_BIT_TESTS
5540 && ! TREE_CONSTANT (index_expr)
5541 && compare_tree_int (range, GET_MODE_BITSIZE (word_mode)) < 0
5542 && compare_tree_int (range, 0) > 0
5543 && lshift_cheap_p ()
5544 && ((uniq == 1 && count >= 3)
5545 || (uniq == 2 && count >= 5)
5546 || (uniq == 3 && count >= 6)))
5548 /* Optimize the case where all the case values fit in a
5549 word without having to subtract MINVAL. In this case,
5550 we can optimize away the subtraction. */
5551 if (compare_tree_int (minval, 0) > 0
5552 && compare_tree_int (maxval, GET_MODE_BITSIZE (word_mode)) < 0)
5554 minval = integer_zero_node;
5555 range = maxval;
5557 emit_case_bit_tests (index_type, index_expr, minval, range,
5558 thiscase->data.case_stmt.case_list,
5559 default_label);
5562 /* If range of values is much bigger than number of values,
5563 make a sequence of conditional branches instead of a dispatch.
5564 If the switch-index is a constant, do it this way
5565 because we can optimize it. */
5567 else if (count < case_values_threshold ()
5568 || compare_tree_int (range,
5569 (optimize_size ? 3 : 10) * count) > 0
5570 /* RANGE may be signed, and really large ranges will show up
5571 as negative numbers. */
5572 || compare_tree_int (range, 0) < 0
5573 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
5574 || flag_pic
5575 #endif
5576 || TREE_CONSTANT (index_expr))
5578 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5580 /* If the index is a short or char that we do not have
5581 an insn to handle comparisons directly, convert it to
5582 a full integer now, rather than letting each comparison
5583 generate the conversion. */
5585 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
5586 && ! have_insn_for (COMPARE, GET_MODE (index)))
5588 enum machine_mode wider_mode;
5589 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
5590 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
5591 if (have_insn_for (COMPARE, wider_mode))
5593 index = convert_to_mode (wider_mode, index, unsignedp);
5594 break;
5598 emit_queue ();
5599 do_pending_stack_adjust ();
5601 index = protect_from_queue (index, 0);
5602 if (GET_CODE (index) == MEM)
5603 index = copy_to_reg (index);
5604 if (GET_CODE (index) == CONST_INT
5605 || TREE_CODE (index_expr) == INTEGER_CST)
5607 /* Make a tree node with the proper constant value
5608 if we don't already have one. */
5609 if (TREE_CODE (index_expr) != INTEGER_CST)
5611 index_expr
5612 = build_int_2 (INTVAL (index),
5613 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
5614 index_expr = convert (index_type, index_expr);
5617 /* For constant index expressions we need only
5618 issue an unconditional branch to the appropriate
5619 target code. The job of removing any unreachable
5620 code is left to the optimization phase if the
5621 "-O" option is specified. */
5622 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5623 if (! tree_int_cst_lt (index_expr, n->low)
5624 && ! tree_int_cst_lt (n->high, index_expr))
5625 break;
5627 if (n)
5628 emit_jump (label_rtx (n->code_label));
5629 else
5630 emit_jump (default_label);
5632 else
5634 /* If the index expression is not constant we generate
5635 a binary decision tree to select the appropriate
5636 target code. This is done as follows:
5638 The list of cases is rearranged into a binary tree,
5639 nearly optimal assuming equal probability for each case.
5641 The tree is transformed into RTL, eliminating
5642 redundant test conditions at the same time.
5644 If program flow could reach the end of the
5645 decision tree an unconditional jump to the
5646 default code is emitted. */
5648 use_cost_table
5649 = (TREE_CODE (orig_type) != ENUMERAL_TYPE
5650 && estimate_case_costs (thiscase->data.case_stmt.case_list));
5651 balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
5652 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
5653 default_label, index_type);
5654 emit_jump_if_reachable (default_label);
5657 else
5659 table_label = gen_label_rtx ();
5660 if (! try_casesi (index_type, index_expr, minval, range,
5661 table_label, default_label))
5663 index_type = thiscase->data.case_stmt.nominal_type;
5665 /* Index jumptables from zero for suitable values of
5666 minval to avoid a subtraction. */
5667 if (! optimize_size
5668 && compare_tree_int (minval, 0) > 0
5669 && compare_tree_int (minval, 3) < 0)
5671 minval = integer_zero_node;
5672 range = maxval;
5675 if (! try_tablejump (index_type, index_expr, minval, range,
5676 table_label, default_label))
5677 abort ();
5680 /* Get table of labels to jump to, in order of case index. */
5682 ncases = tree_low_cst (range, 0) + 1;
5683 labelvec = alloca (ncases * sizeof (rtx));
5684 memset (labelvec, 0, ncases * sizeof (rtx));
5686 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5688 /* Compute the low and high bounds relative to the minimum
5689 value since that should fit in a HOST_WIDE_INT while the
5690 actual values may not. */
5691 HOST_WIDE_INT i_low
5692 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5693 n->low, minval)), 1);
5694 HOST_WIDE_INT i_high
5695 = tree_low_cst (fold (build (MINUS_EXPR, index_type,
5696 n->high, minval)), 1);
5697 HOST_WIDE_INT i;
5699 for (i = i_low; i <= i_high; i ++)
5700 labelvec[i]
5701 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
5704 /* Fill in the gaps with the default. */
5705 for (i = 0; i < ncases; i++)
5706 if (labelvec[i] == 0)
5707 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
5709 /* Output the table. */
5710 emit_label (table_label);
5712 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
5713 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
5714 gen_rtx_LABEL_REF (Pmode, table_label),
5715 gen_rtvec_v (ncases, labelvec),
5716 const0_rtx, const0_rtx));
5717 else
5718 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
5719 gen_rtvec_v (ncases, labelvec)));
5721 /* If the case insn drops through the table,
5722 after the table we must jump to the default-label.
5723 Otherwise record no drop-through after the table. */
5724 #ifdef CASE_DROPS_THROUGH
5725 emit_jump (default_label);
5726 #else
5727 emit_barrier ();
5728 #endif
5731 before_case = NEXT_INSN (before_case);
5732 end = get_last_insn ();
5733 if (squeeze_notes (&before_case, &end))
5734 abort ();
5735 reorder_insns (before_case, end,
5736 thiscase->data.case_stmt.start);
5738 else
5739 end_cleanup_deferral ();
5741 if (thiscase->exit_label && !exit_done)
5742 emit_label (thiscase->exit_label);
5744 POPSTACK (case_stack);
5746 free_temp_slots ();
5749 /* Convert the tree NODE into a list linked by the right field, with the left
5750 field zeroed. RIGHT is used for recursion; it is a list to be placed
5751 rightmost in the resulting list. */
5753 static struct case_node *
5754 case_tree2list (struct case_node *node, struct case_node *right)
5756 struct case_node *left;
5758 if (node->right)
5759 right = case_tree2list (node->right, right);
5761 node->right = right;
5762 if ((left = node->left))
5764 node->left = 0;
5765 return case_tree2list (left, node);
5768 return node;
5771 /* Generate code to jump to LABEL if OP1 and OP2 are equal. */
5773 static void
5774 do_jump_if_equal (rtx op1, rtx op2, rtx label, int unsignedp)
5776 if (GET_CODE (op1) == CONST_INT && GET_CODE (op2) == CONST_INT)
5778 if (op1 == op2)
5779 emit_jump (label);
5781 else
5782 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX,
5783 (GET_MODE (op1) == VOIDmode
5784 ? GET_MODE (op2) : GET_MODE (op1)),
5785 unsignedp, label);
5788 /* Not all case values are encountered equally. This function
5789 uses a heuristic to weight case labels, in cases where that
5790 looks like a reasonable thing to do.
5792 Right now, all we try to guess is text, and we establish the
5793 following weights:
5795 chars above space: 16
5796 digits: 16
5797 default: 12
5798 space, punct: 8
5799 tab: 4
5800 newline: 2
5801 other "\" chars: 1
5802 remaining chars: 0
5804 If we find any cases in the switch that are not either -1 or in the range
5805 of valid ASCII characters, or are control characters other than those
5806 commonly used with "\", don't treat this switch scanning text.
5808 Return 1 if these nodes are suitable for cost estimation, otherwise
5809 return 0. */
5811 static int
5812 estimate_case_costs (case_node_ptr node)
5814 tree min_ascii = integer_minus_one_node;
5815 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5816 case_node_ptr n;
5817 int i;
5819 /* If we haven't already made the cost table, make it now. Note that the
5820 lower bound of the table is -1, not zero. */
5822 if (! cost_table_initialized)
5824 cost_table_initialized = 1;
5826 for (i = 0; i < 128; i++)
5828 if (ISALNUM (i))
5829 COST_TABLE (i) = 16;
5830 else if (ISPUNCT (i))
5831 COST_TABLE (i) = 8;
5832 else if (ISCNTRL (i))
5833 COST_TABLE (i) = -1;
5836 COST_TABLE (' ') = 8;
5837 COST_TABLE ('\t') = 4;
5838 COST_TABLE ('\0') = 4;
5839 COST_TABLE ('\n') = 2;
5840 COST_TABLE ('\f') = 1;
5841 COST_TABLE ('\v') = 1;
5842 COST_TABLE ('\b') = 1;
5845 /* See if all the case expressions look like text. It is text if the
5846 constant is >= -1 and the highest constant is <= 127. Do all comparisons
5847 as signed arithmetic since we don't want to ever access cost_table with a
5848 value less than -1. Also check that none of the constants in a range
5849 are strange control characters. */
5851 for (n = node; n; n = n->right)
5853 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5854 return 0;
5856 for (i = (HOST_WIDE_INT) TREE_INT_CST_LOW (n->low);
5857 i <= (HOST_WIDE_INT) TREE_INT_CST_LOW (n->high); i++)
5858 if (COST_TABLE (i) < 0)
5859 return 0;
5862 /* All interesting values are within the range of interesting
5863 ASCII characters. */
5864 return 1;
5867 /* Determine whether two case labels branch to the same target. */
5869 static bool
5870 same_case_target_p (rtx l1, rtx l2)
5872 rtx i1, i2;
5874 if (l1 == l2)
5875 return true;
5877 i1 = next_real_insn (l1);
5878 i2 = next_real_insn (l2);
5879 if (i1 == i2)
5880 return true;
5882 if (i1 && simplejump_p (i1))
5884 l1 = XEXP (SET_SRC (PATTERN (i1)), 0);
5887 if (i2 && simplejump_p (i2))
5889 l2 = XEXP (SET_SRC (PATTERN (i2)), 0);
5891 return l1 == l2;
5894 /* Delete nodes that branch to the default label from a list of
5895 case nodes. Eg. case 5: default: becomes just default: */
5897 static void
5898 strip_default_case_nodes (case_node_ptr *prev, rtx deflab)
5900 case_node_ptr ptr;
5902 while (*prev)
5904 ptr = *prev;
5905 if (same_case_target_p (label_rtx (ptr->code_label), deflab))
5906 *prev = ptr->right;
5907 else
5908 prev = &ptr->right;
5912 /* Scan an ordered list of case nodes
5913 combining those with consecutive values or ranges.
5915 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
5917 static void
5918 group_case_nodes (case_node_ptr head)
5920 case_node_ptr node = head;
5922 while (node)
5924 rtx lab = label_rtx (node->code_label);
5925 case_node_ptr np = node;
5927 /* Try to group the successors of NODE with NODE. */
5928 while (((np = np->right) != 0)
5929 /* Do they jump to the same place? */
5930 && same_case_target_p (label_rtx (np->code_label), lab)
5931 /* Are their ranges consecutive? */
5932 && tree_int_cst_equal (np->low,
5933 fold (build (PLUS_EXPR,
5934 TREE_TYPE (node->high),
5935 node->high,
5936 integer_one_node)))
5937 /* An overflow is not consecutive. */
5938 && tree_int_cst_lt (node->high,
5939 fold (build (PLUS_EXPR,
5940 TREE_TYPE (node->high),
5941 node->high,
5942 integer_one_node))))
5944 node->high = np->high;
5946 /* NP is the first node after NODE which can't be grouped with it.
5947 Delete the nodes in between, and move on to that node. */
5948 node->right = np;
5949 node = np;
5953 /* Take an ordered list of case nodes
5954 and transform them into a near optimal binary tree,
5955 on the assumption that any target code selection value is as
5956 likely as any other.
5958 The transformation is performed by splitting the ordered
5959 list into two equal sections plus a pivot. The parts are
5960 then attached to the pivot as left and right branches. Each
5961 branch is then transformed recursively. */
5963 static void
5964 balance_case_nodes (case_node_ptr *head, case_node_ptr parent)
5966 case_node_ptr np;
5968 np = *head;
5969 if (np)
5971 int cost = 0;
5972 int i = 0;
5973 int ranges = 0;
5974 case_node_ptr *npp;
5975 case_node_ptr left;
5977 /* Count the number of entries on branch. Also count the ranges. */
5979 while (np)
5981 if (!tree_int_cst_equal (np->low, np->high))
5983 ranges++;
5984 if (use_cost_table)
5985 cost += COST_TABLE (TREE_INT_CST_LOW (np->high));
5988 if (use_cost_table)
5989 cost += COST_TABLE (TREE_INT_CST_LOW (np->low));
5991 i++;
5992 np = np->right;
5995 if (i > 2)
5997 /* Split this list if it is long enough for that to help. */
5998 npp = head;
5999 left = *npp;
6000 if (use_cost_table)
6002 /* Find the place in the list that bisects the list's total cost,
6003 Here I gets half the total cost. */
6004 int n_moved = 0;
6005 i = (cost + 1) / 2;
6006 while (1)
6008 /* Skip nodes while their cost does not reach that amount. */
6009 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
6010 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->high));
6011 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->low));
6012 if (i <= 0)
6013 break;
6014 npp = &(*npp)->right;
6015 n_moved += 1;
6017 if (n_moved == 0)
6019 /* Leave this branch lopsided, but optimize left-hand
6020 side and fill in `parent' fields for right-hand side. */
6021 np = *head;
6022 np->parent = parent;
6023 balance_case_nodes (&np->left, np);
6024 for (; np->right; np = np->right)
6025 np->right->parent = np;
6026 return;
6029 /* If there are just three nodes, split at the middle one. */
6030 else if (i == 3)
6031 npp = &(*npp)->right;
6032 else
6034 /* Find the place in the list that bisects the list's total cost,
6035 where ranges count as 2.
6036 Here I gets half the total cost. */
6037 i = (i + ranges + 1) / 2;
6038 while (1)
6040 /* Skip nodes while their cost does not reach that amount. */
6041 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
6042 i--;
6043 i--;
6044 if (i <= 0)
6045 break;
6046 npp = &(*npp)->right;
6049 *head = np = *npp;
6050 *npp = 0;
6051 np->parent = parent;
6052 np->left = left;
6054 /* Optimize each of the two split parts. */
6055 balance_case_nodes (&np->left, np);
6056 balance_case_nodes (&np->right, np);
6058 else
6060 /* Else leave this branch as one level,
6061 but fill in `parent' fields. */
6062 np = *head;
6063 np->parent = parent;
6064 for (; np->right; np = np->right)
6065 np->right->parent = np;
6070 /* Search the parent sections of the case node tree
6071 to see if a test for the lower bound of NODE would be redundant.
6072 INDEX_TYPE is the type of the index expression.
6074 The instructions to generate the case decision tree are
6075 output in the same order as nodes are processed so it is
6076 known that if a parent node checks the range of the current
6077 node minus one that the current node is bounded at its lower
6078 span. Thus the test would be redundant. */
6080 static int
6081 node_has_low_bound (case_node_ptr node, tree index_type)
6083 tree low_minus_one;
6084 case_node_ptr pnode;
6086 /* If the lower bound of this node is the lowest value in the index type,
6087 we need not test it. */
6089 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
6090 return 1;
6092 /* If this node has a left branch, the value at the left must be less
6093 than that at this node, so it cannot be bounded at the bottom and
6094 we need not bother testing any further. */
6096 if (node->left)
6097 return 0;
6099 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
6100 node->low, integer_one_node));
6102 /* If the subtraction above overflowed, we can't verify anything.
6103 Otherwise, look for a parent that tests our value - 1. */
6105 if (! tree_int_cst_lt (low_minus_one, node->low))
6106 return 0;
6108 for (pnode = node->parent; pnode; pnode = pnode->parent)
6109 if (tree_int_cst_equal (low_minus_one, pnode->high))
6110 return 1;
6112 return 0;
6115 /* Search the parent sections of the case node tree
6116 to see if a test for the upper bound of NODE would be redundant.
6117 INDEX_TYPE is the type of the index expression.
6119 The instructions to generate the case decision tree are
6120 output in the same order as nodes are processed so it is
6121 known that if a parent node checks the range of the current
6122 node plus one that the current node is bounded at its upper
6123 span. Thus the test would be redundant. */
6125 static int
6126 node_has_high_bound (case_node_ptr node, tree index_type)
6128 tree high_plus_one;
6129 case_node_ptr pnode;
6131 /* If there is no upper bound, obviously no test is needed. */
6133 if (TYPE_MAX_VALUE (index_type) == NULL)
6134 return 1;
6136 /* If the upper bound of this node is the highest value in the type
6137 of the index expression, we need not test against it. */
6139 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
6140 return 1;
6142 /* If this node has a right branch, the value at the right must be greater
6143 than that at this node, so it cannot be bounded at the top and
6144 we need not bother testing any further. */
6146 if (node->right)
6147 return 0;
6149 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
6150 node->high, integer_one_node));
6152 /* If the addition above overflowed, we can't verify anything.
6153 Otherwise, look for a parent that tests our value + 1. */
6155 if (! tree_int_cst_lt (node->high, high_plus_one))
6156 return 0;
6158 for (pnode = node->parent; pnode; pnode = pnode->parent)
6159 if (tree_int_cst_equal (high_plus_one, pnode->low))
6160 return 1;
6162 return 0;
6165 /* Search the parent sections of the
6166 case node tree to see if both tests for the upper and lower
6167 bounds of NODE would be redundant. */
6169 static int
6170 node_is_bounded (case_node_ptr node, tree index_type)
6172 return (node_has_low_bound (node, index_type)
6173 && node_has_high_bound (node, index_type));
6176 /* Emit an unconditional jump to LABEL unless it would be dead code. */
6178 static void
6179 emit_jump_if_reachable (rtx label)
6181 if (GET_CODE (get_last_insn ()) != BARRIER)
6182 emit_jump (label);
6185 /* Emit step-by-step code to select a case for the value of INDEX.
6186 The thus generated decision tree follows the form of the
6187 case-node binary tree NODE, whose nodes represent test conditions.
6188 INDEX_TYPE is the type of the index of the switch.
6190 Care is taken to prune redundant tests from the decision tree
6191 by detecting any boundary conditions already checked by
6192 emitted rtx. (See node_has_high_bound, node_has_low_bound
6193 and node_is_bounded, above.)
6195 Where the test conditions can be shown to be redundant we emit
6196 an unconditional jump to the target code. As a further
6197 optimization, the subordinates of a tree node are examined to
6198 check for bounded nodes. In this case conditional and/or
6199 unconditional jumps as a result of the boundary check for the
6200 current node are arranged to target the subordinates associated
6201 code for out of bound conditions on the current node.
6203 We can assume that when control reaches the code generated here,
6204 the index value has already been compared with the parents
6205 of this node, and determined to be on the same side of each parent
6206 as this node is. Thus, if this node tests for the value 51,
6207 and a parent tested for 52, we don't need to consider
6208 the possibility of a value greater than 51. If another parent
6209 tests for the value 50, then this node need not test anything. */
6211 static void
6212 emit_case_nodes (rtx index, case_node_ptr node, rtx default_label,
6213 tree index_type)
6215 /* If INDEX has an unsigned type, we must make unsigned branches. */
6216 int unsignedp = TREE_UNSIGNED (index_type);
6217 enum machine_mode mode = GET_MODE (index);
6218 enum machine_mode imode = TYPE_MODE (index_type);
6220 /* See if our parents have already tested everything for us.
6221 If they have, emit an unconditional jump for this node. */
6222 if (node_is_bounded (node, index_type))
6223 emit_jump (label_rtx (node->code_label));
6225 else if (tree_int_cst_equal (node->low, node->high))
6227 /* Node is single valued. First see if the index expression matches
6228 this node and then check our children, if any. */
6230 do_jump_if_equal (index,
6231 convert_modes (mode, imode,
6232 expand_expr (node->low, NULL_RTX,
6233 VOIDmode, 0),
6234 unsignedp),
6235 label_rtx (node->code_label), unsignedp);
6237 if (node->right != 0 && node->left != 0)
6239 /* This node has children on both sides.
6240 Dispatch to one side or the other
6241 by comparing the index value with this node's value.
6242 If one subtree is bounded, check that one first,
6243 so we can avoid real branches in the tree. */
6245 if (node_is_bounded (node->right, index_type))
6247 emit_cmp_and_jump_insns (index,
6248 convert_modes
6249 (mode, imode,
6250 expand_expr (node->high, NULL_RTX,
6251 VOIDmode, 0),
6252 unsignedp),
6253 GT, NULL_RTX, mode, unsignedp,
6254 label_rtx (node->right->code_label));
6255 emit_case_nodes (index, node->left, default_label, index_type);
6258 else if (node_is_bounded (node->left, index_type))
6260 emit_cmp_and_jump_insns (index,
6261 convert_modes
6262 (mode, imode,
6263 expand_expr (node->high, NULL_RTX,
6264 VOIDmode, 0),
6265 unsignedp),
6266 LT, NULL_RTX, mode, unsignedp,
6267 label_rtx (node->left->code_label));
6268 emit_case_nodes (index, node->right, default_label, index_type);
6271 else
6273 /* Neither node is bounded. First distinguish the two sides;
6274 then emit the code for one side at a time. */
6276 tree test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6278 /* See if the value is on the right. */
6279 emit_cmp_and_jump_insns (index,
6280 convert_modes
6281 (mode, imode,
6282 expand_expr (node->high, NULL_RTX,
6283 VOIDmode, 0),
6284 unsignedp),
6285 GT, NULL_RTX, mode, unsignedp,
6286 label_rtx (test_label));
6288 /* Value must be on the left.
6289 Handle the left-hand subtree. */
6290 emit_case_nodes (index, node->left, default_label, index_type);
6291 /* If left-hand subtree does nothing,
6292 go to default. */
6293 emit_jump_if_reachable (default_label);
6295 /* Code branches here for the right-hand subtree. */
6296 expand_label (test_label);
6297 emit_case_nodes (index, node->right, default_label, index_type);
6301 else if (node->right != 0 && node->left == 0)
6303 /* Here we have a right child but no left so we issue conditional
6304 branch to default and process the right child.
6306 Omit the conditional branch to default if we it avoid only one
6307 right child; it costs too much space to save so little time. */
6309 if (node->right->right || node->right->left
6310 || !tree_int_cst_equal (node->right->low, node->right->high))
6312 if (!node_has_low_bound (node, index_type))
6314 emit_cmp_and_jump_insns (index,
6315 convert_modes
6316 (mode, imode,
6317 expand_expr (node->high, NULL_RTX,
6318 VOIDmode, 0),
6319 unsignedp),
6320 LT, NULL_RTX, mode, unsignedp,
6321 default_label);
6324 emit_case_nodes (index, node->right, default_label, index_type);
6326 else
6327 /* We cannot process node->right normally
6328 since we haven't ruled out the numbers less than
6329 this node's value. So handle node->right explicitly. */
6330 do_jump_if_equal (index,
6331 convert_modes
6332 (mode, imode,
6333 expand_expr (node->right->low, NULL_RTX,
6334 VOIDmode, 0),
6335 unsignedp),
6336 label_rtx (node->right->code_label), unsignedp);
6339 else if (node->right == 0 && node->left != 0)
6341 /* Just one subtree, on the left. */
6342 if (node->left->left || node->left->right
6343 || !tree_int_cst_equal (node->left->low, node->left->high))
6345 if (!node_has_high_bound (node, index_type))
6347 emit_cmp_and_jump_insns (index,
6348 convert_modes
6349 (mode, imode,
6350 expand_expr (node->high, NULL_RTX,
6351 VOIDmode, 0),
6352 unsignedp),
6353 GT, NULL_RTX, mode, unsignedp,
6354 default_label);
6357 emit_case_nodes (index, node->left, default_label, index_type);
6359 else
6360 /* We cannot process node->left normally
6361 since we haven't ruled out the numbers less than
6362 this node's value. So handle node->left explicitly. */
6363 do_jump_if_equal (index,
6364 convert_modes
6365 (mode, imode,
6366 expand_expr (node->left->low, NULL_RTX,
6367 VOIDmode, 0),
6368 unsignedp),
6369 label_rtx (node->left->code_label), unsignedp);
6372 else
6374 /* Node is a range. These cases are very similar to those for a single
6375 value, except that we do not start by testing whether this node
6376 is the one to branch to. */
6378 if (node->right != 0 && node->left != 0)
6380 /* Node has subtrees on both sides.
6381 If the right-hand subtree is bounded,
6382 test for it first, since we can go straight there.
6383 Otherwise, we need to make a branch in the control structure,
6384 then handle the two subtrees. */
6385 tree test_label = 0;
6387 if (node_is_bounded (node->right, index_type))
6388 /* Right hand node is fully bounded so we can eliminate any
6389 testing and branch directly to the target code. */
6390 emit_cmp_and_jump_insns (index,
6391 convert_modes
6392 (mode, imode,
6393 expand_expr (node->high, NULL_RTX,
6394 VOIDmode, 0),
6395 unsignedp),
6396 GT, NULL_RTX, mode, unsignedp,
6397 label_rtx (node->right->code_label));
6398 else
6400 /* Right hand node requires testing.
6401 Branch to a label where we will handle it later. */
6403 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6404 emit_cmp_and_jump_insns (index,
6405 convert_modes
6406 (mode, imode,
6407 expand_expr (node->high, NULL_RTX,
6408 VOIDmode, 0),
6409 unsignedp),
6410 GT, NULL_RTX, mode, unsignedp,
6411 label_rtx (test_label));
6414 /* Value belongs to this node or to the left-hand subtree. */
6416 emit_cmp_and_jump_insns (index,
6417 convert_modes
6418 (mode, imode,
6419 expand_expr (node->low, NULL_RTX,
6420 VOIDmode, 0),
6421 unsignedp),
6422 GE, NULL_RTX, mode, unsignedp,
6423 label_rtx (node->code_label));
6425 /* Handle the left-hand subtree. */
6426 emit_case_nodes (index, node->left, default_label, index_type);
6428 /* If right node had to be handled later, do that now. */
6430 if (test_label)
6432 /* If the left-hand subtree fell through,
6433 don't let it fall into the right-hand subtree. */
6434 emit_jump_if_reachable (default_label);
6436 expand_label (test_label);
6437 emit_case_nodes (index, node->right, default_label, index_type);
6441 else if (node->right != 0 && node->left == 0)
6443 /* Deal with values to the left of this node,
6444 if they are possible. */
6445 if (!node_has_low_bound (node, index_type))
6447 emit_cmp_and_jump_insns (index,
6448 convert_modes
6449 (mode, imode,
6450 expand_expr (node->low, NULL_RTX,
6451 VOIDmode, 0),
6452 unsignedp),
6453 LT, NULL_RTX, mode, unsignedp,
6454 default_label);
6457 /* Value belongs to this node or to the right-hand subtree. */
6459 emit_cmp_and_jump_insns (index,
6460 convert_modes
6461 (mode, imode,
6462 expand_expr (node->high, NULL_RTX,
6463 VOIDmode, 0),
6464 unsignedp),
6465 LE, NULL_RTX, mode, unsignedp,
6466 label_rtx (node->code_label));
6468 emit_case_nodes (index, node->right, default_label, index_type);
6471 else if (node->right == 0 && node->left != 0)
6473 /* Deal with values to the right of this node,
6474 if they are possible. */
6475 if (!node_has_high_bound (node, index_type))
6477 emit_cmp_and_jump_insns (index,
6478 convert_modes
6479 (mode, imode,
6480 expand_expr (node->high, NULL_RTX,
6481 VOIDmode, 0),
6482 unsignedp),
6483 GT, NULL_RTX, mode, unsignedp,
6484 default_label);
6487 /* Value belongs to this node or to the left-hand subtree. */
6489 emit_cmp_and_jump_insns (index,
6490 convert_modes
6491 (mode, imode,
6492 expand_expr (node->low, NULL_RTX,
6493 VOIDmode, 0),
6494 unsignedp),
6495 GE, NULL_RTX, mode, unsignedp,
6496 label_rtx (node->code_label));
6498 emit_case_nodes (index, node->left, default_label, index_type);
6501 else
6503 /* Node has no children so we check low and high bounds to remove
6504 redundant tests. Only one of the bounds can exist,
6505 since otherwise this node is bounded--a case tested already. */
6506 int high_bound = node_has_high_bound (node, index_type);
6507 int low_bound = node_has_low_bound (node, index_type);
6509 if (!high_bound && low_bound)
6511 emit_cmp_and_jump_insns (index,
6512 convert_modes
6513 (mode, imode,
6514 expand_expr (node->high, NULL_RTX,
6515 VOIDmode, 0),
6516 unsignedp),
6517 GT, NULL_RTX, mode, unsignedp,
6518 default_label);
6521 else if (!low_bound && high_bound)
6523 emit_cmp_and_jump_insns (index,
6524 convert_modes
6525 (mode, imode,
6526 expand_expr (node->low, NULL_RTX,
6527 VOIDmode, 0),
6528 unsignedp),
6529 LT, NULL_RTX, mode, unsignedp,
6530 default_label);
6532 else if (!low_bound && !high_bound)
6534 /* Widen LOW and HIGH to the same width as INDEX. */
6535 tree type = (*lang_hooks.types.type_for_mode) (mode, unsignedp);
6536 tree low = build1 (CONVERT_EXPR, type, node->low);
6537 tree high = build1 (CONVERT_EXPR, type, node->high);
6538 rtx low_rtx, new_index, new_bound;
6540 /* Instead of doing two branches, emit one unsigned branch for
6541 (index-low) > (high-low). */
6542 low_rtx = expand_expr (low, NULL_RTX, mode, 0);
6543 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
6544 NULL_RTX, unsignedp,
6545 OPTAB_WIDEN);
6546 new_bound = expand_expr (fold (build (MINUS_EXPR, type,
6547 high, low)),
6548 NULL_RTX, mode, 0);
6550 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
6551 mode, 1, default_label);
6554 emit_jump (label_rtx (node->code_label));
6559 #include "gt-stmt.h"