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