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