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