* verify.c (verify_jvm_instructions): Fix typo.
[official-gcc.git] / gcc / stmt.c
blob123c9d3c2b78cfbc71a2aa6b176c0db2a0b58898
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file handles the generation of rtl code from tree structure
23 above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
24 It also creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 The functions whose names start with `expand_' are called by the
28 parser to generate RTL instructions for various kinds of constructs.
30 Some control and binding constructs require calling several such
31 functions at different times. For example, a simple if-then
32 is expanded by calling `expand_start_cond' (with the condition-expression
33 as argument) before parsing the then-clause and calling `expand_end_cond'
34 after parsing the then-clause. */
36 #include "config.h"
37 #include "system.h"
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-config.h"
46 #include "expr.h"
47 #include "libfuncs.h"
48 #include "hard-reg-set.h"
49 #include "obstack.h"
50 #include "loop.h"
51 #include "recog.h"
52 #include "machmode.h"
53 #include "toplev.h"
54 #include "output.h"
55 #include "ggc.h"
57 #define obstack_chunk_alloc xmalloc
58 #define obstack_chunk_free free
59 struct obstack stmt_obstack;
61 /* Assume that case vectors are not pc-relative. */
62 #ifndef CASE_VECTOR_PC_RELATIVE
63 #define CASE_VECTOR_PC_RELATIVE 0
64 #endif
66 /* Functions and data structures for expanding case statements. */
68 /* Case label structure, used to hold info on labels within case
69 statements. We handle "range" labels; for a single-value label
70 as in C, the high and low limits are the same.
72 An AVL tree of case nodes is initially created, and later transformed
73 to a list linked via the RIGHT fields in the nodes. Nodes with
74 higher case values are later in the list.
76 Switch statements can be output in one of two forms. A branch table
77 is used if there are more than a few labels and the labels are dense
78 within the range between the smallest and largest case value. If a
79 branch table is used, no further manipulations are done with the case
80 node chain.
82 The alternative to the use of a branch table is to generate a series
83 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
84 and PARENT fields to hold a binary tree. Initially the tree is
85 totally unbalanced, with everything on the right. We balance the tree
86 with nodes on the left having lower case values than the parent
87 and nodes on the right having higher values. We then output the tree
88 in order. */
90 struct case_node
92 struct case_node *left; /* Left son in binary tree */
93 struct case_node *right; /* Right son in binary tree; also node chain */
94 struct case_node *parent; /* Parent of node in binary tree */
95 tree low; /* Lowest index value for this label */
96 tree high; /* Highest index value for this label */
97 tree code_label; /* Label to jump to when node matches */
98 int balance;
101 typedef struct case_node case_node;
102 typedef struct case_node *case_node_ptr;
104 /* These are used by estimate_case_costs and balance_case_nodes. */
106 /* This must be a signed type, and non-ANSI compilers lack signed char. */
107 static short cost_table_[129];
108 static int use_cost_table;
109 static int cost_table_initialized;
111 /* Special care is needed because we allow -1, but TREE_INT_CST_LOW
112 is unsigned. */
113 #define COST_TABLE(I) cost_table_[(unsigned HOST_WIDE_INT)((I) + 1)]
115 /* Stack of control and binding constructs we are currently inside.
117 These constructs begin when you call `expand_start_WHATEVER'
118 and end when you call `expand_end_WHATEVER'. This stack records
119 info about how the construct began that tells the end-function
120 what to do. It also may provide information about the construct
121 to alter the behavior of other constructs within the body.
122 For example, they may affect the behavior of C `break' and `continue'.
124 Each construct gets one `struct nesting' object.
125 All of these objects are chained through the `all' field.
126 `nesting_stack' points to the first object (innermost construct).
127 The position of an entry on `nesting_stack' is in its `depth' field.
129 Each type of construct has its own individual stack.
130 For example, loops have `loop_stack'. Each object points to the
131 next object of the same type through the `next' field.
133 Some constructs are visible to `break' exit-statements and others
134 are not. Which constructs are visible depends on the language.
135 Therefore, the data structure allows each construct to be visible
136 or not, according to the args given when the construct is started.
137 The construct is visible if the `exit_label' field is non-null.
138 In that case, the value should be a CODE_LABEL rtx. */
140 struct nesting
142 struct nesting *all;
143 struct nesting *next;
144 int depth;
145 rtx exit_label;
146 union
148 /* For conds (if-then and if-then-else statements). */
149 struct
151 /* Label for the end of the if construct.
152 There is none if EXITFLAG was not set
153 and no `else' has been seen yet. */
154 rtx endif_label;
155 /* Label for the end of this alternative.
156 This may be the end of the if or the next else/elseif. */
157 rtx next_label;
158 } cond;
159 /* For loops. */
160 struct
162 /* Label at the top of the loop; place to loop back to. */
163 rtx start_label;
164 /* Label at the end of the whole construct. */
165 rtx end_label;
166 /* Label before a jump that branches to the end of the whole
167 construct. This is where destructors go if any. */
168 rtx alt_end_label;
169 /* Label for `continue' statement to jump to;
170 this is in front of the stepper of the loop. */
171 rtx continue_label;
172 } loop;
173 /* For variable binding contours. */
174 struct
176 /* Sequence number of this binding contour within the function,
177 in order of entry. */
178 int block_start_count;
179 /* Nonzero => value to restore stack to on exit. */
180 rtx stack_level;
181 /* The NOTE that starts this contour.
182 Used by expand_goto to check whether the destination
183 is within each contour or not. */
184 rtx first_insn;
185 /* Innermost containing binding contour that has a stack level. */
186 struct nesting *innermost_stack_block;
187 /* List of cleanups to be run on exit from this contour.
188 This is a list of expressions to be evaluated.
189 The TREE_PURPOSE of each link is the ..._DECL node
190 which the cleanup pertains to. */
191 tree cleanups;
192 /* List of cleanup-lists of blocks containing this block,
193 as they were at the locus where this block appears.
194 There is an element for each containing block,
195 ordered innermost containing block first.
196 The tail of this list can be 0,
197 if all remaining elements would be empty lists.
198 The element's TREE_VALUE is the cleanup-list of that block,
199 which may be null. */
200 tree outer_cleanups;
201 /* Chain of labels defined inside this binding contour.
202 For contours that have stack levels or cleanups. */
203 struct label_chain *label_chain;
204 /* Number of function calls seen, as of start of this block. */
205 int n_function_calls;
206 /* Nonzero if this is associated with a EH region. */
207 int exception_region;
208 /* The saved target_temp_slot_level from our outer block.
209 We may reset target_temp_slot_level to be the level of
210 this block, if that is done, target_temp_slot_level
211 reverts to the saved target_temp_slot_level at the very
212 end of the block. */
213 int block_target_temp_slot_level;
214 /* True if we are currently emitting insns in an area of
215 output code that is controlled by a conditional
216 expression. This is used by the cleanup handling code to
217 generate conditional cleanup actions. */
218 int conditional_code;
219 /* A place to move the start of the exception region for any
220 of the conditional cleanups, must be at the end or after
221 the start of the last unconditional cleanup, and before any
222 conditional branch points. */
223 rtx last_unconditional_cleanup;
224 /* When in a conditional context, this is the specific
225 cleanup list associated with last_unconditional_cleanup,
226 where we place the conditionalized cleanups. */
227 tree *cleanup_ptr;
228 } block;
229 /* For switch (C) or case (Pascal) statements,
230 and also for dummies (see `expand_start_case_dummy'). */
231 struct
233 /* The insn after which the case dispatch should finally
234 be emitted. Zero for a dummy. */
235 rtx start;
236 /* A list of case labels; it is first built as an AVL tree.
237 During expand_end_case, this is converted to a list, and may be
238 rearranged into a nearly balanced binary tree. */
239 struct case_node *case_list;
240 /* Label to jump to if no case matches. */
241 tree default_label;
242 /* The expression to be dispatched on. */
243 tree index_expr;
244 /* Type that INDEX_EXPR should be converted to. */
245 tree nominal_type;
246 /* Name of this kind of statement, for warnings. */
247 const char *printname;
248 /* Used to save no_line_numbers till we see the first case label.
249 We set this to -1 when we see the first case label in this
250 case statement. */
251 int line_number_status;
252 } case_stmt;
253 } data;
256 /* Allocate and return a new `struct nesting'. */
258 #define ALLOC_NESTING() \
259 (struct nesting *) obstack_alloc (&stmt_obstack, sizeof (struct nesting))
261 /* Pop the nesting stack element by element until we pop off
262 the element which is at the top of STACK.
263 Update all the other stacks, popping off elements from them
264 as we pop them from nesting_stack. */
266 #define POPSTACK(STACK) \
267 do { struct nesting *target = STACK; \
268 struct nesting *this; \
269 do { this = nesting_stack; \
270 if (loop_stack == this) \
271 loop_stack = loop_stack->next; \
272 if (cond_stack == this) \
273 cond_stack = cond_stack->next; \
274 if (block_stack == this) \
275 block_stack = block_stack->next; \
276 if (stack_block_stack == this) \
277 stack_block_stack = stack_block_stack->next; \
278 if (case_stack == this) \
279 case_stack = case_stack->next; \
280 nesting_depth = nesting_stack->depth - 1; \
281 nesting_stack = this->all; \
282 obstack_free (&stmt_obstack, this); } \
283 while (this != target); } while (0)
285 /* In some cases it is impossible to generate code for a forward goto
286 until the label definition is seen. This happens when it may be necessary
287 for the goto to reset the stack pointer: we don't yet know how to do that.
288 So expand_goto puts an entry on this fixup list.
289 Each time a binding contour that resets the stack is exited,
290 we check each fixup.
291 If the target label has now been defined, we can insert the proper code. */
293 struct goto_fixup
295 /* Points to following fixup. */
296 struct goto_fixup *next;
297 /* Points to the insn before the jump insn.
298 If more code must be inserted, it goes after this insn. */
299 rtx before_jump;
300 /* The LABEL_DECL that this jump is jumping to, or 0
301 for break, continue or return. */
302 tree target;
303 /* The BLOCK for the place where this goto was found. */
304 tree context;
305 /* The CODE_LABEL rtx that this is jumping to. */
306 rtx target_rtl;
307 /* Number of binding contours started in current function
308 before the label reference. */
309 int block_start_count;
310 /* The outermost stack level that should be restored for this jump.
311 Each time a binding contour that resets the stack is exited,
312 if the target label is *not* yet defined, this slot is updated. */
313 rtx stack_level;
314 /* List of lists of cleanup expressions to be run by this goto.
315 There is one element for each block that this goto is within.
316 The tail of this list can be 0,
317 if all remaining elements would be empty.
318 The TREE_VALUE contains the cleanup list of that block as of the
319 time this goto was seen.
320 The TREE_ADDRESSABLE flag is 1 for a block that has been exited. */
321 tree cleanup_list_list;
324 /* Within any binding contour that must restore a stack level,
325 all labels are recorded with a chain of these structures. */
327 struct label_chain
329 /* Points to following fixup. */
330 struct label_chain *next;
331 tree label;
334 struct stmt_status
336 /* Chain of all pending binding contours. */
337 struct nesting *x_block_stack;
339 /* If any new stacks are added here, add them to POPSTACKS too. */
341 /* Chain of all pending binding contours that restore stack levels
342 or have cleanups. */
343 struct nesting *x_stack_block_stack;
345 /* Chain of all pending conditional statements. */
346 struct nesting *x_cond_stack;
348 /* Chain of all pending loops. */
349 struct nesting *x_loop_stack;
351 /* Chain of all pending case or switch statements. */
352 struct nesting *x_case_stack;
354 /* Separate chain including all of the above,
355 chained through the `all' field. */
356 struct nesting *x_nesting_stack;
358 /* Number of entries on nesting_stack now. */
359 int x_nesting_depth;
361 /* Number of binding contours started so far in this function. */
362 int x_block_start_count;
364 /* Each time we expand an expression-statement,
365 record the expr's type and its RTL value here. */
366 tree x_last_expr_type;
367 rtx x_last_expr_value;
369 /* Nonzero if within a ({...}) grouping, in which case we must
370 always compute a value for each expr-stmt in case it is the last one. */
371 int x_expr_stmts_for_value;
373 /* Filename and line number of last line-number note,
374 whether we actually emitted it or not. */
375 const char *x_emit_filename;
376 int x_emit_lineno;
378 struct goto_fixup *x_goto_fixup_chain;
381 #define block_stack (cfun->stmt->x_block_stack)
382 #define stack_block_stack (cfun->stmt->x_stack_block_stack)
383 #define cond_stack (cfun->stmt->x_cond_stack)
384 #define loop_stack (cfun->stmt->x_loop_stack)
385 #define case_stack (cfun->stmt->x_case_stack)
386 #define nesting_stack (cfun->stmt->x_nesting_stack)
387 #define nesting_depth (cfun->stmt->x_nesting_depth)
388 #define current_block_start_count (cfun->stmt->x_block_start_count)
389 #define last_expr_type (cfun->stmt->x_last_expr_type)
390 #define last_expr_value (cfun->stmt->x_last_expr_value)
391 #define expr_stmts_for_value (cfun->stmt->x_expr_stmts_for_value)
392 #define emit_filename (cfun->stmt->x_emit_filename)
393 #define emit_lineno (cfun->stmt->x_emit_lineno)
394 #define goto_fixup_chain (cfun->stmt->x_goto_fixup_chain)
396 /* Non-zero if we are using EH to handle cleanus. */
397 static int using_eh_for_cleanups_p = 0;
399 static int n_occurrences PARAMS ((int, const char *));
400 static void expand_goto_internal PARAMS ((tree, rtx, rtx));
401 static int expand_fixup PARAMS ((tree, rtx, rtx));
402 static rtx expand_nl_handler_label PARAMS ((rtx, rtx));
403 static void expand_nl_goto_receiver PARAMS ((void));
404 static void expand_nl_goto_receivers PARAMS ((struct nesting *));
405 static void fixup_gotos PARAMS ((struct nesting *, rtx, tree,
406 rtx, int));
407 static void expand_null_return_1 PARAMS ((rtx));
408 static void expand_value_return PARAMS ((rtx));
409 static int tail_recursion_args PARAMS ((tree, tree));
410 static void expand_cleanups PARAMS ((tree, tree, int, int));
411 static void check_seenlabel PARAMS ((void));
412 static void do_jump_if_equal PARAMS ((rtx, rtx, rtx, int));
413 static int estimate_case_costs PARAMS ((case_node_ptr));
414 static void group_case_nodes PARAMS ((case_node_ptr));
415 static void balance_case_nodes PARAMS ((case_node_ptr *,
416 case_node_ptr));
417 static int node_has_low_bound PARAMS ((case_node_ptr, tree));
418 static int node_has_high_bound PARAMS ((case_node_ptr, tree));
419 static int node_is_bounded PARAMS ((case_node_ptr, tree));
420 static void emit_jump_if_reachable PARAMS ((rtx));
421 static void emit_case_nodes PARAMS ((rtx, case_node_ptr, rtx, tree));
422 static struct case_node *case_tree2list PARAMS ((case_node *, case_node *));
423 static void mark_cond_nesting PARAMS ((struct nesting *));
424 static void mark_loop_nesting PARAMS ((struct nesting *));
425 static void mark_block_nesting PARAMS ((struct nesting *));
426 static void mark_case_nesting PARAMS ((struct nesting *));
427 static void mark_case_node PARAMS ((struct case_node *));
428 static void mark_goto_fixup PARAMS ((struct goto_fixup *));
429 static void free_case_nodes PARAMS ((case_node_ptr));
431 void
432 using_eh_for_cleanups ()
434 using_eh_for_cleanups_p = 1;
437 /* Mark N (known to be a cond-nesting) for GC. */
439 static void
440 mark_cond_nesting (n)
441 struct nesting *n;
443 while (n)
445 ggc_mark_rtx (n->exit_label);
446 ggc_mark_rtx (n->data.cond.endif_label);
447 ggc_mark_rtx (n->data.cond.next_label);
449 n = n->next;
453 /* Mark N (known to be a loop-nesting) for GC. */
455 static void
456 mark_loop_nesting (n)
457 struct nesting *n;
460 while (n)
462 ggc_mark_rtx (n->exit_label);
463 ggc_mark_rtx (n->data.loop.start_label);
464 ggc_mark_rtx (n->data.loop.end_label);
465 ggc_mark_rtx (n->data.loop.alt_end_label);
466 ggc_mark_rtx (n->data.loop.continue_label);
468 n = n->next;
472 /* Mark N (known to be a block-nesting) for GC. */
474 static void
475 mark_block_nesting (n)
476 struct nesting *n;
478 while (n)
480 struct label_chain *l;
482 ggc_mark_rtx (n->exit_label);
483 ggc_mark_rtx (n->data.block.stack_level);
484 ggc_mark_rtx (n->data.block.first_insn);
485 ggc_mark_tree (n->data.block.cleanups);
486 ggc_mark_tree (n->data.block.outer_cleanups);
488 for (l = n->data.block.label_chain; l != NULL; l = l->next)
490 ggc_mark (l);
491 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 ggc_mark_rtx (n->exit_label);
511 ggc_mark_rtx (n->data.case_stmt.start);
513 ggc_mark_tree (n->data.case_stmt.default_label);
514 ggc_mark_tree (n->data.case_stmt.index_expr);
515 ggc_mark_tree (n->data.case_stmt.nominal_type);
517 mark_case_node (n->data.case_stmt.case_list);
518 n = n->next;
522 /* Mark C for GC. */
524 static void
525 mark_case_node (c)
526 struct case_node *c;
528 if (c != 0)
530 ggc_mark_tree (c->low);
531 ggc_mark_tree (c->high);
532 ggc_mark_tree (c->code_label);
534 mark_case_node (c->right);
535 mark_case_node (c->left);
539 /* Mark G for GC. */
541 static void
542 mark_goto_fixup (g)
543 struct goto_fixup *g;
545 while (g)
547 ggc_mark (g);
548 ggc_mark_rtx (g->before_jump);
549 ggc_mark_tree (g->target);
550 ggc_mark_tree (g->context);
551 ggc_mark_rtx (g->target_rtl);
552 ggc_mark_rtx (g->stack_level);
553 ggc_mark_tree (g->cleanup_list_list);
555 g = g->next;
559 /* Clear out all parts of the state in F that can safely be discarded
560 after the function has been compiled, to let garbage collection
561 reclaim the memory. */
563 void
564 free_stmt_status (f)
565 struct function *f;
567 /* We're about to free the function obstack. If we hold pointers to
568 things allocated there, then we'll try to mark them when we do
569 GC. So, we clear them out here explicitly. */
570 if (f->stmt)
571 free (f->stmt);
572 f->stmt = NULL;
575 /* Mark P for GC. */
577 void
578 mark_stmt_status (p)
579 struct stmt_status *p;
581 if (p == 0)
582 return;
584 mark_block_nesting (p->x_block_stack);
585 mark_cond_nesting (p->x_cond_stack);
586 mark_loop_nesting (p->x_loop_stack);
587 mark_case_nesting (p->x_case_stack);
589 ggc_mark_tree (p->x_last_expr_type);
590 /* last_epxr_value is only valid if last_expr_type is nonzero. */
591 if (p->x_last_expr_type)
592 ggc_mark_rtx (p->x_last_expr_value);
594 mark_goto_fixup (p->x_goto_fixup_chain);
597 void
598 init_stmt ()
600 gcc_obstack_init (&stmt_obstack);
603 void
604 init_stmt_for_function ()
606 cfun->stmt = (struct stmt_status *) xmalloc (sizeof (struct stmt_status));
608 /* We are not currently within any block, conditional, loop or case. */
609 block_stack = 0;
610 stack_block_stack = 0;
611 loop_stack = 0;
612 case_stack = 0;
613 cond_stack = 0;
614 nesting_stack = 0;
615 nesting_depth = 0;
617 current_block_start_count = 0;
619 /* No gotos have been expanded yet. */
620 goto_fixup_chain = 0;
622 /* We are not processing a ({...}) grouping. */
623 expr_stmts_for_value = 0;
624 last_expr_type = 0;
625 last_expr_value = NULL_RTX;
628 /* Return nonzero if anything is pushed on the loop, condition, or case
629 stack. */
631 in_control_zone_p ()
633 return cond_stack || loop_stack || case_stack;
636 /* Record the current file and line. Called from emit_line_note. */
637 void
638 set_file_and_line_for_stmt (file, line)
639 const char *file;
640 int line;
642 /* If we're outputting an inline function, and we add a line note,
643 there may be no CFUN->STMT information. So, there's no need to
644 update it. */
645 if (cfun->stmt)
647 emit_filename = file;
648 emit_lineno = line;
652 /* Emit a no-op instruction. */
654 void
655 emit_nop ()
657 rtx last_insn;
659 last_insn = get_last_insn ();
660 if (!optimize
661 && (GET_CODE (last_insn) == CODE_LABEL
662 || (GET_CODE (last_insn) == NOTE
663 && prev_real_insn (last_insn) == 0)))
664 emit_insn (gen_nop ());
667 /* Return the rtx-label that corresponds to a LABEL_DECL,
668 creating it if necessary. */
671 label_rtx (label)
672 tree label;
674 if (TREE_CODE (label) != LABEL_DECL)
675 abort ();
677 if (!DECL_RTL_SET_P (label))
678 SET_DECL_RTL (label, gen_label_rtx ());
680 return DECL_RTL (label);
684 /* Add an unconditional jump to LABEL as the next sequential instruction. */
686 void
687 emit_jump (label)
688 rtx label;
690 do_pending_stack_adjust ();
691 emit_jump_insn (gen_jump (label));
692 emit_barrier ();
695 /* Emit code to jump to the address
696 specified by the pointer expression EXP. */
698 void
699 expand_computed_goto (exp)
700 tree exp;
702 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
704 #ifdef POINTERS_EXTEND_UNSIGNED
705 x = convert_memory_address (Pmode, x);
706 #endif
708 emit_queue ();
709 /* Be sure the function is executable. */
710 if (current_function_check_memory_usage)
711 emit_library_call (chkr_check_exec_libfunc, LCT_CONST_MAKE_BLOCK,
712 VOIDmode, 1, x, ptr_mode);
714 do_pending_stack_adjust ();
715 emit_indirect_jump (x);
717 current_function_has_computed_jump = 1;
720 /* Handle goto statements and the labels that they can go to. */
722 /* Specify the location in the RTL code of a label LABEL,
723 which is a LABEL_DECL tree node.
725 This is used for the kind of label that the user can jump to with a
726 goto statement, and for alternatives of a switch or case statement.
727 RTL labels generated for loops and conditionals don't go through here;
728 they are generated directly at the RTL level, by other functions below.
730 Note that this has nothing to do with defining label *names*.
731 Languages vary in how they do that and what that even means. */
733 void
734 expand_label (label)
735 tree label;
737 struct label_chain *p;
739 do_pending_stack_adjust ();
740 emit_label (label_rtx (label));
741 if (DECL_NAME (label))
742 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
744 if (stack_block_stack != 0)
746 p = (struct label_chain *) ggc_alloc (sizeof (struct label_chain));
747 p->next = stack_block_stack->data.block.label_chain;
748 stack_block_stack->data.block.label_chain = p;
749 p->label = label;
753 /* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
754 from nested functions. */
756 void
757 declare_nonlocal_label (label)
758 tree label;
760 rtx slot = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
762 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
763 LABEL_PRESERVE_P (label_rtx (label)) = 1;
764 if (nonlocal_goto_handler_slots == 0)
766 emit_stack_save (SAVE_NONLOCAL,
767 &nonlocal_goto_stack_level,
768 PREV_INSN (tail_recursion_reentry));
770 nonlocal_goto_handler_slots
771 = gen_rtx_EXPR_LIST (VOIDmode, slot, nonlocal_goto_handler_slots);
774 /* Generate RTL code for a `goto' statement with target label LABEL.
775 LABEL should be a LABEL_DECL tree node that was or will later be
776 defined with `expand_label'. */
778 void
779 expand_goto (label)
780 tree label;
782 tree context;
784 /* Check for a nonlocal goto to a containing function. */
785 context = decl_function_context (label);
786 if (context != 0 && context != current_function_decl)
788 struct function *p = find_function_data (context);
789 rtx label_ref = gen_rtx_LABEL_REF (Pmode, label_rtx (label));
790 rtx handler_slot, static_chain, save_area, insn;
791 tree link;
793 /* Find the corresponding handler slot for this label. */
794 handler_slot = p->x_nonlocal_goto_handler_slots;
795 for (link = p->x_nonlocal_labels; TREE_VALUE (link) != label;
796 link = TREE_CHAIN (link))
797 handler_slot = XEXP (handler_slot, 1);
798 handler_slot = XEXP (handler_slot, 0);
800 p->has_nonlocal_label = 1;
801 current_function_has_nonlocal_goto = 1;
802 LABEL_REF_NONLOCAL_P (label_ref) = 1;
804 /* Copy the rtl for the slots so that they won't be shared in
805 case the virtual stack vars register gets instantiated differently
806 in the parent than in the child. */
808 static_chain = copy_to_reg (lookup_static_chain (label));
810 /* Get addr of containing function's current nonlocal goto handler,
811 which will do any cleanups and then jump to the label. */
812 handler_slot = copy_to_reg (replace_rtx (copy_rtx (handler_slot),
813 virtual_stack_vars_rtx,
814 static_chain));
816 /* Get addr of containing function's nonlocal save area. */
817 save_area = p->x_nonlocal_goto_stack_level;
818 if (save_area)
819 save_area = replace_rtx (copy_rtx (save_area),
820 virtual_stack_vars_rtx, static_chain);
822 #if HAVE_nonlocal_goto
823 if (HAVE_nonlocal_goto)
824 emit_insn (gen_nonlocal_goto (static_chain, handler_slot,
825 save_area, label_ref));
826 else
827 #endif
829 /* Restore frame pointer for containing function.
830 This sets the actual hard register used for the frame pointer
831 to the location of the function's incoming static chain info.
832 The non-local goto handler will then adjust it to contain the
833 proper value and reload the argument pointer, if needed. */
834 emit_move_insn (hard_frame_pointer_rtx, static_chain);
835 emit_stack_restore (SAVE_NONLOCAL, save_area, NULL_RTX);
837 /* USE of hard_frame_pointer_rtx added for consistency;
838 not clear if really needed. */
839 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
840 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
841 emit_indirect_jump (handler_slot);
844 /* Search backwards to the jump insn and mark it as a
845 non-local goto. */
846 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
848 if (GET_CODE (insn) == JUMP_INSN)
850 REG_NOTES (insn) = alloc_EXPR_LIST (REG_NON_LOCAL_GOTO,
851 const0_rtx, REG_NOTES (insn));
852 break;
854 else if (GET_CODE (insn) == CALL_INSN)
855 break;
858 else
859 expand_goto_internal (label, label_rtx (label), NULL_RTX);
862 /* Generate RTL code for a `goto' statement with target label BODY.
863 LABEL should be a LABEL_REF.
864 LAST_INSN, if non-0, is the rtx we should consider as the last
865 insn emitted (for the purposes of cleaning up a return). */
867 static void
868 expand_goto_internal (body, label, last_insn)
869 tree body;
870 rtx label;
871 rtx last_insn;
873 struct nesting *block;
874 rtx stack_level = 0;
876 if (GET_CODE (label) != CODE_LABEL)
877 abort ();
879 /* If label has already been defined, we can tell now
880 whether and how we must alter the stack level. */
882 if (PREV_INSN (label) != 0)
884 /* Find the innermost pending block that contains the label.
885 (Check containment by comparing insn-uids.)
886 Then restore the outermost stack level within that block,
887 and do cleanups of all blocks contained in it. */
888 for (block = block_stack; block; block = block->next)
890 if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
891 break;
892 if (block->data.block.stack_level != 0)
893 stack_level = block->data.block.stack_level;
894 /* Execute the cleanups for blocks we are exiting. */
895 if (block->data.block.cleanups != 0)
897 expand_cleanups (block->data.block.cleanups, NULL_TREE, 1, 1);
898 do_pending_stack_adjust ();
902 if (stack_level)
904 /* Ensure stack adjust isn't done by emit_jump, as this
905 would clobber the stack pointer. This one should be
906 deleted as dead by flow. */
907 clear_pending_stack_adjust ();
908 do_pending_stack_adjust ();
910 /* Don't do this adjust if it's to the end label and this function
911 is to return with a depressed stack pointer. */
912 if (label == return_label
913 && (((TREE_CODE (TREE_TYPE (current_function_decl))
914 == FUNCTION_TYPE)
915 && (TYPE_RETURNS_STACK_DEPRESSED
916 (TREE_TYPE (current_function_decl))))))
918 else
919 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
922 if (body != 0 && DECL_TOO_LATE (body))
923 error ("jump to `%s' invalidly jumps into binding contour",
924 IDENTIFIER_POINTER (DECL_NAME (body)));
926 /* Label not yet defined: may need to put this goto
927 on the fixup list. */
928 else if (! expand_fixup (body, label, last_insn))
930 /* No fixup needed. Record that the label is the target
931 of at least one goto that has no fixup. */
932 if (body != 0)
933 TREE_ADDRESSABLE (body) = 1;
936 emit_jump (label);
939 /* Generate if necessary a fixup for a goto
940 whose target label in tree structure (if any) is TREE_LABEL
941 and whose target in rtl is RTL_LABEL.
943 If LAST_INSN is nonzero, we pretend that the jump appears
944 after insn LAST_INSN instead of at the current point in the insn stream.
946 The fixup will be used later to insert insns just before the goto.
947 Those insns will restore the stack level as appropriate for the
948 target label, and will (in the case of C++) also invoke any object
949 destructors which have to be invoked when we exit the scopes which
950 are exited by the goto.
952 Value is nonzero if a fixup is made. */
954 static int
955 expand_fixup (tree_label, rtl_label, last_insn)
956 tree tree_label;
957 rtx rtl_label;
958 rtx last_insn;
960 struct nesting *block, *end_block;
962 /* See if we can recognize which block the label will be output in.
963 This is possible in some very common cases.
964 If we succeed, set END_BLOCK to that block.
965 Otherwise, set it to 0. */
967 if (cond_stack
968 && (rtl_label == cond_stack->data.cond.endif_label
969 || rtl_label == cond_stack->data.cond.next_label))
970 end_block = cond_stack;
971 /* If we are in a loop, recognize certain labels which
972 are likely targets. This reduces the number of fixups
973 we need to create. */
974 else if (loop_stack
975 && (rtl_label == loop_stack->data.loop.start_label
976 || rtl_label == loop_stack->data.loop.end_label
977 || rtl_label == loop_stack->data.loop.continue_label))
978 end_block = loop_stack;
979 else
980 end_block = 0;
982 /* Now set END_BLOCK to the binding level to which we will return. */
984 if (end_block)
986 struct nesting *next_block = end_block->all;
987 block = block_stack;
989 /* First see if the END_BLOCK is inside the innermost binding level.
990 If so, then no cleanups or stack levels are relevant. */
991 while (next_block && next_block != block)
992 next_block = next_block->all;
994 if (next_block)
995 return 0;
997 /* Otherwise, set END_BLOCK to the innermost binding level
998 which is outside the relevant control-structure nesting. */
999 next_block = block_stack->next;
1000 for (block = block_stack; block != end_block; block = block->all)
1001 if (block == next_block)
1002 next_block = next_block->next;
1003 end_block = next_block;
1006 /* Does any containing block have a stack level or cleanups?
1007 If not, no fixup is needed, and that is the normal case
1008 (the only case, for standard C). */
1009 for (block = block_stack; block != end_block; block = block->next)
1010 if (block->data.block.stack_level != 0
1011 || block->data.block.cleanups != 0)
1012 break;
1014 if (block != end_block)
1016 /* Ok, a fixup is needed. Add a fixup to the list of such. */
1017 struct goto_fixup *fixup
1018 = (struct goto_fixup *) ggc_alloc (sizeof (struct goto_fixup));
1019 /* In case an old stack level is restored, make sure that comes
1020 after any pending stack adjust. */
1021 /* ?? If the fixup isn't to come at the present position,
1022 doing the stack adjust here isn't useful. Doing it with our
1023 settings at that location isn't useful either. Let's hope
1024 someone does it! */
1025 if (last_insn == 0)
1026 do_pending_stack_adjust ();
1027 fixup->target = tree_label;
1028 fixup->target_rtl = rtl_label;
1030 /* Create a BLOCK node and a corresponding matched set of
1031 NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes at
1032 this point. The notes will encapsulate any and all fixup
1033 code which we might later insert at this point in the insn
1034 stream. Also, the BLOCK node will be the parent (i.e. the
1035 `SUPERBLOCK') of any other BLOCK nodes which we might create
1036 later on when we are expanding the fixup code.
1038 Note that optimization passes (including expand_end_loop)
1039 might move the *_BLOCK notes away, so we use a NOTE_INSN_DELETED
1040 as a placeholder. */
1043 register rtx original_before_jump
1044 = last_insn ? last_insn : get_last_insn ();
1045 rtx start;
1046 rtx end;
1047 tree block;
1049 block = make_node (BLOCK);
1050 TREE_USED (block) = 1;
1052 if (!cfun->x_whole_function_mode_p)
1053 insert_block (block);
1054 else
1056 BLOCK_CHAIN (block)
1057 = BLOCK_CHAIN (DECL_INITIAL (current_function_decl));
1058 BLOCK_CHAIN (DECL_INITIAL (current_function_decl))
1059 = block;
1062 start_sequence ();
1063 start = emit_note (NULL, NOTE_INSN_BLOCK_BEG);
1064 if (cfun->x_whole_function_mode_p)
1065 NOTE_BLOCK (start) = block;
1066 fixup->before_jump = emit_note (NULL, NOTE_INSN_DELETED);
1067 end = emit_note (NULL, NOTE_INSN_BLOCK_END);
1068 if (cfun->x_whole_function_mode_p)
1069 NOTE_BLOCK (end) = block;
1070 fixup->context = block;
1071 end_sequence ();
1072 emit_insns_after (start, original_before_jump);
1075 fixup->block_start_count = current_block_start_count;
1076 fixup->stack_level = 0;
1077 fixup->cleanup_list_list
1078 = ((block->data.block.outer_cleanups
1079 || block->data.block.cleanups)
1080 ? tree_cons (NULL_TREE, block->data.block.cleanups,
1081 block->data.block.outer_cleanups)
1082 : 0);
1083 fixup->next = goto_fixup_chain;
1084 goto_fixup_chain = fixup;
1087 return block != 0;
1090 /* Expand any needed fixups in the outputmost binding level of the
1091 function. FIRST_INSN is the first insn in the function. */
1093 void
1094 expand_fixups (first_insn)
1095 rtx first_insn;
1097 fixup_gotos (NULL, NULL_RTX, NULL_TREE, first_insn, 0);
1100 /* When exiting a binding contour, process all pending gotos requiring fixups.
1101 THISBLOCK is the structure that describes the block being exited.
1102 STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
1103 CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
1104 FIRST_INSN is the insn that began this contour.
1106 Gotos that jump out of this contour must restore the
1107 stack level and do the cleanups before actually jumping.
1109 DONT_JUMP_IN nonzero means report error there is a jump into this
1110 contour from before the beginning of the contour.
1111 This is also done if STACK_LEVEL is nonzero. */
1113 static void
1114 fixup_gotos (thisblock, stack_level, cleanup_list, first_insn, dont_jump_in)
1115 struct nesting *thisblock;
1116 rtx stack_level;
1117 tree cleanup_list;
1118 rtx first_insn;
1119 int dont_jump_in;
1121 register struct goto_fixup *f, *prev;
1123 /* F is the fixup we are considering; PREV is the previous one. */
1124 /* We run this loop in two passes so that cleanups of exited blocks
1125 are run first, and blocks that are exited are marked so
1126 afterwards. */
1128 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1130 /* Test for a fixup that is inactive because it is already handled. */
1131 if (f->before_jump == 0)
1133 /* Delete inactive fixup from the chain, if that is easy to do. */
1134 if (prev != 0)
1135 prev->next = f->next;
1137 /* Has this fixup's target label been defined?
1138 If so, we can finalize it. */
1139 else if (PREV_INSN (f->target_rtl) != 0)
1141 register rtx cleanup_insns;
1143 /* If this fixup jumped into this contour from before the beginning
1144 of this contour, report an error. This code used to use
1145 the first non-label insn after f->target_rtl, but that's
1146 wrong since such can be added, by things like put_var_into_stack
1147 and have INSN_UIDs that are out of the range of the block. */
1148 /* ??? Bug: this does not detect jumping in through intermediate
1149 blocks that have stack levels or cleanups.
1150 It detects only a problem with the innermost block
1151 around the label. */
1152 if (f->target != 0
1153 && (dont_jump_in || stack_level || cleanup_list)
1154 && INSN_UID (first_insn) < INSN_UID (f->target_rtl)
1155 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
1156 && ! DECL_ERROR_ISSUED (f->target))
1158 error_with_decl (f->target,
1159 "label `%s' used before containing binding contour");
1160 /* Prevent multiple errors for one label. */
1161 DECL_ERROR_ISSUED (f->target) = 1;
1164 /* We will expand the cleanups into a sequence of their own and
1165 then later on we will attach this new sequence to the insn
1166 stream just ahead of the actual jump insn. */
1168 start_sequence ();
1170 /* Temporarily restore the lexical context where we will
1171 logically be inserting the fixup code. We do this for the
1172 sake of getting the debugging information right. */
1174 pushlevel (0);
1175 set_block (f->context);
1177 /* Expand the cleanups for blocks this jump exits. */
1178 if (f->cleanup_list_list)
1180 tree lists;
1181 for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
1182 /* Marked elements correspond to blocks that have been closed.
1183 Do their cleanups. */
1184 if (TREE_ADDRESSABLE (lists)
1185 && TREE_VALUE (lists) != 0)
1187 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
1188 /* Pop any pushes done in the cleanups,
1189 in case function is about to return. */
1190 do_pending_stack_adjust ();
1194 /* Restore stack level for the biggest contour that this
1195 jump jumps out of. */
1196 if (f->stack_level
1197 && ! (f->target_rtl == return_label
1198 && ((TREE_CODE (TREE_TYPE (current_function_decl))
1199 == FUNCTION_TYPE)
1200 && (TYPE_RETURNS_STACK_DEPRESSED
1201 (TREE_TYPE (current_function_decl))))))
1202 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
1204 /* Finish up the sequence containing the insns which implement the
1205 necessary cleanups, and then attach that whole sequence to the
1206 insn stream just ahead of the actual jump insn. Attaching it
1207 at that point insures that any cleanups which are in fact
1208 implicit C++ object destructions (which must be executed upon
1209 leaving the block) appear (to the debugger) to be taking place
1210 in an area of the generated code where the object(s) being
1211 destructed are still "in scope". */
1213 cleanup_insns = get_insns ();
1214 poplevel (1, 0, 0);
1216 end_sequence ();
1217 emit_insns_after (cleanup_insns, f->before_jump);
1219 f->before_jump = 0;
1223 /* For any still-undefined labels, do the cleanups for this block now.
1224 We must do this now since items in the cleanup list may go out
1225 of scope when the block ends. */
1226 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
1227 if (f->before_jump != 0
1228 && PREV_INSN (f->target_rtl) == 0
1229 /* Label has still not appeared. If we are exiting a block with
1230 a stack level to restore, that started before the fixup,
1231 mark this stack level as needing restoration
1232 when the fixup is later finalized. */
1233 && thisblock != 0
1234 /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared, it
1235 means the label is undefined. That's erroneous, but possible. */
1236 && (thisblock->data.block.block_start_count
1237 <= f->block_start_count))
1239 tree lists = f->cleanup_list_list;
1240 rtx cleanup_insns;
1242 for (; lists; lists = TREE_CHAIN (lists))
1243 /* If the following elt. corresponds to our containing block
1244 then the elt. must be for this block. */
1245 if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
1247 start_sequence ();
1248 pushlevel (0);
1249 set_block (f->context);
1250 expand_cleanups (TREE_VALUE (lists), NULL_TREE, 1, 1);
1251 do_pending_stack_adjust ();
1252 cleanup_insns = get_insns ();
1253 poplevel (1, 0, 0);
1254 end_sequence ();
1255 if (cleanup_insns != 0)
1256 f->before_jump
1257 = emit_insns_after (cleanup_insns, f->before_jump);
1259 f->cleanup_list_list = TREE_CHAIN (lists);
1262 if (stack_level)
1263 f->stack_level = stack_level;
1267 /* Return the number of times character C occurs in string S. */
1268 static int
1269 n_occurrences (c, s)
1270 int c;
1271 const char *s;
1273 int n = 0;
1274 while (*s)
1275 n += (*s++ == c);
1276 return n;
1279 /* Generate RTL for an asm statement (explicit assembler code).
1280 BODY is a STRING_CST node containing the assembler code text,
1281 or an ADDR_EXPR containing a STRING_CST. */
1283 void
1284 expand_asm (body)
1285 tree body;
1287 if (current_function_check_memory_usage)
1289 error ("`asm' cannot be used in function where memory usage is checked");
1290 return;
1293 if (TREE_CODE (body) == ADDR_EXPR)
1294 body = TREE_OPERAND (body, 0);
1296 emit_insn (gen_rtx_ASM_INPUT (VOIDmode,
1297 TREE_STRING_POINTER (body)));
1298 last_expr_type = 0;
1301 /* Parse the output constraint pointed to by *CONSTRAINT_P. It is the
1302 OPERAND_NUMth output operand, indexed from zero. There are NINPUTS
1303 inputs and NOUTPUTS outputs to this extended-asm. Upon return,
1304 *ALLOWS_MEM will be TRUE iff the constraint allows the use of a
1305 memory operand. Similarly, *ALLOWS_REG will be TRUE iff the
1306 constraint allows the use of a register operand. And, *IS_INOUT
1307 will be true if the operand is read-write, i.e., if it is used as
1308 an input as well as an output. If *CONSTRAINT_P is not in
1309 canonical form, it will be made canonical. (Note that `+' will be
1310 rpelaced with `=' as part of this process.)
1312 Returns TRUE if all went well; FALSE if an error occurred. */
1314 bool
1315 parse_output_constraint (constraint_p,
1316 operand_num,
1317 ninputs,
1318 noutputs,
1319 allows_mem,
1320 allows_reg,
1321 is_inout)
1322 const char **constraint_p;
1323 int operand_num;
1324 int ninputs;
1325 int noutputs;
1326 bool *allows_mem;
1327 bool *allows_reg;
1328 bool *is_inout;
1330 const char *constraint = *constraint_p;
1331 const char *p;
1333 /* Assume the constraint doesn't allow the use of either a register
1334 or memory. */
1335 *allows_mem = false;
1336 *allows_reg = false;
1338 /* Allow the `=' or `+' to not be at the beginning of the string,
1339 since it wasn't explicitly documented that way, and there is a
1340 large body of code that puts it last. Swap the character to
1341 the front, so as not to uglify any place else. */
1342 p = strchr (constraint, '=');
1343 if (!p)
1344 p = strchr (constraint, '+');
1346 /* If the string doesn't contain an `=', issue an error
1347 message. */
1348 if (!p)
1350 error ("output operand constraint lacks `='");
1351 return false;
1354 /* If the constraint begins with `+', then the operand is both read
1355 from and written to. */
1356 *is_inout = (*p == '+');
1358 /* Make sure we can specify the matching operand. */
1359 if (*is_inout && operand_num > 9)
1361 error ("output operand constraint %d contains `+'",
1362 operand_num);
1363 return false;
1366 /* Canonicalize the output constraint so that it begins with `='. */
1367 if (p != constraint || is_inout)
1369 char *buf;
1370 size_t c_len = strlen (constraint);
1372 if (p != constraint)
1373 warning ("output constraint `%c' for operand %d is not at the beginning",
1374 *p, operand_num);
1376 /* Make a copy of the constraint. */
1377 buf = alloca (c_len + 1);
1378 strcpy (buf, constraint);
1379 /* Swap the first character and the `=' or `+'. */
1380 buf[p - constraint] = buf[0];
1381 /* Make sure the first character is an `='. (Until we do this,
1382 it might be a `+'.) */
1383 buf[0] = '=';
1384 /* Replace the constraint with the canonicalized string. */
1385 *constraint_p = ggc_alloc_string (buf, c_len);
1386 constraint = *constraint_p;
1389 /* Loop through the constraint string. */
1390 for (p = constraint + 1; *p; ++p)
1391 switch (*p)
1393 case '+':
1394 case '=':
1395 error ("operand constraint contains '+' or '=' at illegal position.");
1396 return false;
1398 case '%':
1399 if (operand_num + 1 == ninputs + noutputs)
1401 error ("`%%' constraint used with last operand");
1402 return false;
1404 break;
1406 case 'V': case 'm': case 'o':
1407 *allows_mem = true;
1408 break;
1410 case '?': case '!': case '*': case '&': case '#':
1411 case 'E': case 'F': case 'G': case 'H':
1412 case 's': case 'i': case 'n':
1413 case 'I': case 'J': case 'K': case 'L': case 'M':
1414 case 'N': case 'O': case 'P': case ',':
1415 break;
1417 case '0': case '1': case '2': case '3': case '4':
1418 case '5': case '6': case '7': case '8': case '9':
1419 error ("matching constraint not valid in output operand");
1420 return false;
1422 case '<': case '>':
1423 /* ??? Before flow, auto inc/dec insns are not supposed to exist,
1424 excepting those that expand_call created. So match memory
1425 and hope. */
1426 *allows_mem = true;
1427 break;
1429 case 'g': case 'X':
1430 *allows_reg = true;
1431 *allows_mem = true;
1432 break;
1434 case 'p': case 'r':
1435 *allows_reg = true;
1436 break;
1438 default:
1439 if (!ISALPHA (*p))
1440 break;
1441 if (REG_CLASS_FROM_LETTER (*p) != NO_REGS)
1442 *allows_reg = true;
1443 #ifdef EXTRA_CONSTRAINT
1444 else
1446 /* Otherwise we can't assume anything about the nature of
1447 the constraint except that it isn't purely registers.
1448 Treat it like "g" and hope for the best. */
1449 *allows_reg = true;
1450 *allows_mem = true;
1452 #endif
1453 break;
1456 return true;
1459 /* Generate RTL for an asm statement with arguments.
1460 STRING is the instruction template.
1461 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
1462 Each output or input has an expression in the TREE_VALUE and
1463 a constraint-string in the TREE_PURPOSE.
1464 CLOBBERS is a list of STRING_CST nodes each naming a hard register
1465 that is clobbered by this insn.
1467 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
1468 Some elements of OUTPUTS may be replaced with trees representing temporary
1469 values. The caller should copy those temporary values to the originally
1470 specified lvalues.
1472 VOL nonzero means the insn is volatile; don't optimize it. */
1474 void
1475 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
1476 tree string, outputs, inputs, clobbers;
1477 int vol;
1478 const char *filename;
1479 int line;
1481 rtvec argvec, constraints;
1482 rtx body;
1483 int ninputs = list_length (inputs);
1484 int noutputs = list_length (outputs);
1485 int ninout = 0;
1486 int nclobbers;
1487 tree tail;
1488 register int i;
1489 /* Vector of RTX's of evaluated output operands. */
1490 rtx *output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1491 int *inout_opnum = (int *) alloca (noutputs * sizeof (int));
1492 rtx *real_output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1493 enum machine_mode *inout_mode
1494 = (enum machine_mode *) alloca (noutputs * sizeof (enum machine_mode));
1495 const char **output_constraints
1496 = alloca (noutputs * sizeof (const char *));
1497 /* The insn we have emitted. */
1498 rtx insn;
1499 int old_generating_concat_p = generating_concat_p;
1501 /* An ASM with no outputs needs to be treated as volatile, for now. */
1502 if (noutputs == 0)
1503 vol = 1;
1505 if (current_function_check_memory_usage)
1507 error ("`asm' cannot be used with `-fcheck-memory-usage'");
1508 return;
1511 #ifdef MD_ASM_CLOBBERS
1512 /* Sometimes we wish to automatically clobber registers across an asm.
1513 Case in point is when the i386 backend moved from cc0 to a hard reg --
1514 maintaining source-level compatability means automatically clobbering
1515 the flags register. */
1516 MD_ASM_CLOBBERS (clobbers);
1517 #endif
1519 if (current_function_check_memory_usage)
1521 error ("`asm' cannot be used in function where memory usage is checked");
1522 return;
1525 /* Count the number of meaningful clobbered registers, ignoring what
1526 we would ignore later. */
1527 nclobbers = 0;
1528 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1530 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1532 i = decode_reg_name (regname);
1533 if (i >= 0 || i == -4)
1534 ++nclobbers;
1535 else if (i == -2)
1536 error ("unknown register name `%s' in `asm'", regname);
1539 last_expr_type = 0;
1541 /* Check that the number of alternatives is constant across all
1542 operands. */
1543 if (outputs || inputs)
1545 tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
1546 int nalternatives = n_occurrences (',', TREE_STRING_POINTER (tmp));
1547 tree next = inputs;
1549 if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
1551 error ("too many alternatives in `asm'");
1552 return;
1555 tmp = outputs;
1556 while (tmp)
1558 const char *constraint = TREE_STRING_POINTER (TREE_PURPOSE (tmp));
1560 if (n_occurrences (',', constraint) != nalternatives)
1562 error ("operand constraints for `asm' differ in number of alternatives");
1563 return;
1566 if (TREE_CHAIN (tmp))
1567 tmp = TREE_CHAIN (tmp);
1568 else
1569 tmp = next, next = 0;
1573 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1575 tree val = TREE_VALUE (tail);
1576 tree type = TREE_TYPE (val);
1577 const char *constraint;
1578 bool is_inout;
1579 bool allows_reg;
1580 bool allows_mem;
1582 /* If there's an erroneous arg, emit no insn. */
1583 if (type == error_mark_node)
1584 return;
1586 /* Make sure constraint has `=' and does not have `+'. Also, see
1587 if it allows any register. Be liberal on the latter test, since
1588 the worst that happens if we get it wrong is we issue an error
1589 message. */
1591 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
1592 output_constraints[i] = constraint;
1594 /* Try to parse the output constraint. If that fails, there's
1595 no point in going further. */
1596 if (!parse_output_constraint (&output_constraints[i],
1598 ninputs,
1599 noutputs,
1600 &allows_mem,
1601 &allows_reg,
1602 &is_inout))
1603 return;
1605 /* If an output operand is not a decl or indirect ref and our constraint
1606 allows a register, make a temporary to act as an intermediate.
1607 Make the asm insn write into that, then our caller will copy it to
1608 the real output operand. Likewise for promoted variables. */
1610 generating_concat_p = 0;
1612 real_output_rtx[i] = NULL_RTX;
1613 if ((TREE_CODE (val) == INDIRECT_REF
1614 && allows_mem)
1615 || (DECL_P (val)
1616 && (allows_mem || GET_CODE (DECL_RTL (val)) == REG)
1617 && ! (GET_CODE (DECL_RTL (val)) == REG
1618 && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
1619 || ! allows_reg
1620 || is_inout)
1622 if (! allows_reg)
1623 mark_addressable (TREE_VALUE (tail));
1625 output_rtx[i]
1626 = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode,
1627 EXPAND_MEMORY_USE_WO);
1629 if (! allows_reg && GET_CODE (output_rtx[i]) != MEM)
1630 error ("output number %d not directly addressable", i);
1631 if ((! allows_mem && GET_CODE (output_rtx[i]) == MEM)
1632 || GET_CODE (output_rtx[i]) == CONCAT)
1634 real_output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1635 output_rtx[i] = gen_reg_rtx (GET_MODE (output_rtx[i]));
1636 if (is_inout)
1637 emit_move_insn (output_rtx[i], real_output_rtx[i]);
1640 else
1642 output_rtx[i] = assign_temp (type, 0, 0, 1);
1643 TREE_VALUE (tail) = make_tree (type, output_rtx[i]);
1646 generating_concat_p = old_generating_concat_p;
1648 if (is_inout)
1650 inout_mode[ninout] = TYPE_MODE (TREE_TYPE (TREE_VALUE (tail)));
1651 inout_opnum[ninout++] = i;
1655 ninputs += ninout;
1656 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1658 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1659 return;
1662 /* Make vectors for the expression-rtx and constraint strings. */
1664 argvec = rtvec_alloc (ninputs);
1665 constraints = rtvec_alloc (ninputs);
1667 body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
1668 : GET_MODE (output_rtx[0])),
1669 TREE_STRING_POINTER (string),
1670 empty_string, 0, argvec, constraints,
1671 filename, line);
1673 MEM_VOLATILE_P (body) = vol;
1675 /* Eval the inputs and put them into ARGVEC.
1676 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1678 i = 0;
1679 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
1681 int j;
1682 int allows_reg = 0, allows_mem = 0;
1683 const char *constraint, *orig_constraint;
1684 int c_len;
1685 rtx op;
1687 /* If there's an erroneous arg, emit no insn,
1688 because the ASM_INPUT would get VOIDmode
1689 and that could cause a crash in reload. */
1690 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1691 return;
1693 /* ??? Can this happen, and does the error message make any sense? */
1694 if (TREE_PURPOSE (tail) == NULL_TREE)
1696 error ("hard register `%s' listed as input operand to `asm'",
1697 TREE_STRING_POINTER (TREE_VALUE (tail)) );
1698 return;
1701 constraint = TREE_STRING_POINTER (TREE_PURPOSE (tail));
1702 c_len = strlen (constraint);
1703 orig_constraint = constraint;
1705 /* Make sure constraint has neither `=', `+', nor '&'. */
1707 for (j = 0; j < c_len; j++)
1708 switch (constraint[j])
1710 case '+': case '=': case '&':
1711 if (constraint == orig_constraint)
1713 error ("input operand constraint contains `%c'",
1714 constraint[j]);
1715 return;
1717 break;
1719 case '%':
1720 if (constraint == orig_constraint
1721 && i + 1 == ninputs - ninout)
1723 error ("`%%' constraint used with last operand");
1724 return;
1726 break;
1728 case 'V': case 'm': case 'o':
1729 allows_mem = 1;
1730 break;
1732 case '<': case '>':
1733 case '?': case '!': case '*': case '#':
1734 case 'E': case 'F': case 'G': case 'H':
1735 case 's': case 'i': case 'n':
1736 case 'I': case 'J': case 'K': case 'L': case 'M':
1737 case 'N': case 'O': case 'P': case ',':
1738 break;
1740 /* Whether or not a numeric constraint allows a register is
1741 decided by the matching constraint, and so there is no need
1742 to do anything special with them. We must handle them in
1743 the default case, so that we don't unnecessarily force
1744 operands to memory. */
1745 case '0': case '1': case '2': case '3': case '4':
1746 case '5': case '6': case '7': case '8': case '9':
1747 if (constraint[j] >= '0' + noutputs)
1749 error
1750 ("matching constraint references invalid operand number");
1751 return;
1754 /* Try and find the real constraint for this dup. */
1755 if ((j == 0 && c_len == 1)
1756 || (j == 1 && c_len == 2 && constraint[0] == '%'))
1758 tree o = outputs;
1760 for (j = constraint[j] - '0'; j > 0; --j)
1761 o = TREE_CHAIN (o);
1763 constraint = TREE_STRING_POINTER (TREE_PURPOSE (o));
1764 c_len = strlen (constraint);
1765 j = 0;
1766 break;
1769 /* Fall through. */
1771 case 'p': case 'r':
1772 allows_reg = 1;
1773 break;
1775 case 'g': case 'X':
1776 allows_reg = 1;
1777 allows_mem = 1;
1778 break;
1780 default:
1781 if (! ISALPHA (constraint[j]))
1783 error ("invalid punctuation `%c' in constraint",
1784 constraint[j]);
1785 return;
1787 if (REG_CLASS_FROM_LETTER (constraint[j]) != NO_REGS)
1788 allows_reg = 1;
1789 #ifdef EXTRA_CONSTRAINT
1790 else
1792 /* Otherwise we can't assume anything about the nature of
1793 the constraint except that it isn't purely registers.
1794 Treat it like "g" and hope for the best. */
1795 allows_reg = 1;
1796 allows_mem = 1;
1798 #endif
1799 break;
1802 if (! allows_reg && allows_mem)
1803 mark_addressable (TREE_VALUE (tail));
1805 op = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
1807 /* Never pass a CONCAT to an ASM. */
1808 generating_concat_p = 0;
1809 if (GET_CODE (op) == CONCAT)
1810 op = force_reg (GET_MODE (op), op);
1812 if (asm_operand_ok (op, constraint) <= 0)
1814 if (allows_reg)
1815 op = force_reg (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))), op);
1816 else if (!allows_mem)
1817 warning ("asm operand %d probably doesn't match constraints", i);
1818 else if (CONSTANT_P (op))
1819 op = force_const_mem (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1820 op);
1821 else if (GET_CODE (op) == REG
1822 || GET_CODE (op) == SUBREG
1823 || GET_CODE (op) == ADDRESSOF
1824 || GET_CODE (op) == CONCAT)
1826 tree type = TREE_TYPE (TREE_VALUE (tail));
1827 tree qual_type = build_qualified_type (type,
1828 (TYPE_QUALS (type)
1829 | TYPE_QUAL_CONST));
1830 rtx memloc = assign_temp (qual_type, 1, 1, 1);
1832 emit_move_insn (memloc, op);
1833 op = memloc;
1836 else if (GET_CODE (op) == MEM && MEM_VOLATILE_P (op))
1837 /* We won't recognize volatile memory as available a
1838 memory_operand at this point. Ignore it. */
1840 else if (queued_subexp_p (op))
1842 else
1843 /* ??? Leave this only until we have experience with what
1844 happens in combine and elsewhere when constraints are
1845 not satisfied. */
1846 warning ("asm operand %d probably doesn't match constraints", i);
1848 generating_concat_p = old_generating_concat_p;
1849 ASM_OPERANDS_INPUT (body, i) = op;
1851 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
1852 = gen_rtx_ASM_INPUT (TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1853 orig_constraint);
1854 i++;
1857 /* Protect all the operands from the queue now that they have all been
1858 evaluated. */
1860 generating_concat_p = 0;
1862 for (i = 0; i < ninputs - ninout; i++)
1863 ASM_OPERANDS_INPUT (body, i)
1864 = protect_from_queue (ASM_OPERANDS_INPUT (body, i), 0);
1866 for (i = 0; i < noutputs; i++)
1867 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1869 /* For in-out operands, copy output rtx to input rtx. */
1870 for (i = 0; i < ninout; i++)
1872 int j = inout_opnum[i];
1874 ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
1875 = output_rtx[j];
1876 ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
1877 = gen_rtx_ASM_INPUT (inout_mode[i], digit_string (j));
1880 generating_concat_p = old_generating_concat_p;
1882 /* Now, for each output, construct an rtx
1883 (set OUTPUT (asm_operands INSN OUTPUTNUMBER OUTPUTCONSTRAINT
1884 ARGVEC CONSTRAINTS))
1885 If there is more than one, put them inside a PARALLEL. */
1887 if (noutputs == 1 && nclobbers == 0)
1889 ASM_OPERANDS_OUTPUT_CONSTRAINT (body)
1890 = output_constraints[0];
1891 insn = emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
1894 else if (noutputs == 0 && nclobbers == 0)
1896 /* No output operands: put in a raw ASM_OPERANDS rtx. */
1897 insn = emit_insn (body);
1900 else
1902 rtx obody = body;
1903 int num = noutputs;
1905 if (num == 0)
1906 num = 1;
1908 body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
1910 /* For each output operand, store a SET. */
1911 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1913 XVECEXP (body, 0, i)
1914 = gen_rtx_SET (VOIDmode,
1915 output_rtx[i],
1916 gen_rtx_ASM_OPERANDS
1917 (GET_MODE (output_rtx[i]),
1918 TREE_STRING_POINTER (string),
1919 output_constraints[i],
1920 i, argvec, constraints,
1921 filename, line));
1923 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1926 /* If there are no outputs (but there are some clobbers)
1927 store the bare ASM_OPERANDS into the PARALLEL. */
1929 if (i == 0)
1930 XVECEXP (body, 0, i++) = obody;
1932 /* Store (clobber REG) for each clobbered register specified. */
1934 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1936 const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
1937 int j = decode_reg_name (regname);
1939 if (j < 0)
1941 if (j == -3) /* `cc', which is not a register */
1942 continue;
1944 if (j == -4) /* `memory', don't cache memory across asm */
1946 XVECEXP (body, 0, i++)
1947 = gen_rtx_CLOBBER (VOIDmode,
1948 gen_rtx_MEM
1949 (BLKmode,
1950 gen_rtx_SCRATCH (VOIDmode)));
1951 continue;
1954 /* Ignore unknown register, error already signaled. */
1955 continue;
1958 /* Use QImode since that's guaranteed to clobber just one reg. */
1959 XVECEXP (body, 0, i++)
1960 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (QImode, j));
1963 insn = emit_insn (body);
1966 /* For any outputs that needed reloading into registers, spill them
1967 back to where they belong. */
1968 for (i = 0; i < noutputs; ++i)
1969 if (real_output_rtx[i])
1970 emit_move_insn (real_output_rtx[i], output_rtx[i]);
1972 free_temp_slots ();
1975 /* Generate RTL to evaluate the expression EXP
1976 and remember it in case this is the VALUE in a ({... VALUE; }) constr. */
1978 void
1979 expand_expr_stmt (exp)
1980 tree exp;
1982 /* If -W, warn about statements with no side effects,
1983 except for an explicit cast to void (e.g. for assert()), and
1984 except inside a ({...}) where they may be useful. */
1985 if (expr_stmts_for_value == 0 && exp != error_mark_node)
1987 if (! TREE_SIDE_EFFECTS (exp))
1989 if ((extra_warnings || warn_unused_value)
1990 && !(TREE_CODE (exp) == CONVERT_EXPR
1991 && VOID_TYPE_P (TREE_TYPE (exp))))
1992 warning_with_file_and_line (emit_filename, emit_lineno,
1993 "statement with no effect");
1995 else if (warn_unused_value)
1996 warn_if_unused_value (exp);
1999 /* If EXP is of function type and we are expanding statements for
2000 value, convert it to pointer-to-function. */
2001 if (expr_stmts_for_value && TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE)
2002 exp = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (exp)), exp);
2004 /* The call to `expand_expr' could cause last_expr_type and
2005 last_expr_value to get reset. Therefore, we set last_expr_value
2006 and last_expr_type *after* calling expand_expr. */
2007 last_expr_value = expand_expr (exp,
2008 (expr_stmts_for_value
2009 ? NULL_RTX : const0_rtx),
2010 VOIDmode, 0);
2011 last_expr_type = TREE_TYPE (exp);
2013 /* If all we do is reference a volatile value in memory,
2014 copy it to a register to be sure it is actually touched. */
2015 if (last_expr_value != 0 && GET_CODE (last_expr_value) == MEM
2016 && TREE_THIS_VOLATILE (exp))
2018 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode)
2020 else if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
2021 copy_to_reg (last_expr_value);
2022 else
2024 rtx lab = gen_label_rtx ();
2026 /* Compare the value with itself to reference it. */
2027 emit_cmp_and_jump_insns (last_expr_value, last_expr_value, EQ,
2028 expand_expr (TYPE_SIZE (last_expr_type),
2029 NULL_RTX, VOIDmode, 0),
2030 BLKmode, 0,
2031 TYPE_ALIGN (last_expr_type) / BITS_PER_UNIT,
2032 lab);
2033 emit_label (lab);
2037 /* If this expression is part of a ({...}) and is in memory, we may have
2038 to preserve temporaries. */
2039 preserve_temp_slots (last_expr_value);
2041 /* Free any temporaries used to evaluate this expression. Any temporary
2042 used as a result of this expression will already have been preserved
2043 above. */
2044 free_temp_slots ();
2046 emit_queue ();
2049 /* Warn if EXP contains any computations whose results are not used.
2050 Return 1 if a warning is printed; 0 otherwise. */
2053 warn_if_unused_value (exp)
2054 tree exp;
2056 if (TREE_USED (exp))
2057 return 0;
2059 /* Don't warn about void constructs. This includes casting to void,
2060 void function calls, and statement expressions with a final cast
2061 to void. */
2062 if (VOID_TYPE_P (TREE_TYPE (exp)))
2063 return 0;
2065 /* If this is an expression with side effects, don't warn. */
2066 if (TREE_SIDE_EFFECTS (exp))
2067 return 0;
2069 switch (TREE_CODE (exp))
2071 case PREINCREMENT_EXPR:
2072 case POSTINCREMENT_EXPR:
2073 case PREDECREMENT_EXPR:
2074 case POSTDECREMENT_EXPR:
2075 case MODIFY_EXPR:
2076 case INIT_EXPR:
2077 case TARGET_EXPR:
2078 case CALL_EXPR:
2079 case METHOD_CALL_EXPR:
2080 case RTL_EXPR:
2081 case TRY_CATCH_EXPR:
2082 case WITH_CLEANUP_EXPR:
2083 case EXIT_EXPR:
2084 return 0;
2086 case BIND_EXPR:
2087 /* For a binding, warn if no side effect within it. */
2088 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2090 case SAVE_EXPR:
2091 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2093 case TRUTH_ORIF_EXPR:
2094 case TRUTH_ANDIF_EXPR:
2095 /* In && or ||, warn if 2nd operand has no side effect. */
2096 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2098 case COMPOUND_EXPR:
2099 if (TREE_NO_UNUSED_WARNING (exp))
2100 return 0;
2101 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
2102 return 1;
2103 /* Let people do `(foo (), 0)' without a warning. */
2104 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2105 return 0;
2106 return warn_if_unused_value (TREE_OPERAND (exp, 1));
2108 case NOP_EXPR:
2109 case CONVERT_EXPR:
2110 case NON_LVALUE_EXPR:
2111 /* Don't warn about conversions not explicit in the user's program. */
2112 if (TREE_NO_UNUSED_WARNING (exp))
2113 return 0;
2114 /* Assignment to a cast usually results in a cast of a modify.
2115 Don't complain about that. There can be an arbitrary number of
2116 casts before the modify, so we must loop until we find the first
2117 non-cast expression and then test to see if that is a modify. */
2119 tree tem = TREE_OPERAND (exp, 0);
2121 while (TREE_CODE (tem) == CONVERT_EXPR || TREE_CODE (tem) == NOP_EXPR)
2122 tem = TREE_OPERAND (tem, 0);
2124 if (TREE_CODE (tem) == MODIFY_EXPR || TREE_CODE (tem) == INIT_EXPR
2125 || TREE_CODE (tem) == CALL_EXPR)
2126 return 0;
2128 goto warn;
2130 case INDIRECT_REF:
2131 /* Don't warn about automatic dereferencing of references, since
2132 the user cannot control it. */
2133 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2134 return warn_if_unused_value (TREE_OPERAND (exp, 0));
2135 /* Fall through. */
2137 default:
2138 /* Referencing a volatile value is a side effect, so don't warn. */
2139 if ((DECL_P (exp)
2140 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
2141 && TREE_THIS_VOLATILE (exp))
2142 return 0;
2144 /* If this is an expression which has no operands, there is no value
2145 to be unused. There are no such language-independent codes,
2146 but front ends may define such. */
2147 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'e'
2148 && TREE_CODE_LENGTH (TREE_CODE (exp)) == 0)
2149 return 0;
2151 warn:
2152 warning_with_file_and_line (emit_filename, emit_lineno,
2153 "value computed is not used");
2154 return 1;
2158 /* Clear out the memory of the last expression evaluated. */
2160 void
2161 clear_last_expr ()
2163 last_expr_type = 0;
2166 /* Begin a statement which will return a value.
2167 Return the RTL_EXPR for this statement expr.
2168 The caller must save that value and pass it to expand_end_stmt_expr. */
2170 tree
2171 expand_start_stmt_expr ()
2173 tree t;
2175 /* Make the RTL_EXPR node temporary, not momentary,
2176 so that rtl_expr_chain doesn't become garbage. */
2177 t = make_node (RTL_EXPR);
2178 do_pending_stack_adjust ();
2179 start_sequence_for_rtl_expr (t);
2180 NO_DEFER_POP;
2181 expr_stmts_for_value++;
2182 return t;
2185 /* Restore the previous state at the end of a statement that returns a value.
2186 Returns a tree node representing the statement's value and the
2187 insns to compute the value.
2189 The nodes of that expression have been freed by now, so we cannot use them.
2190 But we don't want to do that anyway; the expression has already been
2191 evaluated and now we just want to use the value. So generate a RTL_EXPR
2192 with the proper type and RTL value.
2194 If the last substatement was not an expression,
2195 return something with type `void'. */
2197 tree
2198 expand_end_stmt_expr (t)
2199 tree t;
2201 OK_DEFER_POP;
2203 if (last_expr_type == 0)
2205 last_expr_type = void_type_node;
2206 last_expr_value = const0_rtx;
2208 else if (last_expr_value == 0)
2209 /* There are some cases where this can happen, such as when the
2210 statement is void type. */
2211 last_expr_value = const0_rtx;
2212 else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
2213 /* Remove any possible QUEUED. */
2214 last_expr_value = protect_from_queue (last_expr_value, 0);
2216 emit_queue ();
2218 TREE_TYPE (t) = last_expr_type;
2219 RTL_EXPR_RTL (t) = last_expr_value;
2220 RTL_EXPR_SEQUENCE (t) = get_insns ();
2222 rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
2224 end_sequence ();
2226 /* Don't consider deleting this expr or containing exprs at tree level. */
2227 TREE_SIDE_EFFECTS (t) = 1;
2228 /* Propagate volatility of the actual RTL expr. */
2229 TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
2231 last_expr_type = 0;
2232 expr_stmts_for_value--;
2234 return t;
2237 /* Generate RTL for the start of an if-then. COND is the expression
2238 whose truth should be tested.
2240 If EXITFLAG is nonzero, this conditional is visible to
2241 `exit_something'. */
2243 void
2244 expand_start_cond (cond, exitflag)
2245 tree cond;
2246 int exitflag;
2248 struct nesting *thiscond = ALLOC_NESTING ();
2250 /* Make an entry on cond_stack for the cond we are entering. */
2252 thiscond->next = cond_stack;
2253 thiscond->all = nesting_stack;
2254 thiscond->depth = ++nesting_depth;
2255 thiscond->data.cond.next_label = gen_label_rtx ();
2256 /* Before we encounter an `else', we don't need a separate exit label
2257 unless there are supposed to be exit statements
2258 to exit this conditional. */
2259 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
2260 thiscond->data.cond.endif_label = thiscond->exit_label;
2261 cond_stack = thiscond;
2262 nesting_stack = thiscond;
2264 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
2267 /* Generate RTL between then-clause and the elseif-clause
2268 of an if-then-elseif-.... */
2270 void
2271 expand_start_elseif (cond)
2272 tree cond;
2274 if (cond_stack->data.cond.endif_label == 0)
2275 cond_stack->data.cond.endif_label = gen_label_rtx ();
2276 emit_jump (cond_stack->data.cond.endif_label);
2277 emit_label (cond_stack->data.cond.next_label);
2278 cond_stack->data.cond.next_label = gen_label_rtx ();
2279 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2282 /* Generate RTL between the then-clause and the else-clause
2283 of an if-then-else. */
2285 void
2286 expand_start_else ()
2288 if (cond_stack->data.cond.endif_label == 0)
2289 cond_stack->data.cond.endif_label = gen_label_rtx ();
2291 emit_jump (cond_stack->data.cond.endif_label);
2292 emit_label (cond_stack->data.cond.next_label);
2293 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
2296 /* After calling expand_start_else, turn this "else" into an "else if"
2297 by providing another condition. */
2299 void
2300 expand_elseif (cond)
2301 tree cond;
2303 cond_stack->data.cond.next_label = gen_label_rtx ();
2304 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
2307 /* Generate RTL for the end of an if-then.
2308 Pop the record for it off of cond_stack. */
2310 void
2311 expand_end_cond ()
2313 struct nesting *thiscond = cond_stack;
2315 do_pending_stack_adjust ();
2316 if (thiscond->data.cond.next_label)
2317 emit_label (thiscond->data.cond.next_label);
2318 if (thiscond->data.cond.endif_label)
2319 emit_label (thiscond->data.cond.endif_label);
2321 POPSTACK (cond_stack);
2322 last_expr_type = 0;
2325 /* Generate RTL for the start of a loop. EXIT_FLAG is nonzero if this
2326 loop should be exited by `exit_something'. This is a loop for which
2327 `expand_continue' will jump to the top of the loop.
2329 Make an entry on loop_stack to record the labels associated with
2330 this loop. */
2332 struct nesting *
2333 expand_start_loop (exit_flag)
2334 int exit_flag;
2336 register struct nesting *thisloop = ALLOC_NESTING ();
2338 /* Make an entry on loop_stack for the loop we are entering. */
2340 thisloop->next = loop_stack;
2341 thisloop->all = nesting_stack;
2342 thisloop->depth = ++nesting_depth;
2343 thisloop->data.loop.start_label = gen_label_rtx ();
2344 thisloop->data.loop.end_label = gen_label_rtx ();
2345 thisloop->data.loop.alt_end_label = 0;
2346 thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
2347 thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
2348 loop_stack = thisloop;
2349 nesting_stack = thisloop;
2351 do_pending_stack_adjust ();
2352 emit_queue ();
2353 emit_note (NULL, NOTE_INSN_LOOP_BEG);
2354 emit_label (thisloop->data.loop.start_label);
2356 return thisloop;
2359 /* Like expand_start_loop but for a loop where the continuation point
2360 (for expand_continue_loop) will be specified explicitly. */
2362 struct nesting *
2363 expand_start_loop_continue_elsewhere (exit_flag)
2364 int exit_flag;
2366 struct nesting *thisloop = expand_start_loop (exit_flag);
2367 loop_stack->data.loop.continue_label = gen_label_rtx ();
2368 return thisloop;
2371 /* Begin a null, aka do { } while (0) "loop". But since the contents
2372 of said loop can still contain a break, we must frob the loop nest. */
2374 struct nesting *
2375 expand_start_null_loop ()
2377 register struct nesting *thisloop = ALLOC_NESTING ();
2379 /* Make an entry on loop_stack for the loop we are entering. */
2381 thisloop->next = loop_stack;
2382 thisloop->all = nesting_stack;
2383 thisloop->depth = ++nesting_depth;
2384 thisloop->data.loop.start_label = emit_note (NULL, NOTE_INSN_DELETED);
2385 thisloop->data.loop.end_label = gen_label_rtx ();
2386 thisloop->data.loop.alt_end_label = NULL_RTX;
2387 thisloop->data.loop.continue_label = thisloop->data.loop.end_label;
2388 thisloop->exit_label = thisloop->data.loop.end_label;
2389 loop_stack = thisloop;
2390 nesting_stack = thisloop;
2392 return thisloop;
2395 /* Specify the continuation point for a loop started with
2396 expand_start_loop_continue_elsewhere.
2397 Use this at the point in the code to which a continue statement
2398 should jump. */
2400 void
2401 expand_loop_continue_here ()
2403 do_pending_stack_adjust ();
2404 emit_note (NULL, NOTE_INSN_LOOP_CONT);
2405 emit_label (loop_stack->data.loop.continue_label);
2408 /* Finish a loop. Generate a jump back to the top and the loop-exit label.
2409 Pop the block off of loop_stack. */
2411 void
2412 expand_end_loop ()
2414 rtx start_label = loop_stack->data.loop.start_label;
2415 rtx insn = get_last_insn ();
2416 int needs_end_jump = 1;
2418 /* Mark the continue-point at the top of the loop if none elsewhere. */
2419 if (start_label == loop_stack->data.loop.continue_label)
2420 emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
2422 do_pending_stack_adjust ();
2424 /* If optimizing, perhaps reorder the loop.
2425 First, try to use a condjump near the end.
2426 expand_exit_loop_if_false ends loops with unconditional jumps,
2427 like this:
2429 if (test) goto label;
2430 optional: cleanup
2431 goto loop_stack->data.loop.end_label
2432 barrier
2433 label:
2435 If we find such a pattern, we can end the loop earlier. */
2437 if (optimize
2438 && GET_CODE (insn) == CODE_LABEL
2439 && LABEL_NAME (insn) == NULL
2440 && GET_CODE (PREV_INSN (insn)) == BARRIER)
2442 rtx label = insn;
2443 rtx jump = PREV_INSN (PREV_INSN (label));
2445 if (GET_CODE (jump) == JUMP_INSN
2446 && GET_CODE (PATTERN (jump)) == SET
2447 && SET_DEST (PATTERN (jump)) == pc_rtx
2448 && GET_CODE (SET_SRC (PATTERN (jump))) == LABEL_REF
2449 && (XEXP (SET_SRC (PATTERN (jump)), 0)
2450 == loop_stack->data.loop.end_label))
2452 rtx prev;
2454 /* The test might be complex and reference LABEL multiple times,
2455 like the loop in loop_iterations to set vtop. To handle this,
2456 we move LABEL. */
2457 insn = PREV_INSN (label);
2458 reorder_insns (label, label, start_label);
2460 for (prev = PREV_INSN (jump);; prev = PREV_INSN (prev))
2462 /* We ignore line number notes, but if we see any other note,
2463 in particular NOTE_INSN_BLOCK_*, NOTE_INSN_EH_REGION_*,
2464 NOTE_INSN_LOOP_*, we disable this optimization. */
2465 if (GET_CODE (prev) == NOTE)
2467 if (NOTE_LINE_NUMBER (prev) < 0)
2468 break;
2469 continue;
2471 if (GET_CODE (prev) == CODE_LABEL)
2472 break;
2473 if (GET_CODE (prev) == JUMP_INSN)
2475 if (GET_CODE (PATTERN (prev)) == SET
2476 && SET_DEST (PATTERN (prev)) == pc_rtx
2477 && GET_CODE (SET_SRC (PATTERN (prev))) == IF_THEN_ELSE
2478 && (GET_CODE (XEXP (SET_SRC (PATTERN (prev)), 1))
2479 == LABEL_REF)
2480 && XEXP (XEXP (SET_SRC (PATTERN (prev)), 1), 0) == label)
2482 XEXP (XEXP (SET_SRC (PATTERN (prev)), 1), 0)
2483 = start_label;
2484 emit_note_after (NOTE_INSN_LOOP_END, prev);
2485 needs_end_jump = 0;
2487 break;
2493 /* If the loop starts with a loop exit, roll that to the end where
2494 it will optimize together with the jump back.
2496 We look for the conditional branch to the exit, except that once
2497 we find such a branch, we don't look past 30 instructions.
2499 In more detail, if the loop presently looks like this (in pseudo-C):
2501 start_label:
2502 if (test) goto end_label;
2503 body;
2504 goto start_label;
2505 end_label:
2507 transform it to look like:
2509 goto start_label;
2510 newstart_label:
2511 body;
2512 start_label:
2513 if (test) goto end_label;
2514 goto newstart_label;
2515 end_label:
2517 Here, the `test' may actually consist of some reasonably complex
2518 code, terminating in a test. */
2520 if (optimize
2521 && needs_end_jump
2523 ! (GET_CODE (insn) == JUMP_INSN
2524 && GET_CODE (PATTERN (insn)) == SET
2525 && SET_DEST (PATTERN (insn)) == pc_rtx
2526 && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE))
2528 int eh_regions = 0;
2529 int num_insns = 0;
2530 rtx last_test_insn = NULL_RTX;
2532 /* Scan insns from the top of the loop looking for a qualified
2533 conditional exit. */
2534 for (insn = NEXT_INSN (loop_stack->data.loop.start_label); insn;
2535 insn = NEXT_INSN (insn))
2537 if (GET_CODE (insn) == NOTE)
2539 if (optimize < 2
2540 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2541 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2542 /* The code that actually moves the exit test will
2543 carefully leave BLOCK notes in their original
2544 location. That means, however, that we can't debug
2545 the exit test itself. So, we refuse to move code
2546 containing BLOCK notes at low optimization levels. */
2547 break;
2549 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_BEG)
2550 ++eh_regions;
2551 else if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EH_REGION_END)
2553 --eh_regions;
2554 if (eh_regions < 0)
2555 /* We've come to the end of an EH region, but
2556 never saw the beginning of that region. That
2557 means that an EH region begins before the top
2558 of the loop, and ends in the middle of it. The
2559 existence of such a situation violates a basic
2560 assumption in this code, since that would imply
2561 that even when EH_REGIONS is zero, we might
2562 move code out of an exception region. */
2563 abort ();
2566 /* We must not walk into a nested loop. */
2567 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG)
2568 break;
2570 /* We already know this INSN is a NOTE, so there's no
2571 point in looking at it to see if it's a JUMP. */
2572 continue;
2575 if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == INSN)
2576 num_insns++;
2578 if (last_test_insn && num_insns > 30)
2579 break;
2581 if (eh_regions > 0)
2582 /* We don't want to move a partial EH region. Consider:
2584 while ( ( { try {
2585 if (cond ()) 0;
2586 else {
2587 bar();
2590 } catch (...) {
2592 } )) {
2593 body;
2596 This isn't legal C++, but here's what it's supposed to
2597 mean: if cond() is true, stop looping. Otherwise,
2598 call bar, and keep looping. In addition, if cond
2599 throws an exception, catch it and keep looping. Such
2600 constructs are certainy legal in LISP.
2602 We should not move the `if (cond()) 0' test since then
2603 the EH-region for the try-block would be broken up.
2604 (In this case we would the EH_BEG note for the `try'
2605 and `if cond()' but not the call to bar() or the
2606 EH_END note.)
2608 So we don't look for tests within an EH region. */
2609 continue;
2611 if (GET_CODE (insn) == JUMP_INSN
2612 && GET_CODE (PATTERN (insn)) == SET
2613 && SET_DEST (PATTERN (insn)) == pc_rtx)
2615 /* This is indeed a jump. */
2616 rtx dest1 = NULL_RTX;
2617 rtx dest2 = NULL_RTX;
2618 rtx potential_last_test;
2619 if (GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE)
2621 /* A conditional jump. */
2622 dest1 = XEXP (SET_SRC (PATTERN (insn)), 1);
2623 dest2 = XEXP (SET_SRC (PATTERN (insn)), 2);
2624 potential_last_test = insn;
2626 else
2628 /* An unconditional jump. */
2629 dest1 = SET_SRC (PATTERN (insn));
2630 /* Include the BARRIER after the JUMP. */
2631 potential_last_test = NEXT_INSN (insn);
2634 do {
2635 if (dest1 && GET_CODE (dest1) == LABEL_REF
2636 && ((XEXP (dest1, 0)
2637 == loop_stack->data.loop.alt_end_label)
2638 || (XEXP (dest1, 0)
2639 == loop_stack->data.loop.end_label)))
2641 last_test_insn = potential_last_test;
2642 break;
2645 /* If this was a conditional jump, there may be
2646 another label at which we should look. */
2647 dest1 = dest2;
2648 dest2 = NULL_RTX;
2649 } while (dest1);
2653 if (last_test_insn != 0 && last_test_insn != get_last_insn ())
2655 /* We found one. Move everything from there up
2656 to the end of the loop, and add a jump into the loop
2657 to jump to there. */
2658 register rtx newstart_label = gen_label_rtx ();
2659 register rtx start_move = start_label;
2660 rtx next_insn;
2662 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
2663 then we want to move this note also. */
2664 if (GET_CODE (PREV_INSN (start_move)) == NOTE
2665 && (NOTE_LINE_NUMBER (PREV_INSN (start_move))
2666 == NOTE_INSN_LOOP_CONT))
2667 start_move = PREV_INSN (start_move);
2669 emit_label_after (newstart_label, PREV_INSN (start_move));
2671 /* Actually move the insns. Start at the beginning, and
2672 keep copying insns until we've copied the
2673 last_test_insn. */
2674 for (insn = start_move; insn; insn = next_insn)
2676 /* Figure out which insn comes after this one. We have
2677 to do this before we move INSN. */
2678 if (insn == last_test_insn)
2679 /* We've moved all the insns. */
2680 next_insn = NULL_RTX;
2681 else
2682 next_insn = NEXT_INSN (insn);
2684 if (GET_CODE (insn) == NOTE
2685 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
2686 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
2687 /* We don't want to move NOTE_INSN_BLOCK_BEGs or
2688 NOTE_INSN_BLOCK_ENDs because the correct generation
2689 of debugging information depends on these appearing
2690 in the same order in the RTL and in the tree
2691 structure, where they are represented as BLOCKs.
2692 So, we don't move block notes. Of course, moving
2693 the code inside the block is likely to make it
2694 impossible to debug the instructions in the exit
2695 test, but such is the price of optimization. */
2696 continue;
2698 /* Move the INSN. */
2699 reorder_insns (insn, insn, get_last_insn ());
2702 emit_jump_insn_after (gen_jump (start_label),
2703 PREV_INSN (newstart_label));
2704 emit_barrier_after (PREV_INSN (newstart_label));
2705 start_label = newstart_label;
2709 if (needs_end_jump)
2711 emit_jump (start_label);
2712 emit_note (NULL, NOTE_INSN_LOOP_END);
2714 emit_label (loop_stack->data.loop.end_label);
2716 POPSTACK (loop_stack);
2718 last_expr_type = 0;
2721 /* Finish a null loop, aka do { } while (0). */
2723 void
2724 expand_end_null_loop ()
2726 do_pending_stack_adjust ();
2727 emit_label (loop_stack->data.loop.end_label);
2729 POPSTACK (loop_stack);
2731 last_expr_type = 0;
2734 /* Generate a jump to the current loop's continue-point.
2735 This is usually the top of the loop, but may be specified
2736 explicitly elsewhere. If not currently inside a loop,
2737 return 0 and do nothing; caller will print an error message. */
2740 expand_continue_loop (whichloop)
2741 struct nesting *whichloop;
2743 last_expr_type = 0;
2744 if (whichloop == 0)
2745 whichloop = loop_stack;
2746 if (whichloop == 0)
2747 return 0;
2748 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
2749 NULL_RTX);
2750 return 1;
2753 /* Generate a jump to exit the current loop. If not currently inside a loop,
2754 return 0 and do nothing; caller will print an error message. */
2757 expand_exit_loop (whichloop)
2758 struct nesting *whichloop;
2760 last_expr_type = 0;
2761 if (whichloop == 0)
2762 whichloop = loop_stack;
2763 if (whichloop == 0)
2764 return 0;
2765 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
2766 return 1;
2769 /* Generate a conditional jump to exit the current loop if COND
2770 evaluates to zero. If not currently inside a loop,
2771 return 0 and do nothing; caller will print an error message. */
2774 expand_exit_loop_if_false (whichloop, cond)
2775 struct nesting *whichloop;
2776 tree cond;
2778 rtx label = gen_label_rtx ();
2779 rtx last_insn;
2780 last_expr_type = 0;
2782 if (whichloop == 0)
2783 whichloop = loop_stack;
2784 if (whichloop == 0)
2785 return 0;
2786 /* In order to handle fixups, we actually create a conditional jump
2787 around a unconditional branch to exit the loop. If fixups are
2788 necessary, they go before the unconditional branch. */
2790 do_jump (cond, NULL_RTX, label);
2791 last_insn = get_last_insn ();
2792 if (GET_CODE (last_insn) == CODE_LABEL)
2793 whichloop->data.loop.alt_end_label = last_insn;
2794 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label,
2795 NULL_RTX);
2796 emit_label (label);
2798 return 1;
2801 /* Return nonzero if the loop nest is empty. Else return zero. */
2804 stmt_loop_nest_empty ()
2806 /* cfun->stmt can be NULL if we are building a call to get the
2807 EH context for a setjmp/longjmp EH target and the current
2808 function was a deferred inline function. */
2809 return (cfun->stmt == NULL || loop_stack == NULL);
2812 /* Return non-zero if we should preserve sub-expressions as separate
2813 pseudos. We never do so if we aren't optimizing. We always do so
2814 if -fexpensive-optimizations.
2816 Otherwise, we only do so if we are in the "early" part of a loop. I.e.,
2817 the loop may still be a small one. */
2820 preserve_subexpressions_p ()
2822 rtx insn;
2824 if (flag_expensive_optimizations)
2825 return 1;
2827 if (optimize == 0 || cfun == 0 || cfun->stmt == 0 || loop_stack == 0)
2828 return 0;
2830 insn = get_last_insn_anywhere ();
2832 return (insn
2833 && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2834 < n_non_fixed_regs * 3));
2838 /* Generate a jump to exit the current loop, conditional, binding contour
2839 or case statement. Not all such constructs are visible to this function,
2840 only those started with EXIT_FLAG nonzero. Individual languages use
2841 the EXIT_FLAG parameter to control which kinds of constructs you can
2842 exit this way.
2844 If not currently inside anything that can be exited,
2845 return 0 and do nothing; caller will print an error message. */
2848 expand_exit_something ()
2850 struct nesting *n;
2851 last_expr_type = 0;
2852 for (n = nesting_stack; n; n = n->all)
2853 if (n->exit_label != 0)
2855 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
2856 return 1;
2859 return 0;
2862 /* Generate RTL to return from the current function, with no value.
2863 (That is, we do not do anything about returning any value.) */
2865 void
2866 expand_null_return ()
2868 rtx last_insn = get_last_insn ();
2870 /* If this function was declared to return a value, but we
2871 didn't, clobber the return registers so that they are not
2872 propogated live to the rest of the function. */
2873 clobber_return_register ();
2875 expand_null_return_1 (last_insn);
2878 /* Generate RTL to return from the current function, with value VAL. */
2880 static void
2881 expand_value_return (val)
2882 rtx val;
2884 rtx last_insn = get_last_insn ();
2885 rtx return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
2887 /* Copy the value to the return location
2888 unless it's already there. */
2890 if (return_reg != val)
2892 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
2893 #ifdef PROMOTE_FUNCTION_RETURN
2894 int unsignedp = TREE_UNSIGNED (type);
2895 enum machine_mode old_mode
2896 = DECL_MODE (DECL_RESULT (current_function_decl));
2897 enum machine_mode mode
2898 = promote_mode (type, old_mode, &unsignedp, 1);
2900 if (mode != old_mode)
2901 val = convert_modes (mode, old_mode, val, unsignedp);
2902 #endif
2903 if (GET_CODE (return_reg) == PARALLEL)
2904 emit_group_load (return_reg, val, int_size_in_bytes (type),
2905 TYPE_ALIGN (type));
2906 else
2907 emit_move_insn (return_reg, val);
2910 expand_null_return_1 (last_insn);
2913 /* Output a return with no value. If LAST_INSN is nonzero,
2914 pretend that the return takes place after LAST_INSN. */
2916 static void
2917 expand_null_return_1 (last_insn)
2918 rtx last_insn;
2920 rtx end_label = cleanup_label ? cleanup_label : return_label;
2922 clear_pending_stack_adjust ();
2923 do_pending_stack_adjust ();
2924 last_expr_type = 0;
2926 if (end_label == 0)
2927 end_label = return_label = gen_label_rtx ();
2928 expand_goto_internal (NULL_TREE, end_label, last_insn);
2931 /* Generate RTL to evaluate the expression RETVAL and return it
2932 from the current function. */
2934 void
2935 expand_return (retval)
2936 tree retval;
2938 /* If there are any cleanups to be performed, then they will
2939 be inserted following LAST_INSN. It is desirable
2940 that the last_insn, for such purposes, should be the
2941 last insn before computing the return value. Otherwise, cleanups
2942 which call functions can clobber the return value. */
2943 /* ??? rms: I think that is erroneous, because in C++ it would
2944 run destructors on variables that might be used in the subsequent
2945 computation of the return value. */
2946 rtx last_insn = 0;
2947 rtx result_rtl;
2948 register rtx val = 0;
2949 tree retval_rhs;
2951 /* If function wants no value, give it none. */
2952 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2954 expand_expr (retval, NULL_RTX, VOIDmode, 0);
2955 emit_queue ();
2956 expand_null_return ();
2957 return;
2960 if (retval == error_mark_node)
2962 /* Treat this like a return of no value from a function that
2963 returns a value. */
2964 expand_null_return ();
2965 return;
2967 else if (TREE_CODE (retval) == RESULT_DECL)
2968 retval_rhs = retval;
2969 else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
2970 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
2971 retval_rhs = TREE_OPERAND (retval, 1);
2972 else if (VOID_TYPE_P (TREE_TYPE (retval)))
2973 /* Recognize tail-recursive call to void function. */
2974 retval_rhs = retval;
2975 else
2976 retval_rhs = NULL_TREE;
2978 last_insn = get_last_insn ();
2980 /* Distribute return down conditional expr if either of the sides
2981 may involve tail recursion (see test below). This enhances the number
2982 of tail recursions we see. Don't do this always since it can produce
2983 sub-optimal code in some cases and we distribute assignments into
2984 conditional expressions when it would help. */
2986 if (optimize && retval_rhs != 0
2987 && frame_offset == 0
2988 && TREE_CODE (retval_rhs) == COND_EXPR
2989 && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
2990 || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
2992 rtx label = gen_label_rtx ();
2993 tree expr;
2995 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
2996 start_cleanup_deferral ();
2997 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
2998 DECL_RESULT (current_function_decl),
2999 TREE_OPERAND (retval_rhs, 1));
3000 TREE_SIDE_EFFECTS (expr) = 1;
3001 expand_return (expr);
3002 emit_label (label);
3004 expr = build (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (current_function_decl)),
3005 DECL_RESULT (current_function_decl),
3006 TREE_OPERAND (retval_rhs, 2));
3007 TREE_SIDE_EFFECTS (expr) = 1;
3008 expand_return (expr);
3009 end_cleanup_deferral ();
3010 return;
3013 result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
3015 /* If the result is an aggregate that is being returned in one (or more)
3016 registers, load the registers here. The compiler currently can't handle
3017 copying a BLKmode value into registers. We could put this code in a
3018 more general area (for use by everyone instead of just function
3019 call/return), but until this feature is generally usable it is kept here
3020 (and in expand_call). The value must go into a pseudo in case there
3021 are cleanups that will clobber the real return register. */
3023 if (retval_rhs != 0
3024 && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
3025 && GET_CODE (result_rtl) == REG)
3027 int i;
3028 unsigned HOST_WIDE_INT bitpos, xbitpos;
3029 unsigned HOST_WIDE_INT big_endian_correction = 0;
3030 unsigned HOST_WIDE_INT bytes
3031 = int_size_in_bytes (TREE_TYPE (retval_rhs));
3032 int n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3033 unsigned int bitsize
3034 = MIN (TYPE_ALIGN (TREE_TYPE (retval_rhs)), BITS_PER_WORD);
3035 rtx *result_pseudos = (rtx *) alloca (sizeof (rtx) * n_regs);
3036 rtx result_reg, src = NULL_RTX, dst = NULL_RTX;
3037 rtx result_val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
3038 enum machine_mode tmpmode, result_reg_mode;
3040 if (bytes == 0)
3042 expand_null_return ();
3043 return;
3046 /* Structures whose size is not a multiple of a word are aligned
3047 to the least significant byte (to the right). On a BYTES_BIG_ENDIAN
3048 machine, this means we must skip the empty high order bytes when
3049 calculating the bit offset. */
3050 if (BYTES_BIG_ENDIAN && bytes % UNITS_PER_WORD)
3051 big_endian_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
3052 * BITS_PER_UNIT));
3054 /* Copy the structure BITSIZE bits at a time. */
3055 for (bitpos = 0, xbitpos = big_endian_correction;
3056 bitpos < bytes * BITS_PER_UNIT;
3057 bitpos += bitsize, xbitpos += bitsize)
3059 /* We need a new destination pseudo each time xbitpos is
3060 on a word boundary and when xbitpos == big_endian_correction
3061 (the first time through). */
3062 if (xbitpos % BITS_PER_WORD == 0
3063 || xbitpos == big_endian_correction)
3065 /* Generate an appropriate register. */
3066 dst = gen_reg_rtx (word_mode);
3067 result_pseudos[xbitpos / BITS_PER_WORD] = dst;
3069 /* Clobber the destination before we move anything into it. */
3070 emit_insn (gen_rtx_CLOBBER (VOIDmode, dst));
3073 /* We need a new source operand each time bitpos is on a word
3074 boundary. */
3075 if (bitpos % BITS_PER_WORD == 0)
3076 src = operand_subword_force (result_val,
3077 bitpos / BITS_PER_WORD,
3078 BLKmode);
3080 /* Use bitpos for the source extraction (left justified) and
3081 xbitpos for the destination store (right justified). */
3082 store_bit_field (dst, bitsize, xbitpos % BITS_PER_WORD, word_mode,
3083 extract_bit_field (src, bitsize,
3084 bitpos % BITS_PER_WORD, 1,
3085 NULL_RTX, word_mode, word_mode,
3086 bitsize, BITS_PER_WORD),
3087 bitsize, BITS_PER_WORD);
3090 /* Find the smallest integer mode large enough to hold the
3091 entire structure and use that mode instead of BLKmode
3092 on the USE insn for the return register. */
3093 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3094 tmpmode != VOIDmode;
3095 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
3096 /* Have we found a large enough mode? */
3097 if (GET_MODE_SIZE (tmpmode) >= bytes)
3098 break;
3100 /* No suitable mode found. */
3101 if (tmpmode == VOIDmode)
3102 abort ();
3104 PUT_MODE (result_rtl, tmpmode);
3106 if (GET_MODE_SIZE (tmpmode) < GET_MODE_SIZE (word_mode))
3107 result_reg_mode = word_mode;
3108 else
3109 result_reg_mode = tmpmode;
3110 result_reg = gen_reg_rtx (result_reg_mode);
3112 emit_queue ();
3113 for (i = 0; i < n_regs; i++)
3114 emit_move_insn (operand_subword (result_reg, i, 0, result_reg_mode),
3115 result_pseudos[i]);
3117 if (tmpmode != result_reg_mode)
3118 result_reg = gen_lowpart (tmpmode, result_reg);
3120 expand_value_return (result_reg);
3122 else if (retval_rhs != 0
3123 && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
3124 && (GET_CODE (result_rtl) == REG
3125 || (GET_CODE (result_rtl) == PARALLEL)))
3127 /* Calculate the return value into a temporary (usually a pseudo
3128 reg). */
3129 tree ot = TREE_TYPE (DECL_RESULT (current_function_decl));
3130 tree nt = build_qualified_type (ot, TYPE_QUALS (ot) | TYPE_QUAL_CONST);
3132 val = assign_temp (nt, 0, 0, 1);
3133 val = expand_expr (retval_rhs, val, GET_MODE (val), 0);
3134 val = force_not_mem (val);
3135 emit_queue ();
3136 /* Return the calculated value, doing cleanups first. */
3137 expand_value_return (val);
3139 else
3141 /* No cleanups or no hard reg used;
3142 calculate value into hard return reg. */
3143 expand_expr (retval, const0_rtx, VOIDmode, 0);
3144 emit_queue ();
3145 expand_value_return (result_rtl);
3149 /* Return 1 if the end of the generated RTX is not a barrier.
3150 This means code already compiled can drop through. */
3153 drop_through_at_end_p ()
3155 rtx insn = get_last_insn ();
3156 while (insn && GET_CODE (insn) == NOTE)
3157 insn = PREV_INSN (insn);
3158 return insn && GET_CODE (insn) != BARRIER;
3161 /* Attempt to optimize a potential tail recursion call into a goto.
3162 ARGUMENTS are the arguments to a CALL_EXPR; LAST_INSN indicates
3163 where to place the jump to the tail recursion label.
3165 Return TRUE if the call was optimized into a goto. */
3168 optimize_tail_recursion (arguments, last_insn)
3169 tree arguments;
3170 rtx last_insn;
3172 /* Finish checking validity, and if valid emit code to set the
3173 argument variables for the new call. */
3174 if (tail_recursion_args (arguments, DECL_ARGUMENTS (current_function_decl)))
3176 if (tail_recursion_label == 0)
3178 tail_recursion_label = gen_label_rtx ();
3179 emit_label_after (tail_recursion_label,
3180 tail_recursion_reentry);
3182 emit_queue ();
3183 expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
3184 emit_barrier ();
3185 return 1;
3187 return 0;
3190 /* Emit code to alter this function's formal parms for a tail-recursive call.
3191 ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
3192 FORMALS is the chain of decls of formals.
3193 Return 1 if this can be done;
3194 otherwise return 0 and do not emit any code. */
3196 static int
3197 tail_recursion_args (actuals, formals)
3198 tree actuals, formals;
3200 register tree a = actuals, f = formals;
3201 register int i;
3202 register rtx *argvec;
3204 /* Check that number and types of actuals are compatible
3205 with the formals. This is not always true in valid C code.
3206 Also check that no formal needs to be addressable
3207 and that all formals are scalars. */
3209 /* Also count the args. */
3211 for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
3213 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (a)))
3214 != TYPE_MAIN_VARIANT (TREE_TYPE (f)))
3215 return 0;
3216 if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
3217 return 0;
3219 if (a != 0 || f != 0)
3220 return 0;
3222 /* Compute all the actuals. */
3224 argvec = (rtx *) alloca (i * sizeof (rtx));
3226 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3227 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
3229 /* Find which actual values refer to current values of previous formals.
3230 Copy each of them now, before any formal is changed. */
3232 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
3234 int copy = 0;
3235 register int j;
3236 for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
3237 if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
3239 copy = 1;
3240 break;
3242 if (copy)
3243 argvec[i] = copy_to_reg (argvec[i]);
3246 /* Store the values of the actuals into the formals. */
3248 for (f = formals, a = actuals, i = 0; f;
3249 f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
3251 if (GET_MODE (DECL_RTL (f)) == GET_MODE (argvec[i]))
3252 emit_move_insn (DECL_RTL (f), argvec[i]);
3253 else
3254 convert_move (DECL_RTL (f), argvec[i],
3255 TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a))));
3258 free_temp_slots ();
3259 return 1;
3262 /* Generate the RTL code for entering a binding contour.
3263 The variables are declared one by one, by calls to `expand_decl'.
3265 FLAGS is a bitwise or of the following flags:
3267 1 - Nonzero if this construct should be visible to
3268 `exit_something'.
3270 2 - Nonzero if this contour does not require a
3271 NOTE_INSN_BLOCK_BEG note. Virtually all calls from
3272 language-independent code should set this flag because they
3273 will not create corresponding BLOCK nodes. (There should be
3274 a one-to-one correspondence between NOTE_INSN_BLOCK_BEG notes
3275 and BLOCKs.) If this flag is set, MARK_ENDS should be zero
3276 when expand_end_bindings is called.
3278 If we are creating a NOTE_INSN_BLOCK_BEG note, a BLOCK may
3279 optionally be supplied. If so, it becomes the NOTE_BLOCK for the
3280 note. */
3282 void
3283 expand_start_bindings_and_block (flags, block)
3284 int flags;
3285 tree block;
3287 struct nesting *thisblock = ALLOC_NESTING ();
3288 rtx note;
3289 int exit_flag = ((flags & 1) != 0);
3290 int block_flag = ((flags & 2) == 0);
3292 /* If a BLOCK is supplied, then the caller should be requesting a
3293 NOTE_INSN_BLOCK_BEG note. */
3294 if (!block_flag && block)
3295 abort ();
3297 /* Create a note to mark the beginning of the block. */
3298 if (block_flag)
3300 note = emit_note (NULL, NOTE_INSN_BLOCK_BEG);
3301 NOTE_BLOCK (note) = block;
3303 else
3304 note = emit_note (NULL, NOTE_INSN_DELETED);
3306 /* Make an entry on block_stack for the block we are entering. */
3308 thisblock->next = block_stack;
3309 thisblock->all = nesting_stack;
3310 thisblock->depth = ++nesting_depth;
3311 thisblock->data.block.stack_level = 0;
3312 thisblock->data.block.cleanups = 0;
3313 thisblock->data.block.n_function_calls = 0;
3314 thisblock->data.block.exception_region = 0;
3315 thisblock->data.block.block_target_temp_slot_level = target_temp_slot_level;
3317 thisblock->data.block.conditional_code = 0;
3318 thisblock->data.block.last_unconditional_cleanup = note;
3319 /* When we insert instructions after the last unconditional cleanup,
3320 we don't adjust last_insn. That means that a later add_insn will
3321 clobber the instructions we've just added. The easiest way to
3322 fix this is to just insert another instruction here, so that the
3323 instructions inserted after the last unconditional cleanup are
3324 never the last instruction. */
3325 emit_note (NULL, NOTE_INSN_DELETED);
3326 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
3328 if (block_stack
3329 && !(block_stack->data.block.cleanups == NULL_TREE
3330 && block_stack->data.block.outer_cleanups == NULL_TREE))
3331 thisblock->data.block.outer_cleanups
3332 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
3333 block_stack->data.block.outer_cleanups);
3334 else
3335 thisblock->data.block.outer_cleanups = 0;
3336 thisblock->data.block.label_chain = 0;
3337 thisblock->data.block.innermost_stack_block = stack_block_stack;
3338 thisblock->data.block.first_insn = note;
3339 thisblock->data.block.block_start_count = ++current_block_start_count;
3340 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
3341 block_stack = thisblock;
3342 nesting_stack = thisblock;
3344 /* Make a new level for allocating stack slots. */
3345 push_temp_slots ();
3348 /* Specify the scope of temporaries created by TARGET_EXPRs. Similar
3349 to CLEANUP_POINT_EXPR, but handles cases when a series of calls to
3350 expand_expr are made. After we end the region, we know that all
3351 space for all temporaries that were created by TARGET_EXPRs will be
3352 destroyed and their space freed for reuse. */
3354 void
3355 expand_start_target_temps ()
3357 /* This is so that even if the result is preserved, the space
3358 allocated will be freed, as we know that it is no longer in use. */
3359 push_temp_slots ();
3361 /* Start a new binding layer that will keep track of all cleanup
3362 actions to be performed. */
3363 expand_start_bindings (2);
3365 target_temp_slot_level = temp_slot_level;
3368 void
3369 expand_end_target_temps ()
3371 expand_end_bindings (NULL_TREE, 0, 0);
3373 /* This is so that even if the result is preserved, the space
3374 allocated will be freed, as we know that it is no longer in use. */
3375 pop_temp_slots ();
3378 /* Given a pointer to a BLOCK node return non-zero if (and only if) the node
3379 in question represents the outermost pair of curly braces (i.e. the "body
3380 block") of a function or method.
3382 For any BLOCK node representing a "body block" of a function or method, the
3383 BLOCK_SUPERCONTEXT of the node will point to another BLOCK node which
3384 represents the outermost (function) scope for the function or method (i.e.
3385 the one which includes the formal parameters). The BLOCK_SUPERCONTEXT of
3386 *that* node in turn will point to the relevant FUNCTION_DECL node. */
3389 is_body_block (stmt)
3390 register tree stmt;
3392 if (TREE_CODE (stmt) == BLOCK)
3394 tree parent = BLOCK_SUPERCONTEXT (stmt);
3396 if (parent && TREE_CODE (parent) == BLOCK)
3398 tree grandparent = BLOCK_SUPERCONTEXT (parent);
3400 if (grandparent && TREE_CODE (grandparent) == FUNCTION_DECL)
3401 return 1;
3405 return 0;
3408 /* True if we are currently emitting insns in an area of output code
3409 that is controlled by a conditional expression. This is used by
3410 the cleanup handling code to generate conditional cleanup actions. */
3413 conditional_context ()
3415 return block_stack && block_stack->data.block.conditional_code;
3418 /* Return an opaque pointer to the current nesting level, so frontend code
3419 can check its own sanity. */
3421 struct nesting *
3422 current_nesting_level ()
3424 return cfun ? block_stack : 0;
3427 /* Emit a handler label for a nonlocal goto handler.
3428 Also emit code to store the handler label in SLOT before BEFORE_INSN. */
3430 static rtx
3431 expand_nl_handler_label (slot, before_insn)
3432 rtx slot, before_insn;
3434 rtx insns;
3435 rtx handler_label = gen_label_rtx ();
3437 /* Don't let cleanup_cfg delete the handler. */
3438 LABEL_PRESERVE_P (handler_label) = 1;
3440 start_sequence ();
3441 emit_move_insn (slot, gen_rtx_LABEL_REF (Pmode, handler_label));
3442 insns = get_insns ();
3443 end_sequence ();
3444 emit_insns_before (insns, before_insn);
3446 emit_label (handler_label);
3448 return handler_label;
3451 /* Emit code to restore vital registers at the beginning of a nonlocal goto
3452 handler. */
3453 static void
3454 expand_nl_goto_receiver ()
3456 #ifdef HAVE_nonlocal_goto
3457 if (! HAVE_nonlocal_goto)
3458 #endif
3459 /* First adjust our frame pointer to its actual value. It was
3460 previously set to the start of the virtual area corresponding to
3461 the stacked variables when we branched here and now needs to be
3462 adjusted to the actual hardware fp value.
3464 Assignments are to virtual registers are converted by
3465 instantiate_virtual_regs into the corresponding assignment
3466 to the underlying register (fp in this case) that makes
3467 the original assignment true.
3468 So the following insn will actually be
3469 decrementing fp by STARTING_FRAME_OFFSET. */
3470 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
3472 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
3473 if (fixed_regs[ARG_POINTER_REGNUM])
3475 #ifdef ELIMINABLE_REGS
3476 /* If the argument pointer can be eliminated in favor of the
3477 frame pointer, we don't need to restore it. We assume here
3478 that if such an elimination is present, it can always be used.
3479 This is the case on all known machines; if we don't make this
3480 assumption, we do unnecessary saving on many machines. */
3481 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
3482 size_t i;
3484 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
3485 if (elim_regs[i].from == ARG_POINTER_REGNUM
3486 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
3487 break;
3489 if (i == ARRAY_SIZE (elim_regs))
3490 #endif
3492 /* Now restore our arg pointer from the address at which it
3493 was saved in our stack frame. */
3494 emit_move_insn (virtual_incoming_args_rtx,
3495 copy_to_reg (get_arg_pointer_save_area (cfun)));
3498 #endif
3500 #ifdef HAVE_nonlocal_goto_receiver
3501 if (HAVE_nonlocal_goto_receiver)
3502 emit_insn (gen_nonlocal_goto_receiver ());
3503 #endif
3506 /* Make handlers for nonlocal gotos taking place in the function calls in
3507 block THISBLOCK. */
3509 static void
3510 expand_nl_goto_receivers (thisblock)
3511 struct nesting *thisblock;
3513 tree link;
3514 rtx afterward = gen_label_rtx ();
3515 rtx insns, slot;
3516 rtx label_list;
3517 int any_invalid;
3519 /* Record the handler address in the stack slot for that purpose,
3520 during this block, saving and restoring the outer value. */
3521 if (thisblock->next != 0)
3522 for (slot = nonlocal_goto_handler_slots; slot; slot = XEXP (slot, 1))
3524 rtx save_receiver = gen_reg_rtx (Pmode);
3525 emit_move_insn (XEXP (slot, 0), save_receiver);
3527 start_sequence ();
3528 emit_move_insn (save_receiver, XEXP (slot, 0));
3529 insns = get_insns ();
3530 end_sequence ();
3531 emit_insns_before (insns, thisblock->data.block.first_insn);
3534 /* Jump around the handlers; they run only when specially invoked. */
3535 emit_jump (afterward);
3537 /* Make a separate handler for each label. */
3538 link = nonlocal_labels;
3539 slot = nonlocal_goto_handler_slots;
3540 label_list = NULL_RTX;
3541 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3542 /* Skip any labels we shouldn't be able to jump to from here,
3543 we generate one special handler for all of them below which just calls
3544 abort. */
3545 if (! DECL_TOO_LATE (TREE_VALUE (link)))
3547 rtx lab;
3548 lab = expand_nl_handler_label (XEXP (slot, 0),
3549 thisblock->data.block.first_insn);
3550 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3552 expand_nl_goto_receiver ();
3554 /* Jump to the "real" nonlocal label. */
3555 expand_goto (TREE_VALUE (link));
3558 /* A second pass over all nonlocal labels; this time we handle those
3559 we should not be able to jump to at this point. */
3560 link = nonlocal_labels;
3561 slot = nonlocal_goto_handler_slots;
3562 any_invalid = 0;
3563 for (; link; link = TREE_CHAIN (link), slot = XEXP (slot, 1))
3564 if (DECL_TOO_LATE (TREE_VALUE (link)))
3566 rtx lab;
3567 lab = expand_nl_handler_label (XEXP (slot, 0),
3568 thisblock->data.block.first_insn);
3569 label_list = gen_rtx_EXPR_LIST (VOIDmode, lab, label_list);
3570 any_invalid = 1;
3573 if (any_invalid)
3575 expand_nl_goto_receiver ();
3576 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "abort"), 0,
3577 VOIDmode, 0);
3578 emit_barrier ();
3581 nonlocal_goto_handler_labels = label_list;
3582 emit_label (afterward);
3585 /* Warn about any unused VARS (which may contain nodes other than
3586 VAR_DECLs, but such nodes are ignored). The nodes are connected
3587 via the TREE_CHAIN field. */
3589 void
3590 warn_about_unused_variables (vars)
3591 tree vars;
3593 tree decl;
3595 if (warn_unused_variable)
3596 for (decl = vars; decl; decl = TREE_CHAIN (decl))
3597 if (TREE_CODE (decl) == VAR_DECL
3598 && ! TREE_USED (decl)
3599 && ! DECL_IN_SYSTEM_HEADER (decl)
3600 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
3601 warning_with_decl (decl, "unused variable `%s'");
3604 /* Generate RTL code to terminate a binding contour.
3606 VARS is the chain of VAR_DECL nodes for the variables bound in this
3607 contour. There may actually be other nodes in this chain, but any
3608 nodes other than VAR_DECLS are ignored.
3610 MARK_ENDS is nonzero if we should put a note at the beginning
3611 and end of this binding contour.
3613 DONT_JUMP_IN is nonzero if it is not valid to jump into this contour.
3614 (That is true automatically if the contour has a saved stack level.) */
3616 void
3617 expand_end_bindings (vars, mark_ends, dont_jump_in)
3618 tree vars;
3619 int mark_ends;
3620 int dont_jump_in;
3622 register struct nesting *thisblock = block_stack;
3624 /* If any of the variables in this scope were not used, warn the
3625 user. */
3626 warn_about_unused_variables (vars);
3628 if (thisblock->exit_label)
3630 do_pending_stack_adjust ();
3631 emit_label (thisblock->exit_label);
3634 /* If necessary, make handlers for nonlocal gotos taking
3635 place in the function calls in this block. */
3636 if (function_call_count != thisblock->data.block.n_function_calls
3637 && nonlocal_labels
3638 /* Make handler for outermost block
3639 if there were any nonlocal gotos to this function. */
3640 && (thisblock->next == 0 ? current_function_has_nonlocal_label
3641 /* Make handler for inner block if it has something
3642 special to do when you jump out of it. */
3643 : (thisblock->data.block.cleanups != 0
3644 || thisblock->data.block.stack_level != 0)))
3645 expand_nl_goto_receivers (thisblock);
3647 /* Don't allow jumping into a block that has a stack level.
3648 Cleanups are allowed, though. */
3649 if (dont_jump_in
3650 || thisblock->data.block.stack_level != 0)
3652 struct label_chain *chain;
3654 /* Any labels in this block are no longer valid to go to.
3655 Mark them to cause an error message. */
3656 for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
3658 DECL_TOO_LATE (chain->label) = 1;
3659 /* If any goto without a fixup came to this label,
3660 that must be an error, because gotos without fixups
3661 come from outside all saved stack-levels. */
3662 if (TREE_ADDRESSABLE (chain->label))
3663 error_with_decl (chain->label,
3664 "label `%s' used before containing binding contour");
3668 /* Restore stack level in effect before the block
3669 (only if variable-size objects allocated). */
3670 /* Perform any cleanups associated with the block. */
3672 if (thisblock->data.block.stack_level != 0
3673 || thisblock->data.block.cleanups != 0)
3675 int reachable;
3676 rtx insn;
3678 /* Don't let cleanups affect ({...}) constructs. */
3679 int old_expr_stmts_for_value = expr_stmts_for_value;
3680 rtx old_last_expr_value = last_expr_value;
3681 tree old_last_expr_type = last_expr_type;
3682 expr_stmts_for_value = 0;
3684 /* Only clean up here if this point can actually be reached. */
3685 insn = get_last_insn ();
3686 if (GET_CODE (insn) == NOTE)
3687 insn = prev_nonnote_insn (insn);
3688 reachable = (! insn || GET_CODE (insn) != BARRIER);
3690 /* Do the cleanups. */
3691 expand_cleanups (thisblock->data.block.cleanups, NULL_TREE, 0, reachable);
3692 if (reachable)
3693 do_pending_stack_adjust ();
3695 expr_stmts_for_value = old_expr_stmts_for_value;
3696 last_expr_value = old_last_expr_value;
3697 last_expr_type = old_last_expr_type;
3699 /* Restore the stack level. */
3701 if (reachable && thisblock->data.block.stack_level != 0)
3703 emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3704 thisblock->data.block.stack_level, NULL_RTX);
3705 if (nonlocal_goto_handler_slots != 0)
3706 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
3707 NULL_RTX);
3710 /* Any gotos out of this block must also do these things.
3711 Also report any gotos with fixups that came to labels in this
3712 level. */
3713 fixup_gotos (thisblock,
3714 thisblock->data.block.stack_level,
3715 thisblock->data.block.cleanups,
3716 thisblock->data.block.first_insn,
3717 dont_jump_in);
3720 /* Mark the beginning and end of the scope if requested.
3721 We do this now, after running cleanups on the variables
3722 just going out of scope, so they are in scope for their cleanups. */
3724 if (mark_ends)
3726 rtx note = emit_note (NULL, NOTE_INSN_BLOCK_END);
3727 NOTE_BLOCK (note) = NOTE_BLOCK (thisblock->data.block.first_insn);
3729 else
3730 /* Get rid of the beginning-mark if we don't make an end-mark. */
3731 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
3733 /* Restore the temporary level of TARGET_EXPRs. */
3734 target_temp_slot_level = thisblock->data.block.block_target_temp_slot_level;
3736 /* Restore block_stack level for containing block. */
3738 stack_block_stack = thisblock->data.block.innermost_stack_block;
3739 POPSTACK (block_stack);
3741 /* Pop the stack slot nesting and free any slots at this level. */
3742 pop_temp_slots ();
3745 /* Generate code to save the stack pointer at the start of the current block
3746 and set up to restore it on exit. */
3748 void
3749 save_stack_pointer ()
3751 struct nesting *thisblock = block_stack;
3753 if (thisblock->data.block.stack_level == 0)
3755 emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
3756 &thisblock->data.block.stack_level,
3757 thisblock->data.block.first_insn);
3758 stack_block_stack = thisblock;
3762 /* Generate RTL for the automatic variable declaration DECL.
3763 (Other kinds of declarations are simply ignored if seen here.) */
3765 void
3766 expand_decl (decl)
3767 register tree decl;
3769 struct nesting *thisblock;
3770 tree type;
3772 type = TREE_TYPE (decl);
3774 /* For a CONST_DECL, set mode, alignment, and sizes from those of the
3775 type in case this node is used in a reference. */
3776 if (TREE_CODE (decl) == CONST_DECL)
3778 DECL_MODE (decl) = TYPE_MODE (type);
3779 DECL_ALIGN (decl) = TYPE_ALIGN (type);
3780 DECL_SIZE (decl) = TYPE_SIZE (type);
3781 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
3782 return;
3785 /* Otherwise, only automatic variables need any expansion done. Static and
3786 external variables, and external functions, will be handled by
3787 `assemble_variable' (called from finish_decl). TYPE_DECL requires
3788 nothing. PARM_DECLs are handled in `assign_parms'. */
3789 if (TREE_CODE (decl) != VAR_DECL)
3790 return;
3792 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
3793 return;
3795 thisblock = block_stack;
3797 /* Create the RTL representation for the variable. */
3799 if (type == error_mark_node)
3800 SET_DECL_RTL (decl, gen_rtx_MEM (BLKmode, const0_rtx));
3802 else if (DECL_SIZE (decl) == 0)
3803 /* Variable with incomplete type. */
3805 rtx x;
3806 if (DECL_INITIAL (decl) == 0)
3807 /* Error message was already done; now avoid a crash. */
3808 x = gen_rtx_MEM (BLKmode, const0_rtx);
3809 else
3810 /* An initializer is going to decide the size of this array.
3811 Until we know the size, represent its address with a reg. */
3812 x = gen_rtx_MEM (BLKmode, gen_reg_rtx (Pmode));
3814 set_mem_attributes (x, decl, 1);
3815 SET_DECL_RTL (decl, x);
3817 else if (DECL_MODE (decl) != BLKmode
3818 /* If -ffloat-store, don't put explicit float vars
3819 into regs. */
3820 && !(flag_float_store
3821 && TREE_CODE (type) == REAL_TYPE)
3822 && ! TREE_THIS_VOLATILE (decl)
3823 && (DECL_REGISTER (decl) || optimize)
3824 /* if -fcheck-memory-usage, check all variables. */
3825 && ! current_function_check_memory_usage)
3827 /* Automatic variable that can go in a register. */
3828 int unsignedp = TREE_UNSIGNED (type);
3829 enum machine_mode reg_mode
3830 = promote_mode (type, DECL_MODE (decl), &unsignedp, 0);
3832 SET_DECL_RTL (decl, gen_reg_rtx (reg_mode));
3833 mark_user_reg (DECL_RTL (decl));
3835 if (POINTER_TYPE_P (type))
3836 mark_reg_pointer (DECL_RTL (decl),
3837 TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
3839 maybe_set_unchanging (DECL_RTL (decl), decl);
3841 /* If something wants our address, try to use ADDRESSOF. */
3842 if (TREE_ADDRESSABLE (decl))
3843 put_var_into_stack (decl);
3846 else if (TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST
3847 && ! (flag_stack_check && ! STACK_CHECK_BUILTIN
3848 && 0 < compare_tree_int (DECL_SIZE_UNIT (decl),
3849 STACK_CHECK_MAX_VAR_SIZE)))
3851 /* Variable of fixed size that goes on the stack. */
3852 rtx oldaddr = 0;
3853 rtx addr;
3855 /* If we previously made RTL for this decl, it must be an array
3856 whose size was determined by the initializer.
3857 The old address was a register; set that register now
3858 to the proper address. */
3859 if (DECL_RTL_SET_P (decl))
3861 if (GET_CODE (DECL_RTL (decl)) != MEM
3862 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
3863 abort ();
3864 oldaddr = XEXP (DECL_RTL (decl), 0);
3867 SET_DECL_RTL (decl,
3868 assign_temp (TREE_TYPE (decl), 1, 1, 1));
3870 /* Set alignment we actually gave this decl. */
3871 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
3872 : GET_MODE_BITSIZE (DECL_MODE (decl)));
3873 DECL_USER_ALIGN (decl) = 0;
3875 if (oldaddr)
3877 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
3878 if (addr != oldaddr)
3879 emit_move_insn (oldaddr, addr);
3882 else
3883 /* Dynamic-size object: must push space on the stack. */
3885 rtx address, size, x;
3887 /* Record the stack pointer on entry to block, if have
3888 not already done so. */
3889 do_pending_stack_adjust ();
3890 save_stack_pointer ();
3892 /* In function-at-a-time mode, variable_size doesn't expand this,
3893 so do it now. */
3894 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
3895 expand_expr (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
3896 const0_rtx, VOIDmode, 0);
3898 /* Compute the variable's size, in bytes. */
3899 size = expand_expr (DECL_SIZE_UNIT (decl), NULL_RTX, VOIDmode, 0);
3900 free_temp_slots ();
3902 /* Allocate space on the stack for the variable. Note that
3903 DECL_ALIGN says how the variable is to be aligned and we
3904 cannot use it to conclude anything about the alignment of
3905 the size. */
3906 address = allocate_dynamic_stack_space (size, NULL_RTX,
3907 TYPE_ALIGN (TREE_TYPE (decl)));
3909 /* Reference the variable indirect through that rtx. */
3910 x = gen_rtx_MEM (DECL_MODE (decl), address);
3911 set_mem_attributes (x, decl, 1);
3912 SET_DECL_RTL (decl, x);
3915 /* Indicate the alignment we actually gave this variable. */
3916 #ifdef STACK_BOUNDARY
3917 DECL_ALIGN (decl) = STACK_BOUNDARY;
3918 #else
3919 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
3920 #endif
3921 DECL_USER_ALIGN (decl) = 0;
3925 /* Emit code to perform the initialization of a declaration DECL. */
3927 void
3928 expand_decl_init (decl)
3929 tree decl;
3931 int was_used = TREE_USED (decl);
3933 /* If this is a CONST_DECL, we don't have to generate any code, but
3934 if DECL_INITIAL is a constant, call expand_expr to force TREE_CST_RTL
3935 to be set while in the obstack containing the constant. If we don't
3936 do this, we can lose if we have functions nested three deep and the middle
3937 function makes a CONST_DECL whose DECL_INITIAL is a STRING_CST while
3938 the innermost function is the first to expand that STRING_CST. */
3939 if (TREE_CODE (decl) == CONST_DECL)
3941 if (DECL_INITIAL (decl) && TREE_CONSTANT (DECL_INITIAL (decl)))
3942 expand_expr (DECL_INITIAL (decl), NULL_RTX, VOIDmode,
3943 EXPAND_INITIALIZER);
3944 return;
3947 if (TREE_STATIC (decl))
3948 return;
3950 /* Compute and store the initial value now. */
3952 if (DECL_INITIAL (decl) == error_mark_node)
3954 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
3956 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
3957 || code == POINTER_TYPE || code == REFERENCE_TYPE)
3958 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
3959 0, 0);
3960 emit_queue ();
3962 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
3964 emit_line_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3965 expand_assignment (decl, DECL_INITIAL (decl), 0, 0);
3966 emit_queue ();
3969 /* Don't let the initialization count as "using" the variable. */
3970 TREE_USED (decl) = was_used;
3972 /* Free any temporaries we made while initializing the decl. */
3973 preserve_temp_slots (NULL_RTX);
3974 free_temp_slots ();
3977 /* CLEANUP is an expression to be executed at exit from this binding contour;
3978 for example, in C++, it might call the destructor for this variable.
3980 We wrap CLEANUP in an UNSAVE_EXPR node, so that we can expand the
3981 CLEANUP multiple times, and have the correct semantics. This
3982 happens in exception handling, for gotos, returns, breaks that
3983 leave the current scope.
3985 If CLEANUP is nonzero and DECL is zero, we record a cleanup
3986 that is not associated with any particular variable. */
3989 expand_decl_cleanup (decl, cleanup)
3990 tree decl, cleanup;
3992 struct nesting *thisblock;
3994 /* Error if we are not in any block. */
3995 if (cfun == 0 || block_stack == 0)
3996 return 0;
3998 thisblock = block_stack;
4000 /* Record the cleanup if there is one. */
4002 if (cleanup != 0)
4004 tree t;
4005 rtx seq;
4006 tree *cleanups = &thisblock->data.block.cleanups;
4007 int cond_context = conditional_context ();
4009 if (cond_context)
4011 rtx flag = gen_reg_rtx (word_mode);
4012 rtx set_flag_0;
4013 tree cond;
4015 start_sequence ();
4016 emit_move_insn (flag, const0_rtx);
4017 set_flag_0 = get_insns ();
4018 end_sequence ();
4020 thisblock->data.block.last_unconditional_cleanup
4021 = emit_insns_after (set_flag_0,
4022 thisblock->data.block.last_unconditional_cleanup);
4024 emit_move_insn (flag, const1_rtx);
4026 cond = build_decl (VAR_DECL, NULL_TREE, type_for_mode (word_mode, 1));
4027 SET_DECL_RTL (cond, flag);
4029 /* Conditionalize the cleanup. */
4030 cleanup = build (COND_EXPR, void_type_node,
4031 truthvalue_conversion (cond),
4032 cleanup, integer_zero_node);
4033 cleanup = fold (cleanup);
4035 cleanups = thisblock->data.block.cleanup_ptr;
4038 cleanup = unsave_expr (cleanup);
4040 t = *cleanups = tree_cons (decl, cleanup, *cleanups);
4042 if (! cond_context)
4043 /* If this block has a cleanup, it belongs in stack_block_stack. */
4044 stack_block_stack = thisblock;
4046 if (cond_context)
4048 start_sequence ();
4051 if (! using_eh_for_cleanups_p)
4052 TREE_ADDRESSABLE (t) = 1;
4053 else
4054 expand_eh_region_start ();
4056 if (cond_context)
4058 seq = get_insns ();
4059 end_sequence ();
4060 if (seq)
4061 thisblock->data.block.last_unconditional_cleanup
4062 = emit_insns_after (seq,
4063 thisblock->data.block.last_unconditional_cleanup);
4065 else
4067 thisblock->data.block.last_unconditional_cleanup
4068 = get_last_insn ();
4069 /* When we insert instructions after the last unconditional cleanup,
4070 we don't adjust last_insn. That means that a later add_insn will
4071 clobber the instructions we've just added. The easiest way to
4072 fix this is to just insert another instruction here, so that the
4073 instructions inserted after the last unconditional cleanup are
4074 never the last instruction. */
4075 emit_note (NULL, NOTE_INSN_DELETED);
4076 thisblock->data.block.cleanup_ptr = &thisblock->data.block.cleanups;
4079 return 1;
4082 /* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
4083 DECL_ELTS is the list of elements that belong to DECL's type.
4084 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
4086 void
4087 expand_anon_union_decl (decl, cleanup, decl_elts)
4088 tree decl, cleanup, decl_elts;
4090 struct nesting *thisblock = cfun == 0 ? 0 : block_stack;
4091 rtx x;
4092 tree t;
4094 /* If any of the elements are addressable, so is the entire union. */
4095 for (t = decl_elts; t; t = TREE_CHAIN (t))
4096 if (TREE_ADDRESSABLE (TREE_VALUE (t)))
4098 TREE_ADDRESSABLE (decl) = 1;
4099 break;
4102 expand_decl (decl);
4103 expand_decl_cleanup (decl, cleanup);
4104 x = DECL_RTL (decl);
4106 /* Go through the elements, assigning RTL to each. */
4107 for (t = decl_elts; t; t = TREE_CHAIN (t))
4109 tree decl_elt = TREE_VALUE (t);
4110 tree cleanup_elt = TREE_PURPOSE (t);
4111 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
4113 /* Propagate the union's alignment to the elements. */
4114 DECL_ALIGN (decl_elt) = DECL_ALIGN (decl);
4115 DECL_USER_ALIGN (decl_elt) = DECL_USER_ALIGN (decl);
4117 /* If the element has BLKmode and the union doesn't, the union is
4118 aligned such that the element doesn't need to have BLKmode, so
4119 change the element's mode to the appropriate one for its size. */
4120 if (mode == BLKmode && DECL_MODE (decl) != BLKmode)
4121 DECL_MODE (decl_elt) = mode
4122 = mode_for_size_tree (DECL_SIZE (decl_elt), MODE_INT, 1);
4124 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
4125 instead create a new MEM rtx with the proper mode. */
4126 if (GET_CODE (x) == MEM)
4128 if (mode == GET_MODE (x))
4129 SET_DECL_RTL (decl_elt, x);
4130 else
4131 SET_DECL_RTL (decl_elt, adjust_address_nv (x, mode, 0));
4133 else if (GET_CODE (x) == REG)
4135 if (mode == GET_MODE (x))
4136 SET_DECL_RTL (decl_elt, x);
4137 else
4138 SET_DECL_RTL (decl_elt, gen_lowpart_SUBREG (mode, x));
4140 else
4141 abort ();
4143 /* Record the cleanup if there is one. */
4145 if (cleanup != 0)
4146 thisblock->data.block.cleanups
4147 = tree_cons (decl_elt, cleanup_elt,
4148 thisblock->data.block.cleanups);
4152 /* Expand a list of cleanups LIST.
4153 Elements may be expressions or may be nested lists.
4155 If DONT_DO is nonnull, then any list-element
4156 whose TREE_PURPOSE matches DONT_DO is omitted.
4157 This is sometimes used to avoid a cleanup associated with
4158 a value that is being returned out of the scope.
4160 If IN_FIXUP is non-zero, we are generating this cleanup for a fixup
4161 goto and handle protection regions specially in that case.
4163 If REACHABLE, we emit code, otherwise just inform the exception handling
4164 code about this finalization. */
4166 static void
4167 expand_cleanups (list, dont_do, in_fixup, reachable)
4168 tree list;
4169 tree dont_do;
4170 int in_fixup;
4171 int reachable;
4173 tree tail;
4174 for (tail = list; tail; tail = TREE_CHAIN (tail))
4175 if (dont_do == 0 || TREE_PURPOSE (tail) != dont_do)
4177 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
4178 expand_cleanups (TREE_VALUE (tail), dont_do, in_fixup, reachable);
4179 else
4181 if (! in_fixup && using_eh_for_cleanups_p)
4182 expand_eh_region_end_cleanup (TREE_VALUE (tail));
4184 if (reachable)
4186 /* Cleanups may be run multiple times. For example,
4187 when exiting a binding contour, we expand the
4188 cleanups associated with that contour. When a goto
4189 within that binding contour has a target outside that
4190 contour, it will expand all cleanups from its scope to
4191 the target. Though the cleanups are expanded multiple
4192 times, the control paths are non-overlapping so the
4193 cleanups will not be executed twice. */
4195 /* We may need to protect from outer cleanups. */
4196 if (in_fixup && using_eh_for_cleanups_p)
4198 expand_eh_region_start ();
4200 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4202 expand_eh_region_end_fixup (TREE_VALUE (tail));
4204 else
4205 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
4207 free_temp_slots ();
4213 /* Mark when the context we are emitting RTL for as a conditional
4214 context, so that any cleanup actions we register with
4215 expand_decl_init will be properly conditionalized when those
4216 cleanup actions are later performed. Must be called before any
4217 expression (tree) is expanded that is within a conditional context. */
4219 void
4220 start_cleanup_deferral ()
4222 /* block_stack can be NULL if we are inside the parameter list. It is
4223 OK to do nothing, because cleanups aren't possible here. */
4224 if (block_stack)
4225 ++block_stack->data.block.conditional_code;
4228 /* Mark the end of a conditional region of code. Because cleanup
4229 deferrals may be nested, we may still be in a conditional region
4230 after we end the currently deferred cleanups, only after we end all
4231 deferred cleanups, are we back in unconditional code. */
4233 void
4234 end_cleanup_deferral ()
4236 /* block_stack can be NULL if we are inside the parameter list. It is
4237 OK to do nothing, because cleanups aren't possible here. */
4238 if (block_stack)
4239 --block_stack->data.block.conditional_code;
4242 /* Move all cleanups from the current block_stack
4243 to the containing block_stack, where they are assumed to
4244 have been created. If anything can cause a temporary to
4245 be created, but not expanded for more than one level of
4246 block_stacks, then this code will have to change. */
4248 void
4249 move_cleanups_up ()
4251 struct nesting *block = block_stack;
4252 struct nesting *outer = block->next;
4254 outer->data.block.cleanups
4255 = chainon (block->data.block.cleanups,
4256 outer->data.block.cleanups);
4257 block->data.block.cleanups = 0;
4260 tree
4261 last_cleanup_this_contour ()
4263 if (block_stack == 0)
4264 return 0;
4266 return block_stack->data.block.cleanups;
4269 /* Return 1 if there are any pending cleanups at this point.
4270 If THIS_CONTOUR is nonzero, check the current contour as well.
4271 Otherwise, look only at the contours that enclose this one. */
4274 any_pending_cleanups (this_contour)
4275 int this_contour;
4277 struct nesting *block;
4279 if (cfun == NULL || cfun->stmt == NULL || block_stack == 0)
4280 return 0;
4282 if (this_contour && block_stack->data.block.cleanups != NULL)
4283 return 1;
4284 if (block_stack->data.block.cleanups == 0
4285 && block_stack->data.block.outer_cleanups == 0)
4286 return 0;
4288 for (block = block_stack->next; block; block = block->next)
4289 if (block->data.block.cleanups != 0)
4290 return 1;
4292 return 0;
4295 /* Enter a case (Pascal) or switch (C) statement.
4296 Push a block onto case_stack and nesting_stack
4297 to accumulate the case-labels that are seen
4298 and to record the labels generated for the statement.
4300 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
4301 Otherwise, this construct is transparent for `exit_something'.
4303 EXPR is the index-expression to be dispatched on.
4304 TYPE is its nominal type. We could simply convert EXPR to this type,
4305 but instead we take short cuts. */
4307 void
4308 expand_start_case (exit_flag, expr, type, printname)
4309 int exit_flag;
4310 tree expr;
4311 tree type;
4312 const char *printname;
4314 register struct nesting *thiscase = ALLOC_NESTING ();
4316 /* Make an entry on case_stack for the case we are entering. */
4318 thiscase->next = case_stack;
4319 thiscase->all = nesting_stack;
4320 thiscase->depth = ++nesting_depth;
4321 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
4322 thiscase->data.case_stmt.case_list = 0;
4323 thiscase->data.case_stmt.index_expr = expr;
4324 thiscase->data.case_stmt.nominal_type = type;
4325 thiscase->data.case_stmt.default_label = 0;
4326 thiscase->data.case_stmt.printname = printname;
4327 thiscase->data.case_stmt.line_number_status = force_line_numbers ();
4328 case_stack = thiscase;
4329 nesting_stack = thiscase;
4331 do_pending_stack_adjust ();
4333 /* Make sure case_stmt.start points to something that won't
4334 need any transformation before expand_end_case. */
4335 if (GET_CODE (get_last_insn ()) != NOTE)
4336 emit_note (NULL, NOTE_INSN_DELETED);
4338 thiscase->data.case_stmt.start = get_last_insn ();
4340 start_cleanup_deferral ();
4343 /* Start a "dummy case statement" within which case labels are invalid
4344 and are not connected to any larger real case statement.
4345 This can be used if you don't want to let a case statement jump
4346 into the middle of certain kinds of constructs. */
4348 void
4349 expand_start_case_dummy ()
4351 register struct nesting *thiscase = ALLOC_NESTING ();
4353 /* Make an entry on case_stack for the dummy. */
4355 thiscase->next = case_stack;
4356 thiscase->all = nesting_stack;
4357 thiscase->depth = ++nesting_depth;
4358 thiscase->exit_label = 0;
4359 thiscase->data.case_stmt.case_list = 0;
4360 thiscase->data.case_stmt.start = 0;
4361 thiscase->data.case_stmt.nominal_type = 0;
4362 thiscase->data.case_stmt.default_label = 0;
4363 case_stack = thiscase;
4364 nesting_stack = thiscase;
4365 start_cleanup_deferral ();
4368 /* End a dummy case statement. */
4370 void
4371 expand_end_case_dummy ()
4373 end_cleanup_deferral ();
4374 POPSTACK (case_stack);
4377 /* Return the data type of the index-expression
4378 of the innermost case statement, or null if none. */
4380 tree
4381 case_index_expr_type ()
4383 if (case_stack)
4384 return TREE_TYPE (case_stack->data.case_stmt.index_expr);
4385 return 0;
4388 static void
4389 check_seenlabel ()
4391 /* If this is the first label, warn if any insns have been emitted. */
4392 if (case_stack->data.case_stmt.line_number_status >= 0)
4394 rtx insn;
4396 restore_line_number_status
4397 (case_stack->data.case_stmt.line_number_status);
4398 case_stack->data.case_stmt.line_number_status = -1;
4400 for (insn = case_stack->data.case_stmt.start;
4401 insn;
4402 insn = NEXT_INSN (insn))
4404 if (GET_CODE (insn) == CODE_LABEL)
4405 break;
4406 if (GET_CODE (insn) != NOTE
4407 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
4410 insn = PREV_INSN (insn);
4411 while (insn && (GET_CODE (insn) != NOTE || NOTE_LINE_NUMBER (insn) < 0));
4413 /* If insn is zero, then there must have been a syntax error. */
4414 if (insn)
4415 warning_with_file_and_line (NOTE_SOURCE_FILE (insn),
4416 NOTE_LINE_NUMBER (insn),
4417 "unreachable code at beginning of %s",
4418 case_stack->data.case_stmt.printname);
4419 break;
4425 /* Accumulate one case or default label inside a case or switch statement.
4426 VALUE is the value of the case (a null pointer, for a default label).
4427 The function CONVERTER, when applied to arguments T and V,
4428 converts the value V to the type T.
4430 If not currently inside a case or switch statement, return 1 and do
4431 nothing. The caller will print a language-specific error message.
4432 If VALUE is a duplicate or overlaps, return 2 and do nothing
4433 except store the (first) duplicate node in *DUPLICATE.
4434 If VALUE is out of range, return 3 and do nothing.
4435 If we are jumping into the scope of a cleanup or var-sized array, return 5.
4436 Return 0 on success.
4438 Extended to handle range statements. */
4441 pushcase (value, converter, label, duplicate)
4442 register tree value;
4443 tree (*converter) PARAMS ((tree, tree));
4444 register tree label;
4445 tree *duplicate;
4447 tree index_type;
4448 tree nominal_type;
4450 /* Fail if not inside a real case statement. */
4451 if (! (case_stack && case_stack->data.case_stmt.start))
4452 return 1;
4454 if (stack_block_stack
4455 && stack_block_stack->depth > case_stack->depth)
4456 return 5;
4458 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4459 nominal_type = case_stack->data.case_stmt.nominal_type;
4461 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4462 if (index_type == error_mark_node)
4463 return 0;
4465 /* Convert VALUE to the type in which the comparisons are nominally done. */
4466 if (value != 0)
4467 value = (*converter) (nominal_type, value);
4469 check_seenlabel ();
4471 /* Fail if this value is out of range for the actual type of the index
4472 (which may be narrower than NOMINAL_TYPE). */
4473 if (value != 0
4474 && (TREE_CONSTANT_OVERFLOW (value)
4475 || ! int_fits_type_p (value, index_type)))
4476 return 3;
4478 return add_case_node (value, value, label, duplicate);
4481 /* Like pushcase but this case applies to all values between VALUE1 and
4482 VALUE2 (inclusive). If VALUE1 is NULL, the range starts at the lowest
4483 value of the index type and ends at VALUE2. If VALUE2 is NULL, the range
4484 starts at VALUE1 and ends at the highest value of the index type.
4485 If both are NULL, this case applies to all values.
4487 The return value is the same as that of pushcase but there is one
4488 additional error code: 4 means the specified range was empty. */
4491 pushcase_range (value1, value2, converter, label, duplicate)
4492 register tree value1, value2;
4493 tree (*converter) PARAMS ((tree, tree));
4494 register tree label;
4495 tree *duplicate;
4497 tree index_type;
4498 tree nominal_type;
4500 /* Fail if not inside a real case statement. */
4501 if (! (case_stack && case_stack->data.case_stmt.start))
4502 return 1;
4504 if (stack_block_stack
4505 && stack_block_stack->depth > case_stack->depth)
4506 return 5;
4508 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
4509 nominal_type = case_stack->data.case_stmt.nominal_type;
4511 /* If the index is erroneous, avoid more problems: pretend to succeed. */
4512 if (index_type == error_mark_node)
4513 return 0;
4515 check_seenlabel ();
4517 /* Convert VALUEs to type in which the comparisons are nominally done
4518 and replace any unspecified value with the corresponding bound. */
4519 if (value1 == 0)
4520 value1 = TYPE_MIN_VALUE (index_type);
4521 if (value2 == 0)
4522 value2 = TYPE_MAX_VALUE (index_type);
4524 /* Fail if the range is empty. Do this before any conversion since
4525 we want to allow out-of-range empty ranges. */
4526 if (value2 != 0 && tree_int_cst_lt (value2, value1))
4527 return 4;
4529 /* If the max was unbounded, use the max of the nominal_type we are
4530 converting to. Do this after the < check above to suppress false
4531 positives. */
4532 if (value2 == 0)
4533 value2 = TYPE_MAX_VALUE (nominal_type);
4535 value1 = (*converter) (nominal_type, value1);
4536 value2 = (*converter) (nominal_type, value2);
4538 /* Fail if these values are out of range. */
4539 if (TREE_CONSTANT_OVERFLOW (value1)
4540 || ! int_fits_type_p (value1, index_type))
4541 return 3;
4543 if (TREE_CONSTANT_OVERFLOW (value2)
4544 || ! int_fits_type_p (value2, index_type))
4545 return 3;
4547 return add_case_node (value1, value2, label, duplicate);
4550 /* Do the actual insertion of a case label for pushcase and pushcase_range
4551 into case_stack->data.case_stmt.case_list. Use an AVL tree to avoid
4552 slowdown for large switch statements. */
4555 add_case_node (low, high, label, duplicate)
4556 tree low, high;
4557 tree label;
4558 tree *duplicate;
4560 struct case_node *p, **q, *r;
4562 /* If there's no HIGH value, then this is not a case range; it's
4563 just a simple case label. But that's just a degenerate case
4564 range. */
4565 if (!high)
4566 high = low;
4568 /* Handle default labels specially. */
4569 if (!high && !low)
4571 if (case_stack->data.case_stmt.default_label != 0)
4573 *duplicate = case_stack->data.case_stmt.default_label;
4574 return 2;
4576 case_stack->data.case_stmt.default_label = label;
4577 expand_label (label);
4578 return 0;
4581 q = &case_stack->data.case_stmt.case_list;
4582 p = *q;
4584 while ((r = *q))
4586 p = r;
4588 /* Keep going past elements distinctly greater than HIGH. */
4589 if (tree_int_cst_lt (high, p->low))
4590 q = &p->left;
4592 /* or distinctly less than LOW. */
4593 else if (tree_int_cst_lt (p->high, low))
4594 q = &p->right;
4596 else
4598 /* We have an overlap; this is an error. */
4599 *duplicate = p->code_label;
4600 return 2;
4604 /* Add this label to the chain, and succeed. */
4606 r = (struct case_node *) xmalloc (sizeof (struct case_node));
4607 r->low = low;
4609 /* If the bounds are equal, turn this into the one-value case. */
4610 if (tree_int_cst_equal (low, high))
4611 r->high = r->low;
4612 else
4613 r->high = high;
4615 r->code_label = label;
4616 expand_label (label);
4618 *q = r;
4619 r->parent = p;
4620 r->left = 0;
4621 r->right = 0;
4622 r->balance = 0;
4624 while (p)
4626 struct case_node *s;
4628 if (r == p->left)
4630 int b;
4632 if (! (b = p->balance))
4633 /* Growth propagation from left side. */
4634 p->balance = -1;
4635 else if (b < 0)
4637 if (r->balance < 0)
4639 /* R-Rotation */
4640 if ((p->left = s = r->right))
4641 s->parent = p;
4643 r->right = p;
4644 p->balance = 0;
4645 r->balance = 0;
4646 s = p->parent;
4647 p->parent = r;
4649 if ((r->parent = s))
4651 if (s->left == p)
4652 s->left = r;
4653 else
4654 s->right = r;
4656 else
4657 case_stack->data.case_stmt.case_list = r;
4659 else
4660 /* r->balance == +1 */
4662 /* LR-Rotation */
4664 int b2;
4665 struct case_node *t = r->right;
4667 if ((p->left = s = t->right))
4668 s->parent = p;
4670 t->right = p;
4671 if ((r->right = s = t->left))
4672 s->parent = r;
4674 t->left = r;
4675 b = t->balance;
4676 b2 = b < 0;
4677 p->balance = b2;
4678 b2 = -b2 - b;
4679 r->balance = b2;
4680 t->balance = 0;
4681 s = p->parent;
4682 p->parent = t;
4683 r->parent = t;
4685 if ((t->parent = s))
4687 if (s->left == p)
4688 s->left = t;
4689 else
4690 s->right = t;
4692 else
4693 case_stack->data.case_stmt.case_list = t;
4695 break;
4698 else
4700 /* p->balance == +1; growth of left side balances the node. */
4701 p->balance = 0;
4702 break;
4705 else
4706 /* r == p->right */
4708 int b;
4710 if (! (b = p->balance))
4711 /* Growth propagation from right side. */
4712 p->balance++;
4713 else if (b > 0)
4715 if (r->balance > 0)
4717 /* L-Rotation */
4719 if ((p->right = s = r->left))
4720 s->parent = p;
4722 r->left = p;
4723 p->balance = 0;
4724 r->balance = 0;
4725 s = p->parent;
4726 p->parent = r;
4727 if ((r->parent = s))
4729 if (s->left == p)
4730 s->left = r;
4731 else
4732 s->right = r;
4735 else
4736 case_stack->data.case_stmt.case_list = r;
4739 else
4740 /* r->balance == -1 */
4742 /* RL-Rotation */
4743 int b2;
4744 struct case_node *t = r->left;
4746 if ((p->right = s = t->left))
4747 s->parent = p;
4749 t->left = p;
4751 if ((r->left = s = t->right))
4752 s->parent = r;
4754 t->right = r;
4755 b = t->balance;
4756 b2 = b < 0;
4757 r->balance = b2;
4758 b2 = -b2 - b;
4759 p->balance = b2;
4760 t->balance = 0;
4761 s = p->parent;
4762 p->parent = t;
4763 r->parent = t;
4765 if ((t->parent = s))
4767 if (s->left == p)
4768 s->left = t;
4769 else
4770 s->right = t;
4773 else
4774 case_stack->data.case_stmt.case_list = t;
4776 break;
4778 else
4780 /* p->balance == -1; growth of right side balances the node. */
4781 p->balance = 0;
4782 break;
4786 r = p;
4787 p = p->parent;
4790 return 0;
4793 /* Returns the number of possible values of TYPE.
4794 Returns -1 if the number is unknown, variable, or if the number does not
4795 fit in a HOST_WIDE_INT.
4796 Sets *SPARENESS to 2 if TYPE is an ENUMERAL_TYPE whose values
4797 do not increase monotonically (there may be duplicates);
4798 to 1 if the values increase monotonically, but not always by 1;
4799 otherwise sets it to 0. */
4801 HOST_WIDE_INT
4802 all_cases_count (type, spareness)
4803 tree type;
4804 int *spareness;
4806 tree t;
4807 HOST_WIDE_INT count, minval, lastval;
4809 *spareness = 0;
4811 switch (TREE_CODE (type))
4813 case BOOLEAN_TYPE:
4814 count = 2;
4815 break;
4817 case CHAR_TYPE:
4818 count = 1 << BITS_PER_UNIT;
4819 break;
4821 default:
4822 case INTEGER_TYPE:
4823 if (TYPE_MAX_VALUE (type) != 0
4824 && 0 != (t = fold (build (MINUS_EXPR, type, TYPE_MAX_VALUE (type),
4825 TYPE_MIN_VALUE (type))))
4826 && 0 != (t = fold (build (PLUS_EXPR, type, t,
4827 convert (type, integer_zero_node))))
4828 && host_integerp (t, 1))
4829 count = tree_low_cst (t, 1);
4830 else
4831 return -1;
4832 break;
4834 case ENUMERAL_TYPE:
4835 /* Don't waste time with enumeral types with huge values. */
4836 if (! host_integerp (TYPE_MIN_VALUE (type), 0)
4837 || TYPE_MAX_VALUE (type) == 0
4838 || ! host_integerp (TYPE_MAX_VALUE (type), 0))
4839 return -1;
4841 lastval = minval = tree_low_cst (TYPE_MIN_VALUE (type), 0);
4842 count = 0;
4844 for (t = TYPE_VALUES (type); t != NULL_TREE; t = TREE_CHAIN (t))
4846 HOST_WIDE_INT thisval = tree_low_cst (TREE_VALUE (t), 0);
4848 if (*spareness == 2 || thisval < lastval)
4849 *spareness = 2;
4850 else if (thisval != minval + count)
4851 *spareness = 1;
4853 count++;
4857 return count;
4860 #define BITARRAY_TEST(ARRAY, INDEX) \
4861 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4862 & (1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR)))
4863 #define BITARRAY_SET(ARRAY, INDEX) \
4864 ((ARRAY)[(unsigned) (INDEX) / HOST_BITS_PER_CHAR]\
4865 |= 1 << ((unsigned) (INDEX) % HOST_BITS_PER_CHAR))
4867 /* Set the elements of the bitstring CASES_SEEN (which has length COUNT),
4868 with the case values we have seen, assuming the case expression
4869 has the given TYPE.
4870 SPARSENESS is as determined by all_cases_count.
4872 The time needed is proportional to COUNT, unless
4873 SPARSENESS is 2, in which case quadratic time is needed. */
4875 void
4876 mark_seen_cases (type, cases_seen, count, sparseness)
4877 tree type;
4878 unsigned char *cases_seen;
4879 HOST_WIDE_INT count;
4880 int sparseness;
4882 tree next_node_to_try = NULL_TREE;
4883 HOST_WIDE_INT next_node_offset = 0;
4885 register struct case_node *n, *root = case_stack->data.case_stmt.case_list;
4886 tree val = make_node (INTEGER_CST);
4888 TREE_TYPE (val) = type;
4889 if (! root)
4890 /* Do nothing. */
4892 else if (sparseness == 2)
4894 tree t;
4895 unsigned HOST_WIDE_INT xlo;
4897 /* This less efficient loop is only needed to handle
4898 duplicate case values (multiple enum constants
4899 with the same value). */
4900 TREE_TYPE (val) = TREE_TYPE (root->low);
4901 for (t = TYPE_VALUES (type), xlo = 0; t != NULL_TREE;
4902 t = TREE_CHAIN (t), xlo++)
4904 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (TREE_VALUE (t));
4905 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (TREE_VALUE (t));
4906 n = root;
4909 /* Keep going past elements distinctly greater than VAL. */
4910 if (tree_int_cst_lt (val, n->low))
4911 n = n->left;
4913 /* or distinctly less than VAL. */
4914 else if (tree_int_cst_lt (n->high, val))
4915 n = n->right;
4917 else
4919 /* We have found a matching range. */
4920 BITARRAY_SET (cases_seen, xlo);
4921 break;
4924 while (n);
4927 else
4929 if (root->left)
4930 case_stack->data.case_stmt.case_list = root = case_tree2list (root, 0);
4932 for (n = root; n; n = n->right)
4934 TREE_INT_CST_LOW (val) = TREE_INT_CST_LOW (n->low);
4935 TREE_INT_CST_HIGH (val) = TREE_INT_CST_HIGH (n->low);
4936 while (! tree_int_cst_lt (n->high, val))
4938 /* Calculate (into xlo) the "offset" of the integer (val).
4939 The element with lowest value has offset 0, the next smallest
4940 element has offset 1, etc. */
4942 unsigned HOST_WIDE_INT xlo;
4943 HOST_WIDE_INT xhi;
4944 tree t;
4946 if (sparseness && TYPE_VALUES (type) != NULL_TREE)
4948 /* The TYPE_VALUES will be in increasing order, so
4949 starting searching where we last ended. */
4950 t = next_node_to_try;
4951 xlo = next_node_offset;
4952 xhi = 0;
4953 for (;;)
4955 if (t == NULL_TREE)
4957 t = TYPE_VALUES (type);
4958 xlo = 0;
4960 if (tree_int_cst_equal (val, TREE_VALUE (t)))
4962 next_node_to_try = TREE_CHAIN (t);
4963 next_node_offset = xlo + 1;
4964 break;
4966 xlo++;
4967 t = TREE_CHAIN (t);
4968 if (t == next_node_to_try)
4970 xlo = -1;
4971 break;
4975 else
4977 t = TYPE_MIN_VALUE (type);
4978 if (t)
4979 neg_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t),
4980 &xlo, &xhi);
4981 else
4982 xlo = xhi = 0;
4983 add_double (xlo, xhi,
4984 TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
4985 &xlo, &xhi);
4988 if (xhi == 0 && xlo < (unsigned HOST_WIDE_INT) count)
4989 BITARRAY_SET (cases_seen, xlo);
4991 add_double (TREE_INT_CST_LOW (val), TREE_INT_CST_HIGH (val),
4992 1, 0,
4993 &TREE_INT_CST_LOW (val), &TREE_INT_CST_HIGH (val));
4999 /* Called when the index of a switch statement is an enumerated type
5000 and there is no default label.
5002 Checks that all enumeration literals are covered by the case
5003 expressions of a switch. Also, warn if there are any extra
5004 switch cases that are *not* elements of the enumerated type.
5006 If all enumeration literals were covered by the case expressions,
5007 turn one of the expressions into the default expression since it should
5008 not be possible to fall through such a switch. */
5010 void
5011 check_for_full_enumeration_handling (type)
5012 tree type;
5014 register struct case_node *n;
5015 register tree chain;
5017 /* True iff the selector type is a numbered set mode. */
5018 int sparseness = 0;
5020 /* The number of possible selector values. */
5021 HOST_WIDE_INT size;
5023 /* For each possible selector value. a one iff it has been matched
5024 by a case value alternative. */
5025 unsigned char *cases_seen;
5027 /* The allocated size of cases_seen, in chars. */
5028 HOST_WIDE_INT bytes_needed;
5030 if (! warn_switch)
5031 return;
5033 size = all_cases_count (type, &sparseness);
5034 bytes_needed = (size + HOST_BITS_PER_CHAR) / HOST_BITS_PER_CHAR;
5036 if (size > 0 && size < 600000
5037 /* We deliberately use calloc here, not cmalloc, so that we can suppress
5038 this optimization if we don't have enough memory rather than
5039 aborting, as xmalloc would do. */
5040 && (cases_seen =
5041 (unsigned char *) really_call_calloc (bytes_needed, 1)) != NULL)
5043 HOST_WIDE_INT i;
5044 tree v = TYPE_VALUES (type);
5046 /* The time complexity of this code is normally O(N), where
5047 N being the number of members in the enumerated type.
5048 However, if type is a ENUMERAL_TYPE whose values do not
5049 increase monotonically, O(N*log(N)) time may be needed. */
5051 mark_seen_cases (type, cases_seen, size, sparseness);
5053 for (i = 0; v != NULL_TREE && i < size; i++, v = TREE_CHAIN (v))
5054 if (BITARRAY_TEST (cases_seen, i) == 0)
5055 warning ("enumeration value `%s' not handled in switch",
5056 IDENTIFIER_POINTER (TREE_PURPOSE (v)));
5058 free (cases_seen);
5061 /* Now we go the other way around; we warn if there are case
5062 expressions that don't correspond to enumerators. This can
5063 occur since C and C++ don't enforce type-checking of
5064 assignments to enumeration variables. */
5066 if (case_stack->data.case_stmt.case_list
5067 && case_stack->data.case_stmt.case_list->left)
5068 case_stack->data.case_stmt.case_list
5069 = case_tree2list (case_stack->data.case_stmt.case_list, 0);
5070 if (warn_switch)
5071 for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
5073 for (chain = TYPE_VALUES (type);
5074 chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
5075 chain = TREE_CHAIN (chain))
5078 if (!chain)
5080 if (TYPE_NAME (type) == 0)
5081 warning ("case value `%ld' not in enumerated type",
5082 (long) TREE_INT_CST_LOW (n->low));
5083 else
5084 warning ("case value `%ld' not in enumerated type `%s'",
5085 (long) TREE_INT_CST_LOW (n->low),
5086 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5087 == IDENTIFIER_NODE)
5088 ? TYPE_NAME (type)
5089 : DECL_NAME (TYPE_NAME (type))));
5091 if (!tree_int_cst_equal (n->low, n->high))
5093 for (chain = TYPE_VALUES (type);
5094 chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
5095 chain = TREE_CHAIN (chain))
5098 if (!chain)
5100 if (TYPE_NAME (type) == 0)
5101 warning ("case value `%ld' not in enumerated type",
5102 (long) TREE_INT_CST_LOW (n->high));
5103 else
5104 warning ("case value `%ld' not in enumerated type `%s'",
5105 (long) TREE_INT_CST_LOW (n->high),
5106 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
5107 == IDENTIFIER_NODE)
5108 ? TYPE_NAME (type)
5109 : DECL_NAME (TYPE_NAME (type))));
5115 /* Free CN, and its children. */
5117 static void
5118 free_case_nodes (cn)
5119 case_node_ptr cn;
5121 if (cn)
5123 free_case_nodes (cn->left);
5124 free_case_nodes (cn->right);
5125 free (cn);
5131 /* Terminate a case (Pascal) or switch (C) statement
5132 in which ORIG_INDEX is the expression to be tested.
5133 Generate the code to test it and jump to the right place. */
5135 void
5136 expand_end_case (orig_index)
5137 tree orig_index;
5139 tree minval = NULL_TREE, maxval = NULL_TREE, range = NULL_TREE, orig_minval;
5140 rtx default_label = 0;
5141 register struct case_node *n;
5142 unsigned int count;
5143 rtx index;
5144 rtx table_label;
5145 int ncases;
5146 rtx *labelvec;
5147 register int i;
5148 rtx before_case, end;
5149 register struct nesting *thiscase = case_stack;
5150 tree index_expr, index_type;
5151 int unsignedp;
5153 /* Don't crash due to previous errors. */
5154 if (thiscase == NULL)
5155 return;
5157 table_label = gen_label_rtx ();
5158 index_expr = thiscase->data.case_stmt.index_expr;
5159 index_type = TREE_TYPE (index_expr);
5160 unsignedp = TREE_UNSIGNED (index_type);
5162 do_pending_stack_adjust ();
5164 /* This might get an spurious warning in the presence of a syntax error;
5165 it could be fixed by moving the call to check_seenlabel after the
5166 check for error_mark_node, and copying the code of check_seenlabel that
5167 deals with case_stack->data.case_stmt.line_number_status /
5168 restore_line_number_status in front of the call to end_cleanup_deferral;
5169 However, this might miss some useful warnings in the presence of
5170 non-syntax errors. */
5171 check_seenlabel ();
5173 /* An ERROR_MARK occurs for various reasons including invalid data type. */
5174 if (index_type != error_mark_node)
5176 /* If switch expression was an enumerated type, check that all
5177 enumeration literals are covered by the cases.
5178 No sense trying this if there's a default case, however. */
5180 if (!thiscase->data.case_stmt.default_label
5181 && TREE_CODE (TREE_TYPE (orig_index)) == ENUMERAL_TYPE
5182 && TREE_CODE (index_expr) != INTEGER_CST)
5183 check_for_full_enumeration_handling (TREE_TYPE (orig_index));
5185 /* If we don't have a default-label, create one here,
5186 after the body of the switch. */
5187 if (thiscase->data.case_stmt.default_label == 0)
5189 thiscase->data.case_stmt.default_label
5190 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5191 expand_label (thiscase->data.case_stmt.default_label);
5193 default_label = label_rtx (thiscase->data.case_stmt.default_label);
5195 before_case = get_last_insn ();
5197 if (thiscase->data.case_stmt.case_list
5198 && thiscase->data.case_stmt.case_list->left)
5199 thiscase->data.case_stmt.case_list
5200 = case_tree2list (thiscase->data.case_stmt.case_list, 0);
5202 /* Simplify the case-list before we count it. */
5203 group_case_nodes (thiscase->data.case_stmt.case_list);
5205 /* Get upper and lower bounds of case values.
5206 Also convert all the case values to the index expr's data type. */
5208 count = 0;
5209 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5211 /* Check low and high label values are integers. */
5212 if (TREE_CODE (n->low) != INTEGER_CST)
5213 abort ();
5214 if (TREE_CODE (n->high) != INTEGER_CST)
5215 abort ();
5217 n->low = convert (index_type, n->low);
5218 n->high = convert (index_type, n->high);
5220 /* Count the elements and track the largest and smallest
5221 of them (treating them as signed even if they are not). */
5222 if (count++ == 0)
5224 minval = n->low;
5225 maxval = n->high;
5227 else
5229 if (INT_CST_LT (n->low, minval))
5230 minval = n->low;
5231 if (INT_CST_LT (maxval, n->high))
5232 maxval = n->high;
5234 /* A range counts double, since it requires two compares. */
5235 if (! tree_int_cst_equal (n->low, n->high))
5236 count++;
5239 orig_minval = minval;
5241 /* Compute span of values. */
5242 if (count != 0)
5243 range = fold (build (MINUS_EXPR, index_type, maxval, minval));
5245 end_cleanup_deferral ();
5247 if (count == 0)
5249 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
5250 emit_queue ();
5251 emit_jump (default_label);
5254 /* If range of values is much bigger than number of values,
5255 make a sequence of conditional branches instead of a dispatch.
5256 If the switch-index is a constant, do it this way
5257 because we can optimize it. */
5259 else if (count < case_values_threshold ()
5260 || compare_tree_int (range, 10 * count) > 0
5261 /* RANGE may be signed, and really large ranges will show up
5262 as negative numbers. */
5263 || compare_tree_int (range, 0) < 0
5264 #ifndef ASM_OUTPUT_ADDR_DIFF_ELT
5265 || flag_pic
5266 #endif
5267 || TREE_CODE (index_expr) == INTEGER_CST
5268 || (TREE_CODE (index_expr) == COMPOUND_EXPR
5269 && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
5271 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
5273 /* If the index is a short or char that we do not have
5274 an insn to handle comparisons directly, convert it to
5275 a full integer now, rather than letting each comparison
5276 generate the conversion. */
5278 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
5279 && ! have_insn_for (COMPARE, GET_MODE (index)))
5281 enum machine_mode wider_mode;
5282 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
5283 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
5284 if (have_insn_for (COMPARE, wider_mode))
5286 index = convert_to_mode (wider_mode, index, unsignedp);
5287 break;
5291 emit_queue ();
5292 do_pending_stack_adjust ();
5294 index = protect_from_queue (index, 0);
5295 if (GET_CODE (index) == MEM)
5296 index = copy_to_reg (index);
5297 if (GET_CODE (index) == CONST_INT
5298 || TREE_CODE (index_expr) == INTEGER_CST)
5300 /* Make a tree node with the proper constant value
5301 if we don't already have one. */
5302 if (TREE_CODE (index_expr) != INTEGER_CST)
5304 index_expr
5305 = build_int_2 (INTVAL (index),
5306 unsignedp || INTVAL (index) >= 0 ? 0 : -1);
5307 index_expr = convert (index_type, index_expr);
5310 /* For constant index expressions we need only
5311 issue a unconditional branch to the appropriate
5312 target code. The job of removing any unreachable
5313 code is left to the optimisation phase if the
5314 "-O" option is specified. */
5315 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5316 if (! tree_int_cst_lt (index_expr, n->low)
5317 && ! tree_int_cst_lt (n->high, index_expr))
5318 break;
5320 if (n)
5321 emit_jump (label_rtx (n->code_label));
5322 else
5323 emit_jump (default_label);
5325 else
5327 /* If the index expression is not constant we generate
5328 a binary decision tree to select the appropriate
5329 target code. This is done as follows:
5331 The list of cases is rearranged into a binary tree,
5332 nearly optimal assuming equal probability for each case.
5334 The tree is transformed into RTL, eliminating
5335 redundant test conditions at the same time.
5337 If program flow could reach the end of the
5338 decision tree an unconditional jump to the
5339 default code is emitted. */
5341 use_cost_table
5342 = (TREE_CODE (TREE_TYPE (orig_index)) != ENUMERAL_TYPE
5343 && estimate_case_costs (thiscase->data.case_stmt.case_list));
5344 balance_case_nodes (&thiscase->data.case_stmt.case_list, NULL);
5345 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
5346 default_label, index_type);
5347 emit_jump_if_reachable (default_label);
5350 else
5352 if (! try_casesi (index_type, index_expr, minval, range,
5353 table_label, default_label))
5355 index_type = thiscase->data.case_stmt.nominal_type;
5356 if (! try_tablejump (index_type, index_expr, minval, range,
5357 table_label, default_label))
5358 abort ();
5361 /* Get table of labels to jump to, in order of case index. */
5363 ncases = TREE_INT_CST_LOW (range) + 1;
5364 labelvec = (rtx *) alloca (ncases * sizeof (rtx));
5365 memset ((char *) labelvec, 0, ncases * sizeof (rtx));
5367 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
5369 register HOST_WIDE_INT i
5370 = TREE_INT_CST_LOW (n->low) - TREE_INT_CST_LOW (orig_minval);
5372 while (1)
5374 labelvec[i]
5375 = gen_rtx_LABEL_REF (Pmode, label_rtx (n->code_label));
5376 if (i + TREE_INT_CST_LOW (orig_minval)
5377 == TREE_INT_CST_LOW (n->high))
5378 break;
5379 i++;
5383 /* Fill in the gaps with the default. */
5384 for (i = 0; i < ncases; i++)
5385 if (labelvec[i] == 0)
5386 labelvec[i] = gen_rtx_LABEL_REF (Pmode, default_label);
5388 /* Output the table */
5389 emit_label (table_label);
5391 if (CASE_VECTOR_PC_RELATIVE || flag_pic)
5392 emit_jump_insn (gen_rtx_ADDR_DIFF_VEC (CASE_VECTOR_MODE,
5393 gen_rtx_LABEL_REF (Pmode, table_label),
5394 gen_rtvec_v (ncases, labelvec),
5395 const0_rtx, const0_rtx));
5396 else
5397 emit_jump_insn (gen_rtx_ADDR_VEC (CASE_VECTOR_MODE,
5398 gen_rtvec_v (ncases, labelvec)));
5400 /* If the case insn drops through the table,
5401 after the table we must jump to the default-label.
5402 Otherwise record no drop-through after the table. */
5403 #ifdef CASE_DROPS_THROUGH
5404 emit_jump (default_label);
5405 #else
5406 emit_barrier ();
5407 #endif
5410 before_case = NEXT_INSN (before_case);
5411 end = get_last_insn ();
5412 squeeze_notes (&before_case, &end);
5413 reorder_insns (before_case, end,
5414 thiscase->data.case_stmt.start);
5416 else
5417 end_cleanup_deferral ();
5419 if (thiscase->exit_label)
5420 emit_label (thiscase->exit_label);
5422 free_case_nodes (case_stack->data.case_stmt.case_list);
5423 POPSTACK (case_stack);
5425 free_temp_slots ();
5428 /* Convert the tree NODE into a list linked by the right field, with the left
5429 field zeroed. RIGHT is used for recursion; it is a list to be placed
5430 rightmost in the resulting list. */
5432 static struct case_node *
5433 case_tree2list (node, right)
5434 struct case_node *node, *right;
5436 struct case_node *left;
5438 if (node->right)
5439 right = case_tree2list (node->right, right);
5441 node->right = right;
5442 if ((left = node->left))
5444 node->left = 0;
5445 return case_tree2list (left, node);
5448 return node;
5451 /* Generate code to jump to LABEL if OP1 and OP2 are equal. */
5453 static void
5454 do_jump_if_equal (op1, op2, label, unsignedp)
5455 rtx op1, op2, label;
5456 int unsignedp;
5458 if (GET_CODE (op1) == CONST_INT
5459 && GET_CODE (op2) == CONST_INT)
5461 if (INTVAL (op1) == INTVAL (op2))
5462 emit_jump (label);
5464 else
5466 enum machine_mode mode = GET_MODE (op1);
5467 if (mode == VOIDmode)
5468 mode = GET_MODE (op2);
5469 emit_cmp_and_jump_insns (op1, op2, EQ, NULL_RTX, mode, unsignedp,
5470 0, label);
5474 /* Not all case values are encountered equally. This function
5475 uses a heuristic to weight case labels, in cases where that
5476 looks like a reasonable thing to do.
5478 Right now, all we try to guess is text, and we establish the
5479 following weights:
5481 chars above space: 16
5482 digits: 16
5483 default: 12
5484 space, punct: 8
5485 tab: 4
5486 newline: 2
5487 other "\" chars: 1
5488 remaining chars: 0
5490 If we find any cases in the switch that are not either -1 or in the range
5491 of valid ASCII characters, or are control characters other than those
5492 commonly used with "\", don't treat this switch scanning text.
5494 Return 1 if these nodes are suitable for cost estimation, otherwise
5495 return 0. */
5497 static int
5498 estimate_case_costs (node)
5499 case_node_ptr node;
5501 tree min_ascii = integer_minus_one_node;
5502 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
5503 case_node_ptr n;
5504 int i;
5506 /* If we haven't already made the cost table, make it now. Note that the
5507 lower bound of the table is -1, not zero. */
5509 if (! cost_table_initialized)
5511 cost_table_initialized = 1;
5513 for (i = 0; i < 128; i++)
5515 if (ISALNUM (i))
5516 COST_TABLE (i) = 16;
5517 else if (ISPUNCT (i))
5518 COST_TABLE (i) = 8;
5519 else if (ISCNTRL (i))
5520 COST_TABLE (i) = -1;
5523 COST_TABLE (' ') = 8;
5524 COST_TABLE ('\t') = 4;
5525 COST_TABLE ('\0') = 4;
5526 COST_TABLE ('\n') = 2;
5527 COST_TABLE ('\f') = 1;
5528 COST_TABLE ('\v') = 1;
5529 COST_TABLE ('\b') = 1;
5532 /* See if all the case expressions look like text. It is text if the
5533 constant is >= -1 and the highest constant is <= 127. Do all comparisons
5534 as signed arithmetic since we don't want to ever access cost_table with a
5535 value less than -1. Also check that none of the constants in a range
5536 are strange control characters. */
5538 for (n = node; n; n = n->right)
5540 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
5541 return 0;
5543 for (i = (HOST_WIDE_INT) TREE_INT_CST_LOW (n->low);
5544 i <= (HOST_WIDE_INT) TREE_INT_CST_LOW (n->high); i++)
5545 if (COST_TABLE (i) < 0)
5546 return 0;
5549 /* All interesting values are within the range of interesting
5550 ASCII characters. */
5551 return 1;
5554 /* Scan an ordered list of case nodes
5555 combining those with consecutive values or ranges.
5557 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
5559 static void
5560 group_case_nodes (head)
5561 case_node_ptr head;
5563 case_node_ptr node = head;
5565 while (node)
5567 rtx lb = next_real_insn (label_rtx (node->code_label));
5568 rtx lb2;
5569 case_node_ptr np = node;
5571 /* Try to group the successors of NODE with NODE. */
5572 while (((np = np->right) != 0)
5573 /* Do they jump to the same place? */
5574 && ((lb2 = next_real_insn (label_rtx (np->code_label))) == lb
5575 || (lb != 0 && lb2 != 0
5576 && simplejump_p (lb)
5577 && simplejump_p (lb2)
5578 && rtx_equal_p (SET_SRC (PATTERN (lb)),
5579 SET_SRC (PATTERN (lb2)))))
5580 /* Are their ranges consecutive? */
5581 && tree_int_cst_equal (np->low,
5582 fold (build (PLUS_EXPR,
5583 TREE_TYPE (node->high),
5584 node->high,
5585 integer_one_node)))
5586 /* An overflow is not consecutive. */
5587 && tree_int_cst_lt (node->high,
5588 fold (build (PLUS_EXPR,
5589 TREE_TYPE (node->high),
5590 node->high,
5591 integer_one_node))))
5593 node->high = np->high;
5595 /* NP is the first node after NODE which can't be grouped with it.
5596 Delete the nodes in between, and move on to that node. */
5597 node->right = np;
5598 node = np;
5602 /* Take an ordered list of case nodes
5603 and transform them into a near optimal binary tree,
5604 on the assumption that any target code selection value is as
5605 likely as any other.
5607 The transformation is performed by splitting the ordered
5608 list into two equal sections plus a pivot. The parts are
5609 then attached to the pivot as left and right branches. Each
5610 branch is then transformed recursively. */
5612 static void
5613 balance_case_nodes (head, parent)
5614 case_node_ptr *head;
5615 case_node_ptr parent;
5617 register case_node_ptr np;
5619 np = *head;
5620 if (np)
5622 int cost = 0;
5623 int i = 0;
5624 int ranges = 0;
5625 register case_node_ptr *npp;
5626 case_node_ptr left;
5628 /* Count the number of entries on branch. Also count the ranges. */
5630 while (np)
5632 if (!tree_int_cst_equal (np->low, np->high))
5634 ranges++;
5635 if (use_cost_table)
5636 cost += COST_TABLE (TREE_INT_CST_LOW (np->high));
5639 if (use_cost_table)
5640 cost += COST_TABLE (TREE_INT_CST_LOW (np->low));
5642 i++;
5643 np = np->right;
5646 if (i > 2)
5648 /* Split this list if it is long enough for that to help. */
5649 npp = head;
5650 left = *npp;
5651 if (use_cost_table)
5653 /* Find the place in the list that bisects the list's total cost,
5654 Here I gets half the total cost. */
5655 int n_moved = 0;
5656 i = (cost + 1) / 2;
5657 while (1)
5659 /* Skip nodes while their cost does not reach that amount. */
5660 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5661 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->high));
5662 i -= COST_TABLE (TREE_INT_CST_LOW ((*npp)->low));
5663 if (i <= 0)
5664 break;
5665 npp = &(*npp)->right;
5666 n_moved += 1;
5668 if (n_moved == 0)
5670 /* Leave this branch lopsided, but optimize left-hand
5671 side and fill in `parent' fields for right-hand side. */
5672 np = *head;
5673 np->parent = parent;
5674 balance_case_nodes (&np->left, np);
5675 for (; np->right; np = np->right)
5676 np->right->parent = np;
5677 return;
5680 /* If there are just three nodes, split at the middle one. */
5681 else if (i == 3)
5682 npp = &(*npp)->right;
5683 else
5685 /* Find the place in the list that bisects the list's total cost,
5686 where ranges count as 2.
5687 Here I gets half the total cost. */
5688 i = (i + ranges + 1) / 2;
5689 while (1)
5691 /* Skip nodes while their cost does not reach that amount. */
5692 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
5693 i--;
5694 i--;
5695 if (i <= 0)
5696 break;
5697 npp = &(*npp)->right;
5700 *head = np = *npp;
5701 *npp = 0;
5702 np->parent = parent;
5703 np->left = left;
5705 /* Optimize each of the two split parts. */
5706 balance_case_nodes (&np->left, np);
5707 balance_case_nodes (&np->right, np);
5709 else
5711 /* Else leave this branch as one level,
5712 but fill in `parent' fields. */
5713 np = *head;
5714 np->parent = parent;
5715 for (; np->right; np = np->right)
5716 np->right->parent = np;
5721 /* Search the parent sections of the case node tree
5722 to see if a test for the lower bound of NODE would be redundant.
5723 INDEX_TYPE is the type of the index expression.
5725 The instructions to generate the case decision tree are
5726 output in the same order as nodes are processed so it is
5727 known that if a parent node checks the range of the current
5728 node minus one that the current node is bounded at its lower
5729 span. Thus the test would be redundant. */
5731 static int
5732 node_has_low_bound (node, index_type)
5733 case_node_ptr node;
5734 tree index_type;
5736 tree low_minus_one;
5737 case_node_ptr pnode;
5739 /* If the lower bound of this node is the lowest value in the index type,
5740 we need not test it. */
5742 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
5743 return 1;
5745 /* If this node has a left branch, the value at the left must be less
5746 than that at this node, so it cannot be bounded at the bottom and
5747 we need not bother testing any further. */
5749 if (node->left)
5750 return 0;
5752 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
5753 node->low, integer_one_node));
5755 /* If the subtraction above overflowed, we can't verify anything.
5756 Otherwise, look for a parent that tests our value - 1. */
5758 if (! tree_int_cst_lt (low_minus_one, node->low))
5759 return 0;
5761 for (pnode = node->parent; pnode; pnode = pnode->parent)
5762 if (tree_int_cst_equal (low_minus_one, pnode->high))
5763 return 1;
5765 return 0;
5768 /* Search the parent sections of the case node tree
5769 to see if a test for the upper bound of NODE would be redundant.
5770 INDEX_TYPE is the type of the index expression.
5772 The instructions to generate the case decision tree are
5773 output in the same order as nodes are processed so it is
5774 known that if a parent node checks the range of the current
5775 node plus one that the current node is bounded at its upper
5776 span. Thus the test would be redundant. */
5778 static int
5779 node_has_high_bound (node, index_type)
5780 case_node_ptr node;
5781 tree index_type;
5783 tree high_plus_one;
5784 case_node_ptr pnode;
5786 /* If there is no upper bound, obviously no test is needed. */
5788 if (TYPE_MAX_VALUE (index_type) == NULL)
5789 return 1;
5791 /* If the upper bound of this node is the highest value in the type
5792 of the index expression, we need not test against it. */
5794 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
5795 return 1;
5797 /* If this node has a right branch, the value at the right must be greater
5798 than that at this node, so it cannot be bounded at the top and
5799 we need not bother testing any further. */
5801 if (node->right)
5802 return 0;
5804 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
5805 node->high, integer_one_node));
5807 /* If the addition above overflowed, we can't verify anything.
5808 Otherwise, look for a parent that tests our value + 1. */
5810 if (! tree_int_cst_lt (node->high, high_plus_one))
5811 return 0;
5813 for (pnode = node->parent; pnode; pnode = pnode->parent)
5814 if (tree_int_cst_equal (high_plus_one, pnode->low))
5815 return 1;
5817 return 0;
5820 /* Search the parent sections of the
5821 case node tree to see if both tests for the upper and lower
5822 bounds of NODE would be redundant. */
5824 static int
5825 node_is_bounded (node, index_type)
5826 case_node_ptr node;
5827 tree index_type;
5829 return (node_has_low_bound (node, index_type)
5830 && node_has_high_bound (node, index_type));
5833 /* Emit an unconditional jump to LABEL unless it would be dead code. */
5835 static void
5836 emit_jump_if_reachable (label)
5837 rtx label;
5839 if (GET_CODE (get_last_insn ()) != BARRIER)
5840 emit_jump (label);
5843 /* Emit step-by-step code to select a case for the value of INDEX.
5844 The thus generated decision tree follows the form of the
5845 case-node binary tree NODE, whose nodes represent test conditions.
5846 INDEX_TYPE is the type of the index of the switch.
5848 Care is taken to prune redundant tests from the decision tree
5849 by detecting any boundary conditions already checked by
5850 emitted rtx. (See node_has_high_bound, node_has_low_bound
5851 and node_is_bounded, above.)
5853 Where the test conditions can be shown to be redundant we emit
5854 an unconditional jump to the target code. As a further
5855 optimization, the subordinates of a tree node are examined to
5856 check for bounded nodes. In this case conditional and/or
5857 unconditional jumps as a result of the boundary check for the
5858 current node are arranged to target the subordinates associated
5859 code for out of bound conditions on the current node.
5861 We can assume that when control reaches the code generated here,
5862 the index value has already been compared with the parents
5863 of this node, and determined to be on the same side of each parent
5864 as this node is. Thus, if this node tests for the value 51,
5865 and a parent tested for 52, we don't need to consider
5866 the possibility of a value greater than 51. If another parent
5867 tests for the value 50, then this node need not test anything. */
5869 static void
5870 emit_case_nodes (index, node, default_label, index_type)
5871 rtx index;
5872 case_node_ptr node;
5873 rtx default_label;
5874 tree index_type;
5876 /* If INDEX has an unsigned type, we must make unsigned branches. */
5877 int unsignedp = TREE_UNSIGNED (index_type);
5878 enum machine_mode mode = GET_MODE (index);
5879 enum machine_mode imode = TYPE_MODE (index_type);
5881 /* See if our parents have already tested everything for us.
5882 If they have, emit an unconditional jump for this node. */
5883 if (node_is_bounded (node, index_type))
5884 emit_jump (label_rtx (node->code_label));
5886 else if (tree_int_cst_equal (node->low, node->high))
5888 /* Node is single valued. First see if the index expression matches
5889 this node and then check our children, if any. */
5891 do_jump_if_equal (index,
5892 convert_modes (mode, imode,
5893 expand_expr (node->low, NULL_RTX,
5894 VOIDmode, 0),
5895 unsignedp),
5896 label_rtx (node->code_label), unsignedp);
5898 if (node->right != 0 && node->left != 0)
5900 /* This node has children on both sides.
5901 Dispatch to one side or the other
5902 by comparing the index value with this node's value.
5903 If one subtree is bounded, check that one first,
5904 so we can avoid real branches in the tree. */
5906 if (node_is_bounded (node->right, index_type))
5908 emit_cmp_and_jump_insns (index,
5909 convert_modes
5910 (mode, imode,
5911 expand_expr (node->high, NULL_RTX,
5912 VOIDmode, 0),
5913 unsignedp),
5914 GT, NULL_RTX, mode, unsignedp, 0,
5915 label_rtx (node->right->code_label));
5916 emit_case_nodes (index, node->left, default_label, index_type);
5919 else if (node_is_bounded (node->left, index_type))
5921 emit_cmp_and_jump_insns (index,
5922 convert_modes
5923 (mode, imode,
5924 expand_expr (node->high, NULL_RTX,
5925 VOIDmode, 0),
5926 unsignedp),
5927 LT, NULL_RTX, mode, unsignedp, 0,
5928 label_rtx (node->left->code_label));
5929 emit_case_nodes (index, node->right, default_label, index_type);
5932 else
5934 /* Neither node is bounded. First distinguish the two sides;
5935 then emit the code for one side at a time. */
5937 tree test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5939 /* See if the value is on the right. */
5940 emit_cmp_and_jump_insns (index,
5941 convert_modes
5942 (mode, imode,
5943 expand_expr (node->high, NULL_RTX,
5944 VOIDmode, 0),
5945 unsignedp),
5946 GT, NULL_RTX, mode, unsignedp, 0,
5947 label_rtx (test_label));
5949 /* Value must be on the left.
5950 Handle the left-hand subtree. */
5951 emit_case_nodes (index, node->left, default_label, index_type);
5952 /* If left-hand subtree does nothing,
5953 go to default. */
5954 emit_jump_if_reachable (default_label);
5956 /* Code branches here for the right-hand subtree. */
5957 expand_label (test_label);
5958 emit_case_nodes (index, node->right, default_label, index_type);
5962 else if (node->right != 0 && node->left == 0)
5964 /* Here we have a right child but no left so we issue conditional
5965 branch to default and process the right child.
5967 Omit the conditional branch to default if we it avoid only one
5968 right child; it costs too much space to save so little time. */
5970 if (node->right->right || node->right->left
5971 || !tree_int_cst_equal (node->right->low, node->right->high))
5973 if (!node_has_low_bound (node, index_type))
5975 emit_cmp_and_jump_insns (index,
5976 convert_modes
5977 (mode, imode,
5978 expand_expr (node->high, NULL_RTX,
5979 VOIDmode, 0),
5980 unsignedp),
5981 LT, NULL_RTX, mode, unsignedp, 0,
5982 default_label);
5985 emit_case_nodes (index, node->right, default_label, index_type);
5987 else
5988 /* We cannot process node->right normally
5989 since we haven't ruled out the numbers less than
5990 this node's value. So handle node->right explicitly. */
5991 do_jump_if_equal (index,
5992 convert_modes
5993 (mode, imode,
5994 expand_expr (node->right->low, NULL_RTX,
5995 VOIDmode, 0),
5996 unsignedp),
5997 label_rtx (node->right->code_label), unsignedp);
6000 else if (node->right == 0 && node->left != 0)
6002 /* Just one subtree, on the left. */
6003 if (node->left->left || node->left->right
6004 || !tree_int_cst_equal (node->left->low, node->left->high))
6006 if (!node_has_high_bound (node, index_type))
6008 emit_cmp_and_jump_insns (index,
6009 convert_modes
6010 (mode, imode,
6011 expand_expr (node->high, NULL_RTX,
6012 VOIDmode, 0),
6013 unsignedp),
6014 GT, NULL_RTX, mode, unsignedp, 0,
6015 default_label);
6018 emit_case_nodes (index, node->left, default_label, index_type);
6020 else
6021 /* We cannot process node->left normally
6022 since we haven't ruled out the numbers less than
6023 this node's value. So handle node->left explicitly. */
6024 do_jump_if_equal (index,
6025 convert_modes
6026 (mode, imode,
6027 expand_expr (node->left->low, NULL_RTX,
6028 VOIDmode, 0),
6029 unsignedp),
6030 label_rtx (node->left->code_label), unsignedp);
6033 else
6035 /* Node is a range. These cases are very similar to those for a single
6036 value, except that we do not start by testing whether this node
6037 is the one to branch to. */
6039 if (node->right != 0 && node->left != 0)
6041 /* Node has subtrees on both sides.
6042 If the right-hand subtree is bounded,
6043 test for it first, since we can go straight there.
6044 Otherwise, we need to make a branch in the control structure,
6045 then handle the two subtrees. */
6046 tree test_label = 0;
6048 if (node_is_bounded (node->right, index_type))
6049 /* Right hand node is fully bounded so we can eliminate any
6050 testing and branch directly to the target code. */
6051 emit_cmp_and_jump_insns (index,
6052 convert_modes
6053 (mode, imode,
6054 expand_expr (node->high, NULL_RTX,
6055 VOIDmode, 0),
6056 unsignedp),
6057 GT, NULL_RTX, mode, unsignedp, 0,
6058 label_rtx (node->right->code_label));
6059 else
6061 /* Right hand node requires testing.
6062 Branch to a label where we will handle it later. */
6064 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
6065 emit_cmp_and_jump_insns (index,
6066 convert_modes
6067 (mode, imode,
6068 expand_expr (node->high, NULL_RTX,
6069 VOIDmode, 0),
6070 unsignedp),
6071 GT, NULL_RTX, mode, unsignedp, 0,
6072 label_rtx (test_label));
6075 /* Value belongs to this node or to the left-hand subtree. */
6077 emit_cmp_and_jump_insns (index,
6078 convert_modes
6079 (mode, imode,
6080 expand_expr (node->low, NULL_RTX,
6081 VOIDmode, 0),
6082 unsignedp),
6083 GE, NULL_RTX, mode, unsignedp, 0,
6084 label_rtx (node->code_label));
6086 /* Handle the left-hand subtree. */
6087 emit_case_nodes (index, node->left, default_label, index_type);
6089 /* If right node had to be handled later, do that now. */
6091 if (test_label)
6093 /* If the left-hand subtree fell through,
6094 don't let it fall into the right-hand subtree. */
6095 emit_jump_if_reachable (default_label);
6097 expand_label (test_label);
6098 emit_case_nodes (index, node->right, default_label, index_type);
6102 else if (node->right != 0 && node->left == 0)
6104 /* Deal with values to the left of this node,
6105 if they are possible. */
6106 if (!node_has_low_bound (node, index_type))
6108 emit_cmp_and_jump_insns (index,
6109 convert_modes
6110 (mode, imode,
6111 expand_expr (node->low, NULL_RTX,
6112 VOIDmode, 0),
6113 unsignedp),
6114 LT, NULL_RTX, mode, unsignedp, 0,
6115 default_label);
6118 /* Value belongs to this node or to the right-hand subtree. */
6120 emit_cmp_and_jump_insns (index,
6121 convert_modes
6122 (mode, imode,
6123 expand_expr (node->high, NULL_RTX,
6124 VOIDmode, 0),
6125 unsignedp),
6126 LE, NULL_RTX, mode, unsignedp, 0,
6127 label_rtx (node->code_label));
6129 emit_case_nodes (index, node->right, default_label, index_type);
6132 else if (node->right == 0 && node->left != 0)
6134 /* Deal with values to the right of this node,
6135 if they are possible. */
6136 if (!node_has_high_bound (node, index_type))
6138 emit_cmp_and_jump_insns (index,
6139 convert_modes
6140 (mode, imode,
6141 expand_expr (node->high, NULL_RTX,
6142 VOIDmode, 0),
6143 unsignedp),
6144 GT, NULL_RTX, mode, unsignedp, 0,
6145 default_label);
6148 /* Value belongs to this node or to the left-hand subtree. */
6150 emit_cmp_and_jump_insns (index,
6151 convert_modes
6152 (mode, imode,
6153 expand_expr (node->low, NULL_RTX,
6154 VOIDmode, 0),
6155 unsignedp),
6156 GE, NULL_RTX, mode, unsignedp, 0,
6157 label_rtx (node->code_label));
6159 emit_case_nodes (index, node->left, default_label, index_type);
6162 else
6164 /* Node has no children so we check low and high bounds to remove
6165 redundant tests. Only one of the bounds can exist,
6166 since otherwise this node is bounded--a case tested already. */
6167 int high_bound = node_has_high_bound (node, index_type);
6168 int low_bound = node_has_low_bound (node, index_type);
6170 if (!high_bound && low_bound)
6172 emit_cmp_and_jump_insns (index,
6173 convert_modes
6174 (mode, imode,
6175 expand_expr (node->high, NULL_RTX,
6176 VOIDmode, 0),
6177 unsignedp),
6178 GT, NULL_RTX, mode, unsignedp, 0,
6179 default_label);
6182 else if (!low_bound && high_bound)
6184 emit_cmp_and_jump_insns (index,
6185 convert_modes
6186 (mode, imode,
6187 expand_expr (node->low, NULL_RTX,
6188 VOIDmode, 0),
6189 unsignedp),
6190 LT, NULL_RTX, mode, unsignedp, 0,
6191 default_label);
6193 else if (!low_bound && !high_bound)
6195 /* Widen LOW and HIGH to the same width as INDEX. */
6196 tree type = type_for_mode (mode, unsignedp);
6197 tree low = build1 (CONVERT_EXPR, type, node->low);
6198 tree high = build1 (CONVERT_EXPR, type, node->high);
6199 rtx low_rtx, new_index, new_bound;
6201 /* Instead of doing two branches, emit one unsigned branch for
6202 (index-low) > (high-low). */
6203 low_rtx = expand_expr (low, NULL_RTX, mode, 0);
6204 new_index = expand_simple_binop (mode, MINUS, index, low_rtx,
6205 NULL_RTX, unsignedp,
6206 OPTAB_WIDEN);
6207 new_bound = expand_expr (fold (build (MINUS_EXPR, type,
6208 high, low)),
6209 NULL_RTX, mode, 0);
6211 emit_cmp_and_jump_insns (new_index, new_bound, GT, NULL_RTX,
6212 mode, 1, 0, default_label);
6215 emit_jump (label_rtx (node->code_label));