]> gcc.gnu.org Git - gcc.git/blame - gcc/stmt.c
Do not define strcmp=__builtin_strcmp if not -fbuiltin/-fno-builtin
[gcc.git] / gcc / stmt.c
CommitLineData
28d81abb
RK
1/* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21/* This file handles the generation of rtl code from tree structure
22 above the level of expressions, using subroutines in exp*.c and emit-rtl.c.
23 It also creates the rtl expressions for parameters and auto variables
24 and has full responsibility for allocating stack slots.
25
26 The functions whose names start with `expand_' are called by the
27 parser to generate RTL instructions for various kinds of constructs.
28
29 Some control and binding constructs require calling several such
30 functions at different times. For example, a simple if-then
31 is expanded by calling `expand_start_cond' (with the condition-expression
32 as argument) before parsing the then-clause and calling `expand_end_cond'
33 after parsing the then-clause. */
34
35#include "config.h"
36
37#include <stdio.h>
38#include <ctype.h>
39
40#include "rtl.h"
41#include "tree.h"
42#include "flags.h"
43#include "function.h"
44#include "insn-flags.h"
45#include "insn-config.h"
46#include "insn-codes.h"
47#include "expr.h"
48#include "hard-reg-set.h"
49#include "obstack.h"
50#include "loop.h"
51#include "recog.h"
52
53#define obstack_chunk_alloc xmalloc
54#define obstack_chunk_free free
55struct obstack stmt_obstack;
56
28d81abb
RK
57/* Filename and line number of last line-number note,
58 whether we actually emitted it or not. */
59char *emit_filename;
60int emit_lineno;
61
62/* Nonzero if within a ({...}) grouping, in which case we must
63 always compute a value for each expr-stmt in case it is the last one. */
64
65int expr_stmts_for_value;
66
67/* Each time we expand an expression-statement,
68 record the expr's type and its RTL value here. */
69
70static tree last_expr_type;
71static rtx last_expr_value;
72
73/* Number of binding contours started so far in this function. */
74
75int block_start_count;
76
77/* Nonzero if function being compiled needs to
78 return the address of where it has put a structure value. */
79
80extern int current_function_returns_pcc_struct;
81
82/* Label that will go on parm cleanup code, if any.
83 Jumping to this label runs cleanup code for parameters, if
84 such code must be run. Following this code is the logical return label. */
85
86extern rtx cleanup_label;
87
88/* Label that will go on function epilogue.
89 Jumping to this label serves as a "return" instruction
90 on machines which require execution of the epilogue on all returns. */
91
92extern rtx return_label;
93
94/* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
95 So we can mark them all live at the end of the function, if nonopt. */
96extern rtx save_expr_regs;
97
98/* Offset to end of allocated area of stack frame.
99 If stack grows down, this is the address of the last stack slot allocated.
100 If stack grows up, this is the address for the next slot. */
101extern int frame_offset;
102
103/* Label to jump back to for tail recursion, or 0 if we have
104 not yet needed one for this function. */
105extern rtx tail_recursion_label;
106
107/* Place after which to insert the tail_recursion_label if we need one. */
108extern rtx tail_recursion_reentry;
109
110/* Location at which to save the argument pointer if it will need to be
111 referenced. There are two cases where this is done: if nonlocal gotos
112 exist, or if vars whose is an offset from the argument pointer will be
113 needed by inner routines. */
114
115extern rtx arg_pointer_save_area;
116
117/* Chain of all RTL_EXPRs that have insns in them. */
118extern tree rtl_expr_chain;
119
120#if 0 /* Turned off because 0 seems to work just as well. */
121/* Cleanup lists are required for binding levels regardless of whether
122 that binding level has cleanups or not. This node serves as the
123 cleanup list whenever an empty list is required. */
124static tree empty_cleanup_list;
125#endif
126\f
127/* Functions and data structures for expanding case statements. */
128
129/* Case label structure, used to hold info on labels within case
130 statements. We handle "range" labels; for a single-value label
131 as in C, the high and low limits are the same.
132
133 A chain of case nodes is initially maintained via the RIGHT fields
134 in the nodes. Nodes with higher case values are later in the list.
135
136 Switch statements can be output in one of two forms. A branch table
137 is used if there are more than a few labels and the labels are dense
138 within the range between the smallest and largest case value. If a
139 branch table is used, no further manipulations are done with the case
140 node chain.
141
142 The alternative to the use of a branch table is to generate a series
143 of compare and jump insns. When that is done, we use the LEFT, RIGHT,
144 and PARENT fields to hold a binary tree. Initially the tree is
de14fd73
RK
145 totally unbalanced, with everything on the right. We balance the tree
146 with nodes on the left having lower case values than the parent
28d81abb
RK
147 and nodes on the right having higher values. We then output the tree
148 in order. */
149
150struct case_node
151{
152 struct case_node *left; /* Left son in binary tree */
153 struct case_node *right; /* Right son in binary tree; also node chain */
154 struct case_node *parent; /* Parent of node in binary tree */
155 tree low; /* Lowest index value for this label */
156 tree high; /* Highest index value for this label */
157 tree code_label; /* Label to jump to when node matches */
158};
159
160typedef struct case_node case_node;
161typedef struct case_node *case_node_ptr;
162
163/* These are used by estimate_case_costs and balance_case_nodes. */
164
165/* This must be a signed type, and non-ANSI compilers lack signed char. */
166static short *cost_table;
167static int use_cost_table;
168
169static int estimate_case_costs ();
170static void balance_case_nodes ();
171static void emit_case_nodes ();
172static void group_case_nodes ();
173static void emit_jump_if_reachable ();
174
175static int warn_if_unused_value ();
176static void expand_goto_internal ();
177static int expand_fixup ();
178void fixup_gotos ();
179void free_temp_slots ();
180static void expand_cleanups ();
181static void fixup_cleanups ();
182static void expand_null_return_1 ();
183static int tail_recursion_args ();
184static void do_jump_if_equal ();
185\f
186/* Stack of control and binding constructs we are currently inside.
187
188 These constructs begin when you call `expand_start_WHATEVER'
189 and end when you call `expand_end_WHATEVER'. This stack records
190 info about how the construct began that tells the end-function
191 what to do. It also may provide information about the construct
192 to alter the behavior of other constructs within the body.
193 For example, they may affect the behavior of C `break' and `continue'.
194
195 Each construct gets one `struct nesting' object.
196 All of these objects are chained through the `all' field.
197 `nesting_stack' points to the first object (innermost construct).
198 The position of an entry on `nesting_stack' is in its `depth' field.
199
200 Each type of construct has its own individual stack.
201 For example, loops have `loop_stack'. Each object points to the
202 next object of the same type through the `next' field.
203
204 Some constructs are visible to `break' exit-statements and others
205 are not. Which constructs are visible depends on the language.
206 Therefore, the data structure allows each construct to be visible
207 or not, according to the args given when the construct is started.
208 The construct is visible if the `exit_label' field is non-null.
209 In that case, the value should be a CODE_LABEL rtx. */
210
211struct nesting
212{
213 struct nesting *all;
214 struct nesting *next;
215 int depth;
216 rtx exit_label;
217 union
218 {
219 /* For conds (if-then and if-then-else statements). */
220 struct
221 {
222 /* Label for the end of the if construct.
223 There is none if EXITFLAG was not set
224 and no `else' has been seen yet. */
225 rtx endif_label;
226 /* Label for the end of this alternative.
227 This may be the end of the if or the next else/elseif. */
228 rtx next_label;
229 } cond;
230 /* For loops. */
231 struct
232 {
233 /* Label at the top of the loop; place to loop back to. */
234 rtx start_label;
235 /* Label at the end of the whole construct. */
236 rtx end_label;
237 /* Label for `continue' statement to jump to;
238 this is in front of the stepper of the loop. */
239 rtx continue_label;
240 } loop;
241 /* For variable binding contours. */
242 struct
243 {
244 /* Sequence number of this binding contour within the function,
245 in order of entry. */
246 int block_start_count;
247 /* Nonzero => value to restore stack to on exit. */
248 rtx stack_level;
249 /* The NOTE that starts this contour.
250 Used by expand_goto to check whether the destination
251 is within each contour or not. */
252 rtx first_insn;
253 /* Innermost containing binding contour that has a stack level. */
254 struct nesting *innermost_stack_block;
255 /* List of cleanups to be run on exit from this contour.
256 This is a list of expressions to be evaluated.
257 The TREE_PURPOSE of each link is the ..._DECL node
258 which the cleanup pertains to. */
259 tree cleanups;
260 /* List of cleanup-lists of blocks containing this block,
261 as they were at the locus where this block appears.
262 There is an element for each containing block,
263 ordered innermost containing block first.
264 The tail of this list can be 0 (was empty_cleanup_list),
265 if all remaining elements would be empty lists.
266 The element's TREE_VALUE is the cleanup-list of that block,
267 which may be null. */
268 tree outer_cleanups;
269 /* Chain of labels defined inside this binding contour.
270 For contours that have stack levels or cleanups. */
271 struct label_chain *label_chain;
272 /* Number of function calls seen, as of start of this block. */
273 int function_call_count;
274 } block;
275 /* For switch (C) or case (Pascal) statements,
276 and also for dummies (see `expand_start_case_dummy'). */
277 struct
278 {
279 /* The insn after which the case dispatch should finally
280 be emitted. Zero for a dummy. */
281 rtx start;
282 /* A list of case labels, kept in ascending order by value
283 as the list is built.
284 During expand_end_case, this list may be rearranged into a
285 nearly balanced binary tree. */
286 struct case_node *case_list;
287 /* Label to jump to if no case matches. */
288 tree default_label;
289 /* The expression to be dispatched on. */
290 tree index_expr;
291 /* Type that INDEX_EXPR should be converted to. */
292 tree nominal_type;
293 /* Number of range exprs in case statement. */
294 int num_ranges;
295 /* Name of this kind of statement, for warnings. */
296 char *printname;
297 /* Nonzero if a case label has been seen in this case stmt. */
298 char seenlabel;
299 } case_stmt;
300 /* For exception contours. */
301 struct
302 {
303 /* List of exceptions raised. This is a TREE_LIST
304 of whatever you want. */
305 tree raised;
306 /* List of exceptions caught. This is also a TREE_LIST
307 of whatever you want. As a special case, it has the
308 value `void_type_node' if it handles default exceptions. */
309 tree handled;
310
311 /* First insn of TRY block, in case resumptive model is needed. */
312 rtx first_insn;
313 /* Label for the catch clauses. */
314 rtx except_label;
315 /* Label for unhandled exceptions. */
316 rtx unhandled_label;
317 /* Label at the end of whole construct. */
318 rtx after_label;
319 /* Label which "escapes" the exception construct.
320 Like EXIT_LABEL for BREAK construct, but for exceptions. */
321 rtx escape_label;
322 } except_stmt;
323 } data;
324};
325
326/* Chain of all pending binding contours. */
327struct nesting *block_stack;
328
329/* Chain of all pending binding contours that restore stack levels
330 or have cleanups. */
331struct nesting *stack_block_stack;
332
333/* Chain of all pending conditional statements. */
334struct nesting *cond_stack;
335
336/* Chain of all pending loops. */
337struct nesting *loop_stack;
338
339/* Chain of all pending case or switch statements. */
340struct nesting *case_stack;
341
342/* Chain of all pending exception contours. */
343struct nesting *except_stack;
344
345/* Separate chain including all of the above,
346 chained through the `all' field. */
347struct nesting *nesting_stack;
348
349/* Number of entries on nesting_stack now. */
350int nesting_depth;
351
352/* Allocate and return a new `struct nesting'. */
353
354#define ALLOC_NESTING() \
355 (struct nesting *) obstack_alloc (&stmt_obstack, sizeof (struct nesting))
356
357/* Pop one of the sub-stacks, such as `loop_stack' or `cond_stack';
358 and pop off `nesting_stack' down to the same level. */
359
360#define POPSTACK(STACK) \
361do { int initial_depth = nesting_stack->depth; \
362 do { struct nesting *this = STACK; \
363 STACK = this->next; \
364 nesting_stack = this->all; \
365 nesting_depth = this->depth; \
366 obstack_free (&stmt_obstack, this); } \
367 while (nesting_depth > initial_depth); } while (0)
368\f
369/* In some cases it is impossible to generate code for a forward goto
370 until the label definition is seen. This happens when it may be necessary
371 for the goto to reset the stack pointer: we don't yet know how to do that.
372 So expand_goto puts an entry on this fixup list.
373 Each time a binding contour that resets the stack is exited,
374 we check each fixup.
375 If the target label has now been defined, we can insert the proper code. */
376
377struct goto_fixup
378{
379 /* Points to following fixup. */
380 struct goto_fixup *next;
381 /* Points to the insn before the jump insn.
382 If more code must be inserted, it goes after this insn. */
383 rtx before_jump;
384 /* The LABEL_DECL that this jump is jumping to, or 0
385 for break, continue or return. */
386 tree target;
387 /* The CODE_LABEL rtx that this is jumping to. */
388 rtx target_rtl;
389 /* Number of binding contours started in current function
390 before the label reference. */
391 int block_start_count;
392 /* The outermost stack level that should be restored for this jump.
393 Each time a binding contour that resets the stack is exited,
394 if the target label is *not* yet defined, this slot is updated. */
395 rtx stack_level;
396 /* List of lists of cleanup expressions to be run by this goto.
397 There is one element for each block that this goto is within.
398 The tail of this list can be 0 (was empty_cleanup_list),
399 if all remaining elements would be empty.
400 The TREE_VALUE contains the cleanup list of that block as of the
401 time this goto was seen.
402 The TREE_ADDRESSABLE flag is 1 for a block that has been exited. */
403 tree cleanup_list_list;
404};
405
406static struct goto_fixup *goto_fixup_chain;
407
408/* Within any binding contour that must restore a stack level,
409 all labels are recorded with a chain of these structures. */
410
411struct label_chain
412{
413 /* Points to following fixup. */
414 struct label_chain *next;
415 tree label;
416};
417\f
418void
419init_stmt ()
420{
421 gcc_obstack_init (&stmt_obstack);
422#if 0
423 empty_cleanup_list = build_tree_list (NULL_TREE, NULL_TREE);
424#endif
425}
426
427void
428init_stmt_for_function ()
429{
430 /* We are not currently within any block, conditional, loop or case. */
431 block_stack = 0;
432 loop_stack = 0;
433 case_stack = 0;
434 cond_stack = 0;
435 nesting_stack = 0;
436 nesting_depth = 0;
437
438 block_start_count = 0;
439
440 /* No gotos have been expanded yet. */
441 goto_fixup_chain = 0;
442
443 /* We are not processing a ({...}) grouping. */
444 expr_stmts_for_value = 0;
445 last_expr_type = 0;
446}
447
448void
449save_stmt_status (p)
450 struct function *p;
451{
452 p->block_stack = block_stack;
453 p->stack_block_stack = stack_block_stack;
454 p->cond_stack = cond_stack;
455 p->loop_stack = loop_stack;
456 p->case_stack = case_stack;
457 p->nesting_stack = nesting_stack;
458 p->nesting_depth = nesting_depth;
459 p->block_start_count = block_start_count;
460 p->last_expr_type = last_expr_type;
461 p->last_expr_value = last_expr_value;
462 p->expr_stmts_for_value = expr_stmts_for_value;
463 p->emit_filename = emit_filename;
464 p->emit_lineno = emit_lineno;
465 p->goto_fixup_chain = goto_fixup_chain;
466}
467
468void
469restore_stmt_status (p)
470 struct function *p;
471{
472 block_stack = p->block_stack;
473 stack_block_stack = p->stack_block_stack;
474 cond_stack = p->cond_stack;
475 loop_stack = p->loop_stack;
476 case_stack = p->case_stack;
477 nesting_stack = p->nesting_stack;
478 nesting_depth = p->nesting_depth;
479 block_start_count = p->block_start_count;
480 last_expr_type = p->last_expr_type;
481 last_expr_value = p->last_expr_value;
482 expr_stmts_for_value = p->expr_stmts_for_value;
483 emit_filename = p->emit_filename;
484 emit_lineno = p->emit_lineno;
485 goto_fixup_chain = p->goto_fixup_chain;
486}
487\f
488/* Emit a no-op instruction. */
489
490void
491emit_nop ()
492{
493 rtx last_insn = get_last_insn ();
494 if (!optimize
495 && (GET_CODE (last_insn) == CODE_LABEL
496 || prev_real_insn (last_insn) == 0))
497 emit_insn (gen_nop ());
498}
499\f
500/* Return the rtx-label that corresponds to a LABEL_DECL,
501 creating it if necessary. */
502
503rtx
504label_rtx (label)
505 tree label;
506{
507 if (TREE_CODE (label) != LABEL_DECL)
508 abort ();
509
510 if (DECL_RTL (label))
511 return DECL_RTL (label);
512
513 return DECL_RTL (label) = gen_label_rtx ();
514}
515
516/* Add an unconditional jump to LABEL as the next sequential instruction. */
517
518void
519emit_jump (label)
520 rtx label;
521{
522 do_pending_stack_adjust ();
523 emit_jump_insn (gen_jump (label));
524 emit_barrier ();
525}
526
527/* Emit code to jump to the address
528 specified by the pointer expression EXP. */
529
530void
531expand_computed_goto (exp)
532 tree exp;
533{
37366632 534 rtx x = expand_expr (exp, NULL_RTX, VOIDmode, 0);
de14fd73 535 emit_queue ();
28d81abb 536 emit_indirect_jump (x);
28d81abb
RK
537}
538\f
539/* Handle goto statements and the labels that they can go to. */
540
541/* Specify the location in the RTL code of a label LABEL,
542 which is a LABEL_DECL tree node.
543
544 This is used for the kind of label that the user can jump to with a
545 goto statement, and for alternatives of a switch or case statement.
546 RTL labels generated for loops and conditionals don't go through here;
547 they are generated directly at the RTL level, by other functions below.
548
549 Note that this has nothing to do with defining label *names*.
550 Languages vary in how they do that and what that even means. */
551
552void
553expand_label (label)
554 tree label;
555{
556 struct label_chain *p;
557
558 do_pending_stack_adjust ();
559 emit_label (label_rtx (label));
560 if (DECL_NAME (label))
561 LABEL_NAME (DECL_RTL (label)) = IDENTIFIER_POINTER (DECL_NAME (label));
562
563 if (stack_block_stack != 0)
564 {
565 p = (struct label_chain *) oballoc (sizeof (struct label_chain));
566 p->next = stack_block_stack->data.block.label_chain;
567 stack_block_stack->data.block.label_chain = p;
568 p->label = label;
569 }
570}
571
572/* Declare that LABEL (a LABEL_DECL) may be used for nonlocal gotos
573 from nested functions. */
574
575void
576declare_nonlocal_label (label)
577 tree label;
578{
579 nonlocal_labels = tree_cons (NULL_TREE, label, nonlocal_labels);
580 LABEL_PRESERVE_P (label_rtx (label)) = 1;
581 if (nonlocal_goto_handler_slot == 0)
582 {
583 nonlocal_goto_handler_slot
584 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
59257ff7
RK
585 emit_stack_save (SAVE_NONLOCAL,
586 &nonlocal_goto_stack_level,
587 PREV_INSN (tail_recursion_reentry));
28d81abb
RK
588 }
589}
590
591/* Generate RTL code for a `goto' statement with target label LABEL.
592 LABEL should be a LABEL_DECL tree node that was or will later be
593 defined with `expand_label'. */
594
595void
596expand_goto (label)
597 tree label;
598{
599 /* Check for a nonlocal goto to a containing function. */
600 tree context = decl_function_context (label);
601 if (context != 0 && context != current_function_decl)
602 {
603 struct function *p = find_function_data (context);
604 rtx temp;
605 p->has_nonlocal_label = 1;
59257ff7
RK
606
607 /* Copy the rtl for the slots so that they won't be shared in
608 case the virtual stack vars register gets instantiated differently
609 in the parent than in the child. */
610
28d81abb
RK
611#if HAVE_nonlocal_goto
612 if (HAVE_nonlocal_goto)
613 emit_insn (gen_nonlocal_goto (lookup_static_chain (label),
59257ff7
RK
614 copy_rtx (p->nonlocal_goto_handler_slot),
615 copy_rtx (p->nonlocal_goto_stack_level),
28d81abb
RK
616 gen_rtx (LABEL_REF, Pmode,
617 label_rtx (label))));
618 else
619#endif
620 {
59257ff7
RK
621 rtx addr;
622
28d81abb
RK
623 /* Restore frame pointer for containing function.
624 This sets the actual hard register used for the frame pointer
625 to the location of the function's incoming static chain info.
626 The non-local goto handler will then adjust it to contain the
627 proper value and reload the argument pointer, if needed. */
628 emit_move_insn (frame_pointer_rtx, lookup_static_chain (label));
59257ff7
RK
629
630 /* We have now loaded the frame pointer hardware register with
631 the address of that corresponds to the start of the virtual
632 stack vars. So replace virtual_stack_vars_rtx in all
633 addresses we use with stack_pointer_rtx. */
634
28d81abb
RK
635 /* Get addr of containing function's current nonlocal goto handler,
636 which will do any cleanups and then jump to the label. */
59257ff7
RK
637 addr = copy_rtx (p->nonlocal_goto_handler_slot);
638 temp = copy_to_reg (replace_rtx (addr, virtual_stack_vars_rtx,
639 frame_pointer_rtx));
640
28d81abb 641 /* Restore the stack pointer. Note this uses fp just restored. */
59257ff7
RK
642 addr = p->nonlocal_goto_stack_level;
643 if (addr)
5e116627
MM
644 addr = replace_rtx (copy_rtx (addr),
645 virtual_stack_vars_rtx, frame_pointer_rtx);
59257ff7 646
37366632 647 emit_stack_restore (SAVE_NONLOCAL, addr, NULL_RTX);
59257ff7 648
28d81abb
RK
649 /* Put in the static chain register the nonlocal label address. */
650 emit_move_insn (static_chain_rtx,
651 gen_rtx (LABEL_REF, Pmode, label_rtx (label)));
652 /* USE of frame_pointer_rtx added for consistency; not clear if
653 really needed. */
654 emit_insn (gen_rtx (USE, VOIDmode, frame_pointer_rtx));
655 emit_insn (gen_rtx (USE, VOIDmode, stack_pointer_rtx));
656 emit_insn (gen_rtx (USE, VOIDmode, static_chain_rtx));
657 emit_indirect_jump (temp);
658 }
659 }
660 else
37366632 661 expand_goto_internal (label, label_rtx (label), NULL_RTX);
28d81abb
RK
662}
663
664/* Generate RTL code for a `goto' statement with target label BODY.
665 LABEL should be a LABEL_REF.
666 LAST_INSN, if non-0, is the rtx we should consider as the last
667 insn emitted (for the purposes of cleaning up a return). */
668
669static void
670expand_goto_internal (body, label, last_insn)
671 tree body;
672 rtx label;
673 rtx last_insn;
674{
675 struct nesting *block;
676 rtx stack_level = 0;
677
678 if (GET_CODE (label) != CODE_LABEL)
679 abort ();
680
681 /* If label has already been defined, we can tell now
682 whether and how we must alter the stack level. */
683
684 if (PREV_INSN (label) != 0)
685 {
686 /* Find the innermost pending block that contains the label.
687 (Check containment by comparing insn-uids.)
688 Then restore the outermost stack level within that block,
689 and do cleanups of all blocks contained in it. */
690 for (block = block_stack; block; block = block->next)
691 {
692 if (INSN_UID (block->data.block.first_insn) < INSN_UID (label))
693 break;
694 if (block->data.block.stack_level != 0)
695 stack_level = block->data.block.stack_level;
696 /* Execute the cleanups for blocks we are exiting. */
697 if (block->data.block.cleanups != 0)
698 {
37366632 699 expand_cleanups (block->data.block.cleanups, NULL_TREE);
28d81abb
RK
700 do_pending_stack_adjust ();
701 }
702 }
703
704 if (stack_level)
705 {
706 /* Ensure stack adjust isn't done by emit_jump, as this would clobber
707 the stack pointer. This one should be deleted as dead by flow. */
708 clear_pending_stack_adjust ();
709 do_pending_stack_adjust ();
37366632 710 emit_stack_restore (SAVE_BLOCK, stack_level, NULL_RTX);
28d81abb
RK
711 }
712
713 if (body != 0 && DECL_TOO_LATE (body))
714 error ("jump to `%s' invalidly jumps into binding contour",
715 IDENTIFIER_POINTER (DECL_NAME (body)));
716 }
717 /* Label not yet defined: may need to put this goto
718 on the fixup list. */
719 else if (! expand_fixup (body, label, last_insn))
720 {
721 /* No fixup needed. Record that the label is the target
722 of at least one goto that has no fixup. */
723 if (body != 0)
724 TREE_ADDRESSABLE (body) = 1;
725 }
726
727 emit_jump (label);
728}
729\f
730/* Generate if necessary a fixup for a goto
731 whose target label in tree structure (if any) is TREE_LABEL
732 and whose target in rtl is RTL_LABEL.
733
734 If LAST_INSN is nonzero, we pretend that the jump appears
735 after insn LAST_INSN instead of at the current point in the insn stream.
736
737 The fixup will be used later to insert insns at this point
738 to restore the stack level as appropriate for the target label.
739
740 Value is nonzero if a fixup is made. */
741
742static int
743expand_fixup (tree_label, rtl_label, last_insn)
744 tree tree_label;
745 rtx rtl_label;
746 rtx last_insn;
747{
748 struct nesting *block, *end_block;
749
750 /* See if we can recognize which block the label will be output in.
751 This is possible in some very common cases.
752 If we succeed, set END_BLOCK to that block.
753 Otherwise, set it to 0. */
754
755 if (cond_stack
756 && (rtl_label == cond_stack->data.cond.endif_label
757 || rtl_label == cond_stack->data.cond.next_label))
758 end_block = cond_stack;
759 /* If we are in a loop, recognize certain labels which
760 are likely targets. This reduces the number of fixups
761 we need to create. */
762 else if (loop_stack
763 && (rtl_label == loop_stack->data.loop.start_label
764 || rtl_label == loop_stack->data.loop.end_label
765 || rtl_label == loop_stack->data.loop.continue_label))
766 end_block = loop_stack;
767 else
768 end_block = 0;
769
770 /* Now set END_BLOCK to the binding level to which we will return. */
771
772 if (end_block)
773 {
774 struct nesting *next_block = end_block->all;
775 block = block_stack;
776
777 /* First see if the END_BLOCK is inside the innermost binding level.
778 If so, then no cleanups or stack levels are relevant. */
779 while (next_block && next_block != block)
780 next_block = next_block->all;
781
782 if (next_block)
783 return 0;
784
785 /* Otherwise, set END_BLOCK to the innermost binding level
786 which is outside the relevant control-structure nesting. */
787 next_block = block_stack->next;
788 for (block = block_stack; block != end_block; block = block->all)
789 if (block == next_block)
790 next_block = next_block->next;
791 end_block = next_block;
792 }
793
794 /* Does any containing block have a stack level or cleanups?
795 If not, no fixup is needed, and that is the normal case
796 (the only case, for standard C). */
797 for (block = block_stack; block != end_block; block = block->next)
798 if (block->data.block.stack_level != 0
799 || block->data.block.cleanups != 0)
800 break;
801
802 if (block != end_block)
803 {
804 /* Ok, a fixup is needed. Add a fixup to the list of such. */
805 struct goto_fixup *fixup
806 = (struct goto_fixup *) oballoc (sizeof (struct goto_fixup));
807 /* In case an old stack level is restored, make sure that comes
808 after any pending stack adjust. */
809 /* ?? If the fixup isn't to come at the present position,
810 doing the stack adjust here isn't useful. Doing it with our
811 settings at that location isn't useful either. Let's hope
812 someone does it! */
813 if (last_insn == 0)
814 do_pending_stack_adjust ();
815 fixup->before_jump = last_insn ? last_insn : get_last_insn ();
816 fixup->target = tree_label;
817 fixup->target_rtl = rtl_label;
818 fixup->block_start_count = block_start_count;
819 fixup->stack_level = 0;
820 fixup->cleanup_list_list
821 = (((block->data.block.outer_cleanups
822#if 0
823 && block->data.block.outer_cleanups != empty_cleanup_list
824#endif
825 )
826 || block->data.block.cleanups)
37366632 827 ? tree_cons (NULL_TREE, block->data.block.cleanups,
28d81abb
RK
828 block->data.block.outer_cleanups)
829 : 0);
830 fixup->next = goto_fixup_chain;
831 goto_fixup_chain = fixup;
832 }
833
834 return block != 0;
835}
836
837/* When exiting a binding contour, process all pending gotos requiring fixups.
838 THISBLOCK is the structure that describes the block being exited.
839 STACK_LEVEL is the rtx for the stack level to restore exiting this contour.
840 CLEANUP_LIST is a list of expressions to evaluate on exiting this contour.
841 FIRST_INSN is the insn that began this contour.
842
843 Gotos that jump out of this contour must restore the
844 stack level and do the cleanups before actually jumping.
845
846 DONT_JUMP_IN nonzero means report error there is a jump into this
847 contour from before the beginning of the contour.
848 This is also done if STACK_LEVEL is nonzero. */
849
850void
851fixup_gotos (thisblock, stack_level, cleanup_list, first_insn, dont_jump_in)
852 struct nesting *thisblock;
853 rtx stack_level;
854 tree cleanup_list;
855 rtx first_insn;
856 int dont_jump_in;
857{
858 register struct goto_fixup *f, *prev;
859
860 /* F is the fixup we are considering; PREV is the previous one. */
861 /* We run this loop in two passes so that cleanups of exited blocks
862 are run first, and blocks that are exited are marked so
863 afterwards. */
864
865 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
866 {
867 /* Test for a fixup that is inactive because it is already handled. */
868 if (f->before_jump == 0)
869 {
870 /* Delete inactive fixup from the chain, if that is easy to do. */
871 if (prev != 0)
872 prev->next = f->next;
873 }
874 /* Has this fixup's target label been defined?
875 If so, we can finalize it. */
876 else if (PREV_INSN (f->target_rtl) != 0)
877 {
878 /* Get the first non-label after the label
879 this goto jumps to. If that's before this scope begins,
880 we don't have a jump into the scope. */
881 rtx after_label = f->target_rtl;
882 while (after_label != 0 && GET_CODE (after_label) == CODE_LABEL)
883 after_label = NEXT_INSN (after_label);
884
885 /* If this fixup jumped into this contour from before the beginning
886 of this contour, report an error. */
887 /* ??? Bug: this does not detect jumping in through intermediate
888 blocks that have stack levels or cleanups.
889 It detects only a problem with the innermost block
890 around the label. */
891 if (f->target != 0
892 && (dont_jump_in || stack_level || cleanup_list)
893 /* If AFTER_LABEL is 0, it means the jump goes to the end
894 of the rtl, which means it jumps into this scope. */
895 && (after_label == 0
896 || INSN_UID (first_insn) < INSN_UID (after_label))
897 && INSN_UID (first_insn) > INSN_UID (f->before_jump)
44fe2e80 898 && ! DECL_REGISTER (f->target))
28d81abb
RK
899 {
900 error_with_decl (f->target,
901 "label `%s' used before containing binding contour");
902 /* Prevent multiple errors for one label. */
44fe2e80 903 DECL_REGISTER (f->target) = 1;
28d81abb
RK
904 }
905
906 /* Execute cleanups for blocks this jump exits. */
907 if (f->cleanup_list_list)
908 {
909 tree lists;
910 for (lists = f->cleanup_list_list; lists; lists = TREE_CHAIN (lists))
911 /* Marked elements correspond to blocks that have been closed.
912 Do their cleanups. */
913 if (TREE_ADDRESSABLE (lists)
914 && TREE_VALUE (lists) != 0)
915 fixup_cleanups (TREE_VALUE (lists), &f->before_jump);
916 }
917
918 /* Restore stack level for the biggest contour that this
919 jump jumps out of. */
920 if (f->stack_level)
59257ff7 921 emit_stack_restore (SAVE_BLOCK, f->stack_level, f->before_jump);
28d81abb
RK
922 f->before_jump = 0;
923 }
924 }
925
926 /* Mark the cleanups of exited blocks so that they are executed
927 by the code above. */
928 for (prev = 0, f = goto_fixup_chain; f; prev = f, f = f->next)
929 if (f->before_jump != 0
930 && PREV_INSN (f->target_rtl) == 0
931 /* Label has still not appeared. If we are exiting a block with
932 a stack level to restore, that started before the fixup,
933 mark this stack level as needing restoration
934 when the fixup is later finalized.
935 Also mark the cleanup_list_list element for F
936 that corresponds to this block, so that ultimately
937 this block's cleanups will be executed by the code above. */
938 && thisblock != 0
939 /* Note: if THISBLOCK == 0 and we have a label that hasn't appeared,
940 it means the label is undefined. That's erroneous, but possible. */
941 && (thisblock->data.block.block_start_count
942 <= f->block_start_count))
943 {
944 tree lists = f->cleanup_list_list;
945 for (; lists; lists = TREE_CHAIN (lists))
946 /* If the following elt. corresponds to our containing block
947 then the elt. must be for this block. */
948 if (TREE_CHAIN (lists) == thisblock->data.block.outer_cleanups)
949 TREE_ADDRESSABLE (lists) = 1;
950
951 if (stack_level)
952 f->stack_level = stack_level;
953 }
954}
955\f
956/* Generate RTL for an asm statement (explicit assembler code).
957 BODY is a STRING_CST node containing the assembler code text,
958 or an ADDR_EXPR containing a STRING_CST. */
959
960void
961expand_asm (body)
962 tree body;
963{
964 if (TREE_CODE (body) == ADDR_EXPR)
965 body = TREE_OPERAND (body, 0);
966
967 emit_insn (gen_rtx (ASM_INPUT, VOIDmode,
968 TREE_STRING_POINTER (body)));
969 last_expr_type = 0;
970}
971
972/* Generate RTL for an asm statement with arguments.
973 STRING is the instruction template.
974 OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
975 Each output or input has an expression in the TREE_VALUE and
976 a constraint-string in the TREE_PURPOSE.
977 CLOBBERS is a list of STRING_CST nodes each naming a hard register
978 that is clobbered by this insn.
979
980 Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
981 Some elements of OUTPUTS may be replaced with trees representing temporary
982 values. The caller should copy those temporary values to the originally
983 specified lvalues.
984
985 VOL nonzero means the insn is volatile; don't optimize it. */
986
987void
988expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line)
989 tree string, outputs, inputs, clobbers;
990 int vol;
991 char *filename;
992 int line;
993{
994 rtvec argvec, constraints;
995 rtx body;
996 int ninputs = list_length (inputs);
997 int noutputs = list_length (outputs);
b4ccaa16 998 int nclobbers;
28d81abb
RK
999 tree tail;
1000 register int i;
1001 /* Vector of RTX's of evaluated output operands. */
1002 rtx *output_rtx = (rtx *) alloca (noutputs * sizeof (rtx));
1003 /* The insn we have emitted. */
1004 rtx insn;
1005
b4ccaa16
RS
1006 /* Count the number of meaningful clobbered registers, ignoring what
1007 we would ignore later. */
1008 nclobbers = 0;
1009 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
1010 {
1011 char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
c09e6498
RS
1012 i = decode_reg_name (regname);
1013 if (i >= 0 || i == -4)
b4ccaa16
RS
1014 ++nclobbers;
1015 }
1016
28d81abb
RK
1017 last_expr_type = 0;
1018
1019 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1020 {
1021 tree val = TREE_VALUE (tail);
1022 tree val1;
1023 int j;
1024 int found_equal;
1025
1026 /* If there's an erroneous arg, emit no insn. */
1027 if (TREE_TYPE (val) == error_mark_node)
1028 return;
1029
1030 /* Make sure constraint has `=' and does not have `+'. */
1031
1032 found_equal = 0;
1033 for (j = 0; j < TREE_STRING_LENGTH (TREE_PURPOSE (tail)); j++)
1034 {
1035 if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '+')
1036 {
1037 error ("output operand constraint contains `+'");
1038 return;
1039 }
1040 if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '=')
1041 found_equal = 1;
1042 }
1043 if (! found_equal)
1044 {
1045 error ("output operand constraint lacks `='");
1046 return;
1047 }
1048
1049 /* If an output operand is not a variable or indirect ref,
1050 or a part of one,
1051 create a SAVE_EXPR which is a pseudo-reg
1052 to act as an intermediate temporary.
1053 Make the asm insn write into that, then copy it to
1054 the real output operand. */
1055
1056 while (TREE_CODE (val) == COMPONENT_REF
1057 || TREE_CODE (val) == ARRAY_REF)
1058 val = TREE_OPERAND (val, 0);
1059
1060 if (TREE_CODE (val) != VAR_DECL
1061 && TREE_CODE (val) != PARM_DECL
1062 && TREE_CODE (val) != INDIRECT_REF)
1063 TREE_VALUE (tail) = save_expr (TREE_VALUE (tail));
1064
37366632 1065 output_rtx[i] = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
28d81abb
RK
1066 }
1067
1068 if (ninputs + noutputs > MAX_RECOG_OPERANDS)
1069 {
1070 error ("more than %d operands in `asm'", MAX_RECOG_OPERANDS);
1071 return;
1072 }
1073
1074 /* Make vectors for the expression-rtx and constraint strings. */
1075
1076 argvec = rtvec_alloc (ninputs);
1077 constraints = rtvec_alloc (ninputs);
1078
1079 body = gen_rtx (ASM_OPERANDS, VOIDmode,
1080 TREE_STRING_POINTER (string), "", 0, argvec, constraints,
1081 filename, line);
1082 MEM_VOLATILE_P (body) = vol;
1083
1084 /* Eval the inputs and put them into ARGVEC.
1085 Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
1086
1087 i = 0;
1088 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
1089 {
1090 int j;
1091
1092 /* If there's an erroneous arg, emit no insn,
1093 because the ASM_INPUT would get VOIDmode
1094 and that could cause a crash in reload. */
1095 if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
1096 return;
1097 if (TREE_PURPOSE (tail) == NULL_TREE)
1098 {
1099 error ("hard register `%s' listed as input operand to `asm'",
1100 TREE_STRING_POINTER (TREE_VALUE (tail)) );
1101 return;
1102 }
1103
1104 /* Make sure constraint has neither `=' nor `+'. */
1105
1106 for (j = 0; j < TREE_STRING_LENGTH (TREE_PURPOSE (tail)); j++)
1107 if (TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '='
1108 || TREE_STRING_POINTER (TREE_PURPOSE (tail))[j] == '+')
1109 {
1110 error ("input operand constraint contains `%c'",
1111 TREE_STRING_POINTER (TREE_PURPOSE (tail))[j]);
1112 return;
1113 }
1114
1115 XVECEXP (body, 3, i) /* argvec */
37366632 1116 = expand_expr (TREE_VALUE (tail), NULL_RTX, VOIDmode, 0);
28d81abb
RK
1117 XVECEXP (body, 4, i) /* constraints */
1118 = gen_rtx (ASM_INPUT, TYPE_MODE (TREE_TYPE (TREE_VALUE (tail))),
1119 TREE_STRING_POINTER (TREE_PURPOSE (tail)));
1120 i++;
1121 }
1122
1123 /* Protect all the operands from the queue,
1124 now that they have all been evaluated. */
1125
1126 for (i = 0; i < ninputs; i++)
1127 XVECEXP (body, 3, i) = protect_from_queue (XVECEXP (body, 3, i), 0);
1128
1129 for (i = 0; i < noutputs; i++)
1130 output_rtx[i] = protect_from_queue (output_rtx[i], 1);
1131
1132 /* Now, for each output, construct an rtx
1133 (set OUTPUT (asm_operands INSN OUTPUTNUMBER OUTPUTCONSTRAINT
1134 ARGVEC CONSTRAINTS))
1135 If there is more than one, put them inside a PARALLEL. */
1136
1137 if (noutputs == 1 && nclobbers == 0)
1138 {
1139 XSTR (body, 1) = TREE_STRING_POINTER (TREE_PURPOSE (outputs));
1140 insn = emit_insn (gen_rtx (SET, VOIDmode, output_rtx[0], body));
1141 }
1142 else if (noutputs == 0 && nclobbers == 0)
1143 {
1144 /* No output operands: put in a raw ASM_OPERANDS rtx. */
1145 insn = emit_insn (body);
1146 }
1147 else
1148 {
1149 rtx obody = body;
1150 int num = noutputs;
1151 if (num == 0) num = 1;
1152 body = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (num + nclobbers));
1153
1154 /* For each output operand, store a SET. */
1155
1156 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
1157 {
1158 XVECEXP (body, 0, i)
1159 = gen_rtx (SET, VOIDmode,
1160 output_rtx[i],
1161 gen_rtx (ASM_OPERANDS, VOIDmode,
1162 TREE_STRING_POINTER (string),
1163 TREE_STRING_POINTER (TREE_PURPOSE (tail)),
1164 i, argvec, constraints,
1165 filename, line));
1166 MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
1167 }
1168
1169 /* If there are no outputs (but there are some clobbers)
1170 store the bare ASM_OPERANDS into the PARALLEL. */
1171
1172 if (i == 0)
1173 XVECEXP (body, 0, i++) = obody;
1174
1175 /* Store (clobber REG) for each clobbered register specified. */
1176
b4ccaa16 1177 for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
28d81abb 1178 {
28d81abb 1179 char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
b4ac57ab 1180 int j = decode_reg_name (regname);
28d81abb 1181
b4ac57ab 1182 if (j < 0)
28d81abb 1183 {
c09e6498 1184 if (j == -3) /* `cc', which is not a register */
dcfedcd0
RK
1185 continue;
1186
c09e6498
RS
1187 if (j == -4) /* `memory', don't cache memory across asm */
1188 {
1189 XVECEXP (body, 0, i++) = gen_rtx (CLOBBER, VOIDmode, const0_rtx);
1190 continue;
1191 }
1192
28d81abb
RK
1193 error ("unknown register name `%s' in `asm'", regname);
1194 return;
1195 }
1196
1197 /* Use QImode since that's guaranteed to clobber just one reg. */
b4ccaa16 1198 XVECEXP (body, 0, i++)
28d81abb
RK
1199 = gen_rtx (CLOBBER, VOIDmode, gen_rtx (REG, QImode, j));
1200 }
1201
1202 insn = emit_insn (body);
1203 }
1204
1205 free_temp_slots ();
1206}
1207\f
1208/* Generate RTL to evaluate the expression EXP
1209 and remember it in case this is the VALUE in a ({... VALUE; }) constr. */
1210
1211void
1212expand_expr_stmt (exp)
1213 tree exp;
1214{
1215 /* If -W, warn about statements with no side effects,
1216 except for an explicit cast to void (e.g. for assert()), and
1217 except inside a ({...}) where they may be useful. */
1218 if (expr_stmts_for_value == 0 && exp != error_mark_node)
1219 {
1220 if (! TREE_SIDE_EFFECTS (exp) && (extra_warnings || warn_unused)
1221 && !(TREE_CODE (exp) == CONVERT_EXPR
1222 && TREE_TYPE (exp) == void_type_node))
1223 warning_with_file_and_line (emit_filename, emit_lineno,
1224 "statement with no effect");
1225 else if (warn_unused)
1226 warn_if_unused_value (exp);
1227 }
1228 last_expr_type = TREE_TYPE (exp);
1229 if (! flag_syntax_only)
37366632
RK
1230 last_expr_value = expand_expr (exp,
1231 (expr_stmts_for_value
1232 ? NULL_RTX : const0_rtx),
28d81abb
RK
1233 VOIDmode, 0);
1234
1235 /* If all we do is reference a volatile value in memory,
1236 copy it to a register to be sure it is actually touched. */
1237 if (last_expr_value != 0 && GET_CODE (last_expr_value) == MEM
1238 && TREE_THIS_VOLATILE (exp))
1239 {
1240 if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
1241 copy_to_reg (last_expr_value);
1242 else
ddbe9812
RS
1243 {
1244 rtx lab = gen_label_rtx ();
1245
1246 /* Compare the value with itself to reference it. */
1247 emit_cmp_insn (last_expr_value, last_expr_value, EQ,
1248 expand_expr (TYPE_SIZE (last_expr_type),
37366632 1249 NULL_RTX, VOIDmode, 0),
ddbe9812
RS
1250 BLKmode, 0,
1251 TYPE_ALIGN (last_expr_type) / BITS_PER_UNIT);
1252 emit_jump_insn ((*bcc_gen_fctn[(int) EQ]) (lab));
1253 emit_label (lab);
1254 }
28d81abb
RK
1255 }
1256
1257 /* If this expression is part of a ({...}) and is in memory, we may have
1258 to preserve temporaries. */
1259 preserve_temp_slots (last_expr_value);
1260
1261 /* Free any temporaries used to evaluate this expression. Any temporary
1262 used as a result of this expression will already have been preserved
1263 above. */
1264 free_temp_slots ();
1265
1266 emit_queue ();
1267}
1268
1269/* Warn if EXP contains any computations whose results are not used.
1270 Return 1 if a warning is printed; 0 otherwise. */
1271
1272static int
1273warn_if_unused_value (exp)
1274 tree exp;
1275{
1276 if (TREE_USED (exp))
1277 return 0;
1278
1279 switch (TREE_CODE (exp))
1280 {
1281 case PREINCREMENT_EXPR:
1282 case POSTINCREMENT_EXPR:
1283 case PREDECREMENT_EXPR:
1284 case POSTDECREMENT_EXPR:
1285 case MODIFY_EXPR:
1286 case INIT_EXPR:
1287 case TARGET_EXPR:
1288 case CALL_EXPR:
1289 case METHOD_CALL_EXPR:
1290 case RTL_EXPR:
1291 case WRAPPER_EXPR:
1292 case ANTI_WRAPPER_EXPR:
1293 case WITH_CLEANUP_EXPR:
1294 case EXIT_EXPR:
1295 /* We don't warn about COND_EXPR because it may be a useful
1296 construct if either arm contains a side effect. */
1297 case COND_EXPR:
1298 return 0;
1299
1300 case BIND_EXPR:
1301 /* For a binding, warn if no side effect within it. */
1302 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1303
1304 case TRUTH_ORIF_EXPR:
1305 case TRUTH_ANDIF_EXPR:
1306 /* In && or ||, warn if 2nd operand has no side effect. */
1307 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1308
1309 case COMPOUND_EXPR:
1310 if (warn_if_unused_value (TREE_OPERAND (exp, 0)))
1311 return 1;
4d23e509
RS
1312 /* Let people do `(foo (), 0)' without a warning. */
1313 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1314 return 0;
28d81abb
RK
1315 return warn_if_unused_value (TREE_OPERAND (exp, 1));
1316
1317 case NOP_EXPR:
1318 case CONVERT_EXPR:
b4ac57ab 1319 case NON_LVALUE_EXPR:
28d81abb
RK
1320 /* Don't warn about values cast to void. */
1321 if (TREE_TYPE (exp) == void_type_node)
1322 return 0;
1323 /* Don't warn about conversions not explicit in the user's program. */
1324 if (TREE_NO_UNUSED_WARNING (exp))
1325 return 0;
1326 /* Assignment to a cast usually results in a cast of a modify.
1327 Don't complain about that. */
1328 if (TREE_CODE (TREE_OPERAND (exp, 0)) == MODIFY_EXPR)
1329 return 0;
1330 /* Sometimes it results in a cast of a cast of a modify.
1331 Don't complain about that. */
1332 if ((TREE_CODE (TREE_OPERAND (exp, 0)) == CONVERT_EXPR
1333 || TREE_CODE (TREE_OPERAND (exp, 0)) == NOP_EXPR)
1334 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) == MODIFY_EXPR)
1335 return 0;
1336
1337 default:
ddbe9812
RS
1338 /* Referencing a volatile value is a side effect, so don't warn. */
1339 if ((TREE_CODE_CLASS (TREE_CODE (exp)) == 'd'
1340 || TREE_CODE_CLASS (TREE_CODE (exp)) == 'r')
1341 && TREE_THIS_VOLATILE (exp))
1342 return 0;
28d81abb
RK
1343 warning_with_file_and_line (emit_filename, emit_lineno,
1344 "value computed is not used");
1345 return 1;
1346 }
1347}
1348
1349/* Clear out the memory of the last expression evaluated. */
1350
1351void
1352clear_last_expr ()
1353{
1354 last_expr_type = 0;
1355}
1356
1357/* Begin a statement which will return a value.
1358 Return the RTL_EXPR for this statement expr.
1359 The caller must save that value and pass it to expand_end_stmt_expr. */
1360
1361tree
1362expand_start_stmt_expr ()
1363{
1364 /* Make the RTL_EXPR node temporary, not momentary,
1365 so that rtl_expr_chain doesn't become garbage. */
1366 int momentary = suspend_momentary ();
1367 tree t = make_node (RTL_EXPR);
1368 resume_momentary (momentary);
1369 start_sequence ();
1370 NO_DEFER_POP;
1371 expr_stmts_for_value++;
1372 return t;
1373}
1374
1375/* Restore the previous state at the end of a statement that returns a value.
1376 Returns a tree node representing the statement's value and the
1377 insns to compute the value.
1378
1379 The nodes of that expression have been freed by now, so we cannot use them.
1380 But we don't want to do that anyway; the expression has already been
1381 evaluated and now we just want to use the value. So generate a RTL_EXPR
1382 with the proper type and RTL value.
1383
1384 If the last substatement was not an expression,
1385 return something with type `void'. */
1386
1387tree
1388expand_end_stmt_expr (t)
1389 tree t;
1390{
1391 OK_DEFER_POP;
1392
1393 if (last_expr_type == 0)
1394 {
1395 last_expr_type = void_type_node;
1396 last_expr_value = const0_rtx;
1397 }
1398 else if (last_expr_value == 0)
1399 /* There are some cases where this can happen, such as when the
1400 statement is void type. */
1401 last_expr_value = const0_rtx;
1402 else if (GET_CODE (last_expr_value) != REG && ! CONSTANT_P (last_expr_value))
1403 /* Remove any possible QUEUED. */
1404 last_expr_value = protect_from_queue (last_expr_value, 0);
1405
1406 emit_queue ();
1407
1408 TREE_TYPE (t) = last_expr_type;
1409 RTL_EXPR_RTL (t) = last_expr_value;
1410 RTL_EXPR_SEQUENCE (t) = get_insns ();
1411
1412 rtl_expr_chain = tree_cons (NULL_TREE, t, rtl_expr_chain);
1413
1414 end_sequence ();
1415
1416 /* Don't consider deleting this expr or containing exprs at tree level. */
1417 TREE_SIDE_EFFECTS (t) = 1;
1418 /* Propagate volatility of the actual RTL expr. */
1419 TREE_THIS_VOLATILE (t) = volatile_refs_p (last_expr_value);
1420
1421 last_expr_type = 0;
1422 expr_stmts_for_value--;
1423
1424 return t;
1425}
1426\f
1427/* The exception handling nesting looks like this:
1428
1429 <-- Level N-1
1430 { <-- exception handler block
1431 <-- Level N
1432 <-- in an exception handler
1433 { <-- try block
1434 : <-- in a TRY block
1435 : <-- in an exception handler
1436 :
1437 }
1438
1439 { <-- except block
1440 : <-- in an except block
1441 : <-- in an exception handler
1442 :
1443 }
1444
1445 }
a124fd5e 1446*/
28d81abb
RK
1447
1448/* Return nonzero iff in a try block at level LEVEL. */
1449
1450int
1451in_try_block (level)
1452 int level;
1453{
1454 struct nesting *n = except_stack;
1455 while (1)
1456 {
1457 while (n && n->data.except_stmt.after_label != 0)
1458 n = n->next;
1459 if (n == 0)
1460 return 0;
1461 if (level == 0)
1462 return n != 0;
1463 level--;
1464 n = n->next;
1465 }
1466}
1467
1468/* Return nonzero iff in an except block at level LEVEL. */
1469
1470int
1471in_except_block (level)
1472 int level;
1473{
1474 struct nesting *n = except_stack;
1475 while (1)
1476 {
1477 while (n && n->data.except_stmt.after_label == 0)
1478 n = n->next;
1479 if (n == 0)
1480 return 0;
1481 if (level == 0)
1482 return n != 0;
1483 level--;
1484 n = n->next;
1485 }
1486}
1487
1488/* Return nonzero iff in an exception handler at level LEVEL. */
1489
1490int
1491in_exception_handler (level)
1492 int level;
1493{
1494 struct nesting *n = except_stack;
1495 while (n && level--)
1496 n = n->next;
1497 return n != 0;
1498}
1499
1500/* Record the fact that the current exception nesting raises
1501 exception EX. If not in an exception handler, return 0. */
1502int
1503expand_raise (ex)
1504 tree ex;
1505{
1506 tree *raises_ptr;
1507
1508 if (except_stack == 0)
1509 return 0;
1510 raises_ptr = &except_stack->data.except_stmt.raised;
1511 if (! value_member (ex, *raises_ptr))
1512 *raises_ptr = tree_cons (NULL_TREE, ex, *raises_ptr);
1513 return 1;
1514}
1515
1516/* Generate RTL for the start of a try block.
1517
1518 TRY_CLAUSE is the condition to test to enter the try block. */
1519
1520void
1521expand_start_try (try_clause, exitflag, escapeflag)
1522 tree try_clause;
1523 int exitflag;
1524 int escapeflag;
1525{
1526 struct nesting *thishandler = ALLOC_NESTING ();
1527
1528 /* Make an entry on cond_stack for the cond we are entering. */
1529
1530 thishandler->next = except_stack;
1531 thishandler->all = nesting_stack;
1532 thishandler->depth = ++nesting_depth;
1533 thishandler->data.except_stmt.raised = 0;
1534 thishandler->data.except_stmt.handled = 0;
1535 thishandler->data.except_stmt.first_insn = get_insns ();
1536 thishandler->data.except_stmt.except_label = gen_label_rtx ();
1537 thishandler->data.except_stmt.unhandled_label = 0;
1538 thishandler->data.except_stmt.after_label = 0;
1539 thishandler->data.except_stmt.escape_label
1540 = escapeflag ? thishandler->data.except_stmt.except_label : 0;
1541 thishandler->exit_label = exitflag ? gen_label_rtx () : 0;
1542 except_stack = thishandler;
1543 nesting_stack = thishandler;
1544
37366632 1545 do_jump (try_clause, thishandler->data.except_stmt.except_label, NULL_RTX);
28d81abb
RK
1546}
1547
1548/* End of a TRY block. Nothing to do for now. */
1549
1550void
1551expand_end_try ()
1552{
1553 except_stack->data.except_stmt.after_label = gen_label_rtx ();
37366632
RK
1554 expand_goto_internal (NULL_TREE, except_stack->data.except_stmt.after_label,
1555 NULL_RTX);
28d81abb
RK
1556}
1557
1558/* Start an `except' nesting contour.
1559 EXITFLAG says whether this contour should be able to `exit' something.
1560 ESCAPEFLAG says whether this contour should be escapable. */
1561
1562void
1563expand_start_except (exitflag, escapeflag)
1564 int exitflag;
1565 int escapeflag;
1566{
1567 if (exitflag)
1568 {
1569 struct nesting *n;
1570 /* An `exit' from catch clauses goes out to next exit level,
1571 if there is one. Otherwise, it just goes to the end
1572 of the construct. */
1573 for (n = except_stack->next; n; n = n->next)
1574 if (n->exit_label != 0)
1575 {
1576 except_stack->exit_label = n->exit_label;
1577 break;
1578 }
1579 if (n == 0)
1580 except_stack->exit_label = except_stack->data.except_stmt.after_label;
1581 }
1582 if (escapeflag)
1583 {
1584 struct nesting *n;
1585 /* An `escape' from catch clauses goes out to next escape level,
1586 if there is one. Otherwise, it just goes to the end
1587 of the construct. */
1588 for (n = except_stack->next; n; n = n->next)
1589 if (n->data.except_stmt.escape_label != 0)
1590 {
1591 except_stack->data.except_stmt.escape_label
1592 = n->data.except_stmt.escape_label;
1593 break;
1594 }
1595 if (n == 0)
1596 except_stack->data.except_stmt.escape_label
1597 = except_stack->data.except_stmt.after_label;
1598 }
1599 do_pending_stack_adjust ();
1600 emit_label (except_stack->data.except_stmt.except_label);
1601}
1602
1603/* Generate code to `escape' from an exception contour. This
1604 is like `exiting', but does not conflict with constructs which
1605 use `exit_label'.
1606
1607 Return nonzero if this contour is escapable, otherwise
1608 return zero, and language-specific code will emit the
1609 appropriate error message. */
1610int
1611expand_escape_except ()
1612{
1613 struct nesting *n;
1614 last_expr_type = 0;
1615 for (n = except_stack; n; n = n->next)
1616 if (n->data.except_stmt.escape_label != 0)
1617 {
37366632
RK
1618 expand_goto_internal (NULL_TREE,
1619 n->data.except_stmt.escape_label, NULL_RTX);
28d81abb
RK
1620 return 1;
1621 }
1622
1623 return 0;
1624}
1625
1626/* Finish processing and `except' contour.
1627 Culls out all exceptions which might be raise but not
1628 handled, and returns the list to the caller.
1629 Language-specific code is responsible for dealing with these
1630 exceptions. */
1631
1632tree
1633expand_end_except ()
1634{
1635 struct nesting *n;
1636 tree raised = NULL_TREE;
1637
1638 do_pending_stack_adjust ();
1639 emit_label (except_stack->data.except_stmt.after_label);
1640
1641 n = except_stack->next;
1642 if (n)
1643 {
1644 /* Propagate exceptions raised but not handled to next
1645 highest level. */
1646 tree handled = except_stack->data.except_stmt.raised;
1647 if (handled != void_type_node)
1648 {
1649 tree prev = NULL_TREE;
1650 raised = except_stack->data.except_stmt.raised;
1651 while (handled)
1652 {
1653 tree this_raise;
1654 for (this_raise = raised, prev = 0; this_raise;
1655 this_raise = TREE_CHAIN (this_raise))
1656 {
1657 if (value_member (TREE_VALUE (this_raise), handled))
1658 {
1659 if (prev)
1660 TREE_CHAIN (prev) = TREE_CHAIN (this_raise);
1661 else
1662 {
1663 raised = TREE_CHAIN (raised);
1664 if (raised == NULL_TREE)
1665 goto nada;
1666 }
1667 }
1668 else
1669 prev = this_raise;
1670 }
1671 handled = TREE_CHAIN (handled);
1672 }
1673 if (prev == NULL_TREE)
1674 prev = raised;
1675 if (prev)
1676 TREE_CHAIN (prev) = n->data.except_stmt.raised;
1677 nada:
1678 n->data.except_stmt.raised = raised;
1679 }
1680 }
1681
1682 POPSTACK (except_stack);
1683 last_expr_type = 0;
1684 return raised;
1685}
1686
1687/* Record that exception EX is caught by this exception handler.
1688 Return nonzero if in exception handling construct, otherwise return 0. */
1689int
1690expand_catch (ex)
1691 tree ex;
1692{
1693 tree *raises_ptr;
1694
1695 if (except_stack == 0)
1696 return 0;
1697 raises_ptr = &except_stack->data.except_stmt.handled;
1698 if (*raises_ptr != void_type_node
1699 && ex != NULL_TREE
1700 && ! value_member (ex, *raises_ptr))
1701 *raises_ptr = tree_cons (NULL_TREE, ex, *raises_ptr);
1702 return 1;
1703}
1704
1705/* Record that this exception handler catches all exceptions.
1706 Return nonzero if in exception handling construct, otherwise return 0. */
1707
1708int
1709expand_catch_default ()
1710{
1711 if (except_stack == 0)
1712 return 0;
1713 except_stack->data.except_stmt.handled = void_type_node;
1714 return 1;
1715}
1716
1717int
1718expand_end_catch ()
1719{
1720 if (except_stack == 0 || except_stack->data.except_stmt.after_label == 0)
1721 return 0;
37366632
RK
1722 expand_goto_internal (NULL_TREE, except_stack->data.except_stmt.after_label,
1723 NULL_RTX);
28d81abb
RK
1724 return 1;
1725}
1726\f
1727/* Generate RTL for the start of an if-then. COND is the expression
1728 whose truth should be tested.
1729
1730 If EXITFLAG is nonzero, this conditional is visible to
1731 `exit_something'. */
1732
1733void
1734expand_start_cond (cond, exitflag)
1735 tree cond;
1736 int exitflag;
1737{
1738 struct nesting *thiscond = ALLOC_NESTING ();
1739
1740 /* Make an entry on cond_stack for the cond we are entering. */
1741
1742 thiscond->next = cond_stack;
1743 thiscond->all = nesting_stack;
1744 thiscond->depth = ++nesting_depth;
1745 thiscond->data.cond.next_label = gen_label_rtx ();
1746 /* Before we encounter an `else', we don't need a separate exit label
1747 unless there are supposed to be exit statements
1748 to exit this conditional. */
1749 thiscond->exit_label = exitflag ? gen_label_rtx () : 0;
1750 thiscond->data.cond.endif_label = thiscond->exit_label;
1751 cond_stack = thiscond;
1752 nesting_stack = thiscond;
1753
37366632 1754 do_jump (cond, thiscond->data.cond.next_label, NULL_RTX);
28d81abb
RK
1755}
1756
1757/* Generate RTL between then-clause and the elseif-clause
1758 of an if-then-elseif-.... */
1759
1760void
1761expand_start_elseif (cond)
1762 tree cond;
1763{
1764 if (cond_stack->data.cond.endif_label == 0)
1765 cond_stack->data.cond.endif_label = gen_label_rtx ();
1766 emit_jump (cond_stack->data.cond.endif_label);
1767 emit_label (cond_stack->data.cond.next_label);
1768 cond_stack->data.cond.next_label = gen_label_rtx ();
37366632 1769 do_jump (cond, cond_stack->data.cond.next_label, NULL_RTX);
28d81abb
RK
1770}
1771
1772/* Generate RTL between the then-clause and the else-clause
1773 of an if-then-else. */
1774
1775void
1776expand_start_else ()
1777{
1778 if (cond_stack->data.cond.endif_label == 0)
1779 cond_stack->data.cond.endif_label = gen_label_rtx ();
1780 emit_jump (cond_stack->data.cond.endif_label);
1781 emit_label (cond_stack->data.cond.next_label);
1782 cond_stack->data.cond.next_label = 0; /* No more _else or _elseif calls. */
1783}
1784
1785/* Generate RTL for the end of an if-then.
1786 Pop the record for it off of cond_stack. */
1787
1788void
1789expand_end_cond ()
1790{
1791 struct nesting *thiscond = cond_stack;
1792
1793 do_pending_stack_adjust ();
1794 if (thiscond->data.cond.next_label)
1795 emit_label (thiscond->data.cond.next_label);
1796 if (thiscond->data.cond.endif_label)
1797 emit_label (thiscond->data.cond.endif_label);
1798
1799 POPSTACK (cond_stack);
1800 last_expr_type = 0;
1801}
1802\f
1803/* Generate RTL for the start of a loop. EXIT_FLAG is nonzero if this
1804 loop should be exited by `exit_something'. This is a loop for which
1805 `expand_continue' will jump to the top of the loop.
1806
1807 Make an entry on loop_stack to record the labels associated with
1808 this loop. */
1809
1810struct nesting *
1811expand_start_loop (exit_flag)
1812 int exit_flag;
1813{
1814 register struct nesting *thisloop = ALLOC_NESTING ();
1815
1816 /* Make an entry on loop_stack for the loop we are entering. */
1817
1818 thisloop->next = loop_stack;
1819 thisloop->all = nesting_stack;
1820 thisloop->depth = ++nesting_depth;
1821 thisloop->data.loop.start_label = gen_label_rtx ();
1822 thisloop->data.loop.end_label = gen_label_rtx ();
1823 thisloop->data.loop.continue_label = thisloop->data.loop.start_label;
1824 thisloop->exit_label = exit_flag ? thisloop->data.loop.end_label : 0;
1825 loop_stack = thisloop;
1826 nesting_stack = thisloop;
1827
1828 do_pending_stack_adjust ();
1829 emit_queue ();
37366632 1830 emit_note (NULL_PTR, NOTE_INSN_LOOP_BEG);
28d81abb
RK
1831 emit_label (thisloop->data.loop.start_label);
1832
1833 return thisloop;
1834}
1835
1836/* Like expand_start_loop but for a loop where the continuation point
1837 (for expand_continue_loop) will be specified explicitly. */
1838
1839struct nesting *
1840expand_start_loop_continue_elsewhere (exit_flag)
1841 int exit_flag;
1842{
1843 struct nesting *thisloop = expand_start_loop (exit_flag);
1844 loop_stack->data.loop.continue_label = gen_label_rtx ();
1845 return thisloop;
1846}
1847
1848/* Specify the continuation point for a loop started with
1849 expand_start_loop_continue_elsewhere.
1850 Use this at the point in the code to which a continue statement
1851 should jump. */
1852
1853void
1854expand_loop_continue_here ()
1855{
1856 do_pending_stack_adjust ();
37366632 1857 emit_note (NULL_PTR, NOTE_INSN_LOOP_CONT);
28d81abb
RK
1858 emit_label (loop_stack->data.loop.continue_label);
1859}
1860
1861/* Finish a loop. Generate a jump back to the top and the loop-exit label.
1862 Pop the block off of loop_stack. */
1863
1864void
1865expand_end_loop ()
1866{
1867 register rtx insn = get_last_insn ();
1868 register rtx start_label = loop_stack->data.loop.start_label;
1869 rtx last_test_insn = 0;
1870 int num_insns = 0;
1871
1872 /* Mark the continue-point at the top of the loop if none elsewhere. */
1873 if (start_label == loop_stack->data.loop.continue_label)
1874 emit_note_before (NOTE_INSN_LOOP_CONT, start_label);
1875
1876 do_pending_stack_adjust ();
1877
1878 /* If optimizing, perhaps reorder the loop. If the loop
1879 starts with a conditional exit, roll that to the end
1880 where it will optimize together with the jump back.
1881
1882 We look for the last conditional branch to the exit that we encounter
1883 before hitting 30 insns or a CALL_INSN. If we see an unconditional
1884 branch to the exit first, use it.
1885
1886 We must also stop at NOTE_INSN_BLOCK_BEG and NOTE_INSN_BLOCK_END notes
1887 because moving them is not valid. */
1888
1889 if (optimize
1890 &&
1891 ! (GET_CODE (insn) == JUMP_INSN
1892 && GET_CODE (PATTERN (insn)) == SET
1893 && SET_DEST (PATTERN (insn)) == pc_rtx
1894 && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE))
1895 {
1896 /* Scan insns from the top of the loop looking for a qualified
1897 conditional exit. */
1898 for (insn = NEXT_INSN (loop_stack->data.loop.start_label); insn;
1899 insn = NEXT_INSN (insn))
1900 {
1901 if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == CODE_LABEL)
1902 break;
1903
1904 if (GET_CODE (insn) == NOTE
1905 && (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG
1906 || NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END))
1907 break;
1908
1909 if (GET_CODE (insn) == JUMP_INSN || GET_CODE (insn) == INSN)
1910 num_insns++;
1911
1912 if (last_test_insn && num_insns > 30)
1913 break;
1914
1915 if (GET_CODE (insn) == JUMP_INSN && GET_CODE (PATTERN (insn)) == SET
1916 && SET_DEST (PATTERN (insn)) == pc_rtx
1917 && GET_CODE (SET_SRC (PATTERN (insn))) == IF_THEN_ELSE
1918 && ((GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 1)) == LABEL_REF
1919 && (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0)
1920 == loop_stack->data.loop.end_label))
1921 || (GET_CODE (XEXP (SET_SRC (PATTERN (insn)), 2)) == LABEL_REF
1922 && (XEXP (XEXP (SET_SRC (PATTERN (insn)), 2), 0)
1923 == loop_stack->data.loop.end_label))))
1924 last_test_insn = insn;
1925
1926 if (last_test_insn == 0 && GET_CODE (insn) == JUMP_INSN
1927 && GET_CODE (PATTERN (insn)) == SET
1928 && SET_DEST (PATTERN (insn)) == pc_rtx
1929 && GET_CODE (SET_SRC (PATTERN (insn))) == LABEL_REF
1930 && (XEXP (SET_SRC (PATTERN (insn)), 0)
1931 == loop_stack->data.loop.end_label))
1932 /* Include BARRIER. */
1933 last_test_insn = NEXT_INSN (insn);
1934 }
1935
1936 if (last_test_insn != 0 && last_test_insn != get_last_insn ())
1937 {
1938 /* We found one. Move everything from there up
1939 to the end of the loop, and add a jump into the loop
1940 to jump to there. */
1941 register rtx newstart_label = gen_label_rtx ();
1942 register rtx start_move = start_label;
1943
b4ac57ab 1944 /* If the start label is preceded by a NOTE_INSN_LOOP_CONT note,
28d81abb
RK
1945 then we want to move this note also. */
1946 if (GET_CODE (PREV_INSN (start_move)) == NOTE
1947 && (NOTE_LINE_NUMBER (PREV_INSN (start_move))
1948 == NOTE_INSN_LOOP_CONT))
1949 start_move = PREV_INSN (start_move);
1950
1951 emit_label_after (newstart_label, PREV_INSN (start_move));
1952 reorder_insns (start_move, last_test_insn, get_last_insn ());
1953 emit_jump_insn_after (gen_jump (start_label),
1954 PREV_INSN (newstart_label));
1955 emit_barrier_after (PREV_INSN (newstart_label));
1956 start_label = newstart_label;
1957 }
1958 }
1959
1960 emit_jump (start_label);
37366632 1961 emit_note (NULL_PTR, NOTE_INSN_LOOP_END);
28d81abb
RK
1962 emit_label (loop_stack->data.loop.end_label);
1963
1964 POPSTACK (loop_stack);
1965
1966 last_expr_type = 0;
1967}
1968
1969/* Generate a jump to the current loop's continue-point.
1970 This is usually the top of the loop, but may be specified
1971 explicitly elsewhere. If not currently inside a loop,
1972 return 0 and do nothing; caller will print an error message. */
1973
1974int
1975expand_continue_loop (whichloop)
1976 struct nesting *whichloop;
1977{
1978 last_expr_type = 0;
1979 if (whichloop == 0)
1980 whichloop = loop_stack;
1981 if (whichloop == 0)
1982 return 0;
37366632
RK
1983 expand_goto_internal (NULL_TREE, whichloop->data.loop.continue_label,
1984 NULL_RTX);
28d81abb
RK
1985 return 1;
1986}
1987
1988/* Generate a jump to exit the current loop. If not currently inside a loop,
1989 return 0 and do nothing; caller will print an error message. */
1990
1991int
1992expand_exit_loop (whichloop)
1993 struct nesting *whichloop;
1994{
1995 last_expr_type = 0;
1996 if (whichloop == 0)
1997 whichloop = loop_stack;
1998 if (whichloop == 0)
1999 return 0;
37366632 2000 expand_goto_internal (NULL_TREE, whichloop->data.loop.end_label, NULL_RTX);
28d81abb
RK
2001 return 1;
2002}
2003
2004/* Generate a conditional jump to exit the current loop if COND
2005 evaluates to zero. If not currently inside a loop,
2006 return 0 and do nothing; caller will print an error message. */
2007
2008int
2009expand_exit_loop_if_false (whichloop, cond)
2010 struct nesting *whichloop;
2011 tree cond;
2012{
2013 last_expr_type = 0;
2014 if (whichloop == 0)
2015 whichloop = loop_stack;
2016 if (whichloop == 0)
2017 return 0;
37366632 2018 do_jump (cond, whichloop->data.loop.end_label, NULL_RTX);
28d81abb
RK
2019 return 1;
2020}
2021
2022/* Return non-zero if we should preserve sub-expressions as separate
2023 pseudos. We never do so if we aren't optimizing. We always do so
2024 if -fexpensive-optimizations.
2025
2026 Otherwise, we only do so if we are in the "early" part of a loop. I.e.,
2027 the loop may still be a small one. */
2028
2029int
2030preserve_subexpressions_p ()
2031{
2032 rtx insn;
2033
2034 if (flag_expensive_optimizations)
2035 return 1;
2036
2037 if (optimize == 0 || loop_stack == 0)
2038 return 0;
2039
2040 insn = get_last_insn_anywhere ();
2041
2042 return (insn
2043 && (INSN_UID (insn) - INSN_UID (loop_stack->data.loop.start_label)
2044 < n_non_fixed_regs * 3));
2045
2046}
2047
2048/* Generate a jump to exit the current loop, conditional, binding contour
2049 or case statement. Not all such constructs are visible to this function,
2050 only those started with EXIT_FLAG nonzero. Individual languages use
2051 the EXIT_FLAG parameter to control which kinds of constructs you can
2052 exit this way.
2053
2054 If not currently inside anything that can be exited,
2055 return 0 and do nothing; caller will print an error message. */
2056
2057int
2058expand_exit_something ()
2059{
2060 struct nesting *n;
2061 last_expr_type = 0;
2062 for (n = nesting_stack; n; n = n->all)
2063 if (n->exit_label != 0)
2064 {
37366632 2065 expand_goto_internal (NULL_TREE, n->exit_label, NULL_RTX);
28d81abb
RK
2066 return 1;
2067 }
2068
2069 return 0;
2070}
2071\f
2072/* Generate RTL to return from the current function, with no value.
2073 (That is, we do not do anything about returning any value.) */
2074
2075void
2076expand_null_return ()
2077{
2078 struct nesting *block = block_stack;
2079 rtx last_insn = 0;
2080
2081 /* Does any pending block have cleanups? */
2082
2083 while (block && block->data.block.cleanups == 0)
2084 block = block->next;
2085
2086 /* If yes, use a goto to return, since that runs cleanups. */
2087
2088 expand_null_return_1 (last_insn, block != 0);
2089}
2090
2091/* Generate RTL to return from the current function, with value VAL. */
2092
2093void
2094expand_value_return (val)
2095 rtx val;
2096{
2097 struct nesting *block = block_stack;
2098 rtx last_insn = get_last_insn ();
2099 rtx return_reg = DECL_RTL (DECL_RESULT (current_function_decl));
2100
2101 /* Copy the value to the return location
2102 unless it's already there. */
2103
2104 if (return_reg != val)
2105 emit_move_insn (return_reg, val);
2106 if (GET_CODE (return_reg) == REG
2107 && REGNO (return_reg) < FIRST_PSEUDO_REGISTER)
2108 emit_insn (gen_rtx (USE, VOIDmode, return_reg));
2109
2110 /* Does any pending block have cleanups? */
2111
2112 while (block && block->data.block.cleanups == 0)
2113 block = block->next;
2114
2115 /* If yes, use a goto to return, since that runs cleanups.
2116 Use LAST_INSN to put cleanups *before* the move insn emitted above. */
2117
2118 expand_null_return_1 (last_insn, block != 0);
2119}
2120
2121/* Output a return with no value. If LAST_INSN is nonzero,
2122 pretend that the return takes place after LAST_INSN.
2123 If USE_GOTO is nonzero then don't use a return instruction;
2124 go to the return label instead. This causes any cleanups
2125 of pending blocks to be executed normally. */
2126
2127static void
2128expand_null_return_1 (last_insn, use_goto)
2129 rtx last_insn;
2130 int use_goto;
2131{
2132 rtx end_label = cleanup_label ? cleanup_label : return_label;
2133
2134 clear_pending_stack_adjust ();
2135 do_pending_stack_adjust ();
2136 last_expr_type = 0;
2137
2138 /* PCC-struct return always uses an epilogue. */
2139 if (current_function_returns_pcc_struct || use_goto)
2140 {
2141 if (end_label == 0)
2142 end_label = return_label = gen_label_rtx ();
37366632 2143 expand_goto_internal (NULL_TREE, end_label, last_insn);
28d81abb
RK
2144 return;
2145 }
2146
2147 /* Otherwise output a simple return-insn if one is available,
2148 unless it won't do the job. */
2149#ifdef HAVE_return
2150 if (HAVE_return && use_goto == 0 && cleanup_label == 0)
2151 {
2152 emit_jump_insn (gen_return ());
2153 emit_barrier ();
2154 return;
2155 }
2156#endif
2157
2158 /* Otherwise jump to the epilogue. */
37366632 2159 expand_goto_internal (NULL_TREE, end_label, last_insn);
28d81abb
RK
2160}
2161\f
2162/* Generate RTL to evaluate the expression RETVAL and return it
2163 from the current function. */
2164
2165void
2166expand_return (retval)
2167 tree retval;
2168{
2169 /* If there are any cleanups to be performed, then they will
2170 be inserted following LAST_INSN. It is desirable
2171 that the last_insn, for such purposes, should be the
2172 last insn before computing the return value. Otherwise, cleanups
2173 which call functions can clobber the return value. */
2174 /* ??? rms: I think that is erroneous, because in C++ it would
2175 run destructors on variables that might be used in the subsequent
2176 computation of the return value. */
2177 rtx last_insn = 0;
2178 register rtx val = 0;
2179 register rtx op0;
2180 tree retval_rhs;
2181 int cleanups;
2182 struct nesting *block;
2183
2184 /* If function wants no value, give it none. */
2185 if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
2186 {
37366632 2187 expand_expr (retval, NULL_RTX, VOIDmode, 0);
7e70e7c5 2188 emit_queue ();
28d81abb
RK
2189 expand_null_return ();
2190 return;
2191 }
2192
2193 /* Are any cleanups needed? E.g. C++ destructors to be run? */
2194 cleanups = any_pending_cleanups (1);
2195
2196 if (TREE_CODE (retval) == RESULT_DECL)
2197 retval_rhs = retval;
2198 else if ((TREE_CODE (retval) == MODIFY_EXPR || TREE_CODE (retval) == INIT_EXPR)
2199 && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
2200 retval_rhs = TREE_OPERAND (retval, 1);
2201 else if (TREE_TYPE (retval) == void_type_node)
2202 /* Recognize tail-recursive call to void function. */
2203 retval_rhs = retval;
2204 else
2205 retval_rhs = NULL_TREE;
2206
2207 /* Only use `last_insn' if there are cleanups which must be run. */
2208 if (cleanups || cleanup_label != 0)
2209 last_insn = get_last_insn ();
2210
2211 /* Distribute return down conditional expr if either of the sides
2212 may involve tail recursion (see test below). This enhances the number
2213 of tail recursions we see. Don't do this always since it can produce
2214 sub-optimal code in some cases and we distribute assignments into
2215 conditional expressions when it would help. */
2216
2217 if (optimize && retval_rhs != 0
2218 && frame_offset == 0
2219 && TREE_CODE (retval_rhs) == COND_EXPR
2220 && (TREE_CODE (TREE_OPERAND (retval_rhs, 1)) == CALL_EXPR
2221 || TREE_CODE (TREE_OPERAND (retval_rhs, 2)) == CALL_EXPR))
2222 {
2223 rtx label = gen_label_rtx ();
37366632 2224 do_jump (TREE_OPERAND (retval_rhs, 0), label, NULL_RTX);
28d81abb
RK
2225 expand_return (build (MODIFY_EXPR, TREE_TYPE (current_function_decl),
2226 DECL_RESULT (current_function_decl),
2227 TREE_OPERAND (retval_rhs, 1)));
2228 emit_label (label);
2229 expand_return (build (MODIFY_EXPR, TREE_TYPE (current_function_decl),
2230 DECL_RESULT (current_function_decl),
2231 TREE_OPERAND (retval_rhs, 2)));
2232 return;
2233 }
2234
2235 /* For tail-recursive call to current function,
2236 just jump back to the beginning.
2237 It's unsafe if any auto variable in this function
2238 has its address taken; for simplicity,
2239 require stack frame to be empty. */
2240 if (optimize && retval_rhs != 0
2241 && frame_offset == 0
2242 && TREE_CODE (retval_rhs) == CALL_EXPR
2243 && TREE_CODE (TREE_OPERAND (retval_rhs, 0)) == ADDR_EXPR
2244 && TREE_OPERAND (TREE_OPERAND (retval_rhs, 0), 0) == current_function_decl
2245 /* Finish checking validity, and if valid emit code
2246 to set the argument variables for the new call. */
2247 && tail_recursion_args (TREE_OPERAND (retval_rhs, 1),
2248 DECL_ARGUMENTS (current_function_decl)))
2249 {
2250 if (tail_recursion_label == 0)
2251 {
2252 tail_recursion_label = gen_label_rtx ();
2253 emit_label_after (tail_recursion_label,
2254 tail_recursion_reentry);
2255 }
a3229491 2256 emit_queue ();
37366632 2257 expand_goto_internal (NULL_TREE, tail_recursion_label, last_insn);
28d81abb
RK
2258 emit_barrier ();
2259 return;
2260 }
2261#ifdef HAVE_return
2262 /* This optimization is safe if there are local cleanups
2263 because expand_null_return takes care of them.
2264 ??? I think it should also be safe when there is a cleanup label,
2265 because expand_null_return takes care of them, too.
2266 Any reason why not? */
2267 if (HAVE_return && cleanup_label == 0
2268 && ! current_function_returns_pcc_struct)
2269 {
2270 /* If this is return x == y; then generate
2271 if (x == y) return 1; else return 0;
2272 if we can do it with explicit return insns. */
2273 if (retval_rhs)
2274 switch (TREE_CODE (retval_rhs))
2275 {
2276 case EQ_EXPR:
2277 case NE_EXPR:
2278 case GT_EXPR:
2279 case GE_EXPR:
2280 case LT_EXPR:
2281 case LE_EXPR:
2282 case TRUTH_ANDIF_EXPR:
2283 case TRUTH_ORIF_EXPR:
2284 case TRUTH_AND_EXPR:
2285 case TRUTH_OR_EXPR:
2286 case TRUTH_NOT_EXPR:
2287 op0 = gen_label_rtx ();
2288 jumpifnot (retval_rhs, op0);
2289 expand_value_return (const1_rtx);
2290 emit_label (op0);
2291 expand_value_return (const0_rtx);
2292 return;
2293 }
2294 }
2295#endif /* HAVE_return */
2296
2297 if (cleanups
2298 && retval_rhs != 0
2299 && TREE_TYPE (retval_rhs) != void_type_node
2300 && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG)
2301 {
2302 /* Calculate the return value into a pseudo reg. */
37366632 2303 val = expand_expr (retval_rhs, NULL_RTX, VOIDmode, 0);
28d81abb
RK
2304 emit_queue ();
2305 /* All temporaries have now been used. */
2306 free_temp_slots ();
2307 /* Return the calculated value, doing cleanups first. */
2308 expand_value_return (val);
2309 }
2310 else
2311 {
2312 /* No cleanups or no hard reg used;
2313 calculate value into hard return reg. */
37366632 2314 expand_expr (retval, NULL_RTX, VOIDmode, 0);
28d81abb
RK
2315 emit_queue ();
2316 free_temp_slots ();
2317 expand_value_return (DECL_RTL (DECL_RESULT (current_function_decl)));
2318 }
2319}
2320
2321/* Return 1 if the end of the generated RTX is not a barrier.
2322 This means code already compiled can drop through. */
2323
2324int
2325drop_through_at_end_p ()
2326{
2327 rtx insn = get_last_insn ();
2328 while (insn && GET_CODE (insn) == NOTE)
2329 insn = PREV_INSN (insn);
2330 return insn && GET_CODE (insn) != BARRIER;
2331}
2332\f
2333/* Emit code to alter this function's formal parms for a tail-recursive call.
2334 ACTUALS is a list of actual parameter expressions (chain of TREE_LISTs).
2335 FORMALS is the chain of decls of formals.
2336 Return 1 if this can be done;
2337 otherwise return 0 and do not emit any code. */
2338
2339static int
2340tail_recursion_args (actuals, formals)
2341 tree actuals, formals;
2342{
2343 register tree a = actuals, f = formals;
2344 register int i;
2345 register rtx *argvec;
2346
2347 /* Check that number and types of actuals are compatible
2348 with the formals. This is not always true in valid C code.
2349 Also check that no formal needs to be addressable
2350 and that all formals are scalars. */
2351
2352 /* Also count the args. */
2353
2354 for (a = actuals, f = formals, i = 0; a && f; a = TREE_CHAIN (a), f = TREE_CHAIN (f), i++)
2355 {
2356 if (TREE_TYPE (TREE_VALUE (a)) != TREE_TYPE (f))
2357 return 0;
2358 if (GET_CODE (DECL_RTL (f)) != REG || DECL_MODE (f) == BLKmode)
2359 return 0;
2360 }
2361 if (a != 0 || f != 0)
2362 return 0;
2363
2364 /* Compute all the actuals. */
2365
2366 argvec = (rtx *) alloca (i * sizeof (rtx));
2367
2368 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
37366632 2369 argvec[i] = expand_expr (TREE_VALUE (a), NULL_RTX, VOIDmode, 0);
28d81abb
RK
2370
2371 /* Find which actual values refer to current values of previous formals.
2372 Copy each of them now, before any formal is changed. */
2373
2374 for (a = actuals, i = 0; a; a = TREE_CHAIN (a), i++)
2375 {
2376 int copy = 0;
2377 register int j;
2378 for (f = formals, j = 0; j < i; f = TREE_CHAIN (f), j++)
2379 if (reg_mentioned_p (DECL_RTL (f), argvec[i]))
2380 { copy = 1; break; }
2381 if (copy)
2382 argvec[i] = copy_to_reg (argvec[i]);
2383 }
2384
2385 /* Store the values of the actuals into the formals. */
2386
2387 for (f = formals, a = actuals, i = 0; f;
2388 f = TREE_CHAIN (f), a = TREE_CHAIN (a), i++)
2389 {
2390 if (DECL_MODE (f) == GET_MODE (argvec[i]))
2391 emit_move_insn (DECL_RTL (f), argvec[i]);
2392 else
2393 convert_move (DECL_RTL (f), argvec[i],
2394 TREE_UNSIGNED (TREE_TYPE (TREE_VALUE (a))));
2395 }
2396
2397 free_temp_slots ();
2398 return 1;
2399}
2400\f
2401/* Generate the RTL code for entering a binding contour.
2402 The variables are declared one by one, by calls to `expand_decl'.
2403
2404 EXIT_FLAG is nonzero if this construct should be visible to
2405 `exit_something'. */
2406
2407void
2408expand_start_bindings (exit_flag)
2409 int exit_flag;
2410{
2411 struct nesting *thisblock = ALLOC_NESTING ();
2412
37366632 2413 rtx note = emit_note (NULL_PTR, NOTE_INSN_BLOCK_BEG);
28d81abb
RK
2414
2415 /* Make an entry on block_stack for the block we are entering. */
2416
2417 thisblock->next = block_stack;
2418 thisblock->all = nesting_stack;
2419 thisblock->depth = ++nesting_depth;
2420 thisblock->data.block.stack_level = 0;
2421 thisblock->data.block.cleanups = 0;
2422 thisblock->data.block.function_call_count = 0;
2423#if 0
2424 if (block_stack)
2425 {
2426 if (block_stack->data.block.cleanups == NULL_TREE
2427 && (block_stack->data.block.outer_cleanups == NULL_TREE
2428 || block_stack->data.block.outer_cleanups == empty_cleanup_list))
2429 thisblock->data.block.outer_cleanups = empty_cleanup_list;
2430 else
2431 thisblock->data.block.outer_cleanups
2432 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
2433 block_stack->data.block.outer_cleanups);
2434 }
2435 else
2436 thisblock->data.block.outer_cleanups = 0;
2437#endif
2438#if 1
2439 if (block_stack
2440 && !(block_stack->data.block.cleanups == NULL_TREE
2441 && block_stack->data.block.outer_cleanups == NULL_TREE))
2442 thisblock->data.block.outer_cleanups
2443 = tree_cons (NULL_TREE, block_stack->data.block.cleanups,
2444 block_stack->data.block.outer_cleanups);
2445 else
2446 thisblock->data.block.outer_cleanups = 0;
2447#endif
2448 thisblock->data.block.label_chain = 0;
2449 thisblock->data.block.innermost_stack_block = stack_block_stack;
2450 thisblock->data.block.first_insn = note;
2451 thisblock->data.block.block_start_count = ++block_start_count;
2452 thisblock->exit_label = exit_flag ? gen_label_rtx () : 0;
2453 block_stack = thisblock;
2454 nesting_stack = thisblock;
2455
2456 /* Make a new level for allocating stack slots. */
2457 push_temp_slots ();
2458}
2459
2460/* Generate RTL code to terminate a binding contour.
2461 VARS is the chain of VAR_DECL nodes
2462 for the variables bound in this contour.
2463 MARK_ENDS is nonzero if we should put a note at the beginning
2464 and end of this binding contour.
2465
2466 DONT_JUMP_IN is nonzero if it is not valid to jump into this contour.
2467 (That is true automatically if the contour has a saved stack level.) */
2468
2469void
2470expand_end_bindings (vars, mark_ends, dont_jump_in)
2471 tree vars;
2472 int mark_ends;
2473 int dont_jump_in;
2474{
2475 register struct nesting *thisblock = block_stack;
2476 register tree decl;
2477
2478 if (warn_unused)
2479 for (decl = vars; decl; decl = TREE_CHAIN (decl))
7e70e7c5
RS
2480 if (! TREE_USED (decl) && TREE_CODE (decl) == VAR_DECL
2481 && ! DECL_IN_SYSTEM_HEADER (decl))
28d81abb
RK
2482 warning_with_decl (decl, "unused variable `%s'");
2483
2484 /* Mark the beginning and end of the scope if requested. */
2485
2486 if (mark_ends)
37366632 2487 emit_note (NULL_PTR, NOTE_INSN_BLOCK_END);
28d81abb
RK
2488 else
2489 /* Get rid of the beginning-mark if we don't make an end-mark. */
2490 NOTE_LINE_NUMBER (thisblock->data.block.first_insn) = NOTE_INSN_DELETED;
2491
2492 if (thisblock->exit_label)
2493 {
2494 do_pending_stack_adjust ();
2495 emit_label (thisblock->exit_label);
2496 }
2497
2498 /* If necessary, make a handler for nonlocal gotos taking
2499 place in the function calls in this block. */
2500 if (function_call_count != thisblock->data.block.function_call_count
2501 && nonlocal_labels
2502 /* Make handler for outermost block
2503 if there were any nonlocal gotos to this function. */
2504 && (thisblock->next == 0 ? current_function_has_nonlocal_label
2505 /* Make handler for inner block if it has something
2506 special to do when you jump out of it. */
2507 : (thisblock->data.block.cleanups != 0
2508 || thisblock->data.block.stack_level != 0)))
2509 {
2510 tree link;
2511 rtx afterward = gen_label_rtx ();
2512 rtx handler_label = gen_label_rtx ();
2513 rtx save_receiver = gen_reg_rtx (Pmode);
2514
2515 /* Don't let jump_optimize delete the handler. */
2516 LABEL_PRESERVE_P (handler_label) = 1;
2517
2518 /* Record the handler address in the stack slot for that purpose,
2519 during this block, saving and restoring the outer value. */
2520 if (thisblock->next != 0)
2521 {
2522 emit_move_insn (nonlocal_goto_handler_slot, save_receiver);
2523 emit_insn_before (gen_move_insn (save_receiver,
2524 nonlocal_goto_handler_slot),
2525 thisblock->data.block.first_insn);
2526 }
2527 emit_insn_before (gen_move_insn (nonlocal_goto_handler_slot,
2528 gen_rtx (LABEL_REF, Pmode,
2529 handler_label)),
2530 thisblock->data.block.first_insn);
2531
2532 /* Jump around the handler; it runs only when specially invoked. */
2533 emit_jump (afterward);
2534 emit_label (handler_label);
2535
2536#ifdef HAVE_nonlocal_goto
2537 if (! HAVE_nonlocal_goto)
2538#endif
2539 /* First adjust our frame pointer to its actual value. It was
2540 previously set to the start of the virtual area corresponding to
2541 the stacked variables when we branched here and now needs to be
2542 adjusted to the actual hardware fp value.
2543
2544 Assignments are to virtual registers are converted by
2545 instantiate_virtual_regs into the corresponding assignment
2546 to the underlying register (fp in this case) that makes
2547 the original assignment true.
2548 So the following insn will actually be
2549 decrementing fp by STARTING_FRAME_OFFSET. */
2550 emit_move_insn (virtual_stack_vars_rtx, frame_pointer_rtx);
2551
2552#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2553 if (fixed_regs[ARG_POINTER_REGNUM])
2554 {
42495ca0
RK
2555#ifdef ELIMINABLE_REGS
2556 /* If the argument pointer can be eliminated in favor of the
2557 frame pointer, we don't need to restore it. We assume here
2558 that if such an elimination is present, it can always be used.
2559 This is the case on all known machines; if we don't make this
2560 assumption, we do unnecessary saving on many machines. */
2561 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
2562 int i;
2563
2564 for (i = 0; i < sizeof elim_regs / sizeof elim_regs[0]; i++)
2565 if (elim_regs[i].from == ARG_POINTER_REGNUM
2566 && elim_regs[i].to == FRAME_POINTER_REGNUM)
2567 break;
2568
2569 if (i == sizeof elim_regs / sizeof elim_regs [0])
2570#endif
2571 {
2572 /* Now restore our arg pointer from the address at which it
2573 was saved in our stack frame.
2574 If there hasn't be space allocated for it yet, make
2575 some now. */
2576 if (arg_pointer_save_area == 0)
2577 arg_pointer_save_area
2578 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
2579 emit_move_insn (virtual_incoming_args_rtx,
2580 /* We need a pseudo here, or else
2581 instantiate_virtual_regs_1 complains. */
2582 copy_to_reg (arg_pointer_save_area));
2583 }
28d81abb
RK
2584 }
2585#endif
2586
2587 /* The handler expects the desired label address in the static chain
2588 register. It tests the address and does an appropriate jump
2589 to whatever label is desired. */
2590 for (link = nonlocal_labels; link; link = TREE_CHAIN (link))
2591 /* Skip any labels we shouldn't be able to jump to from here. */
2592 if (! DECL_TOO_LATE (TREE_VALUE (link)))
2593 {
2594 rtx not_this = gen_label_rtx ();
2595 rtx this = gen_label_rtx ();
2596 do_jump_if_equal (static_chain_rtx,
2597 gen_rtx (LABEL_REF, Pmode, DECL_RTL (TREE_VALUE (link))),
2598 this, 0);
2599 emit_jump (not_this);
2600 emit_label (this);
2601 expand_goto (TREE_VALUE (link));
2602 emit_label (not_this);
2603 }
2604 /* If label is not recognized, abort. */
2605 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "abort"), 0,
2606 VOIDmode, 0);
2607 emit_label (afterward);
2608 }
2609
2610 /* Don't allow jumping into a block that has cleanups or a stack level. */
2611 if (dont_jump_in
2612 || thisblock->data.block.stack_level != 0
2613 || thisblock->data.block.cleanups != 0)
2614 {
2615 struct label_chain *chain;
2616
2617 /* Any labels in this block are no longer valid to go to.
2618 Mark them to cause an error message. */
2619 for (chain = thisblock->data.block.label_chain; chain; chain = chain->next)
2620 {
2621 DECL_TOO_LATE (chain->label) = 1;
2622 /* If any goto without a fixup came to this label,
2623 that must be an error, because gotos without fixups
2624 come from outside all saved stack-levels and all cleanups. */
2625 if (TREE_ADDRESSABLE (chain->label))
2626 error_with_decl (chain->label,
2627 "label `%s' used before containing binding contour");
2628 }
2629 }
2630
2631 /* Restore stack level in effect before the block
2632 (only if variable-size objects allocated). */
2633 /* Perform any cleanups associated with the block. */
2634
2635 if (thisblock->data.block.stack_level != 0
2636 || thisblock->data.block.cleanups != 0)
2637 {
2638 /* Don't let cleanups affect ({...}) constructs. */
2639 int old_expr_stmts_for_value = expr_stmts_for_value;
2640 rtx old_last_expr_value = last_expr_value;
2641 tree old_last_expr_type = last_expr_type;
2642 expr_stmts_for_value = 0;
2643
2644 /* Do the cleanups. */
37366632 2645 expand_cleanups (thisblock->data.block.cleanups, NULL_TREE);
28d81abb
RK
2646 do_pending_stack_adjust ();
2647
2648 expr_stmts_for_value = old_expr_stmts_for_value;
2649 last_expr_value = old_last_expr_value;
2650 last_expr_type = old_last_expr_type;
2651
2652 /* Restore the stack level. */
2653
2654 if (thisblock->data.block.stack_level != 0)
2655 {
59257ff7 2656 emit_stack_restore (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
37366632 2657 thisblock->data.block.stack_level, NULL_RTX);
59257ff7 2658 if (nonlocal_goto_handler_slot != 0)
37366632
RK
2659 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level,
2660 NULL_RTX);
28d81abb
RK
2661 }
2662
2663 /* Any gotos out of this block must also do these things.
59257ff7
RK
2664 Also report any gotos with fixups that came to labels in this
2665 level. */
28d81abb
RK
2666 fixup_gotos (thisblock,
2667 thisblock->data.block.stack_level,
2668 thisblock->data.block.cleanups,
2669 thisblock->data.block.first_insn,
2670 dont_jump_in);
2671 }
2672
2673 /* If doing stupid register allocation, make sure lives of all
2674 register variables declared here extend thru end of scope. */
2675
2676 if (obey_regdecls)
2677 for (decl = vars; decl; decl = TREE_CHAIN (decl))
2678 {
2679 rtx rtl = DECL_RTL (decl);
2680 if (TREE_CODE (decl) == VAR_DECL && rtl != 0)
2681 use_variable (rtl);
2682 }
2683
2684 /* Restore block_stack level for containing block. */
2685
2686 stack_block_stack = thisblock->data.block.innermost_stack_block;
2687 POPSTACK (block_stack);
2688
2689 /* Pop the stack slot nesting and free any slots at this level. */
2690 pop_temp_slots ();
2691}
2692\f
2693/* Generate RTL for the automatic variable declaration DECL.
2694 (Other kinds of declarations are simply ignored if seen here.)
2695 CLEANUP is an expression to be executed at exit from this binding contour;
2696 for example, in C++, it might call the destructor for this variable.
2697
2698 If CLEANUP contains any SAVE_EXPRs, then you must preevaluate them
2699 either before or after calling `expand_decl' but before compiling
2700 any subsequent expressions. This is because CLEANUP may be expanded
2701 more than once, on different branches of execution.
2702 For the same reason, CLEANUP may not contain a CALL_EXPR
2703 except as its topmost node--else `preexpand_calls' would get confused.
2704
2705 If CLEANUP is nonzero and DECL is zero, we record a cleanup
2706 that is not associated with any particular variable.
2707
2708 There is no special support here for C++ constructors.
2709 They should be handled by the proper code in DECL_INITIAL. */
2710
2711void
2712expand_decl (decl)
2713 register tree decl;
2714{
2715 struct nesting *thisblock = block_stack;
2716 tree type = TREE_TYPE (decl);
2717
2718 /* Only automatic variables need any expansion done.
2719 Static and external variables, and external functions,
2720 will be handled by `assemble_variable' (called from finish_decl).
2721 TYPE_DECL and CONST_DECL require nothing.
2722 PARM_DECLs are handled in `assign_parms'. */
2723
2724 if (TREE_CODE (decl) != VAR_DECL)
2725 return;
44fe2e80 2726 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
28d81abb
RK
2727 return;
2728
2729 /* Create the RTL representation for the variable. */
2730
2731 if (type == error_mark_node)
2732 DECL_RTL (decl) = gen_rtx (MEM, BLKmode, const0_rtx);
2733 else if (DECL_SIZE (decl) == 0)
2734 /* Variable with incomplete type. */
2735 {
2736 if (DECL_INITIAL (decl) == 0)
2737 /* Error message was already done; now avoid a crash. */
2738 DECL_RTL (decl) = assign_stack_temp (DECL_MODE (decl), 0, 1);
2739 else
2740 /* An initializer is going to decide the size of this array.
2741 Until we know the size, represent its address with a reg. */
2742 DECL_RTL (decl) = gen_rtx (MEM, BLKmode, gen_reg_rtx (Pmode));
2743 }
2744 else if (DECL_MODE (decl) != BLKmode
2745 /* If -ffloat-store, don't put explicit float vars
2746 into regs. */
2747 && !(flag_float_store
2748 && TREE_CODE (type) == REAL_TYPE)
2749 && ! TREE_THIS_VOLATILE (decl)
2750 && ! TREE_ADDRESSABLE (decl)
44fe2e80 2751 && (DECL_REGISTER (decl) || ! obey_regdecls))
28d81abb
RK
2752 {
2753 /* Automatic variable that can go in a register. */
2754 DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
2755 if (TREE_CODE (type) == POINTER_TYPE)
2756 mark_reg_pointer (DECL_RTL (decl));
2757 REG_USERVAR_P (DECL_RTL (decl)) = 1;
2758 }
2759 else if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2760 {
2761 /* Variable of fixed size that goes on the stack. */
2762 rtx oldaddr = 0;
2763 rtx addr;
2764
2765 /* If we previously made RTL for this decl, it must be an array
2766 whose size was determined by the initializer.
2767 The old address was a register; set that register now
2768 to the proper address. */
2769 if (DECL_RTL (decl) != 0)
2770 {
2771 if (GET_CODE (DECL_RTL (decl)) != MEM
2772 || GET_CODE (XEXP (DECL_RTL (decl), 0)) != REG)
2773 abort ();
2774 oldaddr = XEXP (DECL_RTL (decl), 0);
2775 }
2776
2777 DECL_RTL (decl)
2778 = assign_stack_temp (DECL_MODE (decl),
2779 ((TREE_INT_CST_LOW (DECL_SIZE (decl))
2780 + BITS_PER_UNIT - 1)
2781 / BITS_PER_UNIT),
2782 1);
2783
2784 /* Set alignment we actually gave this decl. */
2785 DECL_ALIGN (decl) = (DECL_MODE (decl) == BLKmode ? BIGGEST_ALIGNMENT
2786 : GET_MODE_BITSIZE (DECL_MODE (decl)));
2787
2788 if (oldaddr)
2789 {
2790 addr = force_operand (XEXP (DECL_RTL (decl), 0), oldaddr);
2791 if (addr != oldaddr)
2792 emit_move_insn (oldaddr, addr);
2793 }
2794
2795 /* If this is a memory ref that contains aggregate components,
2796 mark it as such for cse and loop optimize. */
2797 MEM_IN_STRUCT_P (DECL_RTL (decl))
2798 = (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
2799 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
2800 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE);
2801#if 0
2802 /* If this is in memory because of -ffloat-store,
2803 set the volatile bit, to prevent optimizations from
2804 undoing the effects. */
2805 if (flag_float_store && TREE_CODE (type) == REAL_TYPE)
2806 MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
2807#endif
2808 }
2809 else
2810 /* Dynamic-size object: must push space on the stack. */
2811 {
2812 rtx address, size;
2813
2814 /* Record the stack pointer on entry to block, if have
2815 not already done so. */
2816 if (thisblock->data.block.stack_level == 0)
2817 {
2818 do_pending_stack_adjust ();
59257ff7
RK
2819 emit_stack_save (thisblock->next ? SAVE_BLOCK : SAVE_FUNCTION,
2820 &thisblock->data.block.stack_level,
2821 thisblock->data.block.first_insn);
28d81abb
RK
2822 stack_block_stack = thisblock;
2823 }
2824
2825 /* Compute the variable's size, in bytes. */
2826 size = expand_expr (size_binop (CEIL_DIV_EXPR,
2827 DECL_SIZE (decl),
2828 size_int (BITS_PER_UNIT)),
37366632 2829 NULL_RTX, VOIDmode, 0);
28d81abb
RK
2830 free_temp_slots ();
2831
59257ff7
RK
2832 /* This is equivalent to calling alloca. */
2833 current_function_calls_alloca = 1;
2834
28d81abb 2835 /* Allocate space on the stack for the variable. */
37366632
RK
2836 address = allocate_dynamic_stack_space (size, NULL_RTX,
2837 DECL_ALIGN (decl));
28d81abb 2838
59257ff7 2839 if (nonlocal_goto_handler_slot != 0)
37366632 2840 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
28d81abb
RK
2841
2842 /* Reference the variable indirect through that rtx. */
2843 DECL_RTL (decl) = gen_rtx (MEM, DECL_MODE (decl), address);
2844
2207e295
RS
2845 /* If this is a memory ref that contains aggregate components,
2846 mark it as such for cse and loop optimize. */
2847 MEM_IN_STRUCT_P (DECL_RTL (decl))
2848 = (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
2849 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
2850 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE);
2851
28d81abb
RK
2852 /* Indicate the alignment we actually gave this variable. */
2853#ifdef STACK_BOUNDARY
2854 DECL_ALIGN (decl) = STACK_BOUNDARY;
2855#else
2856 DECL_ALIGN (decl) = BIGGEST_ALIGNMENT;
2857#endif
2858 }
2859
2860 if (TREE_THIS_VOLATILE (decl))
2861 MEM_VOLATILE_P (DECL_RTL (decl)) = 1;
2862 if (TREE_READONLY (decl))
2863 RTX_UNCHANGING_P (DECL_RTL (decl)) = 1;
2864
2865 /* If doing stupid register allocation, make sure life of any
2866 register variable starts here, at the start of its scope. */
2867
2868 if (obey_regdecls)
2869 use_variable (DECL_RTL (decl));
2870}
2871\f
2872/* Emit code to perform the initialization of a declaration DECL. */
2873
2874void
2875expand_decl_init (decl)
2876 tree decl;
2877{
b4ac57ab
RS
2878 int was_used = TREE_USED (decl);
2879
28d81abb
RK
2880 if (TREE_STATIC (decl))
2881 return;
2882
2883 /* Compute and store the initial value now. */
2884
2885 if (DECL_INITIAL (decl) == error_mark_node)
2886 {
2887 enum tree_code code = TREE_CODE (TREE_TYPE (decl));
2888 if (code == INTEGER_TYPE || code == REAL_TYPE || code == ENUMERAL_TYPE
2889 || code == POINTER_TYPE)
2890 expand_assignment (decl, convert (TREE_TYPE (decl), integer_zero_node),
2891 0, 0);
2892 emit_queue ();
2893 }
2894 else if (DECL_INITIAL (decl) && TREE_CODE (DECL_INITIAL (decl)) != TREE_LIST)
2895 {
2896 emit_line_note (DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2897 expand_assignment (decl, DECL_INITIAL (decl), 0, 0);
2898 emit_queue ();
2899 }
2900
b4ac57ab
RS
2901 /* Don't let the initialization count as "using" the variable. */
2902 TREE_USED (decl) = was_used;
2903
28d81abb
RK
2904 /* Free any temporaries we made while initializing the decl. */
2905 free_temp_slots ();
2906}
2907
2908/* CLEANUP is an expression to be executed at exit from this binding contour;
2909 for example, in C++, it might call the destructor for this variable.
2910
2911 If CLEANUP contains any SAVE_EXPRs, then you must preevaluate them
2912 either before or after calling `expand_decl' but before compiling
2913 any subsequent expressions. This is because CLEANUP may be expanded
2914 more than once, on different branches of execution.
2915 For the same reason, CLEANUP may not contain a CALL_EXPR
2916 except as its topmost node--else `preexpand_calls' would get confused.
2917
2918 If CLEANUP is nonzero and DECL is zero, we record a cleanup
2919 that is not associated with any particular variable. */
2920
2921int
2922expand_decl_cleanup (decl, cleanup)
2923 tree decl, cleanup;
2924{
2925 struct nesting *thisblock = block_stack;
2926
2927 /* Error if we are not in any block. */
2928 if (thisblock == 0)
2929 return 0;
2930
2931 /* Record the cleanup if there is one. */
2932
2933 if (cleanup != 0)
2934 {
2935 thisblock->data.block.cleanups
2936 = temp_tree_cons (decl, cleanup, thisblock->data.block.cleanups);
2937 /* If this block has a cleanup, it belongs in stack_block_stack. */
2938 stack_block_stack = thisblock;
2939 }
2940 return 1;
2941}
2942\f
2943/* DECL is an anonymous union. CLEANUP is a cleanup for DECL.
2944 DECL_ELTS is the list of elements that belong to DECL's type.
2945 In each, the TREE_VALUE is a VAR_DECL, and the TREE_PURPOSE a cleanup. */
2946
2947void
2948expand_anon_union_decl (decl, cleanup, decl_elts)
2949 tree decl, cleanup, decl_elts;
2950{
2951 struct nesting *thisblock = block_stack;
2952 rtx x;
2953
2954 expand_decl (decl, cleanup);
2955 x = DECL_RTL (decl);
2956
2957 while (decl_elts)
2958 {
2959 tree decl_elt = TREE_VALUE (decl_elts);
2960 tree cleanup_elt = TREE_PURPOSE (decl_elts);
2961 enum machine_mode mode = TYPE_MODE (TREE_TYPE (decl_elt));
2962
2963 /* (SUBREG (MEM ...)) at RTL generation time is invalid, so we
2964 instead create a new MEM rtx with the proper mode. */
2965 if (GET_CODE (x) == MEM)
2966 {
2967 if (mode == GET_MODE (x))
2968 DECL_RTL (decl_elt) = x;
2969 else
2970 {
2971 DECL_RTL (decl_elt) = gen_rtx (MEM, mode, copy_rtx (XEXP (x, 0)));
2972 MEM_IN_STRUCT_P (DECL_RTL (decl_elt)) = MEM_IN_STRUCT_P (x);
2973 RTX_UNCHANGING_P (DECL_RTL (decl_elt)) = RTX_UNCHANGING_P (x);
2974 }
2975 }
2976 else if (GET_CODE (x) == REG)
2977 {
2978 if (mode == GET_MODE (x))
2979 DECL_RTL (decl_elt) = x;
2980 else
2981 DECL_RTL (decl_elt) = gen_rtx (SUBREG, mode, x, 0);
2982 }
2983 else
2984 abort ();
2985
2986 /* Record the cleanup if there is one. */
2987
2988 if (cleanup != 0)
2989 thisblock->data.block.cleanups
2990 = temp_tree_cons (decl_elt, cleanup_elt,
2991 thisblock->data.block.cleanups);
2992
2993 decl_elts = TREE_CHAIN (decl_elts);
2994 }
2995}
2996\f
2997/* Expand a list of cleanups LIST.
2998 Elements may be expressions or may be nested lists.
2999
3000 If DONT_DO is nonnull, then any list-element
3001 whose TREE_PURPOSE matches DONT_DO is omitted.
3002 This is sometimes used to avoid a cleanup associated with
3003 a value that is being returned out of the scope. */
3004
3005static void
3006expand_cleanups (list, dont_do)
3007 tree list;
3008 tree dont_do;
3009{
3010 tree tail;
3011 for (tail = list; tail; tail = TREE_CHAIN (tail))
3012 if (dont_do == 0 || TREE_PURPOSE (tail) != dont_do)
3013 {
3014 if (TREE_CODE (TREE_VALUE (tail)) == TREE_LIST)
3015 expand_cleanups (TREE_VALUE (tail), dont_do);
3016 else
3017 {
3018 /* Cleanups may be run multiple times. For example,
3019 when exiting a binding contour, we expand the
3020 cleanups associated with that contour. When a goto
3021 within that binding contour has a target outside that
3022 contour, it will expand all cleanups from its scope to
3023 the target. Though the cleanups are expanded multiple
3024 times, the control paths are non-overlapping so the
3025 cleanups will not be executed twice. */
3026 expand_expr (TREE_VALUE (tail), const0_rtx, VOIDmode, 0);
3027 free_temp_slots ();
3028 }
3029 }
3030}
3031
3032/* Expand a list of cleanups for a goto fixup.
3033 The expansion is put into the insn chain after the insn *BEFORE_JUMP
3034 and *BEFORE_JUMP is set to the insn that now comes before the jump. */
3035
3036static void
3037fixup_cleanups (list, before_jump)
3038 tree list;
3039 rtx *before_jump;
3040{
3041 rtx beyond_jump = get_last_insn ();
3042 rtx new_before_jump;
3043
37366632 3044 expand_cleanups (list, NULL_TREE);
28d81abb
RK
3045 /* Pop any pushes done in the cleanups,
3046 in case function is about to return. */
3047 do_pending_stack_adjust ();
3048
3049 new_before_jump = get_last_insn ();
3050
3051 if (beyond_jump != new_before_jump)
3052 {
3053 /* If cleanups expand to nothing, don't reorder. */
3054 reorder_insns (NEXT_INSN (beyond_jump), new_before_jump, *before_jump);
3055 *before_jump = new_before_jump;
3056 }
3057}
3058
3059/* Move all cleanups from the current block_stack
3060 to the containing block_stack, where they are assumed to
3061 have been created. If anything can cause a temporary to
3062 be created, but not expanded for more than one level of
3063 block_stacks, then this code will have to change. */
3064
3065void
3066move_cleanups_up ()
3067{
3068 struct nesting *block = block_stack;
3069 struct nesting *outer = block->next;
3070
3071 outer->data.block.cleanups
3072 = chainon (block->data.block.cleanups,
3073 outer->data.block.cleanups);
3074 block->data.block.cleanups = 0;
3075}
3076
3077tree
3078last_cleanup_this_contour ()
3079{
3080 if (block_stack == 0)
3081 return 0;
3082
3083 return block_stack->data.block.cleanups;
3084}
3085
3086/* Return 1 if there are any pending cleanups at this point.
3087 If THIS_CONTOUR is nonzero, check the current contour as well.
3088 Otherwise, look only at the contours that enclose this one. */
3089
3090int
3091any_pending_cleanups (this_contour)
3092 int this_contour;
3093{
3094 struct nesting *block;
3095
3096 if (block_stack == 0)
3097 return 0;
3098
3099 if (this_contour && block_stack->data.block.cleanups != NULL)
3100 return 1;
3101 if (block_stack->data.block.cleanups == 0
3102 && (block_stack->data.block.outer_cleanups == 0
3103#if 0
3104 || block_stack->data.block.outer_cleanups == empty_cleanup_list
3105#endif
3106 ))
3107 return 0;
3108
3109 for (block = block_stack->next; block; block = block->next)
3110 if (block->data.block.cleanups != 0)
3111 return 1;
3112
3113 return 0;
3114}
3115\f
3116/* Enter a case (Pascal) or switch (C) statement.
3117 Push a block onto case_stack and nesting_stack
3118 to accumulate the case-labels that are seen
3119 and to record the labels generated for the statement.
3120
3121 EXIT_FLAG is nonzero if `exit_something' should exit this case stmt.
3122 Otherwise, this construct is transparent for `exit_something'.
3123
3124 EXPR is the index-expression to be dispatched on.
3125 TYPE is its nominal type. We could simply convert EXPR to this type,
3126 but instead we take short cuts. */
3127
3128void
3129expand_start_case (exit_flag, expr, type, printname)
3130 int exit_flag;
3131 tree expr;
3132 tree type;
3133 char *printname;
3134{
3135 register struct nesting *thiscase = ALLOC_NESTING ();
3136
3137 /* Make an entry on case_stack for the case we are entering. */
3138
3139 thiscase->next = case_stack;
3140 thiscase->all = nesting_stack;
3141 thiscase->depth = ++nesting_depth;
3142 thiscase->exit_label = exit_flag ? gen_label_rtx () : 0;
3143 thiscase->data.case_stmt.case_list = 0;
3144 thiscase->data.case_stmt.index_expr = expr;
3145 thiscase->data.case_stmt.nominal_type = type;
3146 thiscase->data.case_stmt.default_label = 0;
3147 thiscase->data.case_stmt.num_ranges = 0;
3148 thiscase->data.case_stmt.printname = printname;
3149 thiscase->data.case_stmt.seenlabel = 0;
3150 case_stack = thiscase;
3151 nesting_stack = thiscase;
3152
3153 do_pending_stack_adjust ();
3154
3155 /* Make sure case_stmt.start points to something that won't
3156 need any transformation before expand_end_case. */
3157 if (GET_CODE (get_last_insn ()) != NOTE)
37366632 3158 emit_note (NULL_PTR, NOTE_INSN_DELETED);
28d81abb
RK
3159
3160 thiscase->data.case_stmt.start = get_last_insn ();
3161}
3162
3163/* Start a "dummy case statement" within which case labels are invalid
3164 and are not connected to any larger real case statement.
3165 This can be used if you don't want to let a case statement jump
3166 into the middle of certain kinds of constructs. */
3167
3168void
3169expand_start_case_dummy ()
3170{
3171 register struct nesting *thiscase = ALLOC_NESTING ();
3172
3173 /* Make an entry on case_stack for the dummy. */
3174
3175 thiscase->next = case_stack;
3176 thiscase->all = nesting_stack;
3177 thiscase->depth = ++nesting_depth;
3178 thiscase->exit_label = 0;
3179 thiscase->data.case_stmt.case_list = 0;
3180 thiscase->data.case_stmt.start = 0;
3181 thiscase->data.case_stmt.nominal_type = 0;
3182 thiscase->data.case_stmt.default_label = 0;
3183 thiscase->data.case_stmt.num_ranges = 0;
3184 case_stack = thiscase;
3185 nesting_stack = thiscase;
3186}
3187
3188/* End a dummy case statement. */
3189
3190void
3191expand_end_case_dummy ()
3192{
3193 POPSTACK (case_stack);
3194}
3195
3196/* Return the data type of the index-expression
3197 of the innermost case statement, or null if none. */
3198
3199tree
3200case_index_expr_type ()
3201{
3202 if (case_stack)
3203 return TREE_TYPE (case_stack->data.case_stmt.index_expr);
3204 return 0;
3205}
3206\f
3207/* Accumulate one case or default label inside a case or switch statement.
3208 VALUE is the value of the case (a null pointer, for a default label).
3209
3210 If not currently inside a case or switch statement, return 1 and do
3211 nothing. The caller will print a language-specific error message.
3212 If VALUE is a duplicate or overlaps, return 2 and do nothing
3213 except store the (first) duplicate node in *DUPLICATE.
3214 If VALUE is out of range, return 3 and do nothing.
3215 If we are jumping into the scope of a cleaup or var-sized array, return 5.
3216 Return 0 on success.
3217
3218 Extended to handle range statements. */
3219
3220int
3221pushcase (value, label, duplicate)
3222 register tree value;
3223 register tree label;
3224 tree *duplicate;
3225{
3226 register struct case_node **l;
3227 register struct case_node *n;
3228 tree index_type;
3229 tree nominal_type;
3230
3231 /* Fail if not inside a real case statement. */
3232 if (! (case_stack && case_stack->data.case_stmt.start))
3233 return 1;
3234
3235 if (stack_block_stack
3236 && stack_block_stack->depth > case_stack->depth)
3237 return 5;
3238
3239 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
3240 nominal_type = case_stack->data.case_stmt.nominal_type;
3241
3242 /* If the index is erroneous, avoid more problems: pretend to succeed. */
3243 if (index_type == error_mark_node)
3244 return 0;
3245
3246 /* Convert VALUE to the type in which the comparisons are nominally done. */
3247 if (value != 0)
3248 value = convert (nominal_type, value);
3249
3250 /* If this is the first label, warn if any insns have been emitted. */
3251 if (case_stack->data.case_stmt.seenlabel == 0)
3252 {
3253 rtx insn;
3254 for (insn = case_stack->data.case_stmt.start;
3255 insn;
3256 insn = NEXT_INSN (insn))
3257 {
3258 if (GET_CODE (insn) == CODE_LABEL)
3259 break;
3260 if (GET_CODE (insn) != NOTE
3261 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
3262 {
3263 warning ("unreachable code at beginning of %s",
3264 case_stack->data.case_stmt.printname);
3265 break;
3266 }
3267 }
3268 }
3269 case_stack->data.case_stmt.seenlabel = 1;
3270
3271 /* Fail if this value is out of range for the actual type of the index
3272 (which may be narrower than NOMINAL_TYPE). */
3273 if (value != 0 && ! int_fits_type_p (value, index_type))
3274 return 3;
3275
3276 /* Fail if this is a duplicate or overlaps another entry. */
3277 if (value == 0)
3278 {
3279 if (case_stack->data.case_stmt.default_label != 0)
3280 {
3281 *duplicate = case_stack->data.case_stmt.default_label;
3282 return 2;
3283 }
3284 case_stack->data.case_stmt.default_label = label;
3285 }
3286 else
3287 {
3288 /* Find the elt in the chain before which to insert the new value,
3289 to keep the chain sorted in increasing order.
3290 But report an error if this element is a duplicate. */
3291 for (l = &case_stack->data.case_stmt.case_list;
3292 /* Keep going past elements distinctly less than VALUE. */
3293 *l != 0 && tree_int_cst_lt ((*l)->high, value);
3294 l = &(*l)->right)
3295 ;
3296 if (*l)
3297 {
3298 /* Element we will insert before must be distinctly greater;
3299 overlap means error. */
3300 if (! tree_int_cst_lt (value, (*l)->low))
3301 {
3302 *duplicate = (*l)->code_label;
3303 return 2;
3304 }
3305 }
3306
3307 /* Add this label to the chain, and succeed.
3308 Copy VALUE so it is on temporary rather than momentary
3309 obstack and will thus survive till the end of the case statement. */
3310 n = (struct case_node *) oballoc (sizeof (struct case_node));
3311 n->left = 0;
3312 n->right = *l;
3313 n->high = n->low = copy_node (value);
3314 n->code_label = label;
3315 *l = n;
3316 }
3317
3318 expand_label (label);
3319 return 0;
3320}
3321
3322/* Like pushcase but this case applies to all values
3323 between VALUE1 and VALUE2 (inclusive).
3324 The return value is the same as that of pushcase
3325 but there is one additional error code:
3326 4 means the specified range was empty. */
3327
3328int
3329pushcase_range (value1, value2, label, duplicate)
3330 register tree value1, value2;
3331 register tree label;
3332 tree *duplicate;
3333{
3334 register struct case_node **l;
3335 register struct case_node *n;
3336 tree index_type;
3337 tree nominal_type;
3338
3339 /* Fail if not inside a real case statement. */
3340 if (! (case_stack && case_stack->data.case_stmt.start))
3341 return 1;
3342
3343 if (stack_block_stack
3344 && stack_block_stack->depth > case_stack->depth)
3345 return 5;
3346
3347 index_type = TREE_TYPE (case_stack->data.case_stmt.index_expr);
3348 nominal_type = case_stack->data.case_stmt.nominal_type;
3349
3350 /* If the index is erroneous, avoid more problems: pretend to succeed. */
3351 if (index_type == error_mark_node)
3352 return 0;
3353
3354 /* If this is the first label, warn if any insns have been emitted. */
3355 if (case_stack->data.case_stmt.seenlabel == 0)
3356 {
3357 rtx insn;
3358 for (insn = case_stack->data.case_stmt.start;
3359 insn;
3360 insn = NEXT_INSN (insn))
3361 {
3362 if (GET_CODE (insn) == CODE_LABEL)
3363 break;
3364 if (GET_CODE (insn) != NOTE
3365 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn)) != USE))
3366 {
3367 warning ("unreachable code at beginning of %s",
3368 case_stack->data.case_stmt.printname);
3369 break;
3370 }
3371 }
3372 }
3373 case_stack->data.case_stmt.seenlabel = 1;
3374
3375 /* Convert VALUEs to type in which the comparisons are nominally done. */
3376 if (value1 == 0) /* Negative infinity. */
3377 value1 = TYPE_MIN_VALUE(index_type);
3378 value1 = convert (nominal_type, value1);
3379
3380 if (value2 == 0) /* Positive infinity. */
3381 value2 = TYPE_MAX_VALUE(index_type);
3382 value2 = convert (nominal_type, value2);
3383
3384 /* Fail if these values are out of range. */
3385 if (! int_fits_type_p (value1, index_type))
3386 return 3;
3387
3388 if (! int_fits_type_p (value2, index_type))
3389 return 3;
3390
3391 /* Fail if the range is empty. */
3392 if (tree_int_cst_lt (value2, value1))
3393 return 4;
3394
3395 /* If the bounds are equal, turn this into the one-value case. */
3396 if (tree_int_cst_equal (value1, value2))
3397 return pushcase (value1, label, duplicate);
3398
3399 /* Find the elt in the chain before which to insert the new value,
3400 to keep the chain sorted in increasing order.
3401 But report an error if this element is a duplicate. */
3402 for (l = &case_stack->data.case_stmt.case_list;
3403 /* Keep going past elements distinctly less than this range. */
3404 *l != 0 && tree_int_cst_lt ((*l)->high, value1);
3405 l = &(*l)->right)
3406 ;
3407 if (*l)
3408 {
3409 /* Element we will insert before must be distinctly greater;
3410 overlap means error. */
3411 if (! tree_int_cst_lt (value2, (*l)->low))
3412 {
3413 *duplicate = (*l)->code_label;
3414 return 2;
3415 }
3416 }
3417
3418 /* Add this label to the chain, and succeed.
3419 Copy VALUE1, VALUE2 so they are on temporary rather than momentary
3420 obstack and will thus survive till the end of the case statement. */
3421
3422 n = (struct case_node *) oballoc (sizeof (struct case_node));
3423 n->left = 0;
3424 n->right = *l;
3425 n->low = copy_node (value1);
3426 n->high = copy_node (value2);
3427 n->code_label = label;
3428 *l = n;
3429
3430 expand_label (label);
3431
3432 case_stack->data.case_stmt.num_ranges++;
3433
3434 return 0;
3435}
3436\f
3437/* Called when the index of a switch statement is an enumerated type
3438 and there is no default label.
3439
3440 Checks that all enumeration literals are covered by the case
3441 expressions of a switch. Also, warn if there are any extra
3442 switch cases that are *not* elements of the enumerated type.
3443
3444 If all enumeration literals were covered by the case expressions,
3445 turn one of the expressions into the default expression since it should
3446 not be possible to fall through such a switch. */
3447
3448void
3449check_for_full_enumeration_handling (type)
3450 tree type;
3451{
3452 register struct case_node *n;
3453 register struct case_node **l;
3454 register tree chain;
3455 int all_values = 1;
3456
3457 /* The time complexity of this loop is currently O(N * M), with
3458 N being the number of enumerals in the enumerated type, and
3459 M being the number of case expressions in the switch. */
3460
3461 for (chain = TYPE_VALUES (type);
3462 chain;
3463 chain = TREE_CHAIN (chain))
3464 {
3465 /* Find a match between enumeral and case expression, if possible.
3466 Quit looking when we've gone too far (since case expressions
3467 are kept sorted in ascending order). Warn about enumerals not
3468 handled in the switch statement case expression list. */
3469
3470 for (n = case_stack->data.case_stmt.case_list;
3471 n && tree_int_cst_lt (n->high, TREE_VALUE (chain));
3472 n = n->right)
3473 ;
3474
1ddde1cd 3475 if (!n || tree_int_cst_lt (TREE_VALUE (chain), n->low))
28d81abb
RK
3476 {
3477 if (warn_switch)
1ddde1cd 3478 warning ("enumeration value `%s' not handled in switch",
28d81abb
RK
3479 IDENTIFIER_POINTER (TREE_PURPOSE (chain)));
3480 all_values = 0;
3481 }
3482 }
3483
3484 /* Now we go the other way around; we warn if there are case
3485 expressions that don't correspond to enumerals. This can
3486 occur since C and C++ don't enforce type-checking of
3487 assignments to enumeration variables. */
3488
3489 if (warn_switch)
3490 for (n = case_stack->data.case_stmt.case_list; n; n = n->right)
3491 {
3492 for (chain = TYPE_VALUES (type);
3493 chain && !tree_int_cst_equal (n->low, TREE_VALUE (chain));
3494 chain = TREE_CHAIN (chain))
3495 ;
3496
3497 if (!chain)
3498 warning ("case value `%d' not in enumerated type `%s'",
3499 TREE_INT_CST_LOW (n->low),
3500 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
3501 == IDENTIFIER_NODE)
3502 ? TYPE_NAME (type)
3503 : DECL_NAME (TYPE_NAME (type))));
1ddde1cd
RS
3504 if (!tree_int_cst_equal (n->low, n->high))
3505 {
3506 for (chain = TYPE_VALUES (type);
3507 chain && !tree_int_cst_equal (n->high, TREE_VALUE (chain));
3508 chain = TREE_CHAIN (chain))
3509 ;
3510
3511 if (!chain)
3512 warning ("case value `%d' not in enumerated type `%s'",
3513 TREE_INT_CST_LOW (n->high),
3514 IDENTIFIER_POINTER ((TREE_CODE (TYPE_NAME (type))
3515 == IDENTIFIER_NODE)
3516 ? TYPE_NAME (type)
3517 : DECL_NAME (TYPE_NAME (type))));
3518 }
28d81abb
RK
3519 }
3520
3521 /* If all values were found as case labels, make one of them the default
3522 label. Thus, this switch will never fall through. We arbitrarily pick
3523 the last one to make the default since this is likely the most
3524 efficient choice. */
3525
3526 if (all_values)
3527 {
3528 for (l = &case_stack->data.case_stmt.case_list;
3529 (*l)->right != 0;
3530 l = &(*l)->right)
3531 ;
3532
3533 case_stack->data.case_stmt.default_label = (*l)->code_label;
3534 *l = 0;
3535 }
3536}
3537\f
3538/* Terminate a case (Pascal) or switch (C) statement
9ab0ddd7 3539 in which ORIG_INDEX is the expression to be tested.
28d81abb
RK
3540 Generate the code to test it and jump to the right place. */
3541
3542void
3543expand_end_case (orig_index)
3544 tree orig_index;
3545{
3546 tree minval, maxval, range;
3547 rtx default_label = 0;
3548 register struct case_node *n;
3549 int count;
3550 rtx index;
3551 rtx table_label = gen_label_rtx ();
3552 int ncases;
3553 rtx *labelvec;
3554 register int i;
3555 rtx before_case;
3556 register struct nesting *thiscase = case_stack;
3557 tree index_expr = thiscase->data.case_stmt.index_expr;
3558 int unsignedp = TREE_UNSIGNED (TREE_TYPE (index_expr));
3559
3560 do_pending_stack_adjust ();
3561
3562 /* An ERROR_MARK occurs for various reasons including invalid data type. */
3563 if (TREE_TYPE (index_expr) != error_mark_node)
3564 {
3565 /* If switch expression was an enumerated type, check that all
3566 enumeration literals are covered by the cases.
3567 No sense trying this if there's a default case, however. */
3568
3569 if (!thiscase->data.case_stmt.default_label
3570 && TREE_CODE (TREE_TYPE (orig_index)) == ENUMERAL_TYPE
3571 && TREE_CODE (index_expr) != INTEGER_CST)
3572 check_for_full_enumeration_handling (TREE_TYPE (orig_index));
3573
3574 /* If this is the first label, warn if any insns have been emitted. */
3575 if (thiscase->data.case_stmt.seenlabel == 0)
3576 {
3577 rtx insn;
3578 for (insn = get_last_insn ();
3579 insn != case_stack->data.case_stmt.start;
3580 insn = PREV_INSN (insn))
3581 if (GET_CODE (insn) != NOTE
3582 && (GET_CODE (insn) != INSN || GET_CODE (PATTERN (insn))!= USE))
3583 {
3584 warning ("unreachable code at beginning of %s",
3585 case_stack->data.case_stmt.printname);
3586 break;
3587 }
3588 }
3589
3590 /* If we don't have a default-label, create one here,
3591 after the body of the switch. */
3592 if (thiscase->data.case_stmt.default_label == 0)
3593 {
3594 thiscase->data.case_stmt.default_label
3595 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3596 expand_label (thiscase->data.case_stmt.default_label);
3597 }
3598 default_label = label_rtx (thiscase->data.case_stmt.default_label);
3599
3600 before_case = get_last_insn ();
3601
3602 /* Simplify the case-list before we count it. */
3603 group_case_nodes (thiscase->data.case_stmt.case_list);
3604
3605 /* Get upper and lower bounds of case values.
3606 Also convert all the case values to the index expr's data type. */
3607
3608 count = 0;
3609 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
3610 {
3611 /* Check low and high label values are integers. */
3612 if (TREE_CODE (n->low) != INTEGER_CST)
3613 abort ();
3614 if (TREE_CODE (n->high) != INTEGER_CST)
3615 abort ();
3616
3617 n->low = convert (TREE_TYPE (index_expr), n->low);
3618 n->high = convert (TREE_TYPE (index_expr), n->high);
3619
3620 /* Count the elements and track the largest and smallest
3621 of them (treating them as signed even if they are not). */
3622 if (count++ == 0)
3623 {
3624 minval = n->low;
3625 maxval = n->high;
3626 }
3627 else
3628 {
3629 if (INT_CST_LT (n->low, minval))
3630 minval = n->low;
3631 if (INT_CST_LT (maxval, n->high))
3632 maxval = n->high;
3633 }
3634 /* A range counts double, since it requires two compares. */
3635 if (! tree_int_cst_equal (n->low, n->high))
3636 count++;
3637 }
3638
3639 /* Compute span of values. */
3640 if (count != 0)
3641 range = fold (build (MINUS_EXPR, TREE_TYPE (index_expr),
3642 maxval, minval));
3643
3644 if (count == 0 || TREE_CODE (TREE_TYPE (index_expr)) == ERROR_MARK)
3645 {
3646 expand_expr (index_expr, const0_rtx, VOIDmode, 0);
3647 emit_queue ();
3648 emit_jump (default_label);
3649 }
3650 /* If range of values is much bigger than number of values,
3651 make a sequence of conditional branches instead of a dispatch.
3652 If the switch-index is a constant, do it this way
3653 because we can optimize it. */
4f73c5dd
TW
3654
3655#ifndef CASE_VALUES_THRESHOLD
28d81abb 3656#ifdef HAVE_casesi
4f73c5dd 3657#define CASE_VALUES_THRESHOLD (HAVE_casesi ? 4 : 5)
28d81abb 3658#else
4f73c5dd
TW
3659 /* If machine does not have a case insn that compares the
3660 bounds, this means extra overhead for dispatch tables
3661 which raises the threshold for using them. */
3662#define CASE_VALUES_THRESHOLD 5
3663#endif /* HAVE_casesi */
3664#endif /* CASE_VALUES_THRESHOLD */
3665
3666 else if (TREE_INT_CST_HIGH (range) != 0
3667 || count < CASE_VALUES_THRESHOLD
37366632
RK
3668 || ((unsigned HOST_WIDE_INT) (TREE_INT_CST_LOW (range))
3669 > 10 * count)
28d81abb 3670 || TREE_CODE (index_expr) == INTEGER_CST
b4ac57ab 3671 /* These will reduce to a constant. */
28d81abb 3672 || (TREE_CODE (index_expr) == CALL_EXPR
de14fd73 3673 && TREE_CODE (TREE_OPERAND (index_expr, 0)) == ADDR_EXPR
28d81abb 3674 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == FUNCTION_DECL
b4ac57ab
RS
3675 && DECL_FUNCTION_CODE (TREE_OPERAND (TREE_OPERAND (index_expr, 0), 0)) == BUILT_IN_CLASSIFY_TYPE)
3676 || (TREE_CODE (index_expr) == COMPOUND_EXPR
3677 && TREE_CODE (TREE_OPERAND (index_expr, 1)) == INTEGER_CST))
28d81abb 3678 {
37366632 3679 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
28d81abb
RK
3680
3681 /* If the index is a short or char that we do not have
3682 an insn to handle comparisons directly, convert it to
3683 a full integer now, rather than letting each comparison
3684 generate the conversion. */
3685
3686 if (GET_MODE_CLASS (GET_MODE (index)) == MODE_INT
3687 && (cmp_optab->handlers[(int) GET_MODE(index)].insn_code
3688 == CODE_FOR_nothing))
3689 {
3690 enum machine_mode wider_mode;
3691 for (wider_mode = GET_MODE (index); wider_mode != VOIDmode;
3692 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3693 if (cmp_optab->handlers[(int) wider_mode].insn_code
3694 != CODE_FOR_nothing)
3695 {
3696 index = convert_to_mode (wider_mode, index, unsignedp);
3697 break;
3698 }
3699 }
3700
3701 emit_queue ();
3702 do_pending_stack_adjust ();
3703
3704 index = protect_from_queue (index, 0);
3705 if (GET_CODE (index) == MEM)
3706 index = copy_to_reg (index);
3707 if (GET_CODE (index) == CONST_INT
3708 || TREE_CODE (index_expr) == INTEGER_CST)
3709 {
3710 /* Make a tree node with the proper constant value
3711 if we don't already have one. */
3712 if (TREE_CODE (index_expr) != INTEGER_CST)
3713 {
3714 index_expr
3715 = build_int_2 (INTVAL (index),
3716 !unsignedp && INTVAL (index) >= 0 ? 0 : -1);
3717 index_expr = convert (TREE_TYPE (index_expr), index_expr);
3718 }
3719
3720 /* For constant index expressions we need only
3721 issue a unconditional branch to the appropriate
3722 target code. The job of removing any unreachable
3723 code is left to the optimisation phase if the
3724 "-O" option is specified. */
3725 for (n = thiscase->data.case_stmt.case_list;
3726 n;
3727 n = n->right)
3728 {
3729 if (! tree_int_cst_lt (index_expr, n->low)
3730 && ! tree_int_cst_lt (n->high, index_expr))
3731 break;
3732 }
3733 if (n)
3734 emit_jump (label_rtx (n->code_label));
3735 else
3736 emit_jump (default_label);
3737 }
3738 else
3739 {
3740 /* If the index expression is not constant we generate
3741 a binary decision tree to select the appropriate
3742 target code. This is done as follows:
3743
3744 The list of cases is rearranged into a binary tree,
3745 nearly optimal assuming equal probability for each case.
3746
3747 The tree is transformed into RTL, eliminating
3748 redundant test conditions at the same time.
3749
3750 If program flow could reach the end of the
3751 decision tree an unconditional jump to the
3752 default code is emitted. */
3753
3754 use_cost_table
3755 = (TREE_CODE (TREE_TYPE (orig_index)) != ENUMERAL_TYPE
28d81abb 3756 && estimate_case_costs (thiscase->data.case_stmt.case_list));
37366632
RK
3757 balance_case_nodes (&thiscase->data.case_stmt.case_list,
3758 NULL_PTR);
28d81abb
RK
3759 emit_case_nodes (index, thiscase->data.case_stmt.case_list,
3760 default_label, TREE_TYPE (index_expr));
3761 emit_jump_if_reachable (default_label);
3762 }
3763 }
3764 else
3765 {
3766 int win = 0;
3767#ifdef HAVE_casesi
3768 if (HAVE_casesi)
3769 {
c4fcf531 3770 enum machine_mode index_mode = SImode;
5130a5cc 3771 int index_bits = GET_MODE_BITSIZE (index_mode);
c4fcf531 3772
28d81abb 3773 /* Convert the index to SImode. */
c4fcf531
RS
3774 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (index_expr)))
3775 > GET_MODE_BITSIZE (index_mode))
28d81abb 3776 {
af2682ef 3777 enum machine_mode omode = TYPE_MODE (TREE_TYPE (index_expr));
37366632 3778 rtx rangertx = expand_expr (range, NULL_RTX, VOIDmode, 0);
af2682ef
RS
3779
3780 /* We must handle the endpoints in the original mode. */
28d81abb
RK
3781 index_expr = build (MINUS_EXPR, TREE_TYPE (index_expr),
3782 index_expr, minval);
3783 minval = integer_zero_node;
37366632
RK
3784 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
3785 emit_cmp_insn (rangertx, index, LTU, NULL_RTX, omode, 0, 0);
af2682ef
RS
3786 emit_jump_insn (gen_bltu (default_label));
3787 /* Now we can safely truncate. */
3788 index = convert_to_mode (index_mode, index, 0);
3789 }
3790 else
3791 {
3792 if (TYPE_MODE (TREE_TYPE (index_expr)) != index_mode)
3793 index_expr = convert (type_for_size (index_bits, 0),
3794 index_expr);
37366632 3795 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
28d81abb 3796 }
28d81abb
RK
3797 emit_queue ();
3798 index = protect_from_queue (index, 0);
3799 do_pending_stack_adjust ();
3800
37366632
RK
3801 emit_jump_insn (gen_casesi (index, expand_expr (minval, NULL_RTX,
3802 VOIDmode, 0),
3803 expand_expr (range, NULL_RTX,
3804 VOIDmode, 0),
28d81abb
RK
3805 table_label, default_label));
3806 win = 1;
3807 }
3808#endif
3809#ifdef HAVE_tablejump
3810 if (! win && HAVE_tablejump)
3811 {
3812 index_expr = convert (thiscase->data.case_stmt.nominal_type,
b4ac57ab
RS
3813 fold (build (MINUS_EXPR,
3814 TREE_TYPE (index_expr),
3815 index_expr, minval)));
37366632 3816 index = expand_expr (index_expr, NULL_RTX, VOIDmode, 0);
28d81abb 3817 emit_queue ();
af2682ef 3818 index = protect_from_queue (index, 0);
28d81abb
RK
3819 do_pending_stack_adjust ();
3820
af2682ef 3821 do_tablejump (index, TYPE_MODE (TREE_TYPE (index_expr)),
37366632 3822 expand_expr (range, NULL_RTX, VOIDmode, 0),
28d81abb
RK
3823 table_label, default_label);
3824 win = 1;
3825 }
3826#endif
3827 if (! win)
3828 abort ();
3829
3830 /* Get table of labels to jump to, in order of case index. */
3831
3832 ncases = TREE_INT_CST_LOW (range) + 1;
3833 labelvec = (rtx *) alloca (ncases * sizeof (rtx));
3834 bzero (labelvec, ncases * sizeof (rtx));
3835
3836 for (n = thiscase->data.case_stmt.case_list; n; n = n->right)
3837 {
37366632 3838 register HOST_WIDE_INT i
28d81abb
RK
3839 = TREE_INT_CST_LOW (n->low) - TREE_INT_CST_LOW (minval);
3840
3841 while (1)
3842 {
3843 labelvec[i]
3844 = gen_rtx (LABEL_REF, Pmode, label_rtx (n->code_label));
3845 if (i + TREE_INT_CST_LOW (minval)
3846 == TREE_INT_CST_LOW (n->high))
3847 break;
3848 i++;
3849 }
3850 }
3851
3852 /* Fill in the gaps with the default. */
3853 for (i = 0; i < ncases; i++)
3854 if (labelvec[i] == 0)
3855 labelvec[i] = gen_rtx (LABEL_REF, Pmode, default_label);
3856
3857 /* Output the table */
3858 emit_label (table_label);
3859
3860 /* This would be a lot nicer if CASE_VECTOR_PC_RELATIVE
858a47b1 3861 were an expression, instead of an #ifdef/#ifndef. */
28d81abb
RK
3862 if (
3863#ifdef CASE_VECTOR_PC_RELATIVE
3864 1 ||
3865#endif
3866 flag_pic)
3867 emit_jump_insn (gen_rtx (ADDR_DIFF_VEC, CASE_VECTOR_MODE,
3868 gen_rtx (LABEL_REF, Pmode, table_label),
3869 gen_rtvec_v (ncases, labelvec)));
3870 else
3871 emit_jump_insn (gen_rtx (ADDR_VEC, CASE_VECTOR_MODE,
3872 gen_rtvec_v (ncases, labelvec)));
3873
3874 /* If the case insn drops through the table,
3875 after the table we must jump to the default-label.
3876 Otherwise record no drop-through after the table. */
3877#ifdef CASE_DROPS_THROUGH
3878 emit_jump (default_label);
3879#else
3880 emit_barrier ();
3881#endif
3882 }
3883
915f619f
JW
3884 before_case = squeeze_notes (NEXT_INSN (before_case), get_last_insn ());
3885 reorder_insns (before_case, get_last_insn (),
28d81abb
RK
3886 thiscase->data.case_stmt.start);
3887 }
3888 if (thiscase->exit_label)
3889 emit_label (thiscase->exit_label);
3890
3891 POPSTACK (case_stack);
3892
3893 free_temp_slots ();
3894}
3895
3896/* Generate code to jump to LABEL if OP1 and OP2 are equal. */
3897
3898static void
3899do_jump_if_equal (op1, op2, label, unsignedp)
3900 rtx op1, op2, label;
3901 int unsignedp;
3902{
3903 if (GET_CODE (op1) == CONST_INT
3904 && GET_CODE (op2) == CONST_INT)
3905 {
3906 if (INTVAL (op1) == INTVAL (op2))
3907 emit_jump (label);
3908 }
3909 else
3910 {
3911 enum machine_mode mode = GET_MODE (op1);
3912 if (mode == VOIDmode)
3913 mode = GET_MODE (op2);
37366632 3914 emit_cmp_insn (op1, op2, EQ, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
3915 emit_jump_insn (gen_beq (label));
3916 }
3917}
3918\f
3919/* Not all case values are encountered equally. This function
3920 uses a heuristic to weight case labels, in cases where that
3921 looks like a reasonable thing to do.
3922
3923 Right now, all we try to guess is text, and we establish the
3924 following weights:
3925
3926 chars above space: 16
3927 digits: 16
3928 default: 12
3929 space, punct: 8
3930 tab: 4
3931 newline: 2
3932 other "\" chars: 1
3933 remaining chars: 0
3934
3935 If we find any cases in the switch that are not either -1 or in the range
3936 of valid ASCII characters, or are control characters other than those
3937 commonly used with "\", don't treat this switch scanning text.
3938
3939 Return 1 if these nodes are suitable for cost estimation, otherwise
3940 return 0. */
3941
3942static int
3943estimate_case_costs (node)
3944 case_node_ptr node;
3945{
3946 tree min_ascii = build_int_2 (-1, -1);
3947 tree max_ascii = convert (TREE_TYPE (node->high), build_int_2 (127, 0));
3948 case_node_ptr n;
3949 int i;
3950
3951 /* If we haven't already made the cost table, make it now. Note that the
3952 lower bound of the table is -1, not zero. */
3953
3954 if (cost_table == NULL)
3955 {
3956 cost_table = ((short *) xmalloc (129 * sizeof (short))) + 1;
3957 bzero (cost_table - 1, 129 * sizeof (short));
3958
3959 for (i = 0; i < 128; i++)
3960 {
3961 if (isalnum (i))
3962 cost_table[i] = 16;
3963 else if (ispunct (i))
3964 cost_table[i] = 8;
3965 else if (iscntrl (i))
3966 cost_table[i] = -1;
3967 }
3968
3969 cost_table[' '] = 8;
3970 cost_table['\t'] = 4;
3971 cost_table['\0'] = 4;
3972 cost_table['\n'] = 2;
3973 cost_table['\f'] = 1;
3974 cost_table['\v'] = 1;
3975 cost_table['\b'] = 1;
3976 }
3977
3978 /* See if all the case expressions look like text. It is text if the
3979 constant is >= -1 and the highest constant is <= 127. Do all comparisons
3980 as signed arithmetic since we don't want to ever access cost_table with a
3981 value less than -1. Also check that none of the constants in a range
3982 are strange control characters. */
3983
3984 for (n = node; n; n = n->right)
3985 {
3986 if ((INT_CST_LT (n->low, min_ascii)) || INT_CST_LT (max_ascii, n->high))
3987 return 0;
3988
3989 for (i = TREE_INT_CST_LOW (n->low); i <= TREE_INT_CST_LOW (n->high); i++)
3990 if (cost_table[i] < 0)
3991 return 0;
3992 }
3993
3994 /* All interesting values are within the range of interesting
3995 ASCII characters. */
3996 return 1;
3997}
3998
3999/* Scan an ordered list of case nodes
4000 combining those with consecutive values or ranges.
4001
4002 Eg. three separate entries 1: 2: 3: become one entry 1..3: */
4003
4004static void
4005group_case_nodes (head)
4006 case_node_ptr head;
4007{
4008 case_node_ptr node = head;
4009
4010 while (node)
4011 {
4012 rtx lb = next_real_insn (label_rtx (node->code_label));
4013 case_node_ptr np = node;
4014
4015 /* Try to group the successors of NODE with NODE. */
4016 while (((np = np->right) != 0)
4017 /* Do they jump to the same place? */
4018 && next_real_insn (label_rtx (np->code_label)) == lb
4019 /* Are their ranges consecutive? */
4020 && tree_int_cst_equal (np->low,
4021 fold (build (PLUS_EXPR,
4022 TREE_TYPE (node->high),
4023 node->high,
4024 integer_one_node)))
4025 /* An overflow is not consecutive. */
4026 && tree_int_cst_lt (node->high,
4027 fold (build (PLUS_EXPR,
4028 TREE_TYPE (node->high),
4029 node->high,
4030 integer_one_node))))
4031 {
4032 node->high = np->high;
4033 }
4034 /* NP is the first node after NODE which can't be grouped with it.
4035 Delete the nodes in between, and move on to that node. */
4036 node->right = np;
4037 node = np;
4038 }
4039}
4040
4041/* Take an ordered list of case nodes
4042 and transform them into a near optimal binary tree,
6dc42e49 4043 on the assumption that any target code selection value is as
28d81abb
RK
4044 likely as any other.
4045
4046 The transformation is performed by splitting the ordered
4047 list into two equal sections plus a pivot. The parts are
4048 then attached to the pivot as left and right branches. Each
4049 branch is is then transformed recursively. */
4050
4051static void
4052balance_case_nodes (head, parent)
4053 case_node_ptr *head;
4054 case_node_ptr parent;
4055{
4056 register case_node_ptr np;
4057
4058 np = *head;
4059 if (np)
4060 {
4061 int cost = 0;
4062 int i = 0;
4063 int ranges = 0;
4064 register case_node_ptr *npp;
4065 case_node_ptr left;
4066
4067 /* Count the number of entries on branch. Also count the ranges. */
4068
4069 while (np)
4070 {
4071 if (!tree_int_cst_equal (np->low, np->high))
4072 {
4073 ranges++;
4074 if (use_cost_table)
4075 cost += cost_table[TREE_INT_CST_LOW (np->high)];
4076 }
4077
4078 if (use_cost_table)
4079 cost += cost_table[TREE_INT_CST_LOW (np->low)];
4080
4081 i++;
4082 np = np->right;
4083 }
4084
4085 if (i > 2)
4086 {
4087 /* Split this list if it is long enough for that to help. */
4088 npp = head;
4089 left = *npp;
4090 if (use_cost_table)
4091 {
4092 /* Find the place in the list that bisects the list's total cost,
4093 Here I gets half the total cost. */
4094 int n_moved = 0;
4095 i = (cost + 1) / 2;
4096 while (1)
4097 {
4098 /* Skip nodes while their cost does not reach that amount. */
4099 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
4100 i -= cost_table[TREE_INT_CST_LOW ((*npp)->high)];
4101 i -= cost_table[TREE_INT_CST_LOW ((*npp)->low)];
4102 if (i <= 0)
4103 break;
4104 npp = &(*npp)->right;
4105 n_moved += 1;
4106 }
4107 if (n_moved == 0)
4108 {
4109 /* Leave this branch lopsided, but optimize left-hand
4110 side and fill in `parent' fields for right-hand side. */
4111 np = *head;
4112 np->parent = parent;
4113 balance_case_nodes (&np->left, np);
4114 for (; np->right; np = np->right)
4115 np->right->parent = np;
4116 return;
4117 }
4118 }
4119 /* If there are just three nodes, split at the middle one. */
4120 else if (i == 3)
4121 npp = &(*npp)->right;
4122 else
4123 {
4124 /* Find the place in the list that bisects the list's total cost,
4125 where ranges count as 2.
4126 Here I gets half the total cost. */
4127 i = (i + ranges + 1) / 2;
4128 while (1)
4129 {
4130 /* Skip nodes while their cost does not reach that amount. */
4131 if (!tree_int_cst_equal ((*npp)->low, (*npp)->high))
4132 i--;
4133 i--;
4134 if (i <= 0)
4135 break;
4136 npp = &(*npp)->right;
4137 }
4138 }
4139 *head = np = *npp;
4140 *npp = 0;
4141 np->parent = parent;
4142 np->left = left;
4143
4144 /* Optimize each of the two split parts. */
4145 balance_case_nodes (&np->left, np);
4146 balance_case_nodes (&np->right, np);
4147 }
4148 else
4149 {
4150 /* Else leave this branch as one level,
4151 but fill in `parent' fields. */
4152 np = *head;
4153 np->parent = parent;
4154 for (; np->right; np = np->right)
4155 np->right->parent = np;
4156 }
4157 }
4158}
4159\f
4160/* Search the parent sections of the case node tree
4161 to see if a test for the lower bound of NODE would be redundant.
4162 INDEX_TYPE is the type of the index expression.
4163
4164 The instructions to generate the case decision tree are
4165 output in the same order as nodes are processed so it is
4166 known that if a parent node checks the range of the current
4167 node minus one that the current node is bounded at its lower
4168 span. Thus the test would be redundant. */
4169
4170static int
4171node_has_low_bound (node, index_type)
4172 case_node_ptr node;
4173 tree index_type;
4174{
4175 tree low_minus_one;
4176 case_node_ptr pnode;
4177
4178 /* If the lower bound of this node is the lowest value in the index type,
4179 we need not test it. */
4180
4181 if (tree_int_cst_equal (node->low, TYPE_MIN_VALUE (index_type)))
4182 return 1;
4183
4184 /* If this node has a left branch, the value at the left must be less
4185 than that at this node, so it cannot be bounded at the bottom and
4186 we need not bother testing any further. */
4187
4188 if (node->left)
4189 return 0;
4190
4191 low_minus_one = fold (build (MINUS_EXPR, TREE_TYPE (node->low),
4192 node->low, integer_one_node));
4193
4194 /* If the subtraction above overflowed, we can't verify anything.
4195 Otherwise, look for a parent that tests our value - 1. */
4196
4197 if (! tree_int_cst_lt (low_minus_one, node->low))
4198 return 0;
4199
4200 for (pnode = node->parent; pnode; pnode = pnode->parent)
4201 if (tree_int_cst_equal (low_minus_one, pnode->high))
4202 return 1;
4203
4204 return 0;
4205}
4206
4207/* Search the parent sections of the case node tree
4208 to see if a test for the upper bound of NODE would be redundant.
4209 INDEX_TYPE is the type of the index expression.
4210
4211 The instructions to generate the case decision tree are
4212 output in the same order as nodes are processed so it is
4213 known that if a parent node checks the range of the current
4214 node plus one that the current node is bounded at its upper
4215 span. Thus the test would be redundant. */
4216
4217static int
4218node_has_high_bound (node, index_type)
4219 case_node_ptr node;
4220 tree index_type;
4221{
4222 tree high_plus_one;
4223 case_node_ptr pnode;
4224
4225 /* If the upper bound of this node is the highest value in the type
4226 of the index expression, we need not test against it. */
4227
4228 if (tree_int_cst_equal (node->high, TYPE_MAX_VALUE (index_type)))
4229 return 1;
4230
4231 /* If this node has a right branch, the value at the right must be greater
4232 than that at this node, so it cannot be bounded at the top and
4233 we need not bother testing any further. */
4234
4235 if (node->right)
4236 return 0;
4237
4238 high_plus_one = fold (build (PLUS_EXPR, TREE_TYPE (node->high),
4239 node->high, integer_one_node));
4240
4241 /* If the addition above overflowed, we can't verify anything.
4242 Otherwise, look for a parent that tests our value + 1. */
4243
4244 if (! tree_int_cst_lt (node->high, high_plus_one))
4245 return 0;
4246
4247 for (pnode = node->parent; pnode; pnode = pnode->parent)
4248 if (tree_int_cst_equal (high_plus_one, pnode->low))
4249 return 1;
4250
4251 return 0;
4252}
4253
4254/* Search the parent sections of the
4255 case node tree to see if both tests for the upper and lower
4256 bounds of NODE would be redundant. */
4257
4258static int
4259node_is_bounded (node, index_type)
4260 case_node_ptr node;
4261 tree index_type;
4262{
4263 return (node_has_low_bound (node, index_type)
4264 && node_has_high_bound (node, index_type));
4265}
4266
4267/* Emit an unconditional jump to LABEL unless it would be dead code. */
4268
4269static void
4270emit_jump_if_reachable (label)
4271 rtx label;
4272{
4273 if (GET_CODE (get_last_insn ()) != BARRIER)
4274 emit_jump (label);
4275}
4276\f
4277/* Emit step-by-step code to select a case for the value of INDEX.
4278 The thus generated decision tree follows the form of the
4279 case-node binary tree NODE, whose nodes represent test conditions.
4280 INDEX_TYPE is the type of the index of the switch.
4281
4282 Care is taken to prune redundant tests from the decision tree
4283 by detecting any boundary conditions already checked by
4284 emitted rtx. (See node_has_high_bound, node_has_low_bound
4285 and node_is_bounded, above.)
4286
4287 Where the test conditions can be shown to be redundant we emit
4288 an unconditional jump to the target code. As a further
4289 optimization, the subordinates of a tree node are examined to
4290 check for bounded nodes. In this case conditional and/or
4291 unconditional jumps as a result of the boundary check for the
4292 current node are arranged to target the subordinates associated
4293 code for out of bound conditions on the current node node.
4294
f72aed24 4295 We can assume that when control reaches the code generated here,
28d81abb
RK
4296 the index value has already been compared with the parents
4297 of this node, and determined to be on the same side of each parent
4298 as this node is. Thus, if this node tests for the value 51,
4299 and a parent tested for 52, we don't need to consider
4300 the possibility of a value greater than 51. If another parent
4301 tests for the value 50, then this node need not test anything. */
4302
4303static void
4304emit_case_nodes (index, node, default_label, index_type)
4305 rtx index;
4306 case_node_ptr node;
4307 rtx default_label;
4308 tree index_type;
4309{
4310 /* If INDEX has an unsigned type, we must make unsigned branches. */
4311 int unsignedp = TREE_UNSIGNED (index_type);
4312 typedef rtx rtx_function ();
4313 rtx_function *gen_bgt_pat = unsignedp ? gen_bgtu : gen_bgt;
4314 rtx_function *gen_bge_pat = unsignedp ? gen_bgeu : gen_bge;
4315 rtx_function *gen_blt_pat = unsignedp ? gen_bltu : gen_blt;
4316 rtx_function *gen_ble_pat = unsignedp ? gen_bleu : gen_ble;
4317 enum machine_mode mode = GET_MODE (index);
4318
4319 /* See if our parents have already tested everything for us.
4320 If they have, emit an unconditional jump for this node. */
4321 if (node_is_bounded (node, index_type))
4322 emit_jump (label_rtx (node->code_label));
4323
4324 else if (tree_int_cst_equal (node->low, node->high))
4325 {
4326 /* Node is single valued. First see if the index expression matches
4327 this node and then check our children, if any. */
4328
37366632 4329 do_jump_if_equal (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
28d81abb
RK
4330 label_rtx (node->code_label), unsignedp);
4331
4332 if (node->right != 0 && node->left != 0)
4333 {
4334 /* This node has children on both sides.
4335 Dispatch to one side or the other
4336 by comparing the index value with this node's value.
4337 If one subtree is bounded, check that one first,
4338 so we can avoid real branches in the tree. */
4339
4340 if (node_is_bounded (node->right, index_type))
4341 {
37366632
RK
4342 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4343 VOIDmode, 0),
4344 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4345
4346 emit_jump_insn ((*gen_bgt_pat) (label_rtx (node->right->code_label)));
4347 emit_case_nodes (index, node->left, default_label, index_type);
4348 }
4349
4350 else if (node_is_bounded (node->left, index_type))
4351 {
37366632 4352 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
28d81abb 4353 VOIDmode, 0),
37366632 4354 LT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4355 emit_jump_insn ((*gen_blt_pat) (label_rtx (node->left->code_label)));
4356 emit_case_nodes (index, node->right, default_label, index_type);
4357 }
4358
4359 else
4360 {
4361 /* Neither node is bounded. First distinguish the two sides;
4362 then emit the code for one side at a time. */
4363
4364 tree test_label
4365 = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4366
4367 /* See if the value is on the right. */
37366632 4368 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
28d81abb 4369 VOIDmode, 0),
37366632 4370 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4371 emit_jump_insn ((*gen_bgt_pat) (label_rtx (test_label)));
4372
4373 /* Value must be on the left.
4374 Handle the left-hand subtree. */
4375 emit_case_nodes (index, node->left, default_label, index_type);
4376 /* If left-hand subtree does nothing,
4377 go to default. */
4378 emit_jump_if_reachable (default_label);
4379
4380 /* Code branches here for the right-hand subtree. */
4381 expand_label (test_label);
4382 emit_case_nodes (index, node->right, default_label, index_type);
4383 }
4384 }
4385
4386 else if (node->right != 0 && node->left == 0)
4387 {
4388 /* Here we have a right child but no left so we issue conditional
4389 branch to default and process the right child.
4390
4391 Omit the conditional branch to default if we it avoid only one
4392 right child; it costs too much space to save so little time. */
4393
de14fd73 4394 if (node->right->right || node->right->left
28d81abb
RK
4395 || !tree_int_cst_equal (node->right->low, node->right->high))
4396 {
4397 if (!node_has_low_bound (node, index_type))
4398 {
37366632
RK
4399 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4400 VOIDmode, 0),
4401 LT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4402 emit_jump_insn ((*gen_blt_pat) (default_label));
4403 }
4404
4405 emit_case_nodes (index, node->right, default_label, index_type);
4406 }
4407 else
4408 /* We cannot process node->right normally
4409 since we haven't ruled out the numbers less than
4410 this node's value. So handle node->right explicitly. */
4411 do_jump_if_equal (index,
37366632
RK
4412 expand_expr (node->right->low, NULL_RTX,
4413 VOIDmode, 0),
28d81abb
RK
4414 label_rtx (node->right->code_label), unsignedp);
4415 }
4416
4417 else if (node->right == 0 && node->left != 0)
4418 {
4419 /* Just one subtree, on the left. */
4420
de14fd73
RK
4421#if 0 /* The following code and comment were formerly part
4422 of the condition here, but they didn't work
4423 and I don't understand what the idea was. -- rms. */
4424 /* If our "most probable entry" is less probable
28d81abb
RK
4425 than the default label, emit a jump to
4426 the default label using condition codes
4427 already lying around. With no right branch,
4428 a branch-greater-than will get us to the default
4429 label correctly. */
de14fd73
RK
4430 if (use_cost_table
4431 && cost_table[TREE_INT_CST_LOW (node->high)] < 12)
4432 ;
4433#endif /* 0 */
4434 if (node->left->left || node->left->right
28d81abb
RK
4435 || !tree_int_cst_equal (node->left->low, node->left->high))
4436 {
4437 if (!node_has_high_bound (node, index_type))
4438 {
37366632
RK
4439 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4440 VOIDmode, 0),
4441 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4442 emit_jump_insn ((*gen_bgt_pat) (default_label));
4443 }
4444
4445 emit_case_nodes (index, node->left, default_label, index_type);
4446 }
4447 else
4448 /* We cannot process node->left normally
4449 since we haven't ruled out the numbers less than
4450 this node's value. So handle node->left explicitly. */
4451 do_jump_if_equal (index,
37366632
RK
4452 expand_expr (node->left->low, NULL_RTX,
4453 VOIDmode, 0),
28d81abb
RK
4454 label_rtx (node->left->code_label), unsignedp);
4455 }
4456 }
4457 else
4458 {
4459 /* Node is a range. These cases are very similar to those for a single
4460 value, except that we do not start by testing whether this node
4461 is the one to branch to. */
4462
4463 if (node->right != 0 && node->left != 0)
4464 {
4465 /* Node has subtrees on both sides.
4466 If the right-hand subtree is bounded,
4467 test for it first, since we can go straight there.
4468 Otherwise, we need to make a branch in the control structure,
4469 then handle the two subtrees. */
4470 tree test_label = 0;
4471
37366632
RK
4472 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4473 VOIDmode, 0),
4474 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4475
4476 if (node_is_bounded (node->right, index_type))
4477 /* Right hand node is fully bounded so we can eliminate any
4478 testing and branch directly to the target code. */
4479 emit_jump_insn ((*gen_bgt_pat) (label_rtx (node->right->code_label)));
4480 else
4481 {
4482 /* Right hand node requires testing.
4483 Branch to a label where we will handle it later. */
4484
4485 test_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4486 emit_jump_insn ((*gen_bgt_pat) (label_rtx (test_label)));
4487 }
4488
4489 /* Value belongs to this node or to the left-hand subtree. */
4490
37366632
RK
4491 emit_cmp_insn (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
4492 GE, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4493 emit_jump_insn ((*gen_bge_pat) (label_rtx (node->code_label)));
4494
4495 /* Handle the left-hand subtree. */
4496 emit_case_nodes (index, node->left, default_label, index_type);
4497
4498 /* If right node had to be handled later, do that now. */
4499
4500 if (test_label)
4501 {
4502 /* If the left-hand subtree fell through,
4503 don't let it fall into the right-hand subtree. */
4504 emit_jump_if_reachable (default_label);
4505
4506 expand_label (test_label);
4507 emit_case_nodes (index, node->right, default_label, index_type);
4508 }
4509 }
4510
4511 else if (node->right != 0 && node->left == 0)
4512 {
4513 /* Deal with values to the left of this node,
4514 if they are possible. */
4515 if (!node_has_low_bound (node, index_type))
4516 {
37366632
RK
4517 emit_cmp_insn (index, expand_expr (node->low, NULL_RTX,
4518 VOIDmode, 0),
4519 LT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4520 emit_jump_insn ((*gen_blt_pat) (default_label));
4521 }
4522
4523 /* Value belongs to this node or to the right-hand subtree. */
4524
37366632
RK
4525 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4526 VOIDmode, 0),
4527 LE, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4528 emit_jump_insn ((*gen_ble_pat) (label_rtx (node->code_label)));
4529
4530 emit_case_nodes (index, node->right, default_label, index_type);
4531 }
4532
4533 else if (node->right == 0 && node->left != 0)
4534 {
4535 /* Deal with values to the right of this node,
4536 if they are possible. */
4537 if (!node_has_high_bound (node, index_type))
4538 {
37366632
RK
4539 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4540 VOIDmode, 0),
4541 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4542 emit_jump_insn ((*gen_bgt_pat) (default_label));
4543 }
4544
4545 /* Value belongs to this node or to the left-hand subtree. */
4546
37366632
RK
4547 emit_cmp_insn (index, expand_expr (node->low, NULL_RTX, VOIDmode, 0),
4548 GE, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4549 emit_jump_insn ((*gen_bge_pat) (label_rtx (node->code_label)));
4550
4551 emit_case_nodes (index, node->left, default_label, index_type);
4552 }
4553
4554 else
4555 {
4556 /* Node has no children so we check low and high bounds to remove
4557 redundant tests. Only one of the bounds can exist,
4558 since otherwise this node is bounded--a case tested already. */
4559
4560 if (!node_has_high_bound (node, index_type))
4561 {
37366632
RK
4562 emit_cmp_insn (index, expand_expr (node->high, NULL_RTX,
4563 VOIDmode, 0),
4564 GT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4565 emit_jump_insn ((*gen_bgt_pat) (default_label));
4566 }
4567
4568 if (!node_has_low_bound (node, index_type))
4569 {
37366632
RK
4570 emit_cmp_insn (index, expand_expr (node->low, NULL_RTX,
4571 VOIDmode, 0),
4572 LT, NULL_RTX, mode, unsignedp, 0);
28d81abb
RK
4573 emit_jump_insn ((*gen_blt_pat) (default_label));
4574 }
4575
4576 emit_jump (label_rtx (node->code_label));
4577 }
4578 }
4579}
4580\f
4581/* These routines are used by the loop unrolling code. They copy BLOCK trees
4582 so that the debugging info will be correct for the unrolled loop. */
4583
94dc8b56 4584/* Indexed by block number, contains a pointer to the N'th block node. */
28d81abb 4585
94dc8b56 4586static tree *block_vector;
28d81abb
RK
4587
4588void
94dc8b56 4589find_loop_tree_blocks ()
28d81abb 4590{
94dc8b56 4591 tree block = DECL_INITIAL (current_function_decl);
28d81abb 4592
94dc8b56
JW
4593 /* There first block is for the function body, and does not have
4594 corresponding block notes. Don't include it in the block vector. */
4595 block = BLOCK_SUBBLOCKS (block);
28d81abb 4596
94dc8b56 4597 block_vector = identify_blocks (block, get_insns ());
28d81abb
RK
4598}
4599
28d81abb 4600void
94dc8b56 4601unroll_block_trees ()
28d81abb 4602{
94dc8b56 4603 tree block = DECL_INITIAL (current_function_decl);
28d81abb 4604
94dc8b56 4605 reorder_blocks (block_vector, block, get_insns ());
28d81abb 4606}
94dc8b56 4607
This page took 0.514947 seconds and 5 git commands to generate.