* class.c (check_bitfield_decl): New function, split out from
[official-gcc.git] / gcc / stmt.c
blob715f6e0cd69e83c3d80f9a45a0ae34faa33a3a1c
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 88, 89, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 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"
39 #include "rtl.h"
40 #include "tree.h"
41 #include "tm_p.h"
42 #include "flags.h"
43 #include "except.h"
44 #include "function.h"
45 #include "insn-flags.h"
46 #include "insn-config.h"
47 #include "insn-codes.h"
48 #include "expr.h"
49 #include "hard-reg-set.h"
50 #include "obstack.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"
58 #define obstack_chunk_alloc xmalloc
59 #define obstack_chunk_free free
60 struct obstack stmt_obstack;
62 /* Assume that case vectors are not pc-relative. */
63 #ifndef CASE_VECTOR_PC_RELATIVE
64 #define CASE_VECTOR_PC_RELATIVE 0
65 #endif
68 /* Functions and data structures for expanding case statements. */
70 /* Case label structure, used to hold info on labels within case
71 statements. We handle "range" labels; for a single-value label
72 as in C, the high and low limits are the same.
74 An AVL tree of case nodes is initially created, and later transformed
75 to a list linked via the RIGHT fields in the nodes. Nodes with
76 higher case values are later in the list.
78 Switch statements can be output in one of two forms. A branch table
79 is used if there are more than a few labels and the labels are dense
80 within the range between the smallest and largest case value. If a
81 branch table is used, no further manipulations are done with the case
82 node chain.
84 The alternative to the use of a branch table is to generate a series
85 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
86 and PARENT fields to hold a binary tree. Initially the tree is
87 totally unbalanced, with everything on the right. We balance the tree
88 with nodes on the left having lower case values than the parent
89 and nodes on the right having higher values. We then output the tree
90 in order. */
92 struct case_node
94 struct case_node *left; /* Left son in binary tree */
95 struct case_node *right; /* Right son in binary tree; also node chain */
96 struct case_node *parent; /* Parent of node in binary tree */
97 tree low; /* Lowest index value for this label */
98 tree high; /* Highest index value for this label */
99 tree code_label; /* Label to jump to when node matches */
100 int balance;
103 typedef struct case_node case_node;
104 typedef struct case_node *case_node_ptr;
106 /* These are used by estimate_case_costs and balance_case_nodes. */
108 /* This must be a signed type, and non-ANSI compilers lack signed char. */
109 static short *cost_table;
110 static int use_cost_table;
112 /* Stack of control and binding constructs we are currently inside.
114 These constructs begin when you call `expand_start_WHATEVER'
115 and end when you call `expand_end_WHATEVER'. This stack records
116 info about how the construct began that tells the end-function
117 what to do. It also may provide information about the construct
118 to alter the behavior of other constructs within the body.
119 For example, they may affect the behavior of C `break' and `continue'.
121 Each construct gets one `struct nesting' object.
122 All of these objects are chained through the `all' field.
123 `nesting_stack' points to the first object (innermost construct).
124 The position of an entry on `nesting_stack' is in its `depth' field.
126 Each type of construct has its own individual stack.
127 For example, loops have `loop_stack'. Each object points to the
128 next object of the same type through the `next' field.
130 Some constructs are visible to `break' exit-statements and others
131 are not. Which constructs are visible depends on the language.
132 Therefore, the data structure allows each construct to be visible
133 or not, according to the args given when the construct is started.
134 The construct is visible if the `exit_label' field is non-null.
135 In that case, the value should be a CODE_LABEL rtx. */
137 struct nesting
139 struct nesting *all;
140 struct nesting *next;
141 int depth;
142 rtx exit_label;
143 union
145 /* For conds (if-then and if-then-else statements). */
146 struct
148 /* Label for the end of the if construct.
149 There is none if EXITFLAG was not set
150 and no `else' has been seen yet. */
151 rtx endif_label;
152 /* Label for the end of this alternative.
153 This may be the end of the if or the next else/elseif. */
154 rtx next_label;
155 } cond;
156 /* For loops. */
157 struct
159 /* Label at the top of the loop; place to loop back to. */
160 rtx start_label;
161 /* Label at the end of the whole construct. */
162 rtx end_label;
163 /* Label before a jump that branches to the end of the whole
164 construct. This is where destructors go if any. */
165 rtx alt_end_label;
166 /* Label for `continue' statement to jump to;
167 this is in front of the stepper of the loop. */
168 rtx continue_label;
169 } loop;
170 /* For variable binding contours. */
171 struct
173 /* Sequence number of this binding contour within the function,
174 in order of entry. */
175 int block_start_count;
176 /* Nonzero => value to restore stack to on exit. */
177 rtx stack_level;
178 /* The NOTE that starts this contour.
179 Used by expand_goto to check whether the destination
180 is within each contour or not. */
181 rtx first_insn;
182 /* Innermost containing binding contour that has a stack level. */
183 struct nesting *innermost_stack_block;
184 /* List of cleanups to be run on exit from this contour.
185 This is a list of expressions to be evaluated.
186 The TREE_PURPOSE of each link is the ..._DECL node
187 which the cleanup pertains to. */
188 tree cleanups;
189 /* List of cleanup-lists of blocks containing this block,
190 as they were at the locus where this block appears.
191 There is an element for each containing block,
192 ordered innermost containing block first.
193 The tail of this list can be 0,
194 if all remaining elements would be empty lists.
195 The element's TREE_VALUE is the cleanup-list of that block,
196 which may be null. */
197 tree outer_cleanups;
198 /* Chain of labels defined inside this binding contour.
199 For contours that have stack levels or cleanups. */
200 struct label_chain *label_chain;
201 /* Number of function calls seen, as of start of this block. */
202 int n_function_calls;
203 /* Nonzero if this is associated with a EH region. */
204 int exception_region;
205 /* The saved target_temp_slot_level from our outer block.
206 We may reset target_temp_slot_level to be the level of
207 this block, if that is done, target_temp_slot_level
208 reverts to the saved target_temp_slot_level at the very
209 end of the block. */
210 int block_target_temp_slot_level;
211 /* True if we are currently emitting insns in an area of
212 output code that is controlled by a conditional
213 expression. This is used by the cleanup handling code to
214 generate conditional cleanup actions. */
215 int conditional_code;
216 /* A place to move the start of the exception region for any
217 of the conditional cleanups, must be at the end or after
218 the start of the last unconditional cleanup, and before any
219 conditional branch points. */
220 rtx last_unconditional_cleanup;
221 /* When in a conditional context, this is the specific
222 cleanup list associated with last_unconditional_cleanup,
223 where we place the conditionalized cleanups. */
224 tree *cleanup_ptr;
225 } block;
226 /* For switch (C) or case (Pascal) statements,
227 and also for dummies (see `expand_start_case_dummy'). */
228 struct
230 /* The insn after which the case dispatch should finally
231 be emitted. Zero for a dummy. */
232 rtx start;
233 /* A list of case labels; it is first built as an AVL tree.
234 During expand_end_case, this is converted to a list, and may be
235 rearranged into a nearly balanced binary tree. */
236 struct case_node *case_list;
237 /* Label to jump to if no case matches. */
238 tree default_label;
239 /* The expression to be dispatched on. */
240 tree index_expr;
241 /* Type that INDEX_EXPR should be converted to. */
242 tree nominal_type;
243 /* Number of range exprs in case statement. */
244 int num_ranges;
245 /* Name of this kind of statement, for warnings. */
246 const char *printname;
247 /* Used to save no_line_numbers till we see the first case label.
248 We set this to -1 when we see the first case label in this
249 case statement. */
250 int line_number_status;
251 } case_stmt;
252 } data;
255 /* Allocate and return a new `struct nesting'. */
257 #define ALLOC_NESTING() \
258 (struct nesting *) obstack_alloc (&stmt_obstack, sizeof (struct nesting))
260 /* Pop the nesting stack element by element until we pop off
261 the element which is at the top of STACK.
262 Update all the other stacks, popping off elements from them
263 as we pop them from nesting_stack. */
265 #define POPSTACK(STACK) \
266 do { struct nesting *target = STACK; \
267 struct nesting *this; \
268 do { this = nesting_stack; \
269 if (loop_stack == this) \
270 loop_stack = loop_stack->next; \
271 if (cond_stack == this) \
272 cond_stack = cond_stack->next; \
273 if (block_stack == this) \
274 block_stack = block_stack->next; \
275 if (stack_block_stack == this) \
276 stack_block_stack = stack_block_stack->next; \
277 if (case_stack == this) \
278 case_stack = case_stack->next; \
279 nesting_depth = nesting_stack->depth - 1; \
280 nesting_stack = this->all; \
281 obstack_free (&stmt_obstack, this); } \
282 while (this != target); } while (0)
284 /* In some cases it is impossible to generate code for a forward goto
285 until the label definition is seen. This happens when it may be necessary
286 for the goto to reset the stack pointer: we don't yet know how to do that.
287 So expand_goto puts an entry on this fixup list.
288 Each time a binding contour that resets the stack is exited,
289 we check each fixup.
290 If the target label has now been defined, we can insert the proper code. */
292 struct goto_fixup
294 /* Points to following fixup. */
295 struct goto_fixup *next;
296 /* Points to the insn before the jump insn.
297 If more code must be inserted, it goes after this insn. */
298 rtx before_jump;
299 /* The LABEL_DECL that this jump is jumping to, or 0
300 for break, continue or return. */
301 tree target;
302 /* The BLOCK for the place where this goto was found. */
303 tree context;
304 /* The CODE_LABEL rtx that this is jumping to. */
305 rtx target_rtl;
306 /* Number of binding contours started in current function
307 before the label reference. */
308 int block_start_count;
309 /* The outermost stack level that should be restored for this jump.
310 Each time a binding contour that resets the stack is exited,
311 if the target label is *not* yet defined, this slot is updated. */
312 rtx stack_level;
313 /* List of lists of cleanup expressions to be run by this goto.
314 There is one element for each block that this goto is within.
315 The tail of this list can be 0,
316 if all remaining elements would be empty.
317 The TREE_VALUE contains the cleanup list of that block as of the
318 time this goto was seen.
319 The TREE_ADDRESSABLE flag is 1 for a block that has been exited. */
320 tree cleanup_list_list;
323 /* Within any binding contour that must restore a stack level,
324 all labels are recorded with a chain of these structures. */
326 struct label_chain
328 /* Points to following fixup. */
329 struct label_chain *next;
330 tree label;
333 struct stmt_status
335 /* Chain of all pending binding contours. */
336 struct nesting *x_block_stack;
338 /* If any new stacks are added here, add them to POPSTACKS too. */
340 /* Chain of all pending binding contours that restore stack levels
341 or have cleanups. */
342 struct nesting *x_stack_block_stack;
344 /* Chain of all pending conditional statements. */
345 struct nesting *x_cond_stack;
347 /* Chain of all pending loops. */
348 struct nesting *x_loop_stack;
350 /* Chain of all pending case or switch statements. */
351 struct nesting *x_case_stack;
353 /* Separate chain including all of the above,
354 chained through the `all' field. */
355 struct nesting *x_nesting_stack;
357 /* Number of entries on nesting_stack now. */
358 int x_nesting_depth;
360 /* Number of binding contours started so far in this function. */
361 int x_block_start_count;
363 /* Each time we expand an expression-statement,
364 record the expr's type and its RTL value here. */
365 tree x_last_expr_type;
366 rtx x_last_expr_value;
368 /* Nonzero if within a ({...}) grouping, in which case we must
369 always compute a value for each expr-stmt in case it is the last one. */
370 int x_expr_stmts_for_value;
372 /* Filename and line number of last line-number note,
373 whether we actually emitted it or not. */
374 char *x_emit_filename;
375 int x_emit_lineno;
377 struct goto_fixup *x_goto_fixup_chain;
380 #define block_stack (current_function->stmt->x_block_stack)
381 #define stack_block_stack (current_function->stmt->x_stack_block_stack)
382 #define cond_stack (current_function->stmt->x_cond_stack)
383 #define loop_stack (current_function->stmt->x_loop_stack)
384 #define case_stack (current_function->stmt->x_case_stack)
385 #define nesting_stack (current_function->stmt->x_nesting_stack)
386 #define nesting_depth (current_function->stmt->x_nesting_depth)
387 #define current_block_start_count (current_function->stmt->x_block_start_count)
388 #define last_expr_type (current_function->stmt->x_last_expr_type)
389 #define last_expr_value (current_function->stmt->x_last_expr_value)
390 #define expr_stmts_for_value (current_function->stmt->x_expr_stmts_for_value)
391 #define emit_filename (current_function->stmt->x_emit_filename)
392 #define emit_lineno (current_function->stmt->x_emit_lineno)
393 #define goto_fixup_chain (current_function->stmt->x_goto_fixup_chain)
395 /* Non-zero if we are using EH to handle cleanus. */
396 static int using_eh_for_cleanups_p = 0;
398 /* Character strings, each containing a single decimal digit. */
399 static char *digit_strings[10];
402 static int n_occurrences PROTO((int, const char *));
403 static void expand_goto_internal PROTO((tree, rtx, rtx));
404 static int expand_fixup PROTO((tree, rtx, rtx));
405 static rtx expand_nl_handler_label PROTO((rtx, rtx));
406 static void expand_nl_goto_receiver PROTO((void));
407 static void expand_nl_goto_receivers PROTO((struct nesting *));
408 static void fixup_gotos PROTO((struct nesting *, rtx, tree,
409 rtx, int));
410 static void expand_null_return_1 PROTO((rtx, int));
411 static void expand_value_return PROTO((rtx));
412 static int tail_recursion_args PROTO((tree, tree));
413 static void expand_cleanups PROTO((tree, tree, int, int));
414 static void check_seenlabel PROTO((void));
415 static void do_jump_if_equal PROTO((rtx, rtx, rtx, int));
416 static int estimate_case_costs PROTO((case_node_ptr));
417 static void group_case_nodes PROTO((case_node_ptr));
418 static void balance_case_nodes PROTO((case_node_ptr *,
419 case_node_ptr));
420 static int node_has_low_bound PROTO((case_node_ptr, tree));
421 static int node_has_high_bound PROTO((case_node_ptr, tree));
422 static int node_is_bounded PROTO((case_node_ptr, tree));
423 static void emit_jump_if_reachable PROTO((rtx));
424 static void emit_case_nodes PROTO((rtx, case_node_ptr, rtx, tree));
425 static int add_case_node PROTO((tree, tree, tree, tree *));
426 static struct case_node *case_tree2list PROTO((case_node *, case_node *));
427 static void mark_cond_nesting PROTO((struct nesting *));
428 static void mark_loop_nesting PROTO((struct nesting *));
429 static void mark_block_nesting PROTO((struct nesting *));
430 static void mark_case_nesting PROTO((struct nesting *));
431 static void mark_goto_fixup PROTO((struct goto_fixup *));
434 void
435 using_eh_for_cleanups ()
437 using_eh_for_cleanups_p = 1;
440 /* Mark N (known to be a cond-nesting) for GC. */
442 static void
443 mark_cond_nesting (n)
444 struct nesting *n;
446 while (n)
448 ggc_mark_rtx (n->exit_label);
449 ggc_mark_rtx (n->data.cond.endif_label);
450 ggc_mark_rtx (n->data.cond.next_label);
452 n = n->next;
456 /* Mark N (known to be a loop-nesting) for GC. */
458 static void
459 mark_loop_nesting (n)
460 struct nesting *n;
463 while (n)
465 ggc_mark_rtx (n->exit_label);
466 ggc_mark_rtx (n->data.loop.start_label);
467 ggc_mark_rtx (n->data.loop.end_label);
468 ggc_mark_rtx (n->data.loop.alt_end_label);
469 ggc_mark_rtx (n->data.loop.continue_label);
471 n = n->next;
475 /* Mark N (known to be a block-nesting) for GC. */
477 static void
478 mark_block_nesting (n)
479 struct nesting *n;
481 while (n)
483 struct label_chain *l;
485 ggc_mark_rtx (n->exit_label);
486 ggc_mark_rtx (n->data.block.stack_level);
487 ggc_mark_rtx (n->data.block.first_insn);
488 ggc_mark_tree (n->data.block.cleanups);
489 ggc_mark_tree (n->data.block.outer_cleanups);
491 for (l = n->data.block.label_chain; l != NULL; l = l->next)
492 ggc_mark_tree (l->label);
494 ggc_mark_rtx (n->data.block.last_unconditional_cleanup);
496 /* ??? cleanup_ptr never points outside the stack, does it? */
498 n = n->next;
502 /* Mark N (known to be a case-nesting) for GC. */
504 static void
505 mark_case_nesting (n)
506 struct nesting *n;
508 while (n)
510 struct case_node *node;
512 ggc_mark_rtx (n->exit_label);
513 ggc_mark_rtx (n->data.case_stmt.start);
515 node = n->data.case_stmt.case_list;
516 while (node)
518 ggc_mark_tree (node->low);
519 ggc_mark_tree (node->high);
520 ggc_mark_tree (node->code_label);
521 node = node->right;
524 ggc_mark_tree (n->data.case_stmt.default_label);
525 ggc_mark_tree (n->data.case_stmt.index_expr);
526 ggc_mark_tree (n->data.case_stmt.nominal_type);
528 n = n->next;
532 /* Mark G for GC. */
534 static void
535 mark_goto_fixup (g)
536 struct goto_fixup *g;
538 while (g)
540 ggc_mark_rtx (g->before_jump);
541 ggc_mark_tree (g->target);
542 ggc_mark_tree (g->context);
543 ggc_mark_rtx (g->target_rtl);
544 ggc_mark_rtx (g->stack_level);
545 ggc_mark_tree (g->cleanup_list_list);
547 g = g->next;
551 /* Clear out all parts of the state in F that can safely be discarded
552 after the function has been compiled, to let garbage collection
553 reclaim the memory. */
555 void
556 free_stmt_status (f)
557 struct function *f;
559 /* We're about to free the function obstack. If we hold pointers to
560 things allocated there, then we'll try to mark them when we do
561 GC. So, we clear them out here explicitly. */
562 if (f->stmt)
563 free (f->stmt);
564 f->stmt = NULL;
567 /* Mark P for GC. */
569 void
570 mark_stmt_status (p)
571 struct stmt_status *p;
573 if (p == 0)
574 return;
576 mark_block_nesting (p->x_block_stack);
577 mark_cond_nesting (p->x_cond_stack);
578 mark_loop_nesting (p->x_loop_stack);
579 mark_case_nesting (p->x_case_stack);
581 ggc_mark_tree (p->x_last_expr_type);
582 /* last_epxr_value is only valid if last_expr_type is nonzero. */
583 if (p->x_last_expr_type)
584 ggc_mark_rtx (p->x_last_expr_value);
586 mark_goto_fixup (p->x_goto_fixup_chain);
589 void
590 init_stmt ()
592 int i;
594 gcc_obstack_init (&stmt_obstack);
596 for (i = 0; i < 10; i++)
598 digit_strings[i] = ggc_alloc_string (NULL, 1);
599 digit_strings[i][0] = '0' + i;
601 ggc_add_string_root (digit_strings, 10);
604 void
605 init_stmt_for_function ()
607 current_function->stmt
608 = (struct stmt_status *) xmalloc (sizeof (struct stmt_status));
610 /* We are not currently within any block, conditional, loop or case. */
611 block_stack = 0;
612 stack_block_stack = 0;
613 loop_stack = 0;
614 case_stack = 0;
615 cond_stack = 0;
616 nesting_stack = 0;
617 nesting_depth = 0;
619 current_block_start_count = 0;
621 /* No gotos have been expanded yet. */
622 goto_fixup_chain = 0;
624 /* We are not processing a ({...}) grouping. */
625 expr_stmts_for_value = 0;
626 last_expr_type = 0;
627 last_expr_value = NULL_RTX;
630 /* Return nonzero if anything is pushed on the loop, condition, or case
631 stack. */
633 in_control_zone_p ()
635 return cond_stack || loop_stack || case_stack;
638 /* Record the current file and line. Called from emit_line_note. */
639 void
640 set_file_and_line_for_stmt (file, line)
641 char *file;
642 int line;
644 emit_filename = file;
645 emit_lineno = line;
648 /* Emit a no-op instruction. */
650 void
651 emit_nop ()
653 rtx last_insn;
655 last_insn = get_last_insn ();
656 if (!optimize
657 && (GET_CODE (last_insn) == CODE_LABEL
658 || (GET_CODE (last_insn) == NOTE
659 && prev_real_insn (last_insn) == 0)))
660 emit_insn (gen_nop ());
663 /* Return the rtx-label that corresponds to a LABEL_DECL,
664 creating it if necessary. */
667 label_rtx (label)
668 tree label;
670 if (TREE_CODE (label) != LABEL_DECL)
671 abort ();
673 if (DECL_RTL (label))
674 return DECL_RTL (label);
676 return DECL_RTL (label) = gen_label_rtx ();
679 /* Add an unconditional jump to LABEL as the next sequential instruction. */
681 void
682 emit_jump (label)
683 rtx label;
685 do_pending_stack_adjust ();
686 emit_jump_insn (gen_jump (label));
687 emit_barrier ();
690 /* Emit code to jump to the address
691 specified by the pointer expression EXP. */
693 void
694 expand_computed_goto (exp)
695 tree exp;
697 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
699 #ifdef POINTERS_EXTEND_UNSIGNED
700 x = convert_memory_address (Pmode, x);
701 #endif
703 emit_queue ();
704 /* Be sure the function is executable. */
705 if (current_function_check_memory_usage)
706 emit_library_call (chkr_check_exec_libfunc, 1,
707 VOIDmode, 1, x, ptr_mode);
709 do_pending_stack_adjust ();
710 emit_indirect_jump (x);
712 current_function_has_computed_jump = 1;
715 /* Handle goto statements and the labels that they can go to. */
717 /* Specify the location in the RTL code of a label LABEL,
718 which is a LABEL_DECL tree node.
720 This is used for the kind of label that the user can jump to with a
721 goto statement, and for alternatives of a switch or case statement.
722 RTL labels generated for loops and conditionals don't go through here;
723 they are generated directly at the RTL level, by other functions below.
725 Note that this has nothing to do with defining label *names*.
726 Languages vary in how they do that and what that even means. */
728 void
729 expand_label (label)
730 tree label;
732 struct label_chain *p;
734 do_pending_stack_adjust ();
735 emit_label (label_rtx (label));
736 if (DECL_NAME (label))
737 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
739 if (stack_block_stack != 0)
741 p = (struct label_chain *) oballoc (sizeof (struct label_chain));
742 p->next = stack_block_stack->data.block.label_chain;
743 stack_block_stack->data.block.label_chain = p;
744 p->label = label;
748 /* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
749 from nested functions. */
751 void
752 declare_nonlocal_label (label)
753 tree label;
755 rtx slot = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
757 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
758 LABEL_PRESERVE_P (label_rtx (label)) = 1;
759 if (nonlocal_goto_handler_slots == 0)
761 emit_stack_save (SAVE_NONLOCAL,
762 &nonlocal_goto_stack_level,
763 PREV_INSN (tail_recursion_reentry));
765 nonlocal_goto_handler_slots
766 = gen_rtx_EXPR_LIST (VOIDmode, slot, nonlocal_goto_handler_slots);
769 /* Generate RTL code for a `goto' statement with target label LABEL.
770 LABEL should be a LABEL_DECL tree node that was or will later be
771 defined with `expand_label'. */
773 void
774 expand_goto (label)
775 tree label;
777 tree context;
779 /* Check for a nonlocal goto to a containing function. */
780 context = decl_function_context (label);
781 if (context != 0 && context != current_function_decl)
783 struct function *p = find_function_data (context);
784 rtx label_ref = gen_rtx_LABEL_REF (Pmode, label_rtx (label));
785 rtx temp, handler_slot;
786 tree link;
788 /* Find the corresponding handler slot for this label. */
789 handler_slot = p->x_nonlocal_goto_handler_slots;
790 for (link = p->x_nonlocal_labels; TREE_VALUE (link) != label;
791 link = TREE_CHAIN (link))
792 handler_slot = XEXP (handler_slot, 1);
793 handler_slot = XEXP (handler_slot, 0);
795 p->has_nonlocal_label = 1;
796 current_function_has_nonlocal_goto = 1;
797 LABEL_REF_NONLOCAL_P (label_ref) = 1;
799 /* Copy the rtl for the slots so that they won't be shared in
800 case the virtual stack vars register gets instantiated differently
801 in the parent than in the child. */
803 #if HAVE_nonlocal_goto
804 if (HAVE_nonlocal_goto)
805 emit_insn (gen_nonlocal_goto (lookup_static_chain (label),
806 copy_rtx (handler_slot),
807 copy_rtx (p->x_nonlocal_goto_stack_level),
808 label_ref));
809 else
810 #endif
812 rtx addr;
814 /* Restore frame pointer for containing function.
815 This sets the actual hard register used for the frame pointer
816 to the location of the function's incoming static chain info.
817 The non-local goto handler will then adjust it to contain the
818 proper value and reload the argument pointer, if needed. */
819 emit_move_insn (hard_frame_pointer_rtx, lookup_static_chain (label));
821 /* We have now loaded the frame pointer hardware register with
822 the address of that corresponds to the start of the virtual
823 stack vars. So replace virtual_stack_vars_rtx in all
824 addresses we use with stack_pointer_rtx. */
826 /* Get addr of containing function's current nonlocal goto handler,
827 which will do any cleanups and then jump to the label. */
828 addr = copy_rtx (handler_slot);
829 temp = copy_to_reg (replace_rtx (addr, virtual_stack_vars_rtx,
830 hard_frame_pointer_rtx));
832 /* Restore the stack pointer. Note this uses fp just restored. */
833 addr = p->x_nonlocal_goto_stack_level;
834 if (addr)
835 addr = replace_rtx (copy_rtx (addr),
836 virtual_stack_vars_rtx,
837 hard_frame_pointer_rtx);
839 emit_stack_restore (SAVE_NONLOCAL, addr, NULL_RTX);
841 /* USE of hard_frame_pointer_rtx added for consistency; not clear if
842 really needed. */
843 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
844 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
845 emit_indirect_jump (temp);
848 else
849 expand_goto_internal (label, label_rtx (label), NULL_RTX);
852 /* Generate RTL code for a `goto' statement with target label BODY.
853 LABEL should be a LABEL_REF.
854 LAST_INSN, if non-0, is the rtx we should consider as the last
855 insn emitted (for the purposes of cleaning up a return). */
857 static void
858 expand_goto_internal (body, label, last_insn)
859 tree body;
860 rtx label;
861 rtx last_insn;
863 struct nesting *block;
864 rtx stack_level = 0;
866 if (GET_CODE (label) != CODE_LABEL)
867 abort ();
869 /* If label has already been defined, we can tell now
870 whether and how we must alter the stack level. */
872 if (PREV_INSN (label) != 0)
874 /* Find the innermost pending block that contains the label.
875 (Check containment by comparing insn-uids.)
876 Then restore the outermost stack level within that block,
877 and do cleanups of all blocks contained in it. */
878 for (block = block_stack; block; block = block->next)
880 if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
881 break;
882 if (block->data.block.stack_level != 0)
883 stack_level = block->data.block.stack_level;
884 /* Execute the cleanups for blocks we are exiting. */
885 if (block->data.block.cleanups != 0)
887 expand_cleanups (block->data.block.cleanups, NULL_TREE, 1, 1);
888 do_pending_stack_adjust ();
892 if (stack_level)
894 /* Ensure stack adjust isn't done by emit_jump, as this
895 would clobber the stack pointer. This one should be
896 deleted as dead by flow. */
897 clear_pending_stack_adjust ();
898 do_pending_stack_adjust ();
899 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
902 if (body != 0 && DECL_TOO_LATE (body))
903 error ("jump to `%s' invalidly jumps into binding contour",
904 IDENTIFIER_POINTER (DECL_NAME (body)));
906 /* Label not yet defined: may need to put this goto
907 on the fixup list. */
908 else if (! expand_fixup (body, label, last_insn))
910 /* No fixup needed. Record that the label is the target
911 of at least one goto that has no fixup. */
912 if (body != 0)
913 TREE_ADDRESSABLE (body) = 1;
916 emit_jump (label);
919 /* Generate if necessary a fixup for a goto
920 whose target label in tree structure (if any) is TREE_LABEL
921 and whose target in rtl is RTL_LABEL.
923 If LAST_INSN is nonzero, we pretend that the jump appears
924 after insn LAST_INSN instead of at the current point in the insn stream.
926 The fixup will be used later to insert insns just before the goto.
927 Those insns will restore the stack level as appropriate for the
928 target label, and will (in the case of C++) also invoke any object
929 destructors which have to be invoked when we exit the scopes which
930 are exited by the goto.
932 Value is nonzero if a fixup is made. */
934 static int
935 expand_fixup (tree_label, rtl_label, last_insn)
936 tree tree_label;
937 rtx rtl_label;
938 rtx last_insn;
940 struct nesting *block, *end_block;
942 /* See if we can recognize which block the label will be output in.
943 This is possible in some very common cases.
944 If we succeed, set END_BLOCK to that block.
945 Otherwise, set it to 0. */
947 if (cond_stack
948 && (rtl_label == cond_stack->data.cond.endif_label
949 || rtl_label == cond_stack->data.cond.next_label))
950 end_block = cond_stack;
951 /* If we are in a loop, recognize certain labels which
952 are likely targets. This reduces the number of fixups
953 we need to create. */
954 else if (loop_stack
955 && (rtl_label == loop_stack->data.loop.start_label
956 || rtl_label == loop_stack->data.loop.end_label
957 || rtl_label == loop_stack->data.loop.continue_label))
958 end_block = loop_stack;
959 else
960 end_block = 0;
962 /* Now set END_BLOCK to the binding level to which we will return. */
964 if (end_block)
966 struct nesting *next_block = end_block->all;
967 block = block_stack;
969 /* First see if the END_BLOCK is inside the innermost binding level.
970 If so, then no cleanups or stack levels are relevant. */
971 while (next_block && next_block != block)
972 next_block = next_block->all;
974 if (next_block)
975 return 0;
977 /* Otherwise, set END_BLOCK to the innermost binding level
978 which is outside the relevant control-structure nesting. */
979 next_block = block_stack->next;
980 for (block = block_stack; block != end_block; block = block->all)
981 if (block == next_block)
982 next_block = next_block->next;
983 end_block = next_block;
986 /* Does any containing block have a stack level or cleanups?
987 If not, no fixup is needed, and that is the normal case
988 (the only case, for standard C). */
989 for (block = block_stack; block != end_block; block = block->next)
990 if (block->data.block.stack_level != 0
991 || block->data.block.cleanups != 0)
992 break;
994 if (block != end_block)
996 /* Ok, a fixup is needed. Add a fixup to the list of such. */
997 struct goto_fixup *fixup
998 = (struct goto_fixup *) oballoc (sizeof (struct goto_fixup));
999 /* In case an old stack level is restored, make sure that comes
1000 after any pending stack adjust. */
1001 /* ?? If the fixup isn't to come at the present position,
1002 doing the stack adjust here isn't useful. Doing it with our
1003 settings at that location isn't useful either. Let's hope
1004 someone does it! */
1005 if (last_insn == 0)
1006 do_pending_stack_adjust ();
1007 fixup->target = tree_label;
1008 fixup->target_rtl = rtl_label;
1010 /* Create a BLOCK node and a corresponding matched set of
1011 NOTE_INSN_BEGIN_BLOCK and NOTE_INSN_END_BLOCK notes at
1012 this point. The notes will encapsulate any and all fixup
1013 code which we might later insert at this point in the insn
1014 stream. Also, the BLOCK node will be the parent (i.e. the
1015 `SUPERBLOCK') of any other BLOCK nodes which we might create
1016 later on when we are expanding the fixup code.
1018 Note that optimization passes (including expand_end_loop)
1019 might move the *_BLOCK notes away, so we use a NOTE_INSN_DELETED
1020 as a placeholder. */
1023 register rtx original_before_jump
1024 = last_insn ? last_insn : get_last_insn ();
1025 rtx start;
1026 tree block;
1028 block = make_node (BLOCK);
1029 TREE_USED (block) = 1;
1031 if (current_function->x_whole_function_mode_p)
1033 find_loop_tree_blocks ();
1034 retrofit_block (block, original_before_jump);
1036 else
1037 insert_block (block);
1039 start_sequence ();
1040 start = emit_note (NULL_PTR, NOTE_INSN_BLOCK_BEG);
1041 fixup->before_jump = emit_note (NULL_PTR, NOTE_INSN_DELETED);
1042 emit_note (NULL_PTR, NOTE_INSN_BLOCK_END);
1043 fixup->context = block;
1044 end_sequence ();
1045 emit_insns_after (start, original_before_jump);
1048 fixup->block_start_count = current_block_start_count;
1049 fixup->stack_level = 0;
1050 fixup->cleanup_list_list
1051 = ((block->data.block.outer_cleanups
1052 || block->data.block.cleanups)
1053 ? tree_cons (NULL_TREE, block->data.block.cleanups,
1054 block->data.block.outer_cleanups)
1055 : 0);
1056 fixup->next = goto_fixup_chain;
1057 goto_fixup_chain = fixup;
1060 return block != 0;
1065 /* Expand any needed fixups in the outputmost binding level of the
1066 function. FIRST_INSN is the first insn in the function. */
1068 void
1069 expand_fixups (first_insn)
1070 rtx first_insn;
1072 fixup_gotos (NULL_PTR, NULL_RTX, NULL_TREE, first_insn, 0);
1075 /* When exiting a binding contour, process all pending gotos requiring fixups.
1076 THISBLOCK is the structure that describes the block being exited.
1077 STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
1078 CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
1079 FIRST_INSN is the insn that began this contour.
1081 Gotos that jump out of this contour must restore the
1082 stack level and do the cleanups before actually jumping.
1084 DONT_JUMP_IN nonzero means report error there is a jump into this
1085 contour from before the beginning of the contour.
1086 This is also done if STACK_LEVEL is nonzero. */
1088 static void
1089 fixup_gotos (thisblock, stack_level, cleanup_list, first_insn, dont_jump_in)
1090 struct nesting *thisblock;
1091 rtx stack_level;
1092 tree cleanup_list;
1093 rtx first_insn;
1094 int dont_jump_in;
1096 register struct goto_fixup *f, *prev;
1098 /* F is the fixup we are considering; PREV is the previous one. */
1099 /* We run this loop in two passes so that cleanups of exited blocks
1100 are run first, and blocks that are exited are marked so
1101 afterwards. */
1103 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1105 /* Test for a fixup that is inactive because it is already handled. */
1106 if (f->before_jump == 0)
1108 /* Delete inactive fixup from the chain, if that is easy to do. */
1109 if (prev != 0)
1110 prev->next = f->next;
1112 /* Has this fixup's target label been defined?
1113 If so, we can finalize it. */
1114 else if (PREV_INSN (f->target_rtl) != 0)
1116 register rtx cleanup_insns;
1118 /* If this fixup jumped into this contour from before the beginning
1119 of this contour, report an error. This code used to use
1120 the first non-label insn after f->target_rtl, but that's
1121 wrong since such can be added, by things like put_var_into_stack
1122 and have INSN_UIDs that are out of the range of the block. */
1123 /* ??? Bug: this does not detect jumping in through intermediate
1124 blocks that have stack levels or cleanups.
1125 It detects only a problem with the innermost block
1126 around the label. */
1127 if (f->target != 0
1128 && (dont_jump_in || stack_level || cleanup_list)
1129 && INSN_UID (first_insn) < INSN_UID (f->target_rtl)
1130 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
1131 && ! DECL_ERROR_ISSUED (f->target))
1133 error_with_decl (f->target,
1134 "label `%s' used before containing binding contour");
1135 /* Prevent multiple errors for one label. */
1136 DECL_ERROR_ISSUED (f->target) = 1;
1139 /* We will expand the cleanups into a sequence of their own and
1140 then later on we will attach this new sequence to the insn
1141 stream just ahead of the actual jump insn. */
1143 start_sequence ();
1145 /* Temporarily restore the lexical context where we will
1146 logically be inserting the fixup code. We do this for the
1147 sake of getting the debugging information right. */
1149 pushlevel (0);
1150 set_block (f->context);
1152 /* Expand the cleanups for blocks this jump exits. */
1153 if (f->cleanup_list_list)
1155 tree lists;
1156 for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
1157 /* Marked elements correspond to blocks that have been closed.
1158 Do their cleanups. */
1159 if (TREE_ADDRESSABLE (lists)
1160 && TREE_VALUE (lists) != 0)
1162 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
1163 /* Pop any pushes done in the cleanups,
1164 in case function is about to return. */
1165 do_pending_stack_adjust ();
1169 /* Restore stack level for the biggest contour that this
1170 jump jumps out of. */
1171 if (f->stack_level)
1172 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
1174 /* Finish up the sequence containing the insns which implement the
1175 necessary cleanups, and then attach that whole sequence to the
1176 insn stream just ahead of the actual jump insn. Attaching it
1177 at that point insures that any cleanups which are in fact
1178 implicit C++ object destructions (which must be executed upon
1179 leaving the block) appear (to the debugger) to be taking place
1180 in an area of the generated code where the object(s) being
1181 destructed are still "in scope". */
1183 cleanup_insns = get_insns ();
1184 poplevel (1, 0, 0);
1186 end_sequence ();
1187 emit_insns_after (cleanup_insns, f->before_jump);
1190 f->before_jump = 0;
1194 /* For any still-undefined labels, do the cleanups for this block now.
1195 We must do this now since items in the cleanup list may go out
1196 of scope when the block ends. */
1197 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1198 if (f->before_jump != 0
1199 && PREV_INSN (f->target_rtl) == 0
1200 /* Label has still not appeared. If we are exiting a block with
1201 a stack level to restore, that started before the fixup,
1202 mark this stack level as needing restoration
1203 when the fixup is later finalized. */
1204 && thisblock != 0
1205 /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared, it
1206 means the label is undefined. That's erroneous, but possible. */
1207 && (thisblock->data.block.block_start_count
1208 <= f->block_start_count))
1210 tree lists = f->cleanup_list_list;
1211 rtx cleanup_insns;
1213 for (; lists; lists = TREE_CHAIN (lists))
1214 /* If the following elt. corresponds to our containing block
1215 then the elt. must be for this block. */
1216 if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
1218 start_sequence ();
1219 pushlevel (0);
1220 set_block (f->context);
1221 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
1222 do_pending_stack_adjust ();
1223 cleanup_insns = get_insns ();
1224 poplevel (1, 0, 0);
1225 end_sequence ();
1226 if (cleanup_insns != 0)
1227 f->before_jump
1228 = emit_insns_after (cleanup_insns, f->before_jump);
1230 f->cleanup_list_list = TREE_CHAIN (lists);
1233 if (stack_level)
1234 f->stack_level = stack_level;
1238 /* Return the number of times character C occurs in string S. */
1239 static int
1240 n_occurrences (c, s)
1241 int c;
1242 const char *s;
1244 int n = 0;
1245 while (*s)
1246 n += (*s++ == c);
1247 return n;
1250 /* Generate RTL for an asm statement (explicit assembler code).
1251 BODY is a STRING_CST node containing the assembler code text,
1252 or an ADDR_EXPR containing a STRING_CST. */
1254 void
1255 expand_asm (body)
1256 tree body;
1258 if (current_function_check_memory_usage)
1260 error ("`asm' cannot be used in function where memory usage is checked");
1261 return;
1264 if (TREE_CODE (body) == ADDR_EXPR)
1265 body = TREE_OPERAND (body, 0);
1267 emit_insn (gen_rtx_ASM_INPUT (VOIDmode,
1268 TREE_STRING_POINTER (body)));
1269 last_expr_type = 0;
1272 /* Generate RTL for an asm statement with arguments.
1273 STRING is the instruction template.
1274 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
1275 Each output or input has an expression in the TREE_VALUE and
1276 a constraint-string in the TREE_PURPOSE.
1277 CLOBBERS is a list of STRING_CST nodes each naming a hard register
1278 that is clobbered by this insn.
1280 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
1281 Some elements of OUTPUTS may be replaced with trees representing temporary
1282 values. The caller should copy those temporary values to the originally
1283 specified lvalues.
1285 VOL nonzero means the insn is volatile; don't optimize it. */
1287 void
1288 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
1289 tree string, outputs, inputs, clobbers;
1290 int vol;
1291 char *filename;
1292 int line;
1294 rtvec argvec, constraints;
1295 rtx body;
1296 int ninputs = list_length (inputs);
1297 int noutputs = list_length (outputs);
1298 int ninout = 0;
1299 int nclobbers;
1300 tree tail;
1301 register int i;
1302 /* Vector of RTX's of evaluated output operands. */
1303 rtx *output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1304 int *inout_opnum = (int *) alloca (noutputs * sizeof (int));
1305 rtx *real_output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1306 enum machine_mode *inout_mode
1307 = (enum machine_mode *) alloca (noutputs * sizeof (enum machine_mode));
1308 /* The insn we have emitted. */
1309 rtx insn;
1311 /* An ASM with no outputs needs to be treated as volatile, for now. */
1312 if (noutputs == 0)
1313 vol = 1;
1315 if (current_function_check_memory_usage)
1317 error ("`asm' cannot be used with `-fcheck-memory-usage'");
1318 return;
1321 #ifdef MD_ASM_CLOBBERS
1322 /* Sometimes we wish to automatically clobber registers across an asm.
1323 Case in point is when the i386 backend moved from cc0 to a hard reg --
1324 maintaining source-level compatability means automatically clobbering
1325 the flags register. */
1326 MD_ASM_CLOBBERS (clobbers);
1327 #endif
1329 if (current_function_check_memory_usage)
1331 error ("`asm' cannot be used in function where memory usage is checked");
1332 return;
1335 /* Count the number of meaningful clobbered registers, ignoring what
1336 we would ignore later. */
1337 nclobbers = 0;
1338 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1340 char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1342 i = decode_reg_name (regname);
1343 if (i >= 0 || i == -4)
1344 ++nclobbers;
1345 else if (i == -2)
1346 error ("unknown register name `%s' in `asm'", regname);
1349 last_expr_type = 0;
1351 /* Check that the number of alternatives is constant across all
1352 operands. */
1353 if (outputs || inputs)
1355 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1356 int nalternatives = n_occurrences (',', TREE_STRING_POINTER (tmp));
1357 tree next = inputs;
1359 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1361 error ("too many alternatives in `asm'");
1362 return;
1365 tmp = outputs;
1366 while (tmp)
1368 char *constraint = TREE_STRING_POINTER (TREE_PURPOSE (tmp));
1370 if (n_occurrences (',', constraint) != nalternatives)
1372 error ("operand constraints for `asm' differ in number of alternatives");
1373 return;
1376 if (TREE_CHAIN (tmp))
1377 tmp = TREE_CHAIN (tmp);
1378 else
1379 tmp = next, next = 0;
1383 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1385 tree val = TREE_VALUE (tail);
1386 tree type = TREE_TYPE (val);
1387 char *constraint;
1388 char *p;
1389 int c_len;
1390 int j;
1391 int is_inout = 0;
1392 int allows_reg = 0;
1393 int allows_mem = 0;
1395 /* If there's an erroneous arg, emit no insn. */
1396 if (TREE_TYPE (val) == error_mark_node)
1397 return;
1399 /* Make sure constraint has `=' and does not have `+'. Also, see
1400 if it allows any register. Be liberal on the latter test, since
1401 the worst that happens if we get it wrong is we issue an error
1402 message. */
1404 c_len = strlen (TREE_STRING_POINTER (TREE_PURPOSE (tail)));
1405 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
1407 /* Allow the `=' or `+' to not be at the beginning of the string,
1408 since it wasn't explicitly documented that way, and there is a
1409 large body of code that puts it last. Swap the character to
1410 the front, so as not to uglify any place else. */
1411 switch (c_len)
1413 default:
1414 if ((p = strchr (constraint, '=')) != NULL)
1415 break;
1416 if ((p = strchr (constraint, '+')) != NULL)
1417 break;
1418 case 0:
1419 error ("output operand constraint lacks `='");
1420 return;
1423 if (p != constraint)
1425 j = *p;
1426 bcopy (constraint, constraint+1, p-constraint);
1427 *constraint = j;
1429 warning ("output constraint `%c' for operand %d is not at the beginning", j, i);
1432 is_inout = constraint[0] == '+';
1433 /* Replace '+' with '='. */
1434 constraint[0] = '=';
1435 /* Make sure we can specify the matching operand. */
1436 if (is_inout && i > 9)
1438 error ("output operand constraint %d contains `+'", i);
1439 return;
1442 for (j = 1; j < c_len; j++)
1443 switch (constraint[j])
1445 case '+':
1446 case '=':
1447 error ("operand constraint contains '+' or '=' at illegal position.");
1448 return;
1450 case '%':
1451 if (i + 1 == ninputs + noutputs)
1453 error ("`%%' constraint used with last operand");
1454 return;
1456 break;
1458 case '?': case '!': case '*': case '&':
1459 case 'E': case 'F': case 'G': case 'H':
1460 case 's': case 'i': case 'n':
1461 case 'I': case 'J': case 'K': case 'L': case 'M':
1462 case 'N': case 'O': case 'P': case ',':
1463 #ifdef EXTRA_CONSTRAINT
1464 case 'Q': case 'R': case 'S': case 'T': case 'U':
1465 #endif
1466 break;
1468 case '0': case '1': case '2': case '3': case '4':
1469 case '5': case '6': case '7': case '8': case '9':
1470 error ("matching constraint not valid in output operand");
1471 break;
1473 case 'V': case 'm': case 'o':
1474 allows_mem = 1;
1475 break;
1477 case '<': case '>':
1478 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
1479 excepting those that expand_call created. So match memory
1480 and hope. */
1481 allows_mem = 1;
1482 break;
1484 case 'g': case 'X':
1485 allows_reg = 1;
1486 allows_mem = 1;
1487 break;
1489 case 'p': case 'r':
1490 default:
1491 allows_reg = 1;
1492 break;
1495 /* If an output operand is not a decl or indirect ref and our constraint
1496 allows a register, make a temporary to act as an intermediate.
1497 Make the asm insn write into that, then our caller will copy it to
1498 the real output operand. Likewise for promoted variables. */
1500 real_output_rtx[i] = NULL_RTX;
1501 if ((TREE_CODE (val) == INDIRECT_REF
1502 && allows_mem)
1503 || (TREE_CODE_CLASS (TREE_CODE (val)) == 'd'
1504 && (allows_mem || GET_CODE (DECL_RTL (val)) == REG)
1505 && ! (GET_CODE (DECL_RTL (val)) == REG
1506 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
1507 || ! allows_reg
1508 || is_inout)
1510 if (! allows_reg)
1511 mark_addressable (TREE_VALUE (tail));
1513 output_rtx[i]
1514 = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode,
1515 EXPAND_MEMORY_USE_WO);
1517 if (! allows_reg && GET_CODE (output_rtx[i]) != MEM)
1518 error ("output number %d not directly addressable", i);
1519 if (! allows_mem && GET_CODE (output_rtx[i]) == MEM)
1521 real_output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1522 output_rtx[i] = gen_reg_rtx (GET_MODE (output_rtx[i]));
1523 if (is_inout)
1524 emit_move_insn (output_rtx[i], real_output_rtx[i]);
1527 else
1529 output_rtx[i] = assign_temp (type, 0, 0, 0);
1530 TREE_VALUE (tail) = make_tree (type, output_rtx[i]);
1533 if (is_inout)
1535 inout_mode[ninout] = TYPE_MODE (TREE_TYPE (TREE_VALUE (tail)));
1536 inout_opnum[ninout++] = i;
1540 ninputs += ninout;
1541 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1543 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1544 return;
1547 /* Make vectors for the expression-rtx and constraint strings. */
1549 argvec = rtvec_alloc (ninputs);
1550 constraints = rtvec_alloc (ninputs);
1552 body = gen_rtx_ASM_OPERANDS (VOIDmode, TREE_STRING_POINTER (string),
1553 empty_string, 0, argvec, constraints,
1554 filename, line);
1556 MEM_VOLATILE_P (body) = vol;
1558 /* Eval the inputs and put them into ARGVEC.
1559 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1561 i = 0;
1562 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
1564 int j;
1565 int allows_reg = 0, allows_mem = 0;
1566 char *constraint, *orig_constraint;
1567 int c_len;
1568 rtx op;
1570 /* If there's an erroneous arg, emit no insn,
1571 because the ASM_INPUT would get VOIDmode
1572 and that could cause a crash in reload. */
1573 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1574 return;
1576 /* ??? Can this happen, and does the error message make any sense? */
1577 if (TREE_PURPOSE (tail) == NULL_TREE)
1579 error ("hard register `%s' listed as input operand to `asm'",
1580 TREE_STRING_POINTER (TREE_VALUE (tail)) );
1581 return;
1584 c_len = strlen (TREE_STRING_POINTER (TREE_PURPOSE (tail)));
1585 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
1586 orig_constraint = constraint;
1588 /* Make sure constraint has neither `=', `+', nor '&'. */
1590 for (j = 0; j < c_len; j++)
1591 switch (constraint[j])
1593 case '+': case '=': case '&':
1594 if (constraint == orig_constraint)
1596 error ("input operand constraint contains `%c'",
1597 constraint[j]);
1598 return;
1600 break;
1602 case '%':
1603 if (constraint == orig_constraint
1604 && i + 1 == ninputs - ninout)
1606 error ("`%%' constraint used with last operand");
1607 return;
1609 break;
1611 case 'V': case 'm': case 'o':
1612 allows_mem = 1;
1613 break;
1615 case '<': case '>':
1616 case '?': case '!': case '*':
1617 case 'E': case 'F': case 'G': case 'H': case 'X':
1618 case 's': case 'i': case 'n':
1619 case 'I': case 'J': case 'K': case 'L': case 'M':
1620 case 'N': case 'O': case 'P': case ',':
1621 #ifdef EXTRA_CONSTRAINT
1622 case 'Q': case 'R': case 'S': case 'T': case 'U':
1623 #endif
1624 break;
1626 /* Whether or not a numeric constraint allows a register is
1627 decided by the matching constraint, and so there is no need
1628 to do anything special with them. We must handle them in
1629 the default case, so that we don't unnecessarily force
1630 operands to memory. */
1631 case '0': case '1': case '2': case '3': case '4':
1632 case '5': case '6': case '7': case '8': case '9':
1633 if (constraint[j] >= '0' + noutputs)
1635 error
1636 ("matching constraint references invalid operand number");
1637 return;
1640 /* Try and find the real constraint for this dup. */
1641 if ((j == 0 && c_len == 1)
1642 || (j == 1 && c_len == 2 && constraint[0] == '%'))
1644 tree o = outputs;
1646 for (j = constraint[j] - '0'; j > 0; --j)
1647 o = TREE_CHAIN (o);
1649 c_len = strlen (TREE_STRING_POINTER (TREE_PURPOSE (o)));
1650 constraint = TREE_STRING_POINTER (TREE_PURPOSE (o));
1651 j = 0;
1652 break;
1655 /* ... fall through ... */
1657 case 'p': case 'r':
1658 default:
1659 allows_reg = 1;
1660 break;
1662 case 'g':
1663 allows_reg = 1;
1664 allows_mem = 1;
1665 break;
1668 if (! allows_reg && allows_mem)
1669 mark_addressable (TREE_VALUE (tail));
1671 op = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
1673 if (asm_operand_ok (op, constraint) <= 0)
1675 if (allows_reg)
1676 op = force_reg (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))), op);
1677 else if (!allows_mem)
1678 warning ("asm operand %d probably doesn't match constraints", i);
1679 else if (CONSTANT_P (op))
1680 op = force_const_mem (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1681 op);
1682 else if (GET_CODE (op) == REG
1683 || GET_CODE (op) == SUBREG
1684 || GET_CODE (op) == CONCAT)
1686 tree type = TREE_TYPE (TREE_VALUE (tail));
1687 rtx memloc = assign_temp (type, 1, 1, 1);
1689 emit_move_insn (memloc, op);
1690 op = memloc;
1693 else if (GET_CODE (op) == MEM && MEM_VOLATILE_P (op))
1694 /* We won't recognize volatile memory as available a
1695 memory_operand at this point. Ignore it. */
1697 else if (queued_subexp_p (op))
1699 else
1700 /* ??? Leave this only until we have experience with what
1701 happens in combine and elsewhere when constraints are
1702 not satisfied. */
1703 warning ("asm operand %d probably doesn't match constraints", i);
1705 XVECEXP (body, 3, i) = op;
1707 XVECEXP (body, 4, i) /* constraints */
1708 = gen_rtx_ASM_INPUT (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1709 orig_constraint);
1710 i++;
1713 /* Protect all the operands from the queue now that they have all been
1714 evaluated. */
1716 for (i = 0; i < ninputs - ninout; i++)
1717 XVECEXP (body, 3, i) = protect_from_queue (XVECEXP (body, 3, i), 0);
1719 for (i = 0; i < noutputs; i++)
1720 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1722 /* For in-out operands, copy output rtx to input rtx. */
1723 for (i = 0; i < ninout; i++)
1725 int j = inout_opnum[i];
1727 XVECEXP (body, 3, ninputs - ninout + i) /* argvec */
1728 = output_rtx[j];
1729 XVECEXP (body, 4, ninputs - ninout + i) /* constraints */
1730 = gen_rtx_ASM_INPUT (inout_mode[i], digit_strings[j]);
1733 /* Now, for each output, construct an rtx
1734 (set OUTPUT (asm_operands INSN OUTPUTNUMBER OUTPUTCONSTRAINT
1735 ARGVEC CONSTRAINTS))
1736 If there is more than one, put them inside a PARALLEL. */
1738 if (noutputs == 1 && nclobbers == 0)
1740 XSTR (body, 1) = TREE_STRING_POINTER (TREE_PURPOSE (outputs));
1741 insn = emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
1744 else if (noutputs == 0 && nclobbers == 0)
1746 /* No output operands: put in a raw ASM_OPERANDS rtx. */
1747 insn = emit_insn (body);
1750 else
1752 rtx obody = body;
1753 int num = noutputs;
1755 if (num == 0)
1756 num = 1;
1758 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
1760 /* For each output operand, store a SET. */
1761 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1763 XVECEXP (body, 0, i)
1764 = gen_rtx_SET (VOIDmode,
1765 output_rtx[i],
1766 gen_rtx_ASM_OPERANDS
1767 (VOIDmode,
1768 TREE_STRING_POINTER (string),
1769 TREE_STRING_POINTER (TREE_PURPOSE (tail)),
1770 i, argvec, constraints,
1771 filename, line));
1773 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1776 /* If there are no outputs (but there are some clobbers)
1777 store the bare ASM_OPERANDS into the PARALLEL. */
1779 if (i == 0)
1780 XVECEXP (body, 0, i++) = obody;
1782 /* Store (clobber REG) for each clobbered register specified. */
1784 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1786 char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1787 int j = decode_reg_name (regname);
1789 if (j < 0)
1791 if (j == -3) /* `cc', which is not a register */
1792 continue;
1794 if (j == -4) /* `memory', don't cache memory across asm */
1796 XVECEXP (body, 0, i++)
1797 = gen_rtx_CLOBBER (VOIDmode,
1798 gen_rtx_MEM
1799 (BLKmode,
1800 gen_rtx_SCRATCH (VOIDmode)));
1801 continue;
1804 /* Ignore unknown register, error already signaled. */
1805 continue;
1808 /* Use QImode since that's guaranteed to clobber just one reg. */
1809 XVECEXP (body, 0, i++)
1810 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (QImode, j));
1813 insn = emit_insn (body);
1816 /* For any outputs that needed reloading into registers, spill them
1817 back to where they belong. */
1818 for (i = 0; i < noutputs; ++i)
1819 if (real_output_rtx[i])
1820 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1822 free_temp_slots ();
1825 /* Generate RTL to evaluate the expression EXP
1826 and remember it in case this is the VALUE in a ({... VALUE; }) constr. */
1828 void
1829 expand_expr_stmt (exp)
1830 tree exp;
1832 /* If -W, warn about statements with no side effects,
1833 except for an explicit cast to void (e.g. for assert()), and
1834 except inside a ({...}) where they may be useful. */
1835 if (expr_stmts_for_value == 0 && exp != error_mark_node)
1837 if (! TREE_SIDE_EFFECTS (exp) && (extra_warnings || warn_unused)
1838 && !(TREE_CODE (exp) == CONVERT_EXPR
1839 && TREE_TYPE (exp) == void_type_node))
1840 warning_with_file_and_line (emit_filename, emit_lineno,
1841 "statement with no effect");
1842 else if (warn_unused)
1843 warn_if_unused_value (exp);
1846 /* If EXP is of function type and we are expanding statements for
1847 value, convert it to pointer-to-function. */
1848 if (expr_stmts_for_value && TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE)
1849 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
1851 last_expr_type = TREE_TYPE (exp);
1852 last_expr_value = expand_expr (exp,
1853 (expr_stmts_for_value
1854 ? NULL_RTX : const0_rtx),
1855 VOIDmode, 0);
1857 /* If all we do is reference a volatile value in memory,
1858 copy it to a register to be sure it is actually touched. */
1859 if (last_expr_value != 0 && GET_CODE (last_expr_value) == MEM
1860 && TREE_THIS_VOLATILE (exp))
1862 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode)
1864 else if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
1865 copy_to_reg (last_expr_value);
1866 else
1868 rtx lab = gen_label_rtx ();
1870 /* Compare the value with itself to reference it. */
1871 emit_cmp_and_jump_insns (last_expr_value, last_expr_value, EQ,
1872 expand_expr (TYPE_SIZE (last_expr_type),
1873 NULL_RTX, VOIDmode, 0),
1874 BLKmode, 0,
1875 TYPE_ALIGN (last_expr_type) / BITS_PER_UNIT,
1876 lab);
1877 emit_label (lab);
1881 /* If this expression is part of a ({...}) and is in memory, we may have
1882 to preserve temporaries. */
1883 preserve_temp_slots (last_expr_value);
1885 /* Free any temporaries used to evaluate this expression. Any temporary
1886 used as a result of this expression will already have been preserved
1887 above. */
1888 free_temp_slots ();
1890 emit_queue ();
1893 /* Warn if EXP contains any computations whose results are not used.
1894 Return 1 if a warning is printed; 0 otherwise. */
1897 warn_if_unused_value (exp)
1898 tree exp;
1900 if (TREE_USED (exp))
1901 return 0;
1903 switch (TREE_CODE (exp))
1905 case PREINCREMENT_EXPR:
1906 case POSTINCREMENT_EXPR:
1907 case PREDECREMENT_EXPR:
1908 case POSTDECREMENT_EXPR:
1909 case MODIFY_EXPR:
1910 case INIT_EXPR:
1911 case TARGET_EXPR:
1912 case CALL_EXPR:
1913 case METHOD_CALL_EXPR:
1914 case RTL_EXPR:
1915 case TRY_CATCH_EXPR:
1916 case WITH_CLEANUP_EXPR:
1917 case EXIT_EXPR:
1918 /* We don't warn about COND_EXPR because it may be a useful
1919 construct if either arm contains a side effect. */
1920 case COND_EXPR:
1921 return 0;
1923 case BIND_EXPR:
1924 /* For a binding, warn if no side effect within it. */
1925 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1927 case SAVE_EXPR:
1928 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1930 case TRUTH_ORIF_EXPR:
1931 case TRUTH_ANDIF_EXPR:
1932 /* In && or ||, warn if 2nd operand has no side effect. */
1933 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1935 case COMPOUND_EXPR:
1936 if (TREE_NO_UNUSED_WARNING (exp))
1937 return 0;
1938 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
1939 return 1;
1940 /* Let people do `(foo (), 0)' without a warning. */
1941 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1942 return 0;
1943 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1945 case NOP_EXPR:
1946 case CONVERT_EXPR:
1947 case NON_LVALUE_EXPR:
1948 /* Don't warn about values cast to void. */
1949 if (TREE_TYPE (exp) == void_type_node)
1950 return 0;
1951 /* Don't warn about conversions not explicit in the user's program. */
1952 if (TREE_NO_UNUSED_WARNING (exp))
1953 return 0;
1954 /* Assignment to a cast usually results in a cast of a modify.
1955 Don't complain about that. There can be an arbitrary number of
1956 casts before the modify, so we must loop until we find the first
1957 non-cast expression and then test to see if that is a modify. */
1959 tree tem = TREE_OPERAND (exp, 0);
1961 while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
1962 tem = TREE_OPERAND (tem, 0);
1964 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
1965 || TREE_CODE (tem) == CALL_EXPR)
1966 return 0;
1968 goto warn;
1970 case INDIRECT_REF:
1971 /* Don't warn about automatic dereferencing of references, since
1972 the user cannot control it. */
1973 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1974 return warn_if_unused_value (TREE_OPERAND (exp, 0));
1975 /* ... fall through ... */
1977 default:
1978 /* Referencing a volatile value is a side effect, so don't warn. */
1979 if ((TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
1980 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
1981 && TREE_THIS_VOLATILE (exp))
1982 return 0;
1983 warn:
1984 warning_with_file_and_line (emit_filename, emit_lineno,
1985 "value computed is not used");
1986 return 1;
1990 /* Clear out the memory of the last expression evaluated. */
1992 void
1993 clear_last_expr ()
1995 last_expr_type = 0;
1998 /* Begin a statement which will return a value.
1999 Return the RTL_EXPR for this statement expr.
2000 The caller must save that value and pass it to expand_end_stmt_expr. */
2002 tree
2003 expand_start_stmt_expr ()
2005 int momentary;
2006 tree t;
2008 /* Make the RTL_EXPR node temporary, not momentary,
2009 so that rtl_expr_chain doesn't become garbage. */
2010 momentary = suspend_momentary ();
2011 t = make_node (RTL_EXPR);
2012 resume_momentary (momentary);
2013 do_pending_stack_adjust ();
2014 start_sequence_for_rtl_expr (t);
2015 NO_DEFER_POP;
2016 expr_stmts_for_value++;
2017 return t;
2020 /* Restore the previous state at the end of a statement that returns a value.
2021 Returns a tree node representing the statement's value and the
2022 insns to compute the value.
2024 The nodes of that expression have been freed by now, so we cannot use them.
2025 But we don't want to do that anyway; the expression has already been
2026 evaluated and now we just want to use the value. So generate a RTL_EXPR
2027 with the proper type and RTL value.
2029 If the last substatement was not an expression,
2030 return something with type `void'. */
2032 tree
2033 expand_end_stmt_expr (t)
2034 tree t;
2036 OK_DEFER_POP;
2038 if (last_expr_type == 0)
2040 last_expr_type = void_type_node;
2041 last_expr_value = const0_rtx;
2043 else if (last_expr_value == 0)
2044 /* There are some cases where this can happen, such as when the
2045 statement is void type. */
2046 last_expr_value = const0_rtx;
2047 else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
2048 /* Remove any possible QUEUED. */
2049 last_expr_value = protect_from_queue (last_expr_value, 0);
2051 emit_queue ();
2053 TREE_TYPE (t) = last_expr_type;
2054 RTL_EXPR_RTL (t) = last_expr_value;
2055 RTL_EXPR_SEQUENCE (t) = get_insns ();
2057 rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
2059 end_sequence ();
2061 /* Don't consider deleting this expr or containing exprs at tree level. */
2062 TREE_SIDE_EFFECTS (t) = 1;
2063 /* Propagate volatility of the actual RTL expr. */
2064 TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
2066 last_expr_type = 0;
2067 expr_stmts_for_value--;
2069 return t;
2072 /* Generate RTL for the start of an if-then. COND is the expression
2073 whose truth should be tested.
2075 If EXITFLAG is nonzero, this conditional is visible to
2076 `exit_something'. */
2078 void
2079 expand_start_cond (cond, exitflag)
2080 tree cond;
2081 int exitflag;
2083 struct nesting *thiscond = ALLOC_NESTING ();
2085 /* Make an entry on cond_stack for the cond we are entering. */
2087 thiscond->next = cond_stack;
2088 thiscond->all = nesting_stack;
2089 thiscond->depth = ++nesting_depth;
2090 thiscond->data.cond.next_label = gen_label_rtx ();
2091 /* Before we encounter an `else', we don't need a separate exit label
2092 unless there are supposed to be exit statements
2093 to exit this conditional. */
2094 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
2095 thiscond->data.cond.endif_label = thiscond->exit_label;
2096 cond_stack = thiscond;
2097 nesting_stack = thiscond;
2099 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
2102 /* Generate RTL between then-clause and the elseif-clause
2103 of an if-then-elseif-.... */
2105 void
2106 expand_start_elseif (cond)
2107 tree cond;
2109 if (cond_stack->data.cond.endif_label == 0)
2110 cond_stack->data.cond.endif_label = gen_label_rtx ();
2111 emit_jump (cond_stack->data.cond.endif_label);
2112 emit_label (cond_stack->data.cond.next_label);
2113 cond_stack->data.cond.next_label = gen_label_rtx ();
2114 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2117 /* Generate RTL between the then-clause and the else-clause
2118 of an if-then-else. */
2120 void
2121 expand_start_else ()
2123 if (cond_stack->data.cond.endif_label == 0)
2124 cond_stack->data.cond.endif_label = gen_label_rtx ();
2126 emit_jump (cond_stack->data.cond.endif_label);
2127 emit_label (cond_stack->data.cond.next_label);
2128 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
2131 /* After calling expand_start_else, turn this "else" into an "else if"
2132 by providing another condition. */
2134 void
2135 expand_elseif (cond)
2136 tree cond;
2138 cond_stack->data.cond.next_label = gen_label_rtx ();
2139 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2142 /* Generate RTL for the end of an if-then.
2143 Pop the record for it off of cond_stack. */
2145 void
2146 expand_end_cond ()
2148 struct nesting *thiscond = cond_stack;
2150 do_pending_stack_adjust ();
2151 if (thiscond->data.cond.next_label)
2152 emit_label (thiscond->data.cond.next_label);
2153 if (thiscond->data.cond.endif_label)
2154 emit_label (thiscond->data.cond.endif_label);
2156 POPSTACK (cond_stack);
2157 last_expr_type = 0;
2162 /* Generate RTL for the start of a loop. EXIT_FLAG is nonzero if this
2163 loop should be exited by `exit_something'. This is a loop for which
2164 `expand_continue' will jump to the top of the loop.
2166 Make an entry on loop_stack to record the labels associated with
2167 this loop. */
2169 struct nesting *
2170 expand_start_loop (exit_flag)
2171 int exit_flag;
2173 register struct nesting *thisloop = ALLOC_NESTING ();
2175 /* Make an entry on loop_stack for the loop we are entering. */
2177 thisloop->next = loop_stack;
2178 thisloop->all = nesting_stack;
2179 thisloop->depth = ++nesting_depth;
2180 thisloop->data.loop.start_label = gen_label_rtx ();
2181 thisloop->data.loop.end_label = gen_label_rtx ();
2182 thisloop->data.loop.alt_end_label = 0;
2183 thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
2184 thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
2185 loop_stack = thisloop;
2186 nesting_stack = thisloop;
2188 do_pending_stack_adjust ();
2189 emit_queue ();
2190 emit_note (NULL_PTR, NOTE_INSN_LOOP_BEG);
2191 emit_label (thisloop->data.loop.start_label);
2193 return thisloop;
2196 /* Like expand_start_loop but for a loop where the continuation point
2197 (for expand_continue_loop) will be specified explicitly. */
2199 struct nesting *
2200 expand_start_loop_continue_elsewhere (exit_flag)
2201 int exit_flag;
2203 struct nesting *thisloop = expand_start_loop (exit_flag);
2204 loop_stack->data.loop.continue_label = gen_label_rtx ();
2205 return thisloop;
2208 /* Specify the continuation point for a loop started with
2209 expand_start_loop_continue_elsewhere.
2210 Use this at the point in the code to which a continue statement
2211 should jump. */
2213 void
2214 expand_loop_continue_here ()
2216 do_pending_stack_adjust ();
2217 emit_note (NULL_PTR, NOTE_INSN_LOOP_CONT);
2218 emit_label (loop_stack->data.loop.continue_label);
2221 /* Finish a loop. Generate a jump back to the top and the loop-exit label.
2222 Pop the block off of loop_stack. */
2224 void
2225 expand_end_loop ()
2227 rtx start_label = loop_stack->data.loop.start_label;
2228 rtx insn = get_last_insn ();
2229 int needs_end_jump = 1;
2231 /* Mark the continue-point at the top of the loop if none elsewhere. */
2232 if (start_label == loop_stack->data.loop.continue_label)
2233 emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
2235 do_pending_stack_adjust ();
2237 /* If optimizing, perhaps reorder the loop.
2238 First, try to use a condjump near the end.
2239 expand_exit_loop_if_false ends loops with unconditional jumps,
2240 like this:
2242 if (test) goto label;
2243 optional: cleanup
2244 goto loop_stack->data.loop.end_label
2245 barrier
2246 label:
2248 If we find such a pattern, we can end the loop earlier. */
2250 if (optimize
2251 && GET_CODE (insn) == CODE_LABEL
2252 && LABEL_NAME (insn) == NULL
2253 && GET_CODE (PREV_INSN (insn)) == BARRIER)
2255 rtx label = insn;
2256 rtx jump = PREV_INSN (PREV_INSN (label));
2258 if (GET_CODE (jump) == JUMP_INSN
2259 && GET_CODE (PATTERN (jump)) == SET
2260 && SET_DEST (PATTERN (jump)) == pc_rtx
2261 && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
2262 && (XEXP (SET_SRC (PATTERN (jump)), 0)
2263 == loop_stack->data.loop.end_label))
2265 rtx prev;
2267 /* The test might be complex and reference LABEL multiple times,
2268 like the loop in loop_iterations to set vtop. To handle this,
2269 we move LABEL. */
2270 insn = PREV_INSN (label);
2271 reorder_insns (label, label, start_label);
2273 for (prev = PREV_INSN (jump); ; prev = PREV_INSN (prev))
2275 /* We ignore line number notes, but if we see any other note,
2276 in particular NOTE_INSN_BLOCK_*, NOTE_INSN_EH_REGION_*,
2277 NOTE_INSN_LOOP_*, we disable this optimization. */
2278 if (GET_CODE (prev) == NOTE)
2280 if (NOTE_LINE_NUMBER (prev) < 0)
2281 break;
2282 continue;
2284 if (GET_CODE (prev) == CODE_LABEL)
2285 break;
2286 if (GET_CODE (prev) == JUMP_INSN)
2288 if (GET_CODE (PATTERN (prev)) == SET
2289 && SET_DEST (PATTERN (prev)) == pc_rtx
2290 && GET_CODE (SET_SRC (PATTERN (prev))) == IF_THEN_ELSE
2291 && (GET_CODE (XEXP (SET_SRC (PATTERN (prev)), 1))
2292 == LABEL_REF)
2293 && XEXP (XEXP (SET_SRC (PATTERN (prev)), 1), 0) == label)
2295 XEXP (XEXP (SET_SRC (PATTERN (prev)), 1), 0)
2296 = start_label;
2297 emit_note_after (NOTE_INSN_LOOP_END, prev);
2298 needs_end_jump = 0;
2300 break;
2306 /* If the loop starts with a loop exit, roll that to the end where
2307 it will optimize together with the jump back.
2309 We look for the conditional branch to the exit, except that once
2310 we find such a branch, we don't look past 30 instructions.
2312 In more detail, if the loop presently looks like this (in pseudo-C):
2314 start_label:
2315 if (test) goto end_label;
2316 body;
2317 goto start_label;
2318 end_label:
2320 transform it to look like:
2322 goto start_label;
2323 newstart_label:
2324 body;
2325 start_label:
2326 if (test) goto end_label;
2327 goto newstart_label;
2328 end_label:
2330 Here, the `test' may actually consist of some reasonably complex
2331 code, terminating in a test. */
2333 if (optimize
2334 && needs_end_jump
2336 ! (GET_CODE (insn) == JUMP_INSN
2337 && GET_CODE (PATTERN (insn)) == SET
2338 && SET_DEST (PATTERN (insn)) == pc_rtx
2339 && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE))
2341 int eh_regions = 0;
2342 int num_insns = 0;
2343 rtx last_test_insn = NULL_RTX;
2345 /* Scan insns from the top of the loop looking for a qualified
2346 conditional exit. */
2347 for (insn = NEXT_INSN (loop_stack->data.loop.start_label); insn;
2348 insn = NEXT_INSN (insn))
2350 if (GET_CODE (insn) == NOTE)
2352 if (optimize < 2
2353 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2354 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2355 /* The code that actually moves the exit test will
2356 carefully leave BLOCK notes in their original
2357 location. That means, however, that we can't debug
2358 the exit test itself. So, we refuse to move code
2359 containing BLOCK notes at low optimization levels. */
2360 break;
2362 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2363 ++eh_regions;
2364 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
2366 --eh_regions;
2367 if (eh_regions < 0)
2368 /* We've come to the end of an EH region, but
2369 never saw the beginning of that region. That
2370 means that an EH region begins before the top
2371 of the loop, and ends in the middle of it. The
2372 existence of such a situation violates a basic
2373 assumption in this code, since that would imply
2374 that even when EH_REGIONS is zero, we might
2375 move code out of an exception region. */
2376 abort ();
2379 /* We must not walk into a nested loop. */
2380 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2381 break;
2383 /* We already know this INSN is a NOTE, so there's no
2384 point in looking at it to see if it's a JUMP. */
2385 continue;
2388 if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == INSN)
2389 num_insns++;
2391 if (last_test_insn && num_insns > 30)
2392 break;
2394 if (eh_regions > 0)
2395 /* We don't want to move a partial EH region. Consider:
2397 while ( ( { try {
2398 if (cond ()) 0;
2399 else {
2400 bar();
2403 } catch (...) {
2405 } )) {
2406 body;
2409 This isn't legal C++, but here's what it's supposed to
2410 mean: if cond() is true, stop looping. Otherwise,
2411 call bar, and keep looping. In addition, if cond
2412 throws an exception, catch it and keep looping. Such
2413 constructs are certainy legal in LISP.
2415 We should not move the `if (cond()) 0' test since then
2416 the EH-region for the try-block would be broken up.
2417 (In this case we would the EH_BEG note for the `try'
2418 and `if cond()' but not the call to bar() or the
2419 EH_END note.)
2421 So we don't look for tests within an EH region. */
2422 continue;
2424 if (GET_CODE (insn) == JUMP_INSN
2425 && GET_CODE (PATTERN (insn)) == SET
2426 && SET_DEST (PATTERN (insn)) == pc_rtx)
2428 /* This is indeed a jump. */
2429 rtx dest1 = NULL_RTX;
2430 rtx dest2 = NULL_RTX;
2431 rtx potential_last_test;
2432 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2434 /* A conditional jump. */
2435 dest1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2436 dest2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2437 potential_last_test = insn;
2439 else
2441 /* An unconditional jump. */
2442 dest1 = SET_SRC (PATTERN (insn));
2443 /* Include the BARRIER after the JUMP. */
2444 potential_last_test = NEXT_INSN (insn);
2447 do {
2448 if (dest1 && GET_CODE (dest1) == LABEL_REF
2449 && ((XEXP (dest1, 0)
2450 == loop_stack->data.loop.alt_end_label)
2451 || (XEXP (dest1, 0)
2452 == loop_stack->data.loop.end_label)))
2454 last_test_insn = potential_last_test;
2455 break;
2458 /* If this was a conditional jump, there may be
2459 another label at which we should look. */
2460 dest1 = dest2;
2461 dest2 = NULL_RTX;
2462 } while (dest1);
2466 if (last_test_insn != 0 && last_test_insn != get_last_insn ())
2468 /* We found one. Move everything from there up
2469 to the end of the loop, and add a jump into the loop
2470 to jump to there. */
2471 register rtx newstart_label = gen_label_rtx ();
2472 register rtx start_move = start_label;
2473 rtx next_insn;
2475 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
2476 then we want to move this note also. */
2477 if (GET_CODE (PREV_INSN (start_move)) == NOTE
2478 && (NOTE_LINE_NUMBER (PREV_INSN (start_move))
2479 == NOTE_INSN_LOOP_CONT))
2480 start_move = PREV_INSN (start_move);
2482 emit_label_after (newstart_label, PREV_INSN (start_move));
2484 /* Actually move the insns. Start at the beginning, and
2485 keep copying insns until we've copied the
2486 last_test_insn. */
2487 for (insn = start_move; insn; insn = next_insn)
2489 /* Figure out which insn comes after this one. We have
2490 to do this before we move INSN. */
2491 if (insn == last_test_insn)
2492 /* We've moved all the insns. */
2493 next_insn = NULL_RTX;
2494 else
2495 next_insn = NEXT_INSN (insn);
2497 if (GET_CODE (insn) == NOTE
2498 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2499 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2500 /* We don't want to move NOTE_INSN_BLOCK_BEGs or
2501 NOTE_INSN_BLOCK_ENDs because the correct generation
2502 of debugging information depends on these appearing
2503 in the same order in the RTL and in the tree
2504 structure, where they are represented as BLOCKs.
2505 So, we don't move block notes. Of course, moving
2506 the code inside the block is likely to make it
2507 impossible to debug the instructions in the exit
2508 test, but such is the price of optimization. */
2509 continue;
2511 /* Move the INSN. */
2512 reorder_insns (insn, insn, get_last_insn ());
2515 emit_jump_insn_after (gen_jump (start_label),
2516 PREV_INSN (newstart_label));
2517 emit_barrier_after (PREV_INSN (newstart_label));
2518 start_label = newstart_label;
2522 if (needs_end_jump)
2524 emit_jump (start_label);
2525 emit_note (NULL_PTR, NOTE_INSN_LOOP_END);
2527 emit_label (loop_stack->data.loop.end_label);
2529 POPSTACK (loop_stack);
2531 last_expr_type = 0;
2534 /* Generate a jump to the current loop's continue-point.
2535 This is usually the top of the loop, but may be specified
2536 explicitly elsewhere. If not currently inside a loop,
2537 return 0 and do nothing; caller will print an error message. */
2540 expand_continue_loop (whichloop)
2541 struct nesting *whichloop;
2543 last_expr_type = 0;
2544 if (whichloop == 0)
2545 whichloop = loop_stack;
2546 if (whichloop == 0)
2547 return 0;
2548 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2549 NULL_RTX);
2550 return 1;
2553 /* Generate a jump to exit the current loop. If not currently inside a loop,
2554 return 0 and do nothing; caller will print an error message. */
2557 expand_exit_loop (whichloop)
2558 struct nesting *whichloop;
2560 last_expr_type = 0;
2561 if (whichloop == 0)
2562 whichloop = loop_stack;
2563 if (whichloop == 0)
2564 return 0;
2565 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
2566 return 1;
2569 /* Generate a conditional jump to exit the current loop if COND
2570 evaluates to zero. If not currently inside a loop,
2571 return 0 and do nothing; caller will print an error message. */
2574 expand_exit_loop_if_false (whichloop, cond)
2575 struct nesting *whichloop;
2576 tree cond;
2578 rtx label = gen_label_rtx ();
2579 rtx last_insn;
2580 last_expr_type = 0;
2582 if (whichloop == 0)
2583 whichloop = loop_stack;
2584 if (whichloop == 0)
2585 return 0;
2586 /* In order to handle fixups, we actually create a conditional jump
2587 around a unconditional branch to exit the loop. If fixups are
2588 necessary, they go before the unconditional branch. */
2591 do_jump (cond, NULL_RTX, label);
2592 last_insn = get_last_insn ();
2593 if (GET_CODE (last_insn) == CODE_LABEL)
2594 whichloop->data.loop.alt_end_label = last_insn;
2595 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
2596 NULL_RTX);
2597 emit_label (label);
2599 return 1;
2602 /* Return nonzero if the loop nest is empty. Else return zero. */
2605 stmt_loop_nest_empty ()
2607 return (loop_stack == NULL);
2610 /* Return non-zero if we should preserve sub-expressions as separate
2611 pseudos. We never do so if we aren't optimizing. We always do so
2612 if -fexpensive-optimizations.
2614 Otherwise, we only do so if we are in the "early" part of a loop. I.e.,
2615 the loop may still be a small one. */
2618 preserve_subexpressions_p ()
2620 rtx insn;
2622 if (flag_expensive_optimizations)
2623 return 1;
2625 if (optimize == 0 || current_function == 0
2626 || current_function->stmt == 0 || loop_stack == 0)
2627 return 0;
2629 insn = get_last_insn_anywhere ();
2631 return (insn
2632 && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2633 < n_non_fixed_regs * 3));
2637 /* Generate a jump to exit the current loop, conditional, binding contour
2638 or case statement. Not all such constructs are visible to this function,
2639 only those started with EXIT_FLAG nonzero. Individual languages use
2640 the EXIT_FLAG parameter to control which kinds of constructs you can
2641 exit this way.
2643 If not currently inside anything that can be exited,
2644 return 0 and do nothing; caller will print an error message. */
2647 expand_exit_something ()
2649 struct nesting *n;
2650 last_expr_type = 0;
2651 for (n = nesting_stack; n; n = n->all)
2652 if (n->exit_label != 0)
2654 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
2655 return 1;
2658 return 0;
2661 /* Generate RTL to return from the current function, with no value.
2662 (That is, we do not do anything about returning any value.) */
2664 void
2665 expand_null_return ()
2667 struct nesting *block = block_stack;
2668 rtx last_insn = 0;
2670 /* Does any pending block have cleanups? */
2672 while (block && block->data.block.cleanups == 0)
2673 block = block->next;
2675 /* If yes, use a goto to return, since that runs cleanups. */
2677 expand_null_return_1 (last_insn, block != 0);
2680 /* Generate RTL to return from the current function, with value VAL. */
2682 static void
2683 expand_value_return (val)
2684 rtx val;
2686 struct nesting *block = block_stack;
2687 rtx last_insn = get_last_insn ();
2688 rtx return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
2690 /* Copy the value to the return location
2691 unless it's already there. */
2693 if (return_reg != val)
2695 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
2696 #ifdef PROMOTE_FUNCTION_RETURN
2697 int unsignedp = TREE_UNSIGNED (type);
2698 enum machine_mode old_mode
2699 = DECL_MODE (DECL_RESULT (current_function_decl));
2700 enum machine_mode mode
2701 = promote_mode (type, old_mode, &unsignedp, 1);
2703 if (mode != old_mode)
2704 val = convert_modes (mode, old_mode, val, unsignedp);
2705 #endif
2706 if (GET_CODE (return_reg) == PARALLEL)
2707 emit_group_load (return_reg, val, int_size_in_bytes (type),
2708 TYPE_ALIGN (type) / BITS_PER_UNIT);
2709 else
2710 emit_move_insn (return_reg, val);
2713 if (GET_CODE (return_reg) == REG
2714 && REGNO (return_reg) < FIRST_PSEUDO_REGISTER)
2715 emit_insn (gen_rtx_USE (VOIDmode, return_reg));
2717 /* Handle calls that return values in multiple non-contiguous locations.
2718 The Irix 6 ABI has examples of this. */
2719 else if (GET_CODE (return_reg) == PARALLEL)
2721 int i;
2723 for (i = 0; i < XVECLEN (return_reg, 0); i++)
2725 rtx x = XEXP (XVECEXP (return_reg, 0, i), 0);
2727 if (GET_CODE (x) == REG
2728 && REGNO (x) < FIRST_PSEUDO_REGISTER)
2729 emit_insn (gen_rtx_USE (VOIDmode, x));
2733 /* Does any pending block have cleanups? */
2735 while (block && block->data.block.cleanups == 0)
2736 block = block->next;
2738 /* If yes, use a goto to return, since that runs cleanups.
2739 Use LAST_INSN to put cleanups *before* the move insn emitted above. */
2741 expand_null_return_1 (last_insn, block != 0);
2744 /* Output a return with no value. If LAST_INSN is nonzero,
2745 pretend that the return takes place after LAST_INSN.
2746 If USE_GOTO is nonzero then don't use a return instruction;
2747 go to the return label instead. This causes any cleanups
2748 of pending blocks to be executed normally. */
2750 static void
2751 expand_null_return_1 (last_insn, use_goto)
2752 rtx last_insn;
2753 int use_goto;
2755 rtx end_label = cleanup_label ? cleanup_label : return_label;
2757 clear_pending_stack_adjust ();
2758 do_pending_stack_adjust ();
2759 last_expr_type = 0;
2761 /* PCC-struct return always uses an epilogue. */
2762 if (current_function_returns_pcc_struct || use_goto)
2764 if (end_label == 0)
2765 end_label = return_label = gen_label_rtx ();
2766 expand_goto_internal (NULL_TREE, end_label, last_insn);
2767 return;
2770 /* Otherwise output a simple return-insn if one is available,
2771 unless it won't do the job. */
2772 #ifdef HAVE_return
2773 if (HAVE_return && use_goto == 0 && cleanup_label == 0)
2775 emit_jump_insn (gen_return ());
2776 emit_barrier ();
2777 return;
2779 #endif
2781 /* Otherwise jump to the epilogue. */
2782 expand_goto_internal (NULL_TREE, end_label, last_insn);
2785 /* Generate RTL to evaluate the expression RETVAL and return it
2786 from the current function. */
2788 void
2789 expand_return (retval)
2790 tree retval;
2792 /* If there are any cleanups to be performed, then they will
2793 be inserted following LAST_INSN. It is desirable
2794 that the last_insn, for such purposes, should be the
2795 last insn before computing the return value. Otherwise, cleanups
2796 which call functions can clobber the return value. */
2797 /* ??? rms: I think that is erroneous, because in C++ it would
2798 run destructors on variables that might be used in the subsequent
2799 computation of the return value. */
2800 rtx last_insn = 0;
2801 rtx result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
2802 register rtx val = 0;
2803 register rtx op0;
2804 tree retval_rhs;
2805 int cleanups;
2807 /* If function wants no value, give it none. */
2808 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2810 expand_expr (retval, NULL_RTX, VOIDmode, 0);
2811 emit_queue ();
2812 expand_null_return ();
2813 return;
2816 /* Are any cleanups needed? E.g. C++ destructors to be run? */
2817 /* This is not sufficient. We also need to watch for cleanups of the
2818 expression we are about to expand. Unfortunately, we cannot know
2819 if it has cleanups until we expand it, and we want to change how we
2820 expand it depending upon if we need cleanups. We can't win. */
2821 #if 0
2822 cleanups = any_pending_cleanups (1);
2823 #else
2824 cleanups = 1;
2825 #endif
2827 if (TREE_CODE (retval) == RESULT_DECL)
2828 retval_rhs = retval;
2829 else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
2830 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
2831 retval_rhs = TREE_OPERAND (retval, 1);
2832 else if (TREE_TYPE (retval) == void_type_node)
2833 /* Recognize tail-recursive call to void function. */
2834 retval_rhs = retval;
2835 else
2836 retval_rhs = NULL_TREE;
2838 /* Only use `last_insn' if there are cleanups which must be run. */
2839 if (cleanups || cleanup_label != 0)
2840 last_insn = get_last_insn ();
2842 /* Distribute return down conditional expr if either of the sides
2843 may involve tail recursion (see test below). This enhances the number
2844 of tail recursions we see. Don't do this always since it can produce
2845 sub-optimal code in some cases and we distribute assignments into
2846 conditional expressions when it would help. */
2848 if (optimize && retval_rhs != 0
2849 && frame_offset == 0
2850 && TREE_CODE (retval_rhs) == COND_EXPR
2851 && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
2852 || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
2854 rtx label = gen_label_rtx ();
2855 tree expr;
2857 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
2858 start_cleanup_deferral ();
2859 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
2860 DECL_RESULT (current_function_decl),
2861 TREE_OPERAND (retval_rhs, 1));
2862 TREE_SIDE_EFFECTS (expr) = 1;
2863 expand_return (expr);
2864 emit_label (label);
2866 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
2867 DECL_RESULT (current_function_decl),
2868 TREE_OPERAND (retval_rhs, 2));
2869 TREE_SIDE_EFFECTS (expr) = 1;
2870 expand_return (expr);
2871 end_cleanup_deferral ();
2872 return;
2875 /* Attempt to optimize the call if it is tail recursive. */
2876 if (optimize_tail_recursion (retval_rhs, last_insn))
2877 return;
2879 #ifdef HAVE_return
2880 /* This optimization is safe if there are local cleanups
2881 because expand_null_return takes care of them.
2882 ??? I think it should also be safe when there is a cleanup label,
2883 because expand_null_return takes care of them, too.
2884 Any reason why not? */
2885 if (HAVE_return && cleanup_label == 0
2886 && ! current_function_returns_pcc_struct
2887 && BRANCH_COST <= 1)
2889 /* If this is return x == y; then generate
2890 if (x == y) return 1; else return 0;
2891 if we can do it with explicit return insns and branches are cheap,
2892 but not if we have the corresponding scc insn. */
2893 int has_scc = 0;
2894 if (retval_rhs)
2895 switch (TREE_CODE (retval_rhs))
2897 case EQ_EXPR:
2898 #ifdef HAVE_seq
2899 has_scc = HAVE_seq;
2900 #endif
2901 case NE_EXPR:
2902 #ifdef HAVE_sne
2903 has_scc = HAVE_sne;
2904 #endif
2905 case GT_EXPR:
2906 #ifdef HAVE_sgt
2907 has_scc = HAVE_sgt;
2908 #endif
2909 case GE_EXPR:
2910 #ifdef HAVE_sge
2911 has_scc = HAVE_sge;
2912 #endif
2913 case LT_EXPR:
2914 #ifdef HAVE_slt
2915 has_scc = HAVE_slt;
2916 #endif
2917 case LE_EXPR:
2918 #ifdef HAVE_sle
2919 has_scc = HAVE_sle;
2920 #endif
2921 case TRUTH_ANDIF_EXPR:
2922 case TRUTH_ORIF_EXPR:
2923 case TRUTH_AND_EXPR:
2924 case TRUTH_OR_EXPR:
2925 case TRUTH_NOT_EXPR:
2926 case TRUTH_XOR_EXPR:
2927 if (! has_scc)
2929 op0 = gen_label_rtx ();
2930 jumpifnot (retval_rhs, op0);
2931 expand_value_return (const1_rtx);
2932 emit_label (op0);
2933 expand_value_return (const0_rtx);
2934 return;
2936 break;
2938 default:
2939 break;
2942 #endif /* HAVE_return */
2944 /* If the result is an aggregate that is being returned in one (or more)
2945 registers, load the registers here. The compiler currently can't handle
2946 copying a BLKmode value into registers. We could put this code in a
2947 more general area (for use by everyone instead of just function
2948 call/return), but until this feature is generally usable it is kept here
2949 (and in expand_call). The value must go into a pseudo in case there
2950 are cleanups that will clobber the real return register. */
2952 if (retval_rhs != 0
2953 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
2954 && GET_CODE (result_rtl) == REG)
2956 int i, bitpos, xbitpos;
2957 int big_endian_correction = 0;
2958 int bytes = int_size_in_bytes (TREE_TYPE (retval_rhs));
2959 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2960 int bitsize = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)),
2961 (unsigned int)BITS_PER_WORD);
2962 rtx *result_pseudos = (rtx *) alloca (sizeof (rtx) * n_regs);
2963 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
2964 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
2965 enum machine_mode tmpmode, result_reg_mode;
2967 /* Structures whose size is not a multiple of a word are aligned
2968 to the least significant byte (to the right). On a BYTES_BIG_ENDIAN
2969 machine, this means we must skip the empty high order bytes when
2970 calculating the bit offset. */
2971 if (BYTES_BIG_ENDIAN && bytes % UNITS_PER_WORD)
2972 big_endian_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2973 * BITS_PER_UNIT));
2975 /* Copy the structure BITSIZE bits at a time. */
2976 for (bitpos = 0, xbitpos = big_endian_correction;
2977 bitpos < bytes * BITS_PER_UNIT;
2978 bitpos += bitsize, xbitpos += bitsize)
2980 /* We need a new destination pseudo each time xbitpos is
2981 on a word boundary and when xbitpos == big_endian_correction
2982 (the first time through). */
2983 if (xbitpos % BITS_PER_WORD == 0
2984 || xbitpos == big_endian_correction)
2986 /* Generate an appropriate register. */
2987 dst = gen_reg_rtx (word_mode);
2988 result_pseudos[xbitpos / BITS_PER_WORD] = dst;
2990 /* Clobber the destination before we move anything into it. */
2991 emit_insn (gen_rtx_CLOBBER (VOIDmode, dst));
2994 /* We need a new source operand each time bitpos is on a word
2995 boundary. */
2996 if (bitpos % BITS_PER_WORD == 0)
2997 src = operand_subword_force (result_val,
2998 bitpos / BITS_PER_WORD,
2999 BLKmode);
3001 /* Use bitpos for the source extraction (left justified) and
3002 xbitpos for the destination store (right justified). */
3003 store_bit_field (dst, bitsize, xbitpos % BITS_PER_WORD, word_mode,
3004 extract_bit_field (src, bitsize,
3005 bitpos % BITS_PER_WORD, 1,
3006 NULL_RTX, word_mode,
3007 word_mode,
3008 bitsize / BITS_PER_UNIT,
3009 BITS_PER_WORD),
3010 bitsize / BITS_PER_UNIT, BITS_PER_WORD);
3013 /* Find the smallest integer mode large enough to hold the
3014 entire structure and use that mode instead of BLKmode
3015 on the USE insn for the return register. */
3016 bytes = int_size_in_bytes (TREE_TYPE (retval_rhs));
3017 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3018 tmpmode != VOIDmode;
3019 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
3021 /* Have we found a large enough mode? */
3022 if (GET_MODE_SIZE (tmpmode) >= bytes)
3023 break;
3026 /* No suitable mode found. */
3027 if (tmpmode == VOIDmode)
3028 abort ();
3030 PUT_MODE (result_rtl, tmpmode);
3032 if (GET_MODE_SIZE (tmpmode) < GET_MODE_SIZE (word_mode))
3033 result_reg_mode = word_mode;
3034 else
3035 result_reg_mode = tmpmode;
3036 result_reg = gen_reg_rtx (result_reg_mode);
3038 emit_queue ();
3039 for (i = 0; i < n_regs; i++)
3040 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3041 result_pseudos[i]);
3043 if (tmpmode != result_reg_mode)
3044 result_reg = gen_lowpart (tmpmode, result_reg);
3046 expand_value_return (result_reg);
3048 else if (cleanups
3049 && retval_rhs != 0
3050 && TREE_TYPE (retval_rhs) != void_type_node
3051 && (GET_CODE (result_rtl) == REG
3052 || (GET_CODE (result_rtl) == PARALLEL)))
3054 /* Calculate the return value into a temporary (usually a pseudo
3055 reg). */
3056 val = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)),
3057 0, 0, 1);
3058 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
3059 val = force_not_mem (val);
3060 emit_queue ();
3061 /* Return the calculated value, doing cleanups first. */
3062 expand_value_return (val);
3064 else
3066 /* No cleanups or no hard reg used;
3067 calculate value into hard return reg. */
3068 expand_expr (retval, const0_rtx, VOIDmode, 0);
3069 emit_queue ();
3070 expand_value_return (result_rtl);
3074 /* Return 1 if the end of the generated RTX is not a barrier.
3075 This means code already compiled can drop through. */
3078 drop_through_at_end_p ()
3080 rtx insn = get_last_insn ();
3081 while (insn && GET_CODE (insn) == NOTE)
3082 insn = PREV_INSN (insn);
3083 return insn && GET_CODE (insn) != BARRIER;
3086 /* Test CALL_EXPR to determine if it is a potential tail recursion call
3087 and emit code to optimize the tail recursion. LAST_INSN indicates where
3088 to place the jump to the tail recursion label. Return TRUE if the
3089 call was optimized into a goto.
3091 This is only used by expand_return, but expand_call is expected to
3092 use it soon. */
3095 optimize_tail_recursion (call_expr, last_insn)
3096 tree call_expr;
3097 rtx last_insn;
3099 /* For tail-recursive call to current function,
3100 just jump back to the beginning.
3101 It's unsafe if any auto variable in this function
3102 has its address taken; for simplicity,
3103 require stack frame to be empty. */
3104 if (optimize && call_expr != 0
3105 && frame_offset == 0
3106 && TREE_CODE (call_expr) == CALL_EXPR
3107 && TREE_CODE (TREE_OPERAND (call_expr, 0)) == ADDR_EXPR
3108 && TREE_OPERAND (TREE_OPERAND (call_expr, 0), 0) == current_function_decl
3109 /* Finish checking validity, and if valid emit code
3110 to set the argument variables for the new call. */
3111 && tail_recursion_args (TREE_OPERAND (call_expr, 1),
3112 DECL_ARGUMENTS (current_function_decl)))
3114 if (tail_recursion_label == 0)
3116 tail_recursion_label = gen_label_rtx ();
3117 emit_label_after (tail_recursion_label,
3118 tail_recursion_reentry);
3120 emit_queue ();
3121 expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
3122 emit_barrier ();
3123 return 1;
3126 return 0;
3129 /* Emit code to alter this function's formal parms for a tail-recursive call.
3130 ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
3131 FORMALS is the chain of decls of formals.
3132 Return 1 if this can be done;
3133 otherwise return 0 and do not emit any code. */
3135 static int
3136 tail_recursion_args (actuals, formals)
3137 tree actuals, formals;
3139 register tree a = actuals, f = formals;
3140 register int i;
3141 register rtx *argvec;
3143 /* Check that number and types of actuals are compatible
3144 with the formals. This is not always true in valid C code.
3145 Also check that no formal needs to be addressable
3146 and that all formals are scalars. */
3148 /* Also count the args. */
3150 for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
3152 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (a)))
3153 != TYPE_MAIN_VARIANT (TREE_TYPE (f)))
3154 return 0;
3155 if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
3156 return 0;
3158 if (a != 0 || f != 0)
3159 return 0;
3161 /* Compute all the actuals. */
3163 argvec = (rtx *) alloca (i * sizeof (rtx));
3165 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3166 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
3168 /* Find which actual values refer to current values of previous formals.
3169 Copy each of them now, before any formal is changed. */
3171 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3173 int copy = 0;
3174 register int j;
3175 for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
3176 if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
3177 { copy = 1; break; }
3178 if (copy)
3179 argvec[i] = copy_to_reg (argvec[i]);
3182 /* Store the values of the actuals into the formals. */
3184 for (f = formals, a = actuals, i = 0; f;
3185 f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
3187 if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
3188 emit_move_insn (DECL_RTL (f), argvec[i]);
3189 else
3190 convert_move (DECL_RTL (f), argvec[i],
3191 TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a))));
3194 free_temp_slots ();
3195 return 1;
3198 /* Generate the RTL code for entering a binding contour.
3199 The variables are declared one by one, by calls to `expand_decl'.
3201 FLAGS is a bitwise or of the following flags:
3203 1 - Nonzero if this construct should be visible to
3204 `exit_something'.
3206 2 - Nonzero if this contour does not require a
3207 NOTE_INSN_BLOCK_BEG note. Virtually all calls from
3208 language-independent code should set this flag because they
3209 will not create corresponding BLOCK nodes. (There should be
3210 a one-to-one correspondence between NOTE_INSN_BLOCK_BEG notes
3211 and BLOCKs.) If this flag is set, MARK_ENDS should be zero
3212 when expand_end_bindings is called. */
3214 void
3215 expand_start_bindings (flags)
3216 int flags;
3218 struct nesting *thisblock = ALLOC_NESTING ();
3219 rtx note;
3220 int exit_flag = ((flags & 1) != 0);
3221 int block_flag = ((flags & 2) == 0);
3223 note = emit_note (NULL_PTR,
3224 block_flag ? NOTE_INSN_BLOCK_BEG : NOTE_INSN_DELETED);
3226 /* Make an entry on block_stack for the block we are entering. */
3228 thisblock->next = block_stack;
3229 thisblock->all = nesting_stack;
3230 thisblock->depth = ++nesting_depth;
3231 thisblock->data.block.stack_level = 0;
3232 thisblock->data.block.cleanups = 0;
3233 thisblock->data.block.n_function_calls = 0;
3234 thisblock->data.block.exception_region = 0;
3235 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
3237 thisblock->data.block.conditional_code = 0;
3238 thisblock->data.block.last_unconditional_cleanup = note;
3239 /* When we insert instructions after the last unconditional cleanup,
3240 we don't adjust last_insn. That means that a later add_insn will
3241 clobber the instructions we've just added. The easiest way to
3242 fix this is to just insert another instruction here, so that the
3243 instructions inserted after the last unconditional cleanup are
3244 never the last instruction. */
3245 emit_note (NULL_PTR, NOTE_INSN_DELETED);
3246 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
3248 if (block_stack
3249 && !(block_stack->data.block.cleanups == NULL_TREE
3250 && block_stack->data.block.outer_cleanups == NULL_TREE))
3251 thisblock->data.block.outer_cleanups
3252 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3253 block_stack->data.block.outer_cleanups);
3254 else
3255 thisblock->data.block.outer_cleanups = 0;
3256 thisblock->data.block.label_chain = 0;
3257 thisblock->data.block.innermost_stack_block = stack_block_stack;
3258 thisblock->data.block.first_insn = note;
3259 thisblock->data.block.block_start_count = ++current_block_start_count;
3260 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3261 block_stack = thisblock;
3262 nesting_stack = thisblock;
3264 /* Make a new level for allocating stack slots. */
3265 push_temp_slots ();
3268 /* Specify the scope of temporaries created by TARGET_EXPRs. Similar
3269 to CLEANUP_POINT_EXPR, but handles cases when a series of calls to
3270 expand_expr are made. After we end the region, we know that all
3271 space for all temporaries that were created by TARGET_EXPRs will be
3272 destroyed and their space freed for reuse. */
3274 void
3275 expand_start_target_temps ()
3277 /* This is so that even if the result is preserved, the space
3278 allocated will be freed, as we know that it is no longer in use. */
3279 push_temp_slots ();
3281 /* Start a new binding layer that will keep track of all cleanup
3282 actions to be performed. */
3283 expand_start_bindings (2);
3285 target_temp_slot_level = temp_slot_level;
3288 void
3289 expand_end_target_temps ()
3291 expand_end_bindings (NULL_TREE, 0, 0);
3293 /* This is so that even if the result is preserved, the space
3294 allocated will be freed, as we know that it is no longer in use. */
3295 pop_temp_slots ();
3298 /* Mark top block of block_stack as an implicit binding for an
3299 exception region. This is used to prevent infinite recursion when
3300 ending a binding with expand_end_bindings. It is only ever called
3301 by expand_eh_region_start, as that it the only way to create a
3302 block stack for a exception region. */
3304 void
3305 mark_block_as_eh_region ()
3307 block_stack->data.block.exception_region = 1;
3308 if (block_stack->next
3309 && block_stack->next->data.block.conditional_code)
3311 block_stack->data.block.conditional_code
3312 = block_stack->next->data.block.conditional_code;
3313 block_stack->data.block.last_unconditional_cleanup
3314 = block_stack->next->data.block.last_unconditional_cleanup;
3315 block_stack->data.block.cleanup_ptr
3316 = block_stack->next->data.block.cleanup_ptr;
3320 /* True if we are currently emitting insns in an area of output code
3321 that is controlled by a conditional expression. This is used by
3322 the cleanup handling code to generate conditional cleanup actions. */
3325 conditional_context ()
3327 return block_stack && block_stack->data.block.conditional_code;
3330 /* Mark top block of block_stack as not for an implicit binding for an
3331 exception region. This is only ever done by expand_eh_region_end
3332 to let expand_end_bindings know that it is being called explicitly
3333 to end the binding layer for just the binding layer associated with
3334 the exception region, otherwise expand_end_bindings would try and
3335 end all implicit binding layers for exceptions regions, and then
3336 one normal binding layer. */
3338 void
3339 mark_block_as_not_eh_region ()
3341 block_stack->data.block.exception_region = 0;
3344 /* True if the top block of block_stack was marked as for an exception
3345 region by mark_block_as_eh_region. */
3348 is_eh_region ()
3350 return (current_function && block_stack
3351 && block_stack->data.block.exception_region);
3354 /* Emit a handler label for a nonlocal goto handler.
3355 Also emit code to store the handler label in SLOT before BEFORE_INSN. */
3357 static rtx
3358 expand_nl_handler_label (slot, before_insn)
3359 rtx slot, before_insn;
3361 rtx insns;
3362 rtx handler_label = gen_label_rtx ();
3364 /* Don't let jump_optimize delete the handler. */
3365 LABEL_PRESERVE_P (handler_label) = 1;
3367 start_sequence ();
3368 emit_move_insn (slot, gen_rtx_LABEL_REF (Pmode, handler_label));
3369 insns = get_insns ();
3370 end_sequence ();
3371 emit_insns_before (insns, before_insn);
3373 emit_label (handler_label);
3375 return handler_label;
3378 /* Emit code to restore vital registers at the beginning of a nonlocal goto
3379 handler. */
3380 static void
3381 expand_nl_goto_receiver ()
3383 #ifdef HAVE_nonlocal_goto
3384 if (! HAVE_nonlocal_goto)
3385 #endif
3386 /* First adjust our frame pointer to its actual value. It was
3387 previously set to the start of the virtual area corresponding to
3388 the stacked variables when we branched here and now needs to be
3389 adjusted to the actual hardware fp value.
3391 Assignments are to virtual registers are converted by
3392 instantiate_virtual_regs into the corresponding assignment
3393 to the underlying register (fp in this case) that makes
3394 the original assignment true.
3395 So the following insn will actually be
3396 decrementing fp by STARTING_FRAME_OFFSET. */
3397 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
3399 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3400 if (fixed_regs[ARG_POINTER_REGNUM])
3402 #ifdef ELIMINABLE_REGS
3403 /* If the argument pointer can be eliminated in favor of the
3404 frame pointer, we don't need to restore it. We assume here
3405 that if such an elimination is present, it can always be used.
3406 This is the case on all known machines; if we don't make this
3407 assumption, we do unnecessary saving on many machines. */
3408 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
3409 size_t i;
3411 for (i = 0; i < sizeof elim_regs / sizeof elim_regs[0]; i++)
3412 if (elim_regs[i].from == ARG_POINTER_REGNUM
3413 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3414 break;
3416 if (i == sizeof elim_regs / sizeof elim_regs [0])
3417 #endif
3419 /* Now restore our arg pointer from the address at which it
3420 was saved in our stack frame.
3421 If there hasn't be space allocated for it yet, make
3422 some now. */
3423 if (arg_pointer_save_area == 0)
3424 arg_pointer_save_area
3425 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
3426 emit_move_insn (virtual_incoming_args_rtx,
3427 /* We need a pseudo here, or else
3428 instantiate_virtual_regs_1 complains. */
3429 copy_to_reg (arg_pointer_save_area));
3432 #endif
3434 #ifdef HAVE_nonlocal_goto_receiver
3435 if (HAVE_nonlocal_goto_receiver)
3436 emit_insn (gen_nonlocal_goto_receiver ());
3437 #endif
3440 /* Make handlers for nonlocal gotos taking place in the function calls in
3441 block THISBLOCK. */
3443 static void
3444 expand_nl_goto_receivers (thisblock)
3445 struct nesting *thisblock;
3447 tree link;
3448 rtx afterward = gen_label_rtx ();
3449 rtx insns, slot;
3450 rtx label_list;
3451 int any_invalid;
3453 /* Record the handler address in the stack slot for that purpose,
3454 during this block, saving and restoring the outer value. */
3455 if (thisblock->next != 0)
3456 for (slot = nonlocal_goto_handler_slots; slot; slot = XEXP (slot, 1))
3458 rtx save_receiver = gen_reg_rtx (Pmode);
3459 emit_move_insn (XEXP (slot, 0), save_receiver);
3461 start_sequence ();
3462 emit_move_insn (save_receiver, XEXP (slot, 0));
3463 insns = get_insns ();
3464 end_sequence ();
3465 emit_insns_before (insns, thisblock->data.block.first_insn);
3468 /* Jump around the handlers; they run only when specially invoked. */
3469 emit_jump (afterward);
3471 /* Make a separate handler for each label. */
3472 link = nonlocal_labels;
3473 slot = nonlocal_goto_handler_slots;
3474 label_list = NULL_RTX;
3475 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3476 /* Skip any labels we shouldn't be able to jump to from here,
3477 we generate one special handler for all of them below which just calls
3478 abort. */
3479 if (! DECL_TOO_LATE (TREE_VALUE (link)))
3481 rtx lab;
3482 lab = expand_nl_handler_label (XEXP (slot, 0),
3483 thisblock->data.block.first_insn);
3484 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3486 expand_nl_goto_receiver ();
3488 /* Jump to the "real" nonlocal label. */
3489 expand_goto (TREE_VALUE (link));
3492 /* A second pass over all nonlocal labels; this time we handle those
3493 we should not be able to jump to at this point. */
3494 link = nonlocal_labels;
3495 slot = nonlocal_goto_handler_slots;
3496 any_invalid = 0;
3497 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3498 if (DECL_TOO_LATE (TREE_VALUE (link)))
3500 rtx lab;
3501 lab = expand_nl_handler_label (XEXP (slot, 0),
3502 thisblock->data.block.first_insn);
3503 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3504 any_invalid = 1;
3507 if (any_invalid)
3509 expand_nl_goto_receiver ();
3510 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "abort"), 0,
3511 VOIDmode, 0);
3512 emit_barrier ();
3515 nonlocal_goto_handler_labels = label_list;
3516 emit_label (afterward);
3519 /* Warn about any unused VARS (which may contain nodes other than
3520 VAR_DECLs, but such nodes are ignored). The nodes are connected
3521 via the TREE_CHAIN field. */
3523 void
3524 warn_about_unused_variables (vars)
3525 tree vars;
3527 tree decl;
3529 if (warn_unused)
3530 for (decl = vars; decl; decl = TREE_CHAIN (decl))
3531 if (TREE_CODE (decl) == VAR_DECL
3532 && ! TREE_USED (decl)
3533 && ! DECL_IN_SYSTEM_HEADER (decl)
3534 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
3535 warning_with_decl (decl, "unused variable `%s'");
3538 /* Generate RTL code to terminate a binding contour.
3540 VARS is the chain of VAR_DECL nodes for the variables bound in this
3541 contour. There may actually be other nodes in this chain, but any
3542 nodes other than VAR_DECLS are ignored.
3544 MARK_ENDS is nonzero if we should put a note at the beginning
3545 and end of this binding contour.
3547 DONT_JUMP_IN is nonzero if it is not valid to jump into this contour.
3548 (That is true automatically if the contour has a saved stack level.) */
3550 void
3551 expand_end_bindings (vars, mark_ends, dont_jump_in)
3552 tree vars;
3553 int mark_ends;
3554 int dont_jump_in;
3556 register struct nesting *thisblock;
3557 register tree decl;
3559 while (block_stack->data.block.exception_region)
3561 /* Because we don't need or want a new temporary level and
3562 because we didn't create one in expand_eh_region_start,
3563 create a fake one now to avoid removing one in
3564 expand_end_bindings. */
3565 push_temp_slots ();
3567 block_stack->data.block.exception_region = 0;
3569 expand_end_bindings (NULL_TREE, 0, 0);
3572 /* Since expand_eh_region_start does an expand_start_bindings, we
3573 have to first end all the bindings that were created by
3574 expand_eh_region_start. */
3576 thisblock = block_stack;
3578 /* If any of the variables in this scope were not used, warn the
3579 user. */
3580 warn_about_unused_variables (vars);
3582 if (thisblock->exit_label)
3584 do_pending_stack_adjust ();
3585 emit_label (thisblock->exit_label);
3588 /* If necessary, make handlers for nonlocal gotos taking
3589 place in the function calls in this block. */
3590 if (function_call_count != thisblock->data.block.n_function_calls
3591 && nonlocal_labels
3592 /* Make handler for outermost block
3593 if there were any nonlocal gotos to this function. */
3594 && (thisblock->next == 0 ? current_function_has_nonlocal_label
3595 /* Make handler for inner block if it has something
3596 special to do when you jump out of it. */
3597 : (thisblock->data.block.cleanups != 0
3598 || thisblock->data.block.stack_level != 0)))
3599 expand_nl_goto_receivers (thisblock);
3601 /* Don't allow jumping into a block that has a stack level.
3602 Cleanups are allowed, though. */
3603 if (dont_jump_in
3604 || thisblock->data.block.stack_level != 0)
3606 struct label_chain *chain;
3608 /* Any labels in this block are no longer valid to go to.
3609 Mark them to cause an error message. */
3610 for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
3612 DECL_TOO_LATE (chain->label) = 1;
3613 /* If any goto without a fixup came to this label,
3614 that must be an error, because gotos without fixups
3615 come from outside all saved stack-levels. */
3616 if (TREE_ADDRESSABLE (chain->label))
3617 error_with_decl (chain->label,
3618 "label `%s' used before containing binding contour");
3622 /* Restore stack level in effect before the block
3623 (only if variable-size objects allocated). */
3624 /* Perform any cleanups associated with the block. */
3626 if (thisblock->data.block.stack_level != 0
3627 || thisblock->data.block.cleanups != 0)
3629 /* Only clean up here if this point can actually be reached. */
3630 int reachable = GET_CODE (get_last_insn ()) != BARRIER;
3632 /* Don't let cleanups affect ({...}) constructs. */
3633 int old_expr_stmts_for_value = expr_stmts_for_value;
3634 rtx old_last_expr_value = last_expr_value;
3635 tree old_last_expr_type = last_expr_type;
3636 expr_stmts_for_value = 0;
3638 /* Do the cleanups. */
3639 expand_cleanups (thisblock->data.block.cleanups, NULL_TREE, 0, reachable);
3640 if (reachable)
3641 do_pending_stack_adjust ();
3643 expr_stmts_for_value = old_expr_stmts_for_value;
3644 last_expr_value = old_last_expr_value;
3645 last_expr_type = old_last_expr_type;
3647 /* Restore the stack level. */
3649 if (reachable && thisblock->data.block.stack_level != 0)
3651 emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3652 thisblock->data.block.stack_level, NULL_RTX);
3653 if (nonlocal_goto_handler_slots != 0)
3654 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3655 NULL_RTX);
3658 /* Any gotos out of this block must also do these things.
3659 Also report any gotos with fixups that came to labels in this
3660 level. */
3661 fixup_gotos (thisblock,
3662 thisblock->data.block.stack_level,
3663 thisblock->data.block.cleanups,
3664 thisblock->data.block.first_insn,
3665 dont_jump_in);
3668 /* Mark the beginning and end of the scope if requested.
3669 We do this now, after running cleanups on the variables
3670 just going out of scope, so they are in scope for their cleanups. */
3672 if (mark_ends)
3673 emit_note (NULL_PTR, NOTE_INSN_BLOCK_END);
3674 else
3675 /* Get rid of the beginning-mark if we don't make an end-mark. */
3676 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
3678 /* If doing stupid register allocation, make sure lives of all
3679 register variables declared here extend thru end of scope. */
3681 if (obey_regdecls)
3682 for (decl = vars; decl; decl = TREE_CHAIN (decl))
3683 if (TREE_CODE (decl) == VAR_DECL && DECL_RTL (decl))
3684 use_variable (DECL_RTL (decl));
3686 /* Restore the temporary level of TARGET_EXPRs. */
3687 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
3689 /* Restore block_stack level for containing block. */
3691 stack_block_stack = thisblock->data.block.innermost_stack_block;
3692 POPSTACK (block_stack);
3694 /* Pop the stack slot nesting and free any slots at this level. */
3695 pop_temp_slots ();
3698 /* Generate RTL for the automatic variable declaration DECL.
3699 (Other kinds of declarations are simply ignored if seen here.) */
3701 void
3702 expand_decl (decl)
3703 register tree decl;
3705 struct nesting *thisblock;
3706 tree type;
3708 type = TREE_TYPE (decl);
3710 /* Only automatic variables need any expansion done.
3711 Static and external variables, and external functions,
3712 will be handled by `assemble_variable' (called from finish_decl).
3713 TYPE_DECL and CONST_DECL require nothing.
3714 PARM_DECLs are handled in `assign_parms'. */
3716 if (TREE_CODE (decl) != VAR_DECL)
3717 return;
3718 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3719 return;
3721 thisblock = block_stack;
3723 /* Create the RTL representation for the variable. */
3725 if (type == error_mark_node)
3726 DECL_RTL (decl) = gen_rtx_MEM (BLKmode, const0_rtx);
3727 else if (DECL_SIZE (decl) == 0)
3728 /* Variable with incomplete type. */
3730 if (DECL_INITIAL (decl) == 0)
3731 /* Error message was already done; now avoid a crash. */
3732 DECL_RTL (decl) = assign_stack_temp (DECL_MODE (decl), 0, 1);
3733 else
3734 /* An initializer is going to decide the size of this array.
3735 Until we know the size, represent its address with a reg. */
3736 DECL_RTL (decl) = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3737 MEM_SET_IN_STRUCT_P (DECL_RTL (decl), AGGREGATE_TYPE_P (type));
3739 else if (DECL_MODE (decl) != BLKmode
3740 /* If -ffloat-store, don't put explicit float vars
3741 into regs. */
3742 && !(flag_float_store
3743 && TREE_CODE (type) == REAL_TYPE)
3744 && ! TREE_THIS_VOLATILE (decl)
3745 && ! TREE_ADDRESSABLE (decl)
3746 && (DECL_REGISTER (decl) || ! obey_regdecls)
3747 /* if -fcheck-memory-usage, check all variables. */
3748 && ! current_function_check_memory_usage)
3750 /* Automatic variable that can go in a register. */
3751 int unsignedp = TREE_UNSIGNED (type);
3752 enum machine_mode reg_mode
3753 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
3755 DECL_RTL (decl) = gen_reg_rtx (reg_mode);
3756 mark_user_reg (DECL_RTL (decl));
3758 if (POINTER_TYPE_P (type))
3759 mark_reg_pointer (DECL_RTL (decl),
3760 (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl)))
3761 / BITS_PER_UNIT));
3764 else if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST
3765 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
3766 && (TREE_INT_CST_HIGH (DECL_SIZE (decl)) != 0
3767 || (TREE_INT_CST_LOW (DECL_SIZE (decl))
3768 > STACK_CHECK_MAX_VAR_SIZE * BITS_PER_UNIT))))
3770 /* Variable of fixed size that goes on the stack. */
3771 rtx oldaddr = 0;
3772 rtx addr;
3774 /* If we previously made RTL for this decl, it must be an array
3775 whose size was determined by the initializer.
3776 The old address was a register; set that register now
3777 to the proper address. */
3778 if (DECL_RTL (decl) != 0)
3780 if (GET_CODE (DECL_RTL (decl)) != MEM
3781 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
3782 abort ();
3783 oldaddr = XEXP (DECL_RTL (decl), 0);
3786 DECL_RTL (decl) = assign_temp (TREE_TYPE (decl), 1, 1, 1);
3787 MEM_SET_IN_STRUCT_P (DECL_RTL (decl),
3788 AGGREGATE_TYPE_P (TREE_TYPE (decl)));
3790 /* Set alignment we actually gave this decl. */
3791 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
3792 : GET_MODE_BITSIZE (DECL_MODE (decl)));
3794 if (oldaddr)
3796 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
3797 if (addr != oldaddr)
3798 emit_move_insn (oldaddr, addr);
3801 /* If this is a memory ref that contains aggregate components,
3802 mark it as such for cse and loop optimize. */
3803 MEM_SET_IN_STRUCT_P (DECL_RTL (decl),
3804 AGGREGATE_TYPE_P (TREE_TYPE (decl)));
3805 #if 0
3806 /* If this is in memory because of -ffloat-store,
3807 set the volatile bit, to prevent optimizations from
3808 undoing the effects. */
3809 if (flag_float_store && TREE_CODE (type) == REAL_TYPE)
3810 MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
3811 #endif
3813 MEM_ALIAS_SET (DECL_RTL (decl)) = get_alias_set (decl);
3815 else
3816 /* Dynamic-size object: must push space on the stack. */
3818 rtx address, size;
3820 /* Record the stack pointer on entry to block, if have
3821 not already done so. */
3822 if (thisblock->data.block.stack_level == 0)
3824 do_pending_stack_adjust ();
3825 emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3826 &thisblock->data.block.stack_level,
3827 thisblock->data.block.first_insn);
3828 stack_block_stack = thisblock;
3831 /* Compute the variable's size, in bytes. */
3832 size = expand_expr (size_binop (CEIL_DIV_EXPR,
3833 DECL_SIZE (decl),
3834 size_int (BITS_PER_UNIT)),
3835 NULL_RTX, VOIDmode, 0);
3836 free_temp_slots ();
3838 /* Allocate space on the stack for the variable. Note that
3839 DECL_ALIGN says how the variable is to be aligned and we
3840 cannot use it to conclude anything about the alignment of
3841 the size. */
3842 address = allocate_dynamic_stack_space (size, NULL_RTX,
3843 TYPE_ALIGN (TREE_TYPE (decl)));
3845 /* Reference the variable indirect through that rtx. */
3846 DECL_RTL (decl) = gen_rtx_MEM (DECL_MODE (decl), address);
3848 /* If this is a memory ref that contains aggregate components,
3849 mark it as such for cse and loop optimize. */
3850 MEM_SET_IN_STRUCT_P (DECL_RTL (decl),
3851 AGGREGATE_TYPE_P (TREE_TYPE (decl)));
3853 /* Indicate the alignment we actually gave this variable. */
3854 #ifdef STACK_BOUNDARY
3855 DECL_ALIGN (decl) = STACK_BOUNDARY;
3856 #else
3857 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
3858 #endif
3861 if (TREE_THIS_VOLATILE (decl))
3862 MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
3863 #if 0 /* A variable is not necessarily unchanging
3864 just because it is const. RTX_UNCHANGING_P
3865 means no change in the function,
3866 not merely no change in the variable's scope.
3867 It is correct to set RTX_UNCHANGING_P if the variable's scope
3868 is the whole function. There's no convenient way to test that. */
3869 if (TREE_READONLY (decl))
3870 RTX_UNCHANGING_P (DECL_RTL (decl)) = 1;
3871 #endif
3873 /* If doing stupid register allocation, make sure life of any
3874 register variable starts here, at the start of its scope. */
3876 if (obey_regdecls)
3877 use_variable (DECL_RTL (decl));
3882 /* Emit code to perform the initialization of a declaration DECL. */
3884 void
3885 expand_decl_init (decl)
3886 tree decl;
3888 int was_used = TREE_USED (decl);
3890 /* If this is a CONST_DECL, we don't have to generate any code, but
3891 if DECL_INITIAL is a constant, call expand_expr to force TREE_CST_RTL
3892 to be set while in the obstack containing the constant. If we don't
3893 do this, we can lose if we have functions nested three deep and the middle
3894 function makes a CONST_DECL whose DECL_INITIAL is a STRING_CST while
3895 the innermost function is the first to expand that STRING_CST. */
3896 if (TREE_CODE (decl) == CONST_DECL)
3898 if (DECL_INITIAL (decl) && TREE_CONSTANT (DECL_INITIAL (decl)))
3899 expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
3900 EXPAND_INITIALIZER);
3901 return;
3904 if (TREE_STATIC (decl))
3905 return;
3907 /* Compute and store the initial value now. */
3909 if (DECL_INITIAL (decl) == error_mark_node)
3911 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
3913 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
3914 || code == POINTER_TYPE || code == REFERENCE_TYPE)
3915 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
3916 0, 0);
3917 emit_queue ();
3919 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
3921 emit_line_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3922 expand_assignment (decl, DECL_INITIAL (decl), 0, 0);
3923 emit_queue ();
3926 /* Don't let the initialization count as "using" the variable. */
3927 TREE_USED (decl) = was_used;
3929 /* Free any temporaries we made while initializing the decl. */
3930 preserve_temp_slots (NULL_RTX);
3931 free_temp_slots ();
3934 /* CLEANUP is an expression to be executed at exit from this binding contour;
3935 for example, in C++, it might call the destructor for this variable.
3937 We wrap CLEANUP in an UNSAVE_EXPR node, so that we can expand the
3938 CLEANUP multiple times, and have the correct semantics. This
3939 happens in exception handling, for gotos, returns, breaks that
3940 leave the current scope.
3942 If CLEANUP is nonzero and DECL is zero, we record a cleanup
3943 that is not associated with any particular variable. */
3946 expand_decl_cleanup (decl, cleanup)
3947 tree decl, cleanup;
3949 struct nesting *thisblock;
3951 /* Error if we are not in any block. */
3952 if (current_function == 0 || block_stack == 0)
3953 return 0;
3955 thisblock = block_stack;
3957 /* Record the cleanup if there is one. */
3959 if (cleanup != 0)
3961 tree t;
3962 rtx seq;
3963 tree *cleanups = &thisblock->data.block.cleanups;
3964 int cond_context = conditional_context ();
3966 if (cond_context)
3968 rtx flag = gen_reg_rtx (word_mode);
3969 rtx set_flag_0;
3970 tree cond;
3972 start_sequence ();
3973 emit_move_insn (flag, const0_rtx);
3974 set_flag_0 = get_insns ();
3975 end_sequence ();
3977 thisblock->data.block.last_unconditional_cleanup
3978 = emit_insns_after (set_flag_0,
3979 thisblock->data.block.last_unconditional_cleanup);
3981 emit_move_insn (flag, const1_rtx);
3983 /* All cleanups must be on the function_obstack. */
3984 push_obstacks_nochange ();
3985 resume_temporary_allocation ();
3987 cond = build_decl (VAR_DECL, NULL_TREE, type_for_mode (word_mode, 1));
3988 DECL_RTL (cond) = flag;
3990 /* Conditionalize the cleanup. */
3991 cleanup = build (COND_EXPR, void_type_node,
3992 truthvalue_conversion (cond),
3993 cleanup, integer_zero_node);
3994 cleanup = fold (cleanup);
3996 pop_obstacks ();
3998 cleanups = thisblock->data.block.cleanup_ptr;
4001 /* All cleanups must be on the function_obstack. */
4002 push_obstacks_nochange ();
4003 resume_temporary_allocation ();
4004 cleanup = unsave_expr (cleanup);
4005 pop_obstacks ();
4007 t = *cleanups = temp_tree_cons (decl, cleanup, *cleanups);
4009 if (! cond_context)
4010 /* If this block has a cleanup, it belongs in stack_block_stack. */
4011 stack_block_stack = thisblock;
4013 if (cond_context)
4015 start_sequence ();
4018 /* If this was optimized so that there is no exception region for the
4019 cleanup, then mark the TREE_LIST node, so that we can later tell
4020 if we need to call expand_eh_region_end. */
4021 if (! using_eh_for_cleanups_p
4022 || expand_eh_region_start_tree (decl, cleanup))
4023 TREE_ADDRESSABLE (t) = 1;
4024 /* If that started a new EH region, we're in a new block. */
4025 thisblock = block_stack;
4027 if (cond_context)
4029 seq = get_insns ();
4030 end_sequence ();
4031 if (seq)
4032 thisblock->data.block.last_unconditional_cleanup
4033 = emit_insns_after (seq,
4034 thisblock->data.block.last_unconditional_cleanup);
4036 else
4038 thisblock->data.block.last_unconditional_cleanup
4039 = get_last_insn ();
4040 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
4043 return 1;
4046 /* Like expand_decl_cleanup, but suppress generating an exception handler
4047 to perform the cleanup. */
4049 #if 0
4051 expand_decl_cleanup_no_eh (decl, cleanup)
4052 tree decl, cleanup;
4054 int save_eh = using_eh_for_cleanups_p;
4055 int result;
4057 using_eh_for_cleanups_p = 0;
4058 result = expand_decl_cleanup (decl, cleanup);
4059 using_eh_for_cleanups_p = save_eh;
4061 return result;
4063 #endif
4065 /* Arrange for the top element of the dynamic cleanup chain to be
4066 popped if we exit the current binding contour. DECL is the
4067 associated declaration, if any, otherwise NULL_TREE. If the
4068 current contour is left via an exception, then __sjthrow will pop
4069 the top element off the dynamic cleanup chain. The code that
4070 avoids doing the action we push into the cleanup chain in the
4071 exceptional case is contained in expand_cleanups.
4073 This routine is only used by expand_eh_region_start, and that is
4074 the only way in which an exception region should be started. This
4075 routine is only used when using the setjmp/longjmp codegen method
4076 for exception handling. */
4079 expand_dcc_cleanup (decl)
4080 tree decl;
4082 struct nesting *thisblock;
4083 tree cleanup;
4085 /* Error if we are not in any block. */
4086 if (current_function == 0 || block_stack == 0)
4087 return 0;
4088 thisblock = block_stack;
4090 /* Record the cleanup for the dynamic handler chain. */
4092 /* All cleanups must be on the function_obstack. */
4093 push_obstacks_nochange ();
4094 resume_temporary_allocation ();
4095 cleanup = make_node (POPDCC_EXPR);
4096 pop_obstacks ();
4098 /* Add the cleanup in a manner similar to expand_decl_cleanup. */
4099 thisblock->data.block.cleanups
4100 = temp_tree_cons (decl, cleanup, thisblock->data.block.cleanups);
4102 /* If this block has a cleanup, it belongs in stack_block_stack. */
4103 stack_block_stack = thisblock;
4104 return 1;
4107 /* Arrange for the top element of the dynamic handler chain to be
4108 popped if we exit the current binding contour. DECL is the
4109 associated declaration, if any, otherwise NULL_TREE. If the current
4110 contour is left via an exception, then __sjthrow will pop the top
4111 element off the dynamic handler chain. The code that avoids doing
4112 the action we push into the handler chain in the exceptional case
4113 is contained in expand_cleanups.
4115 This routine is only used by expand_eh_region_start, and that is
4116 the only way in which an exception region should be started. This
4117 routine is only used when using the setjmp/longjmp codegen method
4118 for exception handling. */
4121 expand_dhc_cleanup (decl)
4122 tree decl;
4124 struct nesting *thisblock;
4125 tree cleanup;
4127 /* Error if we are not in any block. */
4128 if (current_function == 0 || block_stack == 0)
4129 return 0;
4130 thisblock = block_stack;
4132 /* Record the cleanup for the dynamic handler chain. */
4134 /* All cleanups must be on the function_obstack. */
4135 push_obstacks_nochange ();
4136 resume_temporary_allocation ();
4137 cleanup = make_node (POPDHC_EXPR);
4138 pop_obstacks ();
4140 /* Add the cleanup in a manner similar to expand_decl_cleanup. */
4141 thisblock->data.block.cleanups
4142 = temp_tree_cons (decl, cleanup, thisblock->data.block.cleanups);
4144 /* If this block has a cleanup, it belongs in stack_block_stack. */
4145 stack_block_stack = thisblock;
4146 return 1;
4149 /* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
4150 DECL_ELTS is the list of elements that belong to DECL's type.
4151 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
4153 void
4154 expand_anon_union_decl (decl, cleanup, decl_elts)
4155 tree decl, cleanup, decl_elts;
4157 struct nesting *thisblock = current_function == 0 ? 0 : block_stack;
4158 rtx x;
4159 tree t;
4161 /* If any of the elements are addressable, so is the entire union. */
4162 for (t = decl_elts; t; t = TREE_CHAIN (t))
4163 if (TREE_ADDRESSABLE (TREE_VALUE (t)))
4165 TREE_ADDRESSABLE (decl) = 1;
4166 break;
4169 expand_decl (decl);
4170 expand_decl_cleanup (decl, cleanup);
4171 x = DECL_RTL (decl);
4173 /* Go through the elements, assigning RTL to each. */
4174 for (t = decl_elts; t; t = TREE_CHAIN (t))
4176 tree decl_elt = TREE_VALUE (t);
4177 tree cleanup_elt = TREE_PURPOSE (t);
4178 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
4180 /* Propagate the union's alignment to the elements. */
4181 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
4183 /* If the element has BLKmode and the union doesn't, the union is
4184 aligned such that the element doesn't need to have BLKmode, so
4185 change the element's mode to the appropriate one for its size. */
4186 if (mode == BLKmode && DECL_MODE (decl) != BLKmode)
4187 DECL_MODE (decl_elt) = mode
4188 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl_elt)),
4189 MODE_INT, 1);
4191 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
4192 instead create a new MEM rtx with the proper mode. */
4193 if (GET_CODE (x) == MEM)
4195 if (mode == GET_MODE (x))
4196 DECL_RTL (decl_elt) = x;
4197 else
4199 DECL_RTL (decl_elt) = gen_rtx_MEM (mode, copy_rtx (XEXP (x, 0)));
4200 MEM_COPY_ATTRIBUTES (DECL_RTL (decl_elt), x);
4201 RTX_UNCHANGING_P (DECL_RTL (decl_elt)) = RTX_UNCHANGING_P (x);
4204 else if (GET_CODE (x) == REG)
4206 if (mode == GET_MODE (x))
4207 DECL_RTL (decl_elt) = x;
4208 else
4209 DECL_RTL (decl_elt) = gen_rtx_SUBREG (mode, x, 0);
4211 else
4212 abort ();
4214 /* Record the cleanup if there is one. */
4216 if (cleanup != 0)
4217 thisblock->data.block.cleanups
4218 = temp_tree_cons (decl_elt, cleanup_elt,
4219 thisblock->data.block.cleanups);
4223 /* Expand a list of cleanups LIST.
4224 Elements may be expressions or may be nested lists.
4226 If DONT_DO is nonnull, then any list-element
4227 whose TREE_PURPOSE matches DONT_DO is omitted.
4228 This is sometimes used to avoid a cleanup associated with
4229 a value that is being returned out of the scope.
4231 If IN_FIXUP is non-zero, we are generating this cleanup for a fixup
4232 goto and handle protection regions specially in that case.
4234 If REACHABLE, we emit code, otherwise just inform the exception handling
4235 code about this finalization. */
4237 static void
4238 expand_cleanups (list, dont_do, in_fixup, reachable)
4239 tree list;
4240 tree dont_do;
4241 int in_fixup;
4242 int reachable;
4244 tree tail;
4245 for (tail = list; tail; tail = TREE_CHAIN (tail))
4246 if (dont_do == 0 || TREE_PURPOSE (tail) != dont_do)
4248 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
4249 expand_cleanups (TREE_VALUE (tail), dont_do, in_fixup, reachable);
4250 else
4252 if (! in_fixup)
4254 tree cleanup = TREE_VALUE (tail);
4256 /* See expand_d{h,c}c_cleanup for why we avoid this. */
4257 if (TREE_CODE (cleanup) != POPDHC_EXPR
4258 && TREE_CODE (cleanup) != POPDCC_EXPR
4259 /* See expand_eh_region_start_tree for this case. */
4260 && ! TREE_ADDRESSABLE (tail))
4262 cleanup = protect_with_terminate (cleanup);
4263 expand_eh_region_end (cleanup);
4267 if (reachable)
4269 /* Cleanups may be run multiple times. For example,
4270 when exiting a binding contour, we expand the
4271 cleanups associated with that contour. When a goto
4272 within that binding contour has a target outside that
4273 contour, it will expand all cleanups from its scope to
4274 the target. Though the cleanups are expanded multiple
4275 times, the control paths are non-overlapping so the
4276 cleanups will not be executed twice. */
4278 /* We may need to protect fixups with rethrow regions. */
4279 int protect = (in_fixup && ! TREE_ADDRESSABLE (tail));
4281 if (protect)
4282 expand_fixup_region_start ();
4284 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4285 if (protect)
4286 expand_fixup_region_end (TREE_VALUE (tail));
4287 free_temp_slots ();
4293 /* Mark when the context we are emitting RTL for as a conditional
4294 context, so that any cleanup actions we register with
4295 expand_decl_init will be properly conditionalized when those
4296 cleanup actions are later performed. Must be called before any
4297 expression (tree) is expanded that is within a conditional context. */
4299 void
4300 start_cleanup_deferral ()
4302 /* block_stack can be NULL if we are inside the parameter list. It is
4303 OK to do nothing, because cleanups aren't possible here. */
4304 if (block_stack)
4305 ++block_stack->data.block.conditional_code;
4308 /* Mark the end of a conditional region of code. Because cleanup
4309 deferrals may be nested, we may still be in a conditional region
4310 after we end the currently deferred cleanups, only after we end all
4311 deferred cleanups, are we back in unconditional code. */
4313 void
4314 end_cleanup_deferral ()
4316 /* block_stack can be NULL if we are inside the parameter list. It is
4317 OK to do nothing, because cleanups aren't possible here. */
4318 if (block_stack)
4319 --block_stack->data.block.conditional_code;
4322 /* Move all cleanups from the current block_stack
4323 to the containing block_stack, where they are assumed to
4324 have been created. If anything can cause a temporary to
4325 be created, but not expanded for more than one level of
4326 block_stacks, then this code will have to change. */
4328 void
4329 move_cleanups_up ()
4331 struct nesting *block = block_stack;
4332 struct nesting *outer = block->next;
4334 outer->data.block.cleanups
4335 = chainon (block->data.block.cleanups,
4336 outer->data.block.cleanups);
4337 block->data.block.cleanups = 0;
4340 tree
4341 last_cleanup_this_contour ()
4343 if (block_stack == 0)
4344 return 0;
4346 return block_stack->data.block.cleanups;
4349 /* Return 1 if there are any pending cleanups at this point.
4350 If THIS_CONTOUR is nonzero, check the current contour as well.
4351 Otherwise, look only at the contours that enclose this one. */
4354 any_pending_cleanups (this_contour)
4355 int this_contour;
4357 struct nesting *block;
4359 if (current_function == NULL || current_function->stmt == NULL
4360 || block_stack == 0)
4361 return 0;
4363 if (this_contour && block_stack->data.block.cleanups != NULL)
4364 return 1;
4365 if (block_stack->data.block.cleanups == 0
4366 && block_stack->data.block.outer_cleanups == 0)
4367 return 0;
4369 for (block = block_stack->next; block; block = block->next)
4370 if (block->data.block.cleanups != 0)
4371 return 1;
4373 return 0;
4376 /* Enter a case (Pascal) or switch (C) statement.
4377 Push a block onto case_stack and nesting_stack
4378 to accumulate the case-labels that are seen
4379 and to record the labels generated for the statement.
4381 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
4382 Otherwise, this construct is transparent for `exit_something'.
4384 EXPR is the index-expression to be dispatched on.
4385 TYPE is its nominal type. We could simply convert EXPR to this type,
4386 but instead we take short cuts. */
4388 void
4389 expand_start_case (exit_flag, expr, type, printname)
4390 int exit_flag;
4391 tree expr;
4392 tree type;
4393 const char *printname;
4395 register struct nesting *thiscase = ALLOC_NESTING ();
4397 /* Make an entry on case_stack for the case we are entering. */
4399 thiscase->next = case_stack;
4400 thiscase->all = nesting_stack;
4401 thiscase->depth = ++nesting_depth;
4402 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
4403 thiscase->data.case_stmt.case_list = 0;
4404 thiscase->data.case_stmt.index_expr = expr;
4405 thiscase->data.case_stmt.nominal_type = type;
4406 thiscase->data.case_stmt.default_label = 0;
4407 thiscase->data.case_stmt.num_ranges = 0;
4408 thiscase->data.case_stmt.printname = printname;
4409 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
4410 case_stack = thiscase;
4411 nesting_stack = thiscase;
4413 do_pending_stack_adjust ();
4415 /* Make sure case_stmt.start points to something that won't
4416 need any transformation before expand_end_case. */
4417 if (GET_CODE (get_last_insn ()) != NOTE)
4418 emit_note (NULL_PTR, NOTE_INSN_DELETED);
4420 thiscase->data.case_stmt.start = get_last_insn ();
4422 start_cleanup_deferral ();
4426 /* Start a "dummy case statement" within which case labels are invalid
4427 and are not connected to any larger real case statement.
4428 This can be used if you don't want to let a case statement jump
4429 into the middle of certain kinds of constructs. */
4431 void
4432 expand_start_case_dummy ()
4434 register struct nesting *thiscase = ALLOC_NESTING ();
4436 /* Make an entry on case_stack for the dummy. */
4438 thiscase->next = case_stack;
4439 thiscase->all = nesting_stack;
4440 thiscase->depth = ++nesting_depth;
4441 thiscase->exit_label = 0;
4442 thiscase->data.case_stmt.case_list = 0;
4443 thiscase->data.case_stmt.start = 0;
4444 thiscase->data.case_stmt.nominal_type = 0;
4445 thiscase->data.case_stmt.default_label = 0;
4446 thiscase->data.case_stmt.num_ranges = 0;
4447 case_stack = thiscase;
4448 nesting_stack = thiscase;
4449 start_cleanup_deferral ();
4452 /* End a dummy case statement. */
4454 void
4455 expand_end_case_dummy ()
4457 end_cleanup_deferral ();
4458 POPSTACK (case_stack);
4461 /* Return the data type of the index-expression
4462 of the innermost case statement, or null if none. */
4464 tree
4465 case_index_expr_type ()
4467 if (case_stack)
4468 return TREE_TYPE (case_stack->data.case_stmt.index_expr);
4469 return 0;
4472 static void
4473 check_seenlabel ()
4475 /* If this is the first label, warn if any insns have been emitted. */
4476 if (case_stack->data.case_stmt.line_number_status >= 0)
4478 rtx insn;
4480 restore_line_number_status
4481 (case_stack->data.case_stmt.line_number_status);
4482 case_stack->data.case_stmt.line_number_status = -1;
4484 for (insn = case_stack->data.case_stmt.start;
4485 insn;
4486 insn = NEXT_INSN (insn))
4488 if (GET_CODE (insn) == CODE_LABEL)
4489 break;
4490 if (GET_CODE (insn) != NOTE
4491 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4494 insn = PREV_INSN (insn);
4495 while (insn && (GET_CODE (insn) != NOTE || NOTE_LINE_NUMBER (insn) < 0));
4497 /* If insn is zero, then there must have been a syntax error. */
4498 if (insn)
4499 warning_with_file_and_line (NOTE_SOURCE_FILE(insn),
4500 NOTE_LINE_NUMBER(insn),
4501 "unreachable code at beginning of %s",
4502 case_stack->data.case_stmt.printname);
4503 break;
4509 /* Accumulate one case or default label inside a case or switch statement.
4510 VALUE is the value of the case (a null pointer, for a default label).
4511 The function CONVERTER, when applied to arguments T and V,
4512 converts the value V to the type T.
4514 If not currently inside a case or switch statement, return 1 and do
4515 nothing. The caller will print a language-specific error message.
4516 If VALUE is a duplicate or overlaps, return 2 and do nothing
4517 except store the (first) duplicate node in *DUPLICATE.
4518 If VALUE is out of range, return 3 and do nothing.
4519 If we are jumping into the scope of a cleanup or var-sized array, return 5.
4520 Return 0 on success.
4522 Extended to handle range statements. */
4525 pushcase (value, converter, label, duplicate)
4526 register tree value;
4527 tree (*converter) PROTO((tree, tree));
4528 register tree label;
4529 tree *duplicate;
4531 tree index_type;
4532 tree nominal_type;
4534 /* Fail if not inside a real case statement. */
4535 if (! (case_stack && case_stack->data.case_stmt.start))
4536 return 1;
4538 if (stack_block_stack
4539 && stack_block_stack->depth > case_stack->depth)
4540 return 5;
4542 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4543 nominal_type = case_stack->data.case_stmt.nominal_type;
4545 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4546 if (index_type == error_mark_node)
4547 return 0;
4549 check_seenlabel ();
4551 /* Fail if this value is out of range for the actual type of the index
4552 (which may be narrower than NOMINAL_TYPE). */
4553 if (value != 0
4554 && (TREE_CONSTANT_OVERFLOW (value)
4555 || ! int_fits_type_p (value, index_type)))
4556 return 3;
4558 /* Convert VALUE to the type in which the comparisons are nominally done. */
4559 if (value != 0)
4560 value = (*converter) (nominal_type, value);
4562 /* Fail if this is a duplicate or overlaps another entry. */
4563 if (value == 0)
4565 if (case_stack->data.case_stmt.default_label != 0)
4567 *duplicate = case_stack->data.case_stmt.default_label;
4568 return 2;
4570 case_stack->data.case_stmt.default_label = label;
4572 else
4573 return add_case_node (value, value, label, duplicate);
4575 expand_label (label);
4576 return 0;
4579 /* Like pushcase but this case applies to all values between VALUE1 and
4580 VALUE2 (inclusive). If VALUE1 is NULL, the range starts at the lowest
4581 value of the index type and ends at VALUE2. If VALUE2 is NULL, the range
4582 starts at VALUE1 and ends at the highest value of the index type.
4583 If both are NULL, this case applies to all values.
4585 The return value is the same as that of pushcase but there is one
4586 additional error code: 4 means the specified range was empty. */
4589 pushcase_range (value1, value2, converter, label, duplicate)
4590 register tree value1, value2;
4591 tree (*converter) PROTO((tree, tree));
4592 register tree label;
4593 tree *duplicate;
4595 tree index_type;
4596 tree nominal_type;
4598 /* Fail if not inside a real case statement. */
4599 if (! (case_stack && case_stack->data.case_stmt.start))
4600 return 1;
4602 if (stack_block_stack
4603 && stack_block_stack->depth > case_stack->depth)
4604 return 5;
4606 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4607 nominal_type = case_stack->data.case_stmt.nominal_type;
4609 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4610 if (index_type == error_mark_node)
4611 return 0;
4613 check_seenlabel ();
4615 /* Convert VALUEs to type in which the comparisons are nominally done
4616 and replace any unspecified value with the corresponding bound. */
4617 if (value1 == 0)
4618 value1 = TYPE_MIN_VALUE (index_type);
4619 if (value2 == 0)
4620 value2 = TYPE_MAX_VALUE (index_type);
4622 /* Fail if the range is empty. Do this before any conversion since
4623 we want to allow out-of-range empty ranges. */
4624 if (value2 != 0 && tree_int_cst_lt (value2, value1))
4625 return 4;
4627 /* If the max was unbounded, use the max of the nominal_type we are
4628 converting to. Do this after the < check above to suppress false
4629 positives. */
4630 if (value2 == 0)
4631 value2 = TYPE_MAX_VALUE (nominal_type);
4633 /* Fail if these values are out of range. */
4634 if (TREE_CONSTANT_OVERFLOW (value1)
4635 || ! int_fits_type_p (value1, index_type))
4636 return 3;
4638 if (TREE_CONSTANT_OVERFLOW (value2)
4639 || ! int_fits_type_p (value2, index_type))
4640 return 3;
4642 value1 = (*converter) (nominal_type, value1);
4643 value2 = (*converter) (nominal_type, value2);
4645 return add_case_node (value1, value2, label, duplicate);
4648 /* Do the actual insertion of a case label for pushcase and pushcase_range
4649 into case_stack->data.case_stmt.case_list. Use an AVL tree to avoid
4650 slowdown for large switch statements. */
4652 static int
4653 add_case_node (low, high, label, duplicate)
4654 tree low, high;
4655 tree label;
4656 tree *duplicate;
4658 struct case_node *p, **q, *r;
4660 q = &case_stack->data.case_stmt.case_list;
4661 p = *q;
4663 while ((r = *q))
4665 p = r;
4667 /* Keep going past elements distinctly greater than HIGH. */
4668 if (tree_int_cst_lt (high, p->low))
4669 q = &p->left;
4671 /* or distinctly less than LOW. */
4672 else if (tree_int_cst_lt (p->high, low))
4673 q = &p->right;
4675 else
4677 /* We have an overlap; this is an error. */
4678 *duplicate = p->code_label;
4679 return 2;
4683 /* Add this label to the chain, and succeed.
4684 Copy LOW, HIGH so they are on temporary rather than momentary
4685 obstack and will thus survive till the end of the case statement. */
4687 r = (struct case_node *) oballoc (sizeof (struct case_node));
4688 r->low = copy_node (low);
4690 /* If the bounds are equal, turn this into the one-value case. */
4692 if (tree_int_cst_equal (low, high))
4693 r->high = r->low;
4694 else
4696 r->high = copy_node (high);
4697 case_stack->data.case_stmt.num_ranges++;
4700 r->code_label = label;
4701 expand_label (label);
4703 *q = r;
4704 r->parent = p;
4705 r->left = 0;
4706 r->right = 0;
4707 r->balance = 0;
4709 while (p)
4711 struct case_node *s;
4713 if (r == p->left)
4715 int b;
4717 if (! (b = p->balance))
4718 /* Growth propagation from left side. */
4719 p->balance = -1;
4720 else if (b < 0)
4722 if (r->balance < 0)
4724 /* R-Rotation */
4725 if ((p->left = s = r->right))
4726 s->parent = p;
4728 r->right = p;
4729 p->balance = 0;
4730 r->balance = 0;
4731 s = p->parent;
4732 p->parent = r;
4734 if ((r->parent = s))
4736 if (s->left == p)
4737 s->left = r;
4738 else
4739 s->right = r;
4741 else
4742 case_stack->data.case_stmt.case_list = r;
4744 else
4745 /* r->balance == +1 */
4747 /* LR-Rotation */
4749 int b2;
4750 struct case_node *t = r->right;
4752 if ((p->left = s = t->right))
4753 s->parent = p;
4755 t->right = p;
4756 if ((r->right = s = t->left))
4757 s->parent = r;
4759 t->left = r;
4760 b = t->balance;
4761 b2 = b < 0;
4762 p->balance = b2;
4763 b2 = -b2 - b;
4764 r->balance = b2;
4765 t->balance = 0;
4766 s = p->parent;
4767 p->parent = t;
4768 r->parent = t;
4770 if ((t->parent = s))
4772 if (s->left == p)
4773 s->left = t;
4774 else
4775 s->right = t;
4777 else
4778 case_stack->data.case_stmt.case_list = t;
4780 break;
4783 else
4785 /* p->balance == +1; growth of left side balances the node. */
4786 p->balance = 0;
4787 break;
4790 else
4791 /* r == p->right */
4793 int b;
4795 if (! (b = p->balance))
4796 /* Growth propagation from right side. */
4797 p->balance++;
4798 else if (b > 0)
4800 if (r->balance > 0)
4802 /* L-Rotation */
4804 if ((p->right = s = r->left))
4805 s->parent = p;
4807 r->left = p;
4808 p->balance = 0;
4809 r->balance = 0;
4810 s = p->parent;
4811 p->parent = r;
4812 if ((r->parent = s))
4814 if (s->left == p)
4815 s->left = r;
4816 else
4817 s->right = r;
4820 else
4821 case_stack->data.case_stmt.case_list = r;
4824 else
4825 /* r->balance == -1 */
4827 /* RL-Rotation */
4828 int b2;
4829 struct case_node *t = r->left;
4831 if ((p->right = s = t->left))
4832 s->parent = p;
4834 t->left = p;
4836 if ((r->left = s = t->right))
4837 s->parent = r;
4839 t->right = r;
4840 b = t->balance;
4841 b2 = b < 0;
4842 r->balance = b2;
4843 b2 = -b2 - b;
4844 p->balance = b2;
4845 t->balance = 0;
4846 s = p->parent;
4847 p->parent = t;
4848 r->parent = t;
4850 if ((t->parent = s))
4852 if (s->left == p)
4853 s->left = t;
4854 else
4855 s->right = t;
4858 else
4859 case_stack->data.case_stmt.case_list = t;
4861 break;
4863 else
4865 /* p->balance == -1; growth of right side balances the node. */
4866 p->balance = 0;
4867 break;
4871 r = p;
4872 p = p->parent;
4875 return 0;
4879 /* Returns the number of possible values of TYPE.
4880 Returns -1 if the number is unknown or variable.
4881 Returns -2 if the number does not fit in a HOST_WIDE_INT.
4882 Sets *SPARENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
4883 do not increase monotonically (there may be duplicates);
4884 to 1 if the values increase monotonically, but not always by 1;
4885 otherwise sets it to 0. */
4887 HOST_WIDE_INT
4888 all_cases_count (type, spareness)
4889 tree type;
4890 int *spareness;
4892 HOST_WIDE_INT count;
4893 *spareness = 0;
4895 switch (TREE_CODE (type))
4897 tree t;
4898 case BOOLEAN_TYPE:
4899 count = 2;
4900 break;
4901 case CHAR_TYPE:
4902 count = 1 << BITS_PER_UNIT;
4903 break;
4904 default:
4905 case INTEGER_TYPE:
4906 if (TREE_CODE (TYPE_MIN_VALUE (type)) != INTEGER_CST
4907 || TYPE_MAX_VALUE (type) == NULL
4908 || TREE_CODE (TYPE_MAX_VALUE (type)) != INTEGER_CST)
4909 return -1;
4910 else
4912 /* count
4913 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (type))
4914 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) + 1
4915 but with overflow checking. */
4916 tree mint = TYPE_MIN_VALUE (type);
4917 tree maxt = TYPE_MAX_VALUE (type);
4918 HOST_WIDE_INT lo, hi;
4919 neg_double(TREE_INT_CST_LOW (mint), TREE_INT_CST_HIGH (mint),
4920 &lo, &hi);
4921 add_double(TREE_INT_CST_LOW (maxt), TREE_INT_CST_HIGH (maxt),
4922 lo, hi, &lo, &hi);
4923 add_double (lo, hi, 1, 0, &lo, &hi);
4924 if (hi != 0 || lo < 0)
4925 return -2;
4926 count = lo;
4928 break;
4929 case ENUMERAL_TYPE:
4930 count = 0;
4931 for (t = TYPE_VALUES (type); t != NULL_TREE; t = TREE_CHAIN (t))
4933 if (TREE_CODE (TYPE_MIN_VALUE (type)) != INTEGER_CST
4934 || TREE_CODE (TREE_VALUE (t)) != INTEGER_CST
4935 || TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) + count
4936 != TREE_INT_CST_LOW (TREE_VALUE (t)))
4937 *spareness = 1;
4938 count++;
4940 if (*spareness == 1)
4942 tree prev = TREE_VALUE (TYPE_VALUES (type));
4943 for (t = TYPE_VALUES (type); t = TREE_CHAIN (t), t != NULL_TREE; )
4945 if (! tree_int_cst_lt (prev, TREE_VALUE (t)))
4947 *spareness = 2;
4948 break;
4950 prev = TREE_VALUE (t);
4955 return count;
4959 #define BITARRAY_TEST(ARRAY, INDEX) \
4960 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4961 & (1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR)))
4962 #define BITARRAY_SET(ARRAY, INDEX) \
4963 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4964 |= 1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR))
4966 /* Set the elements of the bitstring CASES_SEEN (which has length COUNT),
4967 with the case values we have seen, assuming the case expression
4968 has the given TYPE.
4969 SPARSENESS is as determined by all_cases_count.
4971 The time needed is proportional to COUNT, unless
4972 SPARSENESS is 2, in which case quadratic time is needed. */
4974 void
4975 mark_seen_cases (type, cases_seen, count, sparseness)
4976 tree type;
4977 unsigned char *cases_seen;
4978 long count;
4979 int sparseness;
4981 tree next_node_to_try = NULL_TREE;
4982 long next_node_offset = 0;
4984 register struct case_node *n, *root = case_stack->data.case_stmt.case_list;
4985 tree val = make_node (INTEGER_CST);
4986 TREE_TYPE (val) = type;
4987 if (! root)
4988 ; /* Do nothing */
4989 else if (sparseness == 2)
4991 tree t;
4992 HOST_WIDE_INT xlo;
4994 /* This less efficient loop is only needed to handle
4995 duplicate case values (multiple enum constants
4996 with the same value). */
4997 TREE_TYPE (val) = TREE_TYPE (root->low);
4998 for (t = TYPE_VALUES (type), xlo = 0; t != NULL_TREE;
4999 t = TREE_CHAIN (t), xlo++)
5001 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (TREE_VALUE (t));
5002 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (TREE_VALUE (t));
5003 n = root;
5006 /* Keep going past elements distinctly greater than VAL. */
5007 if (tree_int_cst_lt (val, n->low))
5008 n = n->left;
5010 /* or distinctly less than VAL. */
5011 else if (tree_int_cst_lt (n->high, val))
5012 n = n->right;
5014 else
5016 /* We have found a matching range. */
5017 BITARRAY_SET (cases_seen, xlo);
5018 break;
5021 while (n);
5024 else
5026 if (root->left)
5027 case_stack->data.case_stmt.case_list = root = case_tree2list (root, 0);
5028 for (n = root; n; n = n->right)
5030 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (n->low);
5031 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (n->low);
5032 while ( ! tree_int_cst_lt (n->high, val))
5034 /* Calculate (into xlo) the "offset" of the integer (val).
5035 The element with lowest value has offset 0, the next smallest
5036 element has offset 1, etc. */
5038 HOST_WIDE_INT xlo, xhi;
5039 tree t;
5040 if (sparseness && TYPE_VALUES (type) != NULL_TREE)
5042 /* The TYPE_VALUES will be in increasing order, so
5043 starting searching where we last ended. */
5044 t = next_node_to_try;
5045 xlo = next_node_offset;
5046 xhi = 0;
5047 for (;;)
5049 if (t == NULL_TREE)
5051 t = TYPE_VALUES (type);
5052 xlo = 0;
5054 if (tree_int_cst_equal (val, TREE_VALUE (t)))
5056 next_node_to_try = TREE_CHAIN (t);
5057 next_node_offset = xlo + 1;
5058 break;
5060 xlo++;
5061 t = TREE_CHAIN (t);
5062 if (t == next_node_to_try)
5064 xlo = -1;
5065 break;
5069 else
5071 t = TYPE_MIN_VALUE (type);
5072 if (t)
5073 neg_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t),
5074 &xlo, &xhi);
5075 else
5076 xlo = xhi = 0;
5077 add_double (xlo, xhi,
5078 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
5079 &xlo, &xhi);
5082 if (xhi == 0 && xlo >= 0 && xlo < count)
5083 BITARRAY_SET (cases_seen, xlo);
5084 add_double (TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
5085 1, 0,
5086 &TREE_INT_CST_LOW (val), &TREE_INT_CST_HIGH (val));
5092 /* Called when the index of a switch statement is an enumerated type
5093 and there is no default label.
5095 Checks that all enumeration literals are covered by the case
5096 expressions of a switch. Also, warn if there are any extra
5097 switch cases that are *not* elements of the enumerated type.
5099 If all enumeration literals were covered by the case expressions,
5100 turn one of the expressions into the default expression since it should
5101 not be possible to fall through such a switch. */
5103 void
5104 check_for_full_enumeration_handling (type)
5105 tree type;
5107 register struct case_node *n;
5108 register tree chain;
5109 #if 0 /* variable used by 'if 0'ed code below. */
5110 register struct case_node **l;
5111 int all_values = 1;
5112 #endif
5114 /* True iff the selector type is a numbered set mode. */
5115 int sparseness = 0;
5117 /* The number of possible selector values. */
5118 HOST_WIDE_INT size;
5120 /* For each possible selector value. a one iff it has been matched
5121 by a case value alternative. */
5122 unsigned char *cases_seen;
5124 /* The allocated size of cases_seen, in chars. */
5125 long bytes_needed;
5127 if (! warn_switch)
5128 return;
5130 size = all_cases_count (type, &sparseness);
5131 bytes_needed = (size + HOST_BITS_PER_CHAR) / HOST_BITS_PER_CHAR;
5133 if (size > 0 && size < 600000
5134 /* We deliberately use calloc here, not cmalloc, so that we can suppress
5135 this optimization if we don't have enough memory rather than
5136 aborting, as xmalloc would do. */
5137 && (cases_seen = (unsigned char *) calloc (bytes_needed, 1)) != NULL)
5139 long i;
5140 tree v = TYPE_VALUES (type);
5142 /* The time complexity of this code is normally O(N), where
5143 N being the number of members in the enumerated type.
5144 However, if type is a ENUMERAL_TYPE whose values do not
5145 increase monotonically, O(N*log(N)) time may be needed. */
5147 mark_seen_cases (type, cases_seen, size, sparseness);
5149 for (i = 0; v != NULL_TREE && i < size; i++, v = TREE_CHAIN (v))
5151 if (BITARRAY_TEST(cases_seen, i) == 0)
5152 warning ("enumeration value `%s' not handled in switch",
5153 IDENTIFIER_POINTER (TREE_PURPOSE (v)));
5156 free (cases_seen);
5159 /* Now we go the other way around; we warn if there are case
5160 expressions that don't correspond to enumerators. This can
5161 occur since C and C++ don't enforce type-checking of
5162 assignments to enumeration variables. */
5164 if (case_stack->data.case_stmt.case_list
5165 && case_stack->data.case_stmt.case_list->left)
5166 case_stack->data.case_stmt.case_list
5167 = case_tree2list (case_stack->data.case_stmt.case_list, 0);
5168 if (warn_switch)
5169 for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
5171 for (chain = TYPE_VALUES (type);
5172 chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
5173 chain = TREE_CHAIN (chain))
5176 if (!chain)
5178 if (TYPE_NAME (type) == 0)
5179 warning ("case value `%ld' not in enumerated type",
5180 (long) TREE_INT_CST_LOW (n->low));
5181 else
5182 warning ("case value `%ld' not in enumerated type `%s'",
5183 (long) TREE_INT_CST_LOW (n->low),
5184 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5185 == IDENTIFIER_NODE)
5186 ? TYPE_NAME (type)
5187 : DECL_NAME (TYPE_NAME (type))));
5189 if (!tree_int_cst_equal (n->low, n->high))
5191 for (chain = TYPE_VALUES (type);
5192 chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
5193 chain = TREE_CHAIN (chain))
5196 if (!chain)
5198 if (TYPE_NAME (type) == 0)
5199 warning ("case value `%ld' not in enumerated type",
5200 (long) TREE_INT_CST_LOW (n->high));
5201 else
5202 warning ("case value `%ld' not in enumerated type `%s'",
5203 (long) TREE_INT_CST_LOW (n->high),
5204 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5205 == IDENTIFIER_NODE)
5206 ? TYPE_NAME (type)
5207 : DECL_NAME (TYPE_NAME (type))));
5212 #if 0
5213 /* ??? This optimization is disabled because it causes valid programs to
5214 fail. ANSI C does not guarantee that an expression with enum type
5215 will have a value that is the same as one of the enumeration literals. */
5217 /* If all values were found as case labels, make one of them the default
5218 label. Thus, this switch will never fall through. We arbitrarily pick
5219 the last one to make the default since this is likely the most
5220 efficient choice. */
5222 if (all_values)
5224 for (l = &case_stack->data.case_stmt.case_list;
5225 (*l)->right != 0;
5226 l = &(*l)->right)
5229 case_stack->data.case_stmt.default_label = (*l)->code_label;
5230 *l = 0;
5232 #endif /* 0 */
5236 /* Terminate a case (Pascal) or switch (C) statement
5237 in which ORIG_INDEX is the expression to be tested.
5238 Generate the code to test it and jump to the right place. */
5240 void
5241 expand_end_case (orig_index)
5242 tree orig_index;
5244 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE, orig_minval;
5245 rtx default_label = 0;
5246 register struct case_node *n;
5247 unsigned int count;
5248 rtx index;
5249 rtx table_label;
5250 int ncases;
5251 rtx *labelvec;
5252 register int i;
5253 rtx before_case;
5254 register struct nesting *thiscase = case_stack;
5255 tree index_expr, index_type;
5256 int unsignedp;
5258 /* Don't crash due to previous errors. */
5259 if (thiscase == NULL)
5260 return;
5262 table_label = gen_label_rtx ();
5263 index_expr = thiscase->data.case_stmt.index_expr;
5264 index_type = TREE_TYPE (index_expr);
5265 unsignedp = TREE_UNSIGNED (index_type);
5267 do_pending_stack_adjust ();
5269 /* This might get an spurious warning in the presence of a syntax error;
5270 it could be fixed by moving the call to check_seenlabel after the
5271 check for error_mark_node, and copying the code of check_seenlabel that
5272 deals with case_stack->data.case_stmt.line_number_status /
5273 restore_line_number_status in front of the call to end_cleanup_deferral;
5274 However, this might miss some useful warnings in the presence of
5275 non-syntax errors. */
5276 check_seenlabel ();
5278 /* An ERROR_MARK occurs for various reasons including invalid data type. */
5279 if (index_type != error_mark_node)
5281 /* If switch expression was an enumerated type, check that all
5282 enumeration literals are covered by the cases.
5283 No sense trying this if there's a default case, however. */
5285 if (!thiscase->data.case_stmt.default_label
5286 && TREE_CODE (TREE_TYPE (orig_index)) == ENUMERAL_TYPE
5287 && TREE_CODE (index_expr) != INTEGER_CST)
5288 check_for_full_enumeration_handling (TREE_TYPE (orig_index));
5290 /* If we don't have a default-label, create one here,
5291 after the body of the switch. */
5292 if (thiscase->data.case_stmt.default_label == 0)
5294 thiscase->data.case_stmt.default_label
5295 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5296 expand_label (thiscase->data.case_stmt.default_label);
5298 default_label = label_rtx (thiscase->data.case_stmt.default_label);
5300 before_case = get_last_insn ();
5302 if (thiscase->data.case_stmt.case_list
5303 && thiscase->data.case_stmt.case_list->left)
5304 thiscase->data.case_stmt.case_list
5305 = case_tree2list(thiscase->data.case_stmt.case_list, 0);
5307 /* Simplify the case-list before we count it. */
5308 group_case_nodes (thiscase->data.case_stmt.case_list);
5310 /* Get upper and lower bounds of case values.
5311 Also convert all the case values to the index expr's data type. */
5313 count = 0;
5314 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5316 /* Check low and high label values are integers. */
5317 if (TREE_CODE (n->low) != INTEGER_CST)
5318 abort ();
5319 if (TREE_CODE (n->high) != INTEGER_CST)
5320 abort ();
5322 n->low = convert (index_type, n->low);
5323 n->high = convert (index_type, n->high);
5325 /* Count the elements and track the largest and smallest
5326 of them (treating them as signed even if they are not). */
5327 if (count++ == 0)
5329 minval = n->low;
5330 maxval = n->high;
5332 else
5334 if (INT_CST_LT (n->low, minval))
5335 minval = n->low;
5336 if (INT_CST_LT (maxval, n->high))
5337 maxval = n->high;
5339 /* A range counts double, since it requires two compares. */
5340 if (! tree_int_cst_equal (n->low, n->high))
5341 count++;
5344 orig_minval = minval;
5346 /* Compute span of values. */
5347 if (count != 0)
5348 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
5350 end_cleanup_deferral ();
5352 if (count == 0)
5354 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
5355 emit_queue ();
5356 emit_jump (default_label);
5359 /* If range of values is much bigger than number of values,
5360 make a sequence of conditional branches instead of a dispatch.
5361 If the switch-index is a constant, do it this way
5362 because we can optimize it. */
5364 #ifndef CASE_VALUES_THRESHOLD
5365 #ifdef HAVE_casesi
5366 #define CASE_VALUES_THRESHOLD (HAVE_casesi ? 4 : 5)
5367 #else
5368 /* If machine does not have a case insn that compares the
5369 bounds, this means extra overhead for dispatch tables
5370 which raises the threshold for using them. */
5371 #define CASE_VALUES_THRESHOLD 5
5372 #endif /* HAVE_casesi */
5373 #endif /* CASE_VALUES_THRESHOLD */
5375 else if (TREE_INT_CST_HIGH (range) != 0
5376 || count < (unsigned int) CASE_VALUES_THRESHOLD
5377 || ((unsigned HOST_WIDE_INT) (TREE_INT_CST_LOW (range))
5378 > 10 * count)
5379 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
5380 || flag_pic
5381 #endif
5382 || TREE_CODE (index_expr) == INTEGER_CST
5383 /* These will reduce to a constant. */
5384 || (TREE_CODE (index_expr) == CALL_EXPR
5385 && TREE_CODE (TREE_OPERAND (index_expr, 0)) == ADDR_EXPR
5386 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == FUNCTION_DECL
5387 && DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == BUILT_IN_NORMAL
5388 && DECL_FUNCTION_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == BUILT_IN_CLASSIFY_TYPE)
5389 || (TREE_CODE (index_expr) == COMPOUND_EXPR
5390 && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
5392 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5394 /* If the index is a short or char that we do not have
5395 an insn to handle comparisons directly, convert it to
5396 a full integer now, rather than letting each comparison
5397 generate the conversion. */
5399 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
5400 && (cmp_optab->handlers[(int) GET_MODE(index)].insn_code
5401 == CODE_FOR_nothing))
5403 enum machine_mode wider_mode;
5404 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
5405 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
5406 if (cmp_optab->handlers[(int) wider_mode].insn_code
5407 != CODE_FOR_nothing)
5409 index = convert_to_mode (wider_mode, index, unsignedp);
5410 break;
5414 emit_queue ();
5415 do_pending_stack_adjust ();
5417 index = protect_from_queue (index, 0);
5418 if (GET_CODE (index) == MEM)
5419 index = copy_to_reg (index);
5420 if (GET_CODE (index) == CONST_INT
5421 || TREE_CODE (index_expr) == INTEGER_CST)
5423 /* Make a tree node with the proper constant value
5424 if we don't already have one. */
5425 if (TREE_CODE (index_expr) != INTEGER_CST)
5427 index_expr
5428 = build_int_2 (INTVAL (index),
5429 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
5430 index_expr = convert (index_type, index_expr);
5433 /* For constant index expressions we need only
5434 issue a unconditional branch to the appropriate
5435 target code. The job of removing any unreachable
5436 code is left to the optimisation phase if the
5437 "-O" option is specified. */
5438 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5439 if (! tree_int_cst_lt (index_expr, n->low)
5440 && ! tree_int_cst_lt (n->high, index_expr))
5441 break;
5443 if (n)
5444 emit_jump (label_rtx (n->code_label));
5445 else
5446 emit_jump (default_label);
5448 else
5450 /* If the index expression is not constant we generate
5451 a binary decision tree to select the appropriate
5452 target code. This is done as follows:
5454 The list of cases is rearranged into a binary tree,
5455 nearly optimal assuming equal probability for each case.
5457 The tree is transformed into RTL, eliminating
5458 redundant test conditions at the same time.
5460 If program flow could reach the end of the
5461 decision tree an unconditional jump to the
5462 default code is emitted. */
5464 use_cost_table
5465 = (TREE_CODE (TREE_TYPE (orig_index)) != ENUMERAL_TYPE
5466 && estimate_case_costs (thiscase->data.case_stmt.case_list));
5467 balance_case_nodes (&thiscase->data.case_stmt.case_list,
5468 NULL_PTR);
5469 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
5470 default_label, index_type);
5471 emit_jump_if_reachable (default_label);
5474 else
5476 int win = 0;
5477 #ifdef HAVE_casesi
5478 if (HAVE_casesi)
5480 enum machine_mode index_mode = SImode;
5481 int index_bits = GET_MODE_BITSIZE (index_mode);
5482 rtx op1, op2;
5483 enum machine_mode op_mode;
5485 /* Convert the index to SImode. */
5486 if (GET_MODE_BITSIZE (TYPE_MODE (index_type))
5487 > GET_MODE_BITSIZE (index_mode))
5489 enum machine_mode omode = TYPE_MODE (index_type);
5490 rtx rangertx = expand_expr (range, NULL_RTX, VOIDmode, 0);
5492 /* We must handle the endpoints in the original mode. */
5493 index_expr = build (MINUS_EXPR, index_type,
5494 index_expr, minval);
5495 minval = integer_zero_node;
5496 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5497 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
5498 omode, 1, 0, default_label);
5499 /* Now we can safely truncate. */
5500 index = convert_to_mode (index_mode, index, 0);
5502 else
5504 if (TYPE_MODE (index_type) != index_mode)
5506 index_expr = convert (type_for_size (index_bits, 0),
5507 index_expr);
5508 index_type = TREE_TYPE (index_expr);
5511 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5513 emit_queue ();
5514 index = protect_from_queue (index, 0);
5515 do_pending_stack_adjust ();
5517 op_mode = insn_data[(int)CODE_FOR_casesi].operand[0].mode;
5518 if (! (*insn_data[(int)CODE_FOR_casesi].operand[0].predicate)
5519 (index, op_mode))
5520 index = copy_to_mode_reg (op_mode, index);
5522 op1 = expand_expr (minval, NULL_RTX, VOIDmode, 0);
5524 op_mode = insn_data[(int)CODE_FOR_casesi].operand[1].mode;
5525 if (! (*insn_data[(int)CODE_FOR_casesi].operand[1].predicate)
5526 (op1, op_mode))
5527 op1 = copy_to_mode_reg (op_mode, op1);
5529 op2 = expand_expr (range, NULL_RTX, VOIDmode, 0);
5531 op_mode = insn_data[(int)CODE_FOR_casesi].operand[2].mode;
5532 if (! (*insn_data[(int)CODE_FOR_casesi].operand[2].predicate)
5533 (op2, op_mode))
5534 op2 = copy_to_mode_reg (op_mode, op2);
5536 emit_jump_insn (gen_casesi (index, op1, op2,
5537 table_label, default_label));
5538 win = 1;
5540 #endif
5541 #ifdef HAVE_tablejump
5542 if (! win && HAVE_tablejump)
5544 index_expr = convert (thiscase->data.case_stmt.nominal_type,
5545 fold (build (MINUS_EXPR, index_type,
5546 index_expr, minval)));
5547 index_type = TREE_TYPE (index_expr);
5548 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5549 emit_queue ();
5550 index = protect_from_queue (index, 0);
5551 do_pending_stack_adjust ();
5553 do_tablejump (index, TYPE_MODE (index_type),
5554 expand_expr (range, NULL_RTX, VOIDmode, 0),
5555 table_label, default_label);
5556 win = 1;
5558 #endif
5559 if (! win)
5560 abort ();
5562 /* Get table of labels to jump to, in order of case index. */
5564 ncases = TREE_INT_CST_LOW (range) + 1;
5565 labelvec = (rtx *) alloca (ncases * sizeof (rtx));
5566 bzero ((char *) labelvec, ncases * sizeof (rtx));
5568 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5570 register HOST_WIDE_INT i
5571 = TREE_INT_CST_LOW (n->low) - TREE_INT_CST_LOW (orig_minval);
5573 while (1)
5575 labelvec[i]
5576 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
5577 if (i + TREE_INT_CST_LOW (orig_minval)
5578 == TREE_INT_CST_LOW (n->high))
5579 break;
5580 i++;
5584 /* Fill in the gaps with the default. */
5585 for (i = 0; i < ncases; i++)
5586 if (labelvec[i] == 0)
5587 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
5589 /* Output the table */
5590 emit_label (table_label);
5592 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
5593 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
5594 gen_rtx_LABEL_REF (Pmode, table_label),
5595 gen_rtvec_v (ncases, labelvec),
5596 const0_rtx, const0_rtx));
5597 else
5598 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
5599 gen_rtvec_v (ncases, labelvec)));
5601 /* If the case insn drops through the table,
5602 after the table we must jump to the default-label.
5603 Otherwise record no drop-through after the table. */
5604 #ifdef CASE_DROPS_THROUGH
5605 emit_jump (default_label);
5606 #else
5607 emit_barrier ();
5608 #endif
5611 before_case = squeeze_notes (NEXT_INSN (before_case), get_last_insn ());
5612 reorder_insns (before_case, get_last_insn (),
5613 thiscase->data.case_stmt.start);
5615 else
5616 end_cleanup_deferral ();
5618 if (thiscase->exit_label)
5619 emit_label (thiscase->exit_label);
5621 POPSTACK (case_stack);
5623 free_temp_slots ();
5626 /* Convert the tree NODE into a list linked by the right field, with the left
5627 field zeroed. RIGHT is used for recursion; it is a list to be placed
5628 rightmost in the resulting list. */
5630 static struct case_node *
5631 case_tree2list (node, right)
5632 struct case_node *node, *right;
5634 struct case_node *left;
5636 if (node->right)
5637 right = case_tree2list (node->right, right);
5639 node->right = right;
5640 if ((left = node->left))
5642 node->left = 0;
5643 return case_tree2list (left, node);
5646 return node;
5649 /* Generate code to jump to LABEL if OP1 and OP2 are equal. */
5651 static void
5652 do_jump_if_equal (op1, op2, label, unsignedp)
5653 rtx op1, op2, label;
5654 int unsignedp;
5656 if (GET_CODE (op1) == CONST_INT
5657 && GET_CODE (op2) == CONST_INT)
5659 if (INTVAL (op1) == INTVAL (op2))
5660 emit_jump (label);
5662 else
5664 enum machine_mode mode = GET_MODE (op1);
5665 if (mode == VOIDmode)
5666 mode = GET_MODE (op2);
5667 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX, mode, unsignedp,
5668 0, label);
5672 /* Not all case values are encountered equally. This function
5673 uses a heuristic to weight case labels, in cases where that
5674 looks like a reasonable thing to do.
5676 Right now, all we try to guess is text, and we establish the
5677 following weights:
5679 chars above space: 16
5680 digits: 16
5681 default: 12
5682 space, punct: 8
5683 tab: 4
5684 newline: 2
5685 other "\" chars: 1
5686 remaining chars: 0
5688 If we find any cases in the switch that are not either -1 or in the range
5689 of valid ASCII characters, or are control characters other than those
5690 commonly used with "\", don't treat this switch scanning text.
5692 Return 1 if these nodes are suitable for cost estimation, otherwise
5693 return 0. */
5695 static int
5696 estimate_case_costs (node)
5697 case_node_ptr node;
5699 tree min_ascii = build_int_2 (-1, -1);
5700 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5701 case_node_ptr n;
5702 int i;
5704 /* If we haven't already made the cost table, make it now. Note that the
5705 lower bound of the table is -1, not zero. */
5707 if (cost_table == NULL)
5709 cost_table = ((short *) xcalloc (129, sizeof (short))) + 1;
5711 for (i = 0; i < 128; i++)
5713 if (ISALNUM (i))
5714 cost_table[i] = 16;
5715 else if (ISPUNCT (i))
5716 cost_table[i] = 8;
5717 else if (ISCNTRL (i))
5718 cost_table[i] = -1;
5721 cost_table[' '] = 8;
5722 cost_table['\t'] = 4;
5723 cost_table['\0'] = 4;
5724 cost_table['\n'] = 2;
5725 cost_table['\f'] = 1;
5726 cost_table['\v'] = 1;
5727 cost_table['\b'] = 1;
5730 /* See if all the case expressions look like text. It is text if the
5731 constant is >= -1 and the highest constant is <= 127. Do all comparisons
5732 as signed arithmetic since we don't want to ever access cost_table with a
5733 value less than -1. Also check that none of the constants in a range
5734 are strange control characters. */
5736 for (n = node; n; n = n->right)
5738 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5739 return 0;
5741 for (i = TREE_INT_CST_LOW (n->low); i <= TREE_INT_CST_LOW (n->high); i++)
5742 if (cost_table[i] < 0)
5743 return 0;
5746 /* All interesting values are within the range of interesting
5747 ASCII characters. */
5748 return 1;
5751 /* Scan an ordered list of case nodes
5752 combining those with consecutive values or ranges.
5754 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
5756 static void
5757 group_case_nodes (head)
5758 case_node_ptr head;
5760 case_node_ptr node = head;
5762 while (node)
5764 rtx lb = next_real_insn (label_rtx (node->code_label));
5765 rtx lb2;
5766 case_node_ptr np = node;
5768 /* Try to group the successors of NODE with NODE. */
5769 while (((np = np->right) != 0)
5770 /* Do they jump to the same place? */
5771 && ((lb2 = next_real_insn (label_rtx (np->code_label))) == lb
5772 || (lb != 0 && lb2 != 0
5773 && simplejump_p (lb)
5774 && simplejump_p (lb2)
5775 && rtx_equal_p (SET_SRC (PATTERN (lb)),
5776 SET_SRC (PATTERN (lb2)))))
5777 /* Are their ranges consecutive? */
5778 && tree_int_cst_equal (np->low,
5779 fold (build (PLUS_EXPR,
5780 TREE_TYPE (node->high),
5781 node->high,
5782 integer_one_node)))
5783 /* An overflow is not consecutive. */
5784 && tree_int_cst_lt (node->high,
5785 fold (build (PLUS_EXPR,
5786 TREE_TYPE (node->high),
5787 node->high,
5788 integer_one_node))))
5790 node->high = np->high;
5792 /* NP is the first node after NODE which can't be grouped with it.
5793 Delete the nodes in between, and move on to that node. */
5794 node->right = np;
5795 node = np;
5799 /* Take an ordered list of case nodes
5800 and transform them into a near optimal binary tree,
5801 on the assumption that any target code selection value is as
5802 likely as any other.
5804 The transformation is performed by splitting the ordered
5805 list into two equal sections plus a pivot. The parts are
5806 then attached to the pivot as left and right branches. Each
5807 branch is then transformed recursively. */
5809 static void
5810 balance_case_nodes (head, parent)
5811 case_node_ptr *head;
5812 case_node_ptr parent;
5814 register case_node_ptr np;
5816 np = *head;
5817 if (np)
5819 int cost = 0;
5820 int i = 0;
5821 int ranges = 0;
5822 register case_node_ptr *npp;
5823 case_node_ptr left;
5825 /* Count the number of entries on branch. Also count the ranges. */
5827 while (np)
5829 if (!tree_int_cst_equal (np->low, np->high))
5831 ranges++;
5832 if (use_cost_table)
5833 cost += cost_table[TREE_INT_CST_LOW (np->high)];
5836 if (use_cost_table)
5837 cost += cost_table[TREE_INT_CST_LOW (np->low)];
5839 i++;
5840 np = np->right;
5843 if (i > 2)
5845 /* Split this list if it is long enough for that to help. */
5846 npp = head;
5847 left = *npp;
5848 if (use_cost_table)
5850 /* Find the place in the list that bisects the list's total cost,
5851 Here I gets half the total cost. */
5852 int n_moved = 0;
5853 i = (cost + 1) / 2;
5854 while (1)
5856 /* Skip nodes while their cost does not reach that amount. */
5857 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5858 i -= cost_table[TREE_INT_CST_LOW ((*npp)->high)];
5859 i -= cost_table[TREE_INT_CST_LOW ((*npp)->low)];
5860 if (i <= 0)
5861 break;
5862 npp = &(*npp)->right;
5863 n_moved += 1;
5865 if (n_moved == 0)
5867 /* Leave this branch lopsided, but optimize left-hand
5868 side and fill in `parent' fields for right-hand side. */
5869 np = *head;
5870 np->parent = parent;
5871 balance_case_nodes (&np->left, np);
5872 for (; np->right; np = np->right)
5873 np->right->parent = np;
5874 return;
5877 /* If there are just three nodes, split at the middle one. */
5878 else if (i == 3)
5879 npp = &(*npp)->right;
5880 else
5882 /* Find the place in the list that bisects the list's total cost,
5883 where ranges count as 2.
5884 Here I gets half the total cost. */
5885 i = (i + ranges + 1) / 2;
5886 while (1)
5888 /* Skip nodes while their cost does not reach that amount. */
5889 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5890 i--;
5891 i--;
5892 if (i <= 0)
5893 break;
5894 npp = &(*npp)->right;
5897 *head = np = *npp;
5898 *npp = 0;
5899 np->parent = parent;
5900 np->left = left;
5902 /* Optimize each of the two split parts. */
5903 balance_case_nodes (&np->left, np);
5904 balance_case_nodes (&np->right, np);
5906 else
5908 /* Else leave this branch as one level,
5909 but fill in `parent' fields. */
5910 np = *head;
5911 np->parent = parent;
5912 for (; np->right; np = np->right)
5913 np->right->parent = np;
5918 /* Search the parent sections of the case node tree
5919 to see if a test for the lower bound of NODE would be redundant.
5920 INDEX_TYPE is the type of the index expression.
5922 The instructions to generate the case decision tree are
5923 output in the same order as nodes are processed so it is
5924 known that if a parent node checks the range of the current
5925 node minus one that the current node is bounded at its lower
5926 span. Thus the test would be redundant. */
5928 static int
5929 node_has_low_bound (node, index_type)
5930 case_node_ptr node;
5931 tree index_type;
5933 tree low_minus_one;
5934 case_node_ptr pnode;
5936 /* If the lower bound of this node is the lowest value in the index type,
5937 we need not test it. */
5939 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
5940 return 1;
5942 /* If this node has a left branch, the value at the left must be less
5943 than that at this node, so it cannot be bounded at the bottom and
5944 we need not bother testing any further. */
5946 if (node->left)
5947 return 0;
5949 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
5950 node->low, integer_one_node));
5952 /* If the subtraction above overflowed, we can't verify anything.
5953 Otherwise, look for a parent that tests our value - 1. */
5955 if (! tree_int_cst_lt (low_minus_one, node->low))
5956 return 0;
5958 for (pnode = node->parent; pnode; pnode = pnode->parent)
5959 if (tree_int_cst_equal (low_minus_one, pnode->high))
5960 return 1;
5962 return 0;
5965 /* Search the parent sections of the case node tree
5966 to see if a test for the upper bound of NODE would be redundant.
5967 INDEX_TYPE is the type of the index expression.
5969 The instructions to generate the case decision tree are
5970 output in the same order as nodes are processed so it is
5971 known that if a parent node checks the range of the current
5972 node plus one that the current node is bounded at its upper
5973 span. Thus the test would be redundant. */
5975 static int
5976 node_has_high_bound (node, index_type)
5977 case_node_ptr node;
5978 tree index_type;
5980 tree high_plus_one;
5981 case_node_ptr pnode;
5983 /* If there is no upper bound, obviously no test is needed. */
5985 if (TYPE_MAX_VALUE (index_type) == NULL)
5986 return 1;
5988 /* If the upper bound of this node is the highest value in the type
5989 of the index expression, we need not test against it. */
5991 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
5992 return 1;
5994 /* If this node has a right branch, the value at the right must be greater
5995 than that at this node, so it cannot be bounded at the top and
5996 we need not bother testing any further. */
5998 if (node->right)
5999 return 0;
6001 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
6002 node->high, integer_one_node));
6004 /* If the addition above overflowed, we can't verify anything.
6005 Otherwise, look for a parent that tests our value + 1. */
6007 if (! tree_int_cst_lt (node->high, high_plus_one))
6008 return 0;
6010 for (pnode = node->parent; pnode; pnode = pnode->parent)
6011 if (tree_int_cst_equal (high_plus_one, pnode->low))
6012 return 1;
6014 return 0;
6017 /* Search the parent sections of the
6018 case node tree to see if both tests for the upper and lower
6019 bounds of NODE would be redundant. */
6021 static int
6022 node_is_bounded (node, index_type)
6023 case_node_ptr node;
6024 tree index_type;
6026 return (node_has_low_bound (node, index_type)
6027 && node_has_high_bound (node, index_type));
6030 /* Emit an unconditional jump to LABEL unless it would be dead code. */
6032 static void
6033 emit_jump_if_reachable (label)
6034 rtx label;
6036 if (GET_CODE (get_last_insn ()) != BARRIER)
6037 emit_jump (label);
6040 /* Emit step-by-step code to select a case for the value of INDEX.
6041 The thus generated decision tree follows the form of the
6042 case-node binary tree NODE, whose nodes represent test conditions.
6043 INDEX_TYPE is the type of the index of the switch.
6045 Care is taken to prune redundant tests from the decision tree
6046 by detecting any boundary conditions already checked by
6047 emitted rtx. (See node_has_high_bound, node_has_low_bound
6048 and node_is_bounded, above.)
6050 Where the test conditions can be shown to be redundant we emit
6051 an unconditional jump to the target code. As a further
6052 optimization, the subordinates of a tree node are examined to
6053 check for bounded nodes. In this case conditional and/or
6054 unconditional jumps as a result of the boundary check for the
6055 current node are arranged to target the subordinates associated
6056 code for out of bound conditions on the current node.
6058 We can assume that when control reaches the code generated here,
6059 the index value has already been compared with the parents
6060 of this node, and determined to be on the same side of each parent
6061 as this node is. Thus, if this node tests for the value 51,
6062 and a parent tested for 52, we don't need to consider
6063 the possibility of a value greater than 51. If another parent
6064 tests for the value 50, then this node need not test anything. */
6066 static void
6067 emit_case_nodes (index, node, default_label, index_type)
6068 rtx index;
6069 case_node_ptr node;
6070 rtx default_label;
6071 tree index_type;
6073 /* If INDEX has an unsigned type, we must make unsigned branches. */
6074 int unsignedp = TREE_UNSIGNED (index_type);
6075 enum machine_mode mode = GET_MODE (index);
6077 /* See if our parents have already tested everything for us.
6078 If they have, emit an unconditional jump for this node. */
6079 if (node_is_bounded (node, index_type))
6080 emit_jump (label_rtx (node->code_label));
6082 else if (tree_int_cst_equal (node->low, node->high))
6084 /* Node is single valued. First see if the index expression matches
6085 this node and then check our children, if any. */
6087 do_jump_if_equal (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
6088 label_rtx (node->code_label), unsignedp);
6090 if (node->right != 0 && node->left != 0)
6092 /* This node has children on both sides.
6093 Dispatch to one side or the other
6094 by comparing the index value with this node's value.
6095 If one subtree is bounded, check that one first,
6096 so we can avoid real branches in the tree. */
6098 if (node_is_bounded (node->right, index_type))
6100 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6101 VOIDmode, 0),
6102 GT, NULL_RTX, mode, unsignedp, 0,
6103 label_rtx (node->right->code_label));
6104 emit_case_nodes (index, node->left, default_label, index_type);
6107 else if (node_is_bounded (node->left, index_type))
6109 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6110 VOIDmode, 0),
6111 LT, NULL_RTX, mode, unsignedp, 0,
6112 label_rtx (node->left->code_label));
6113 emit_case_nodes (index, node->right, default_label, index_type);
6116 else
6118 /* Neither node is bounded. First distinguish the two sides;
6119 then emit the code for one side at a time. */
6121 tree test_label
6122 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6124 /* See if the value is on the right. */
6125 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6126 VOIDmode, 0),
6127 GT, NULL_RTX, mode, unsignedp, 0,
6128 label_rtx (test_label));
6130 /* Value must be on the left.
6131 Handle the left-hand subtree. */
6132 emit_case_nodes (index, node->left, default_label, index_type);
6133 /* If left-hand subtree does nothing,
6134 go to default. */
6135 emit_jump_if_reachable (default_label);
6137 /* Code branches here for the right-hand subtree. */
6138 expand_label (test_label);
6139 emit_case_nodes (index, node->right, default_label, index_type);
6143 else if (node->right != 0 && node->left == 0)
6145 /* Here we have a right child but no left so we issue conditional
6146 branch to default and process the right child.
6148 Omit the conditional branch to default if we it avoid only one
6149 right child; it costs too much space to save so little time. */
6151 if (node->right->right || node->right->left
6152 || !tree_int_cst_equal (node->right->low, node->right->high))
6154 if (!node_has_low_bound (node, index_type))
6156 emit_cmp_and_jump_insns (index, expand_expr (node->high,
6157 NULL_RTX,
6158 VOIDmode, 0),
6159 LT, NULL_RTX, mode, unsignedp, 0,
6160 default_label);
6163 emit_case_nodes (index, node->right, default_label, index_type);
6165 else
6166 /* We cannot process node->right normally
6167 since we haven't ruled out the numbers less than
6168 this node's value. So handle node->right explicitly. */
6169 do_jump_if_equal (index,
6170 expand_expr (node->right->low, NULL_RTX,
6171 VOIDmode, 0),
6172 label_rtx (node->right->code_label), unsignedp);
6175 else if (node->right == 0 && node->left != 0)
6177 /* Just one subtree, on the left. */
6179 #if 0 /* The following code and comment were formerly part
6180 of the condition here, but they didn't work
6181 and I don't understand what the idea was. -- rms. */
6182 /* If our "most probable entry" is less probable
6183 than the default label, emit a jump to
6184 the default label using condition codes
6185 already lying around. With no right branch,
6186 a branch-greater-than will get us to the default
6187 label correctly. */
6188 if (use_cost_table
6189 && cost_table[TREE_INT_CST_LOW (node->high)] < 12)
6191 #endif /* 0 */
6192 if (node->left->left || node->left->right
6193 || !tree_int_cst_equal (node->left->low, node->left->high))
6195 if (!node_has_high_bound (node, index_type))
6197 emit_cmp_and_jump_insns (index, expand_expr (node->high,
6198 NULL_RTX,
6199 VOIDmode, 0),
6200 GT, NULL_RTX, mode, unsignedp, 0,
6201 default_label);
6204 emit_case_nodes (index, node->left, default_label, index_type);
6206 else
6207 /* We cannot process node->left normally
6208 since we haven't ruled out the numbers less than
6209 this node's value. So handle node->left explicitly. */
6210 do_jump_if_equal (index,
6211 expand_expr (node->left->low, NULL_RTX,
6212 VOIDmode, 0),
6213 label_rtx (node->left->code_label), unsignedp);
6216 else
6218 /* Node is a range. These cases are very similar to those for a single
6219 value, except that we do not start by testing whether this node
6220 is the one to branch to. */
6222 if (node->right != 0 && node->left != 0)
6224 /* Node has subtrees on both sides.
6225 If the right-hand subtree is bounded,
6226 test for it first, since we can go straight there.
6227 Otherwise, we need to make a branch in the control structure,
6228 then handle the two subtrees. */
6229 tree test_label = 0;
6232 if (node_is_bounded (node->right, index_type))
6233 /* Right hand node is fully bounded so we can eliminate any
6234 testing and branch directly to the target code. */
6235 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6236 VOIDmode, 0),
6237 GT, NULL_RTX, mode, unsignedp, 0,
6238 label_rtx (node->right->code_label));
6239 else
6241 /* Right hand node requires testing.
6242 Branch to a label where we will handle it later. */
6244 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6245 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6246 VOIDmode, 0),
6247 GT, NULL_RTX, mode, unsignedp, 0,
6248 label_rtx (test_label));
6251 /* Value belongs to this node or to the left-hand subtree. */
6253 emit_cmp_and_jump_insns (index, expand_expr (node->low, NULL_RTX,
6254 VOIDmode, 0),
6255 GE, NULL_RTX, mode, unsignedp, 0,
6256 label_rtx (node->code_label));
6258 /* Handle the left-hand subtree. */
6259 emit_case_nodes (index, node->left, default_label, index_type);
6261 /* If right node had to be handled later, do that now. */
6263 if (test_label)
6265 /* If the left-hand subtree fell through,
6266 don't let it fall into the right-hand subtree. */
6267 emit_jump_if_reachable (default_label);
6269 expand_label (test_label);
6270 emit_case_nodes (index, node->right, default_label, index_type);
6274 else if (node->right != 0 && node->left == 0)
6276 /* Deal with values to the left of this node,
6277 if they are possible. */
6278 if (!node_has_low_bound (node, index_type))
6280 emit_cmp_and_jump_insns (index, expand_expr (node->low, NULL_RTX,
6281 VOIDmode, 0),
6282 LT, NULL_RTX, mode, unsignedp, 0,
6283 default_label);
6286 /* Value belongs to this node or to the right-hand subtree. */
6288 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6289 VOIDmode, 0),
6290 LE, NULL_RTX, mode, unsignedp, 0,
6291 label_rtx (node->code_label));
6293 emit_case_nodes (index, node->right, default_label, index_type);
6296 else if (node->right == 0 && node->left != 0)
6298 /* Deal with values to the right of this node,
6299 if they are possible. */
6300 if (!node_has_high_bound (node, index_type))
6302 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6303 VOIDmode, 0),
6304 GT, NULL_RTX, mode, unsignedp, 0,
6305 default_label);
6308 /* Value belongs to this node or to the left-hand subtree. */
6310 emit_cmp_and_jump_insns (index, expand_expr (node->low, NULL_RTX,
6311 VOIDmode, 0),
6312 GE, NULL_RTX, mode, unsignedp, 0,
6313 label_rtx (node->code_label));
6315 emit_case_nodes (index, node->left, default_label, index_type);
6318 else
6320 /* Node has no children so we check low and high bounds to remove
6321 redundant tests. Only one of the bounds can exist,
6322 since otherwise this node is bounded--a case tested already. */
6324 if (!node_has_high_bound (node, index_type))
6326 emit_cmp_and_jump_insns (index, expand_expr (node->high, NULL_RTX,
6327 VOIDmode, 0),
6328 GT, NULL_RTX, mode, unsignedp, 0,
6329 default_label);
6332 if (!node_has_low_bound (node, index_type))
6334 emit_cmp_and_jump_insns (index, expand_expr (node->low, NULL_RTX,
6335 VOIDmode, 0),
6336 LT, NULL_RTX, mode, unsignedp, 0,
6337 default_label);
6340 emit_jump (label_rtx (node->code_label));
6345 /* These routines are used by the loop unrolling code. They copy BLOCK trees
6346 so that the debugging info will be correct for the unrolled loop. */
6348 void
6349 find_loop_tree_blocks ()
6351 identify_blocks (DECL_INITIAL (current_function_decl), get_insns ());
6354 void
6355 unroll_block_trees ()
6357 tree block = DECL_INITIAL (current_function_decl);
6359 reorder_blocks (block, get_insns ());