(TARGET_FILE_SWITCHING): Define.
[official-gcc.git] / gcc / function.c
blob750f33396dbbc545a1093c78aaf167a242813c44
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2 Copyright (C) 1987, 88, 89, 91-96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This file handles the generation of rtl code from tree structure
23 at the level of the function as a whole.
24 It creates the rtl expressions for parameters and auto variables
25 and has full responsibility for allocating stack slots.
27 `expand_function_start' is called at the beginning of a function,
28 before the function body is parsed, and `expand_function_end' is
29 called after parsing the body.
31 Call `assign_stack_local' to allocate a stack slot for a local variable.
32 This is usually done during the RTL generation for the function body,
33 but it can also be done in the reload pass when a pseudo-register does
34 not get a hard register.
36 Call `put_var_into_stack' when you learn, belatedly, that a variable
37 previously given a pseudo-register must in fact go in the stack.
38 This function changes the DECL_RTL to be a stack slot instead of a reg
39 then scans all the RTL instructions so far generated to correct them. */
41 #include "config.h"
42 #include <stdio.h>
43 #include "rtl.h"
44 #include "tree.h"
45 #include "flags.h"
46 #include "except.h"
47 #include "function.h"
48 #include "insn-flags.h"
49 #include "expr.h"
50 #include "insn-codes.h"
51 #include "regs.h"
52 #include "hard-reg-set.h"
53 #include "insn-config.h"
54 #include "recog.h"
55 #include "output.h"
56 #include "basic-block.h"
57 #include "obstack.h"
58 #include "bytecode.h"
59 #include "bc-emit.h"
61 #ifndef TRAMPOLINE_ALIGNMENT
62 #define TRAMPOLINE_ALIGNMENT FUNCTION_BOUNDARY
63 #endif
65 /* Some systems use __main in a way incompatible with its use in gcc, in these
66 cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
67 give the same symbol without quotes for an alternative entry point. You
68 must define both, or neither. */
69 #ifndef NAME__MAIN
70 #define NAME__MAIN "__main"
71 #define SYMBOL__MAIN __main
72 #endif
74 /* Round a value to the lowest integer less than it that is a multiple of
75 the required alignment. Avoid using division in case the value is
76 negative. Assume the alignment is a power of two. */
77 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
79 /* Similar, but round to the next highest integer that meets the
80 alignment. */
81 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
83 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
84 during rtl generation. If they are different register numbers, this is
85 always true. It may also be true if
86 FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
87 generation. See fix_lexical_addr for details. */
89 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
90 #define NEED_SEPARATE_AP
91 #endif
93 /* Number of bytes of args popped by function being compiled on its return.
94 Zero if no bytes are to be popped.
95 May affect compilation of return insn or of function epilogue. */
97 int current_function_pops_args;
99 /* Nonzero if function being compiled needs to be given an address
100 where the value should be stored. */
102 int current_function_returns_struct;
104 /* Nonzero if function being compiled needs to
105 return the address of where it has put a structure value. */
107 int current_function_returns_pcc_struct;
109 /* Nonzero if function being compiled needs to be passed a static chain. */
111 int current_function_needs_context;
113 /* Nonzero if function being compiled can call setjmp. */
115 int current_function_calls_setjmp;
117 /* Nonzero if function being compiled can call longjmp. */
119 int current_function_calls_longjmp;
121 /* Nonzero if function being compiled receives nonlocal gotos
122 from nested functions. */
124 int current_function_has_nonlocal_label;
126 /* Nonzero if function being compiled has nonlocal gotos to parent
127 function. */
129 int current_function_has_nonlocal_goto;
131 /* Nonzero if function being compiled contains nested functions. */
133 int current_function_contains_functions;
135 /* Nonzero if the current function is a thunk (a lightweight function that
136 just adjusts one of its arguments and forwards to another function), so
137 we should try to cut corners where we can. */
138 int current_function_is_thunk;
140 /* Nonzero if function being compiled can call alloca,
141 either as a subroutine or builtin. */
143 int current_function_calls_alloca;
145 /* Nonzero if the current function returns a pointer type */
147 int current_function_returns_pointer;
149 /* If some insns can be deferred to the delay slots of the epilogue, the
150 delay list for them is recorded here. */
152 rtx current_function_epilogue_delay_list;
154 /* If function's args have a fixed size, this is that size, in bytes.
155 Otherwise, it is -1.
156 May affect compilation of return insn or of function epilogue. */
158 int current_function_args_size;
160 /* # bytes the prologue should push and pretend that the caller pushed them.
161 The prologue must do this, but only if parms can be passed in registers. */
163 int current_function_pretend_args_size;
165 /* # of bytes of outgoing arguments. If ACCUMULATE_OUTGOING_ARGS is
166 defined, the needed space is pushed by the prologue. */
168 int current_function_outgoing_args_size;
170 /* This is the offset from the arg pointer to the place where the first
171 anonymous arg can be found, if there is one. */
173 rtx current_function_arg_offset_rtx;
175 /* Nonzero if current function uses varargs.h or equivalent.
176 Zero for functions that use stdarg.h. */
178 int current_function_varargs;
180 /* Nonzero if current function uses stdarg.h or equivalent.
181 Zero for functions that use varargs.h. */
183 int current_function_stdarg;
185 /* Quantities of various kinds of registers
186 used for the current function's args. */
188 CUMULATIVE_ARGS current_function_args_info;
190 /* Name of function now being compiled. */
192 char *current_function_name;
194 /* If non-zero, an RTL expression for that location at which the current
195 function returns its result. Always equal to
196 DECL_RTL (DECL_RESULT (current_function_decl)), but provided
197 independently of the tree structures. */
199 rtx current_function_return_rtx;
201 /* Nonzero if the current function uses the constant pool. */
203 int current_function_uses_const_pool;
205 /* Nonzero if the current function uses pic_offset_table_rtx. */
206 int current_function_uses_pic_offset_table;
208 /* The arg pointer hard register, or the pseudo into which it was copied. */
209 rtx current_function_internal_arg_pointer;
211 /* The FUNCTION_DECL for an inline function currently being expanded. */
212 tree inline_function_decl;
214 /* Number of function calls seen so far in current function. */
216 int function_call_count;
218 /* List (chain of TREE_LIST) of LABEL_DECLs for all nonlocal labels
219 (labels to which there can be nonlocal gotos from nested functions)
220 in this function. */
222 tree nonlocal_labels;
224 /* RTX for stack slot that holds the current handler for nonlocal gotos.
225 Zero when function does not have nonlocal labels. */
227 rtx nonlocal_goto_handler_slot;
229 /* RTX for stack slot that holds the stack pointer value to restore
230 for a nonlocal goto.
231 Zero when function does not have nonlocal labels. */
233 rtx nonlocal_goto_stack_level;
235 /* Label that will go on parm cleanup code, if any.
236 Jumping to this label runs cleanup code for parameters, if
237 such code must be run. Following this code is the logical return label. */
239 rtx cleanup_label;
241 /* Label that will go on function epilogue.
242 Jumping to this label serves as a "return" instruction
243 on machines which require execution of the epilogue on all returns. */
245 rtx return_label;
247 /* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
248 So we can mark them all live at the end of the function, if nonopt. */
249 rtx save_expr_regs;
251 /* List (chain of EXPR_LISTs) of all stack slots in this function.
252 Made for the sake of unshare_all_rtl. */
253 rtx stack_slot_list;
255 /* Chain of all RTL_EXPRs that have insns in them. */
256 tree rtl_expr_chain;
258 /* Label to jump back to for tail recursion, or 0 if we have
259 not yet needed one for this function. */
260 rtx tail_recursion_label;
262 /* Place after which to insert the tail_recursion_label if we need one. */
263 rtx tail_recursion_reentry;
265 /* Location at which to save the argument pointer if it will need to be
266 referenced. There are two cases where this is done: if nonlocal gotos
267 exist, or if vars stored at an offset from the argument pointer will be
268 needed by inner routines. */
270 rtx arg_pointer_save_area;
272 /* Offset to end of allocated area of stack frame.
273 If stack grows down, this is the address of the last stack slot allocated.
274 If stack grows up, this is the address for the next slot. */
275 HOST_WIDE_INT frame_offset;
277 /* List (chain of TREE_LISTs) of static chains for containing functions.
278 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
279 in an RTL_EXPR in the TREE_VALUE. */
280 static tree context_display;
282 /* List (chain of TREE_LISTs) of trampolines for nested functions.
283 The trampoline sets up the static chain and jumps to the function.
284 We supply the trampoline's address when the function's address is requested.
286 Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
287 in an RTL_EXPR in the TREE_VALUE. */
288 static tree trampoline_list;
290 /* Insn after which register parms and SAVE_EXPRs are born, if nonopt. */
291 static rtx parm_birth_insn;
293 #if 0
294 /* Nonzero if a stack slot has been generated whose address is not
295 actually valid. It means that the generated rtl must all be scanned
296 to detect and correct the invalid addresses where they occur. */
297 static int invalid_stack_slot;
298 #endif
300 /* Last insn of those whose job was to put parms into their nominal homes. */
301 static rtx last_parm_insn;
303 /* 1 + last pseudo register number used for loading a copy
304 of a parameter of this function. */
305 static int max_parm_reg;
307 /* Vector indexed by REGNO, containing location on stack in which
308 to put the parm which is nominally in pseudo register REGNO,
309 if we discover that that parm must go in the stack. */
310 static rtx *parm_reg_stack_loc;
312 /* Nonzero once virtual register instantiation has been done.
313 assign_stack_local uses frame_pointer_rtx when this is nonzero. */
314 static int virtuals_instantiated;
316 /* These variables hold pointers to functions to
317 save and restore machine-specific data,
318 in push_function_context and pop_function_context. */
319 void (*save_machine_status) PROTO((struct function *));
320 void (*restore_machine_status) PROTO((struct function *));
322 /* Nonzero if we need to distinguish between the return value of this function
323 and the return value of a function called by this function. This helps
324 integrate.c */
326 extern int rtx_equal_function_value_matters;
327 extern tree sequence_rtl_expr;
329 /* In order to evaluate some expressions, such as function calls returning
330 structures in memory, we need to temporarily allocate stack locations.
331 We record each allocated temporary in the following structure.
333 Associated with each temporary slot is a nesting level. When we pop up
334 one level, all temporaries associated with the previous level are freed.
335 Normally, all temporaries are freed after the execution of the statement
336 in which they were created. However, if we are inside a ({...}) grouping,
337 the result may be in a temporary and hence must be preserved. If the
338 result could be in a temporary, we preserve it if we can determine which
339 one it is in. If we cannot determine which temporary may contain the
340 result, all temporaries are preserved. A temporary is preserved by
341 pretending it was allocated at the previous nesting level.
343 Automatic variables are also assigned temporary slots, at the nesting
344 level where they are defined. They are marked a "kept" so that
345 free_temp_slots will not free them. */
347 struct temp_slot
349 /* Points to next temporary slot. */
350 struct temp_slot *next;
351 /* The rtx to used to reference the slot. */
352 rtx slot;
353 /* The rtx used to represent the address if not the address of the
354 slot above. May be an EXPR_LIST if multiple addresses exist. */
355 rtx address;
356 /* The size, in units, of the slot. */
357 int size;
358 /* The value of `sequence_rtl_expr' when this temporary is allocated. */
359 tree rtl_expr;
360 /* Non-zero if this temporary is currently in use. */
361 char in_use;
362 /* Non-zero if this temporary has its address taken. */
363 char addr_taken;
364 /* Nesting level at which this slot is being used. */
365 int level;
366 /* Non-zero if this should survive a call to free_temp_slots. */
367 int keep;
368 /* The offset of the slot from the frame_pointer, including extra space
369 for alignment. This info is for combine_temp_slots. */
370 int base_offset;
371 /* The size of the slot, including extra space for alignment. This
372 info is for combine_temp_slots. */
373 int full_size;
376 /* List of all temporaries allocated, both available and in use. */
378 struct temp_slot *temp_slots;
380 /* Current nesting level for temporaries. */
382 int temp_slot_level;
384 /* The FUNCTION_DECL node for the current function. */
385 static tree this_function_decl;
387 /* Callinfo pointer for the current function. */
388 static rtx this_function_callinfo;
390 /* The label in the bytecode file of this function's actual bytecode.
391 Not an rtx. */
392 static char *this_function_bytecode;
394 /* The call description vector for the current function. */
395 static rtx this_function_calldesc;
397 /* Size of the local variables allocated for the current function. */
398 int local_vars_size;
400 /* Current depth of the bytecode evaluation stack. */
401 int stack_depth;
403 /* Maximum depth of the evaluation stack in this function. */
404 int max_stack_depth;
406 /* Current depth in statement expressions. */
407 static int stmt_expr_depth;
409 /* This structure is used to record MEMs or pseudos used to replace VAR, any
410 SUBREGs of VAR, and any MEMs containing VAR as an address. We need to
411 maintain this list in case two operands of an insn were required to match;
412 in that case we must ensure we use the same replacement. */
414 struct fixup_replacement
416 rtx old;
417 rtx new;
418 struct fixup_replacement *next;
421 /* Forward declarations. */
423 static struct temp_slot *find_temp_slot_from_address PROTO((rtx));
424 static void put_reg_into_stack PROTO((struct function *, rtx, tree,
425 enum machine_mode, enum machine_mode,
426 int));
427 static void fixup_var_refs PROTO((rtx, enum machine_mode, int));
428 static struct fixup_replacement
429 *find_fixup_replacement PROTO((struct fixup_replacement **, rtx));
430 static void fixup_var_refs_insns PROTO((rtx, enum machine_mode, int,
431 rtx, int));
432 static void fixup_var_refs_1 PROTO((rtx, enum machine_mode, rtx *, rtx,
433 struct fixup_replacement **));
434 static rtx fixup_memory_subreg PROTO((rtx, rtx, int));
435 static rtx walk_fixup_memory_subreg PROTO((rtx, rtx, int));
436 static rtx fixup_stack_1 PROTO((rtx, rtx));
437 static void optimize_bit_field PROTO((rtx, rtx, rtx *));
438 static void instantiate_decls PROTO((tree, int));
439 static void instantiate_decls_1 PROTO((tree, int));
440 static void instantiate_decl PROTO((rtx, int, int));
441 static int instantiate_virtual_regs_1 PROTO((rtx *, rtx, int));
442 static void delete_handlers PROTO((void));
443 static void pad_to_arg_alignment PROTO((struct args_size *, int));
444 static void pad_below PROTO((struct args_size *, enum machine_mode,
445 tree));
446 static tree round_down PROTO((tree, int));
447 static rtx round_trampoline_addr PROTO((rtx));
448 static tree blocks_nreverse PROTO((tree));
449 static int all_blocks PROTO((tree, tree *));
450 static int *record_insns PROTO((rtx));
451 static int contains PROTO((rtx, int *));
453 /* Pointer to chain of `struct function' for containing functions. */
454 struct function *outer_function_chain;
456 /* Given a function decl for a containing function,
457 return the `struct function' for it. */
459 struct function *
460 find_function_data (decl)
461 tree decl;
463 struct function *p;
464 for (p = outer_function_chain; p; p = p->next)
465 if (p->decl == decl)
466 return p;
467 abort ();
470 /* Save the current context for compilation of a nested function.
471 This is called from language-specific code.
472 The caller is responsible for saving any language-specific status,
473 since this function knows only about language-independent variables. */
475 void
476 push_function_context_to (context)
477 tree context;
479 struct function *p = (struct function *) xmalloc (sizeof (struct function));
481 p->next = outer_function_chain;
482 outer_function_chain = p;
484 p->name = current_function_name;
485 p->decl = current_function_decl;
486 p->pops_args = current_function_pops_args;
487 p->returns_struct = current_function_returns_struct;
488 p->returns_pcc_struct = current_function_returns_pcc_struct;
489 p->returns_pointer = current_function_returns_pointer;
490 p->needs_context = current_function_needs_context;
491 p->calls_setjmp = current_function_calls_setjmp;
492 p->calls_longjmp = current_function_calls_longjmp;
493 p->calls_alloca = current_function_calls_alloca;
494 p->has_nonlocal_label = current_function_has_nonlocal_label;
495 p->has_nonlocal_goto = current_function_has_nonlocal_goto;
496 p->contains_functions = current_function_contains_functions;
497 p->is_thunk = current_function_is_thunk;
498 p->args_size = current_function_args_size;
499 p->pretend_args_size = current_function_pretend_args_size;
500 p->arg_offset_rtx = current_function_arg_offset_rtx;
501 p->varargs = current_function_varargs;
502 p->stdarg = current_function_stdarg;
503 p->uses_const_pool = current_function_uses_const_pool;
504 p->uses_pic_offset_table = current_function_uses_pic_offset_table;
505 p->internal_arg_pointer = current_function_internal_arg_pointer;
506 p->max_parm_reg = max_parm_reg;
507 p->parm_reg_stack_loc = parm_reg_stack_loc;
508 p->outgoing_args_size = current_function_outgoing_args_size;
509 p->return_rtx = current_function_return_rtx;
510 p->nonlocal_goto_handler_slot = nonlocal_goto_handler_slot;
511 p->nonlocal_goto_stack_level = nonlocal_goto_stack_level;
512 p->nonlocal_labels = nonlocal_labels;
513 p->cleanup_label = cleanup_label;
514 p->return_label = return_label;
515 p->save_expr_regs = save_expr_regs;
516 p->stack_slot_list = stack_slot_list;
517 p->parm_birth_insn = parm_birth_insn;
518 p->frame_offset = frame_offset;
519 p->tail_recursion_label = tail_recursion_label;
520 p->tail_recursion_reentry = tail_recursion_reentry;
521 p->arg_pointer_save_area = arg_pointer_save_area;
522 p->rtl_expr_chain = rtl_expr_chain;
523 p->last_parm_insn = last_parm_insn;
524 p->context_display = context_display;
525 p->trampoline_list = trampoline_list;
526 p->function_call_count = function_call_count;
527 p->temp_slots = temp_slots;
528 p->temp_slot_level = temp_slot_level;
529 p->fixup_var_refs_queue = 0;
530 p->epilogue_delay_list = current_function_epilogue_delay_list;
531 p->args_info = current_function_args_info;
533 save_tree_status (p, context);
534 save_storage_status (p);
535 save_emit_status (p);
536 init_emit ();
537 save_expr_status (p);
538 save_stmt_status (p);
539 save_varasm_status (p);
541 if (save_machine_status)
542 (*save_machine_status) (p);
545 void
546 push_function_context ()
548 push_function_context_to (current_function_decl);
551 /* Restore the last saved context, at the end of a nested function.
552 This function is called from language-specific code. */
554 void
555 pop_function_context_from (context)
556 tree context;
558 struct function *p = outer_function_chain;
560 outer_function_chain = p->next;
562 current_function_contains_functions
563 = p->contains_functions || p->inline_obstacks
564 || context == current_function_decl;
565 current_function_name = p->name;
566 current_function_decl = p->decl;
567 current_function_pops_args = p->pops_args;
568 current_function_returns_struct = p->returns_struct;
569 current_function_returns_pcc_struct = p->returns_pcc_struct;
570 current_function_returns_pointer = p->returns_pointer;
571 current_function_needs_context = p->needs_context;
572 current_function_calls_setjmp = p->calls_setjmp;
573 current_function_calls_longjmp = p->calls_longjmp;
574 current_function_calls_alloca = p->calls_alloca;
575 current_function_has_nonlocal_label = p->has_nonlocal_label;
576 current_function_has_nonlocal_goto = p->has_nonlocal_goto;
577 current_function_is_thunk = p->is_thunk;
578 current_function_args_size = p->args_size;
579 current_function_pretend_args_size = p->pretend_args_size;
580 current_function_arg_offset_rtx = p->arg_offset_rtx;
581 current_function_varargs = p->varargs;
582 current_function_stdarg = p->stdarg;
583 current_function_uses_const_pool = p->uses_const_pool;
584 current_function_uses_pic_offset_table = p->uses_pic_offset_table;
585 current_function_internal_arg_pointer = p->internal_arg_pointer;
586 max_parm_reg = p->max_parm_reg;
587 parm_reg_stack_loc = p->parm_reg_stack_loc;
588 current_function_outgoing_args_size = p->outgoing_args_size;
589 current_function_return_rtx = p->return_rtx;
590 nonlocal_goto_handler_slot = p->nonlocal_goto_handler_slot;
591 nonlocal_goto_stack_level = p->nonlocal_goto_stack_level;
592 nonlocal_labels = p->nonlocal_labels;
593 cleanup_label = p->cleanup_label;
594 return_label = p->return_label;
595 save_expr_regs = p->save_expr_regs;
596 stack_slot_list = p->stack_slot_list;
597 parm_birth_insn = p->parm_birth_insn;
598 frame_offset = p->frame_offset;
599 tail_recursion_label = p->tail_recursion_label;
600 tail_recursion_reentry = p->tail_recursion_reentry;
601 arg_pointer_save_area = p->arg_pointer_save_area;
602 rtl_expr_chain = p->rtl_expr_chain;
603 last_parm_insn = p->last_parm_insn;
604 context_display = p->context_display;
605 trampoline_list = p->trampoline_list;
606 function_call_count = p->function_call_count;
607 temp_slots = p->temp_slots;
608 temp_slot_level = p->temp_slot_level;
609 current_function_epilogue_delay_list = p->epilogue_delay_list;
610 reg_renumber = 0;
611 current_function_args_info = p->args_info;
613 restore_tree_status (p);
614 restore_storage_status (p);
615 restore_expr_status (p);
616 restore_emit_status (p);
617 restore_stmt_status (p);
618 restore_varasm_status (p);
620 if (restore_machine_status)
621 (*restore_machine_status) (p);
623 /* Finish doing put_var_into_stack for any of our variables
624 which became addressable during the nested function. */
626 struct var_refs_queue *queue = p->fixup_var_refs_queue;
627 for (; queue; queue = queue->next)
628 fixup_var_refs (queue->modified, queue->promoted_mode, queue->unsignedp);
631 free (p);
633 /* Reset variables that have known state during rtx generation. */
634 rtx_equal_function_value_matters = 1;
635 virtuals_instantiated = 0;
638 void pop_function_context ()
640 pop_function_context_from (current_function_decl);
643 /* Allocate fixed slots in the stack frame of the current function. */
645 /* Return size needed for stack frame based on slots so far allocated.
646 This size counts from zero. It is not rounded to STACK_BOUNDARY;
647 the caller may have to do that. */
649 HOST_WIDE_INT
650 get_frame_size ()
652 #ifdef FRAME_GROWS_DOWNWARD
653 return -frame_offset;
654 #else
655 return frame_offset;
656 #endif
659 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
660 with machine mode MODE.
662 ALIGN controls the amount of alignment for the address of the slot:
663 0 means according to MODE,
664 -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
665 positive specifies alignment boundary in bits.
667 We do not round to stack_boundary here. */
670 assign_stack_local (mode, size, align)
671 enum machine_mode mode;
672 int size;
673 int align;
675 register rtx x, addr;
676 int bigend_correction = 0;
677 int alignment;
679 if (align == 0)
681 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
682 if (mode == BLKmode)
683 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
685 else if (align == -1)
687 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
688 size = CEIL_ROUND (size, alignment);
690 else
691 alignment = align / BITS_PER_UNIT;
693 /* Round frame offset to that alignment.
694 We must be careful here, since FRAME_OFFSET might be negative and
695 division with a negative dividend isn't as well defined as we might
696 like. So we instead assume that ALIGNMENT is a power of two and
697 use logical operations which are unambiguous. */
698 #ifdef FRAME_GROWS_DOWNWARD
699 frame_offset = FLOOR_ROUND (frame_offset, alignment);
700 #else
701 frame_offset = CEIL_ROUND (frame_offset, alignment);
702 #endif
704 /* On a big-endian machine, if we are allocating more space than we will use,
705 use the least significant bytes of those that are allocated. */
706 if (BYTES_BIG_ENDIAN && mode != BLKmode)
707 bigend_correction = size - GET_MODE_SIZE (mode);
709 #ifdef FRAME_GROWS_DOWNWARD
710 frame_offset -= size;
711 #endif
713 /* If we have already instantiated virtual registers, return the actual
714 address relative to the frame pointer. */
715 if (virtuals_instantiated)
716 addr = plus_constant (frame_pointer_rtx,
717 (frame_offset + bigend_correction
718 + STARTING_FRAME_OFFSET));
719 else
720 addr = plus_constant (virtual_stack_vars_rtx,
721 frame_offset + bigend_correction);
723 #ifndef FRAME_GROWS_DOWNWARD
724 frame_offset += size;
725 #endif
727 x = gen_rtx (MEM, mode, addr);
729 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, x, stack_slot_list);
731 return x;
734 /* Assign a stack slot in a containing function.
735 First three arguments are same as in preceding function.
736 The last argument specifies the function to allocate in. */
739 assign_outer_stack_local (mode, size, align, function)
740 enum machine_mode mode;
741 int size;
742 int align;
743 struct function *function;
745 register rtx x, addr;
746 int bigend_correction = 0;
747 int alignment;
749 /* Allocate in the memory associated with the function in whose frame
750 we are assigning. */
751 push_obstacks (function->function_obstack,
752 function->function_maybepermanent_obstack);
754 if (align == 0)
756 alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
757 if (mode == BLKmode)
758 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
760 else if (align == -1)
762 alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
763 size = CEIL_ROUND (size, alignment);
765 else
766 alignment = align / BITS_PER_UNIT;
768 /* Round frame offset to that alignment. */
769 #ifdef FRAME_GROWS_DOWNWARD
770 function->frame_offset = FLOOR_ROUND (function->frame_offset, alignment);
771 #else
772 function->frame_offset = CEIL_ROUND (function->frame_offset, alignment);
773 #endif
775 /* On a big-endian machine, if we are allocating more space than we will use,
776 use the least significant bytes of those that are allocated. */
777 if (BYTES_BIG_ENDIAN && mode != BLKmode)
778 bigend_correction = size - GET_MODE_SIZE (mode);
780 #ifdef FRAME_GROWS_DOWNWARD
781 function->frame_offset -= size;
782 #endif
783 addr = plus_constant (virtual_stack_vars_rtx,
784 function->frame_offset + bigend_correction);
785 #ifndef FRAME_GROWS_DOWNWARD
786 function->frame_offset += size;
787 #endif
789 x = gen_rtx (MEM, mode, addr);
791 function->stack_slot_list
792 = gen_rtx (EXPR_LIST, VOIDmode, x, function->stack_slot_list);
794 pop_obstacks ();
796 return x;
799 /* Allocate a temporary stack slot and record it for possible later
800 reuse.
802 MODE is the machine mode to be given to the returned rtx.
804 SIZE is the size in units of the space required. We do no rounding here
805 since assign_stack_local will do any required rounding.
807 KEEP is 1 if this slot is to be retained after a call to
808 free_temp_slots. Automatic variables for a block are allocated
809 with this flag. KEEP is 2, if we allocate a longer term temporary,
810 whose lifetime is controlled by CLEANUP_POINT_EXPRs. */
813 assign_stack_temp (mode, size, keep)
814 enum machine_mode mode;
815 int size;
816 int keep;
818 struct temp_slot *p, *best_p = 0;
820 /* If SIZE is -1 it means that somebody tried to allocate a temporary
821 of a variable size. */
822 if (size == -1)
823 abort ();
825 /* First try to find an available, already-allocated temporary that is the
826 exact size we require. */
827 for (p = temp_slots; p; p = p->next)
828 if (p->size == size && GET_MODE (p->slot) == mode && ! p->in_use)
829 break;
831 /* If we didn't find, one, try one that is larger than what we want. We
832 find the smallest such. */
833 if (p == 0)
834 for (p = temp_slots; p; p = p->next)
835 if (p->size > size && GET_MODE (p->slot) == mode && ! p->in_use
836 && (best_p == 0 || best_p->size > p->size))
837 best_p = p;
839 /* Make our best, if any, the one to use. */
840 if (best_p)
842 /* If there are enough aligned bytes left over, make them into a new
843 temp_slot so that the extra bytes don't get wasted. Do this only
844 for BLKmode slots, so that we can be sure of the alignment. */
845 if (GET_MODE (best_p->slot) == BLKmode)
847 int alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
848 int rounded_size = CEIL_ROUND (size, alignment);
850 if (best_p->size - rounded_size >= alignment)
852 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
853 p->in_use = p->addr_taken = 0;
854 p->size = best_p->size - rounded_size;
855 p->base_offset = best_p->base_offset + rounded_size;
856 p->full_size = best_p->full_size - rounded_size;
857 p->slot = gen_rtx (MEM, BLKmode,
858 plus_constant (XEXP (best_p->slot, 0),
859 rounded_size));
860 p->address = 0;
861 p->rtl_expr = 0;
862 p->next = temp_slots;
863 temp_slots = p;
865 stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, p->slot,
866 stack_slot_list);
868 best_p->size = rounded_size;
869 best_p->full_size = rounded_size;
873 p = best_p;
876 /* If we still didn't find one, make a new temporary. */
877 if (p == 0)
879 int frame_offset_old = frame_offset;
880 p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
881 /* If the temp slot mode doesn't indicate the alignment,
882 use the largest possible, so no one will be disappointed. */
883 p->slot = assign_stack_local (mode, size, mode == BLKmode ? -1 : 0);
884 /* The following slot size computation is necessary because we don't
885 know the actual size of the temporary slot until assign_stack_local
886 has performed all the frame alignment and size rounding for the
887 requested temporary. Note that extra space added for alignment
888 can be either above or below this stack slot depending on which
889 way the frame grows. We include the extra space if and only if it
890 is above this slot. */
891 #ifdef FRAME_GROWS_DOWNWARD
892 p->size = frame_offset_old - frame_offset;
893 #else
894 p->size = size;
895 #endif
896 /* Now define the fields used by combine_temp_slots. */
897 #ifdef FRAME_GROWS_DOWNWARD
898 p->base_offset = frame_offset;
899 p->full_size = frame_offset_old - frame_offset;
900 #else
901 p->base_offset = frame_offset_old;
902 p->full_size = frame_offset - frame_offset_old;
903 #endif
904 p->address = 0;
905 p->next = temp_slots;
906 temp_slots = p;
909 p->in_use = 1;
910 p->addr_taken = 0;
911 p->rtl_expr = sequence_rtl_expr;
913 if (keep == 2)
915 p->level = target_temp_slot_level;
916 p->keep = 0;
918 else
920 p->level = temp_slot_level;
921 p->keep = keep;
924 /* We may be reusing an old slot, so clear any MEM flags that may have been
925 set from before. */
926 RTX_UNCHANGING_P (p->slot) = 0;
927 MEM_IN_STRUCT_P (p->slot) = 0;
928 return p->slot;
931 /* Assign a temporary of given TYPE.
932 KEEP is as for assign_stack_temp.
933 MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
934 it is 0 if a register is OK.
935 DONT_PROMOTE is 1 if we should not promote values in register
936 to wider modes. */
939 assign_temp (type, keep, memory_required, dont_promote)
940 tree type;
941 int keep;
942 int memory_required;
943 int dont_promote;
945 enum machine_mode mode = TYPE_MODE (type);
946 int unsignedp = TREE_UNSIGNED (type);
948 if (mode == BLKmode || memory_required)
950 int size = int_size_in_bytes (type);
951 rtx tmp;
953 /* Unfortunately, we don't yet know how to allocate variable-sized
954 temporaries. However, sometimes we have a fixed upper limit on
955 the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
956 instead. This is the case for Chill variable-sized strings. */
957 if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
958 && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
959 && TREE_CODE (TYPE_ARRAY_MAX_SIZE (type)) == INTEGER_CST)
960 size = TREE_INT_CST_LOW (TYPE_ARRAY_MAX_SIZE (type));
962 tmp = assign_stack_temp (mode, size, keep);
963 MEM_IN_STRUCT_P (tmp) = AGGREGATE_TYPE_P (type);
964 return tmp;
967 #ifndef PROMOTE_FOR_CALL_ONLY
968 if (! dont_promote)
969 mode = promote_mode (type, mode, &unsignedp, 0);
970 #endif
972 return gen_reg_rtx (mode);
975 /* Combine temporary stack slots which are adjacent on the stack.
977 This allows for better use of already allocated stack space. This is only
978 done for BLKmode slots because we can be sure that we won't have alignment
979 problems in this case. */
981 void
982 combine_temp_slots ()
984 struct temp_slot *p, *q;
985 struct temp_slot *prev_p, *prev_q;
986 /* Determine where to free back to after this function. */
987 rtx free_pointer = rtx_alloc (CONST_INT);
989 for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
991 int delete_p = 0;
992 if (! p->in_use && GET_MODE (p->slot) == BLKmode)
993 for (q = p->next, prev_q = p; q; q = prev_q->next)
995 int delete_q = 0;
996 if (! q->in_use && GET_MODE (q->slot) == BLKmode)
998 if (p->base_offset + p->full_size == q->base_offset)
1000 /* Q comes after P; combine Q into P. */
1001 p->size += q->size;
1002 p->full_size += q->full_size;
1003 delete_q = 1;
1005 else if (q->base_offset + q->full_size == p->base_offset)
1007 /* P comes after Q; combine P into Q. */
1008 q->size += p->size;
1009 q->full_size += p->full_size;
1010 delete_p = 1;
1011 break;
1014 /* Either delete Q or advance past it. */
1015 if (delete_q)
1016 prev_q->next = q->next;
1017 else
1018 prev_q = q;
1020 /* Either delete P or advance past it. */
1021 if (delete_p)
1023 if (prev_p)
1024 prev_p->next = p->next;
1025 else
1026 temp_slots = p->next;
1028 else
1029 prev_p = p;
1032 /* Free all the RTL made by plus_constant. */
1033 rtx_free (free_pointer);
1036 /* Find the temp slot corresponding to the object at address X. */
1038 static struct temp_slot *
1039 find_temp_slot_from_address (x)
1040 rtx x;
1042 struct temp_slot *p;
1043 rtx next;
1045 for (p = temp_slots; p; p = p->next)
1047 if (! p->in_use)
1048 continue;
1049 else if (XEXP (p->slot, 0) == x
1050 || p->address == x
1051 || (GET_CODE (x) == PLUS
1052 && XEXP (x, 0) == virtual_stack_vars_rtx
1053 && GET_CODE (XEXP (x, 1)) == CONST_INT
1054 && INTVAL (XEXP (x, 1)) >= p->base_offset
1055 && INTVAL (XEXP (x, 1)) < p->base_offset + p->full_size))
1056 return p;
1058 else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
1059 for (next = p->address; next; next = XEXP (next, 1))
1060 if (XEXP (next, 0) == x)
1061 return p;
1064 return 0;
1067 /* Indicate that NEW is an alternate way of referring to the temp slot
1068 that previous was known by OLD. */
1070 void
1071 update_temp_slot_address (old, new)
1072 rtx old, new;
1074 struct temp_slot *p = find_temp_slot_from_address (old);
1076 /* If none, return. Else add NEW as an alias. */
1077 if (p == 0)
1078 return;
1079 else if (p->address == 0)
1080 p->address = new;
1081 else
1083 if (GET_CODE (p->address) != EXPR_LIST)
1084 p->address = gen_rtx (EXPR_LIST, VOIDmode, p->address, NULL_RTX);
1086 p->address = gen_rtx (EXPR_LIST, VOIDmode, new, p->address);
1090 /* If X could be a reference to a temporary slot, mark the fact that its
1091 address was taken. */
1093 void
1094 mark_temp_addr_taken (x)
1095 rtx x;
1097 struct temp_slot *p;
1099 if (x == 0)
1100 return;
1102 /* If X is not in memory or is at a constant address, it cannot be in
1103 a temporary slot. */
1104 if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1105 return;
1107 p = find_temp_slot_from_address (XEXP (x, 0));
1108 if (p != 0)
1109 p->addr_taken = 1;
1112 /* If X could be a reference to a temporary slot, mark that slot as
1113 belonging to the to one level higher than the current level. If X
1114 matched one of our slots, just mark that one. Otherwise, we can't
1115 easily predict which it is, so upgrade all of them. Kept slots
1116 need not be touched.
1118 This is called when an ({...}) construct occurs and a statement
1119 returns a value in memory. */
1121 void
1122 preserve_temp_slots (x)
1123 rtx x;
1125 struct temp_slot *p = 0;
1127 /* If there is no result, we still might have some objects whose address
1128 were taken, so we need to make sure they stay around. */
1129 if (x == 0)
1131 for (p = temp_slots; p; p = p->next)
1132 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1133 p->level--;
1135 return;
1138 /* If X is a register that is being used as a pointer, see if we have
1139 a temporary slot we know it points to. To be consistent with
1140 the code below, we really should preserve all non-kept slots
1141 if we can't find a match, but that seems to be much too costly. */
1142 if (GET_CODE (x) == REG && REGNO_POINTER_FLAG (REGNO (x)))
1143 p = find_temp_slot_from_address (x);
1145 /* If X is not in memory or is at a constant address, it cannot be in
1146 a temporary slot, but it can contain something whose address was
1147 taken. */
1148 if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1150 for (p = temp_slots; p; p = p->next)
1151 if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1152 p->level--;
1154 return;
1157 /* First see if we can find a match. */
1158 if (p == 0)
1159 p = find_temp_slot_from_address (XEXP (x, 0));
1161 if (p != 0)
1163 /* Move everything at our level whose address was taken to our new
1164 level in case we used its address. */
1165 struct temp_slot *q;
1167 if (p->level == temp_slot_level)
1169 for (q = temp_slots; q; q = q->next)
1170 if (q != p && q->addr_taken && q->level == p->level)
1171 q->level--;
1173 p->level--;
1174 p->addr_taken = 0;
1176 return;
1179 /* Otherwise, preserve all non-kept slots at this level. */
1180 for (p = temp_slots; p; p = p->next)
1181 if (p->in_use && p->level == temp_slot_level && ! p->keep)
1182 p->level--;
1185 /* X is the result of an RTL_EXPR. If it is a temporary slot associated
1186 with that RTL_EXPR, promote it into a temporary slot at the present
1187 level so it will not be freed when we free slots made in the
1188 RTL_EXPR. */
1190 void
1191 preserve_rtl_expr_result (x)
1192 rtx x;
1194 struct temp_slot *p;
1196 /* If X is not in memory or is at a constant address, it cannot be in
1197 a temporary slot. */
1198 if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1199 return;
1201 /* If we can find a match, move it to our level unless it is already at
1202 an upper level. */
1203 p = find_temp_slot_from_address (XEXP (x, 0));
1204 if (p != 0)
1206 p->level = MIN (p->level, temp_slot_level);
1207 p->rtl_expr = 0;
1210 return;
1213 /* Free all temporaries used so far. This is normally called at the end
1214 of generating code for a statement. Don't free any temporaries
1215 currently in use for an RTL_EXPR that hasn't yet been emitted.
1216 We could eventually do better than this since it can be reused while
1217 generating the same RTL_EXPR, but this is complex and probably not
1218 worthwhile. */
1220 void
1221 free_temp_slots ()
1223 struct temp_slot *p;
1225 for (p = temp_slots; p; p = p->next)
1226 if (p->in_use && p->level == temp_slot_level && ! p->keep
1227 && p->rtl_expr == 0)
1228 p->in_use = 0;
1230 combine_temp_slots ();
1233 /* Free all temporary slots used in T, an RTL_EXPR node. */
1235 void
1236 free_temps_for_rtl_expr (t)
1237 tree t;
1239 struct temp_slot *p;
1241 for (p = temp_slots; p; p = p->next)
1242 if (p->rtl_expr == t)
1243 p->in_use = 0;
1245 combine_temp_slots ();
1248 /* Mark all temporaries ever allocated in this functon as not suitable
1249 for reuse until the current level is exited. */
1251 void
1252 mark_all_temps_used ()
1254 struct temp_slot *p;
1256 for (p = temp_slots; p; p = p->next)
1258 p->in_use = p->keep = 1;
1259 p->level = MIN (p->level, temp_slot_level);
1263 /* Push deeper into the nesting level for stack temporaries. */
1265 void
1266 push_temp_slots ()
1268 temp_slot_level++;
1271 /* Pop a temporary nesting level. All slots in use in the current level
1272 are freed. */
1274 void
1275 pop_temp_slots ()
1277 struct temp_slot *p;
1279 for (p = temp_slots; p; p = p->next)
1280 if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1281 p->in_use = 0;
1283 combine_temp_slots ();
1285 temp_slot_level--;
1288 /* Initialize temporary slots. */
1290 void
1291 init_temp_slots ()
1293 /* We have not allocated any temporaries yet. */
1294 temp_slots = 0;
1295 temp_slot_level = 0;
1296 target_temp_slot_level = 0;
1299 /* Retroactively move an auto variable from a register to a stack slot.
1300 This is done when an address-reference to the variable is seen. */
1302 void
1303 put_var_into_stack (decl)
1304 tree decl;
1306 register rtx reg;
1307 enum machine_mode promoted_mode, decl_mode;
1308 struct function *function = 0;
1309 tree context;
1311 if (output_bytecode)
1312 return;
1314 context = decl_function_context (decl);
1316 /* Get the current rtl used for this object and it's original mode. */
1317 reg = TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl) : DECL_RTL (decl);
1319 /* No need to do anything if decl has no rtx yet
1320 since in that case caller is setting TREE_ADDRESSABLE
1321 and a stack slot will be assigned when the rtl is made. */
1322 if (reg == 0)
1323 return;
1325 /* Get the declared mode for this object. */
1326 decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1327 : DECL_MODE (decl));
1328 /* Get the mode it's actually stored in. */
1329 promoted_mode = GET_MODE (reg);
1331 /* If this variable comes from an outer function,
1332 find that function's saved context. */
1333 if (context != current_function_decl)
1334 for (function = outer_function_chain; function; function = function->next)
1335 if (function->decl == context)
1336 break;
1338 /* If this is a variable-size object with a pseudo to address it,
1339 put that pseudo into the stack, if the var is nonlocal. */
1340 if (DECL_NONLOCAL (decl)
1341 && GET_CODE (reg) == MEM
1342 && GET_CODE (XEXP (reg, 0)) == REG
1343 && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1345 reg = XEXP (reg, 0);
1346 decl_mode = promoted_mode = GET_MODE (reg);
1349 /* Now we should have a value that resides in one or more pseudo regs. */
1351 if (GET_CODE (reg) == REG)
1352 put_reg_into_stack (function, reg, TREE_TYPE (decl),
1353 promoted_mode, decl_mode, TREE_SIDE_EFFECTS (decl));
1354 else if (GET_CODE (reg) == CONCAT)
1356 /* A CONCAT contains two pseudos; put them both in the stack.
1357 We do it so they end up consecutive. */
1358 enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1359 tree part_type = TREE_TYPE (TREE_TYPE (decl));
1360 #ifdef FRAME_GROWS_DOWNWARD
1361 /* Since part 0 should have a lower address, do it second. */
1362 put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1363 part_mode, TREE_SIDE_EFFECTS (decl));
1364 put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1365 part_mode, TREE_SIDE_EFFECTS (decl));
1366 #else
1367 put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1368 part_mode, TREE_SIDE_EFFECTS (decl));
1369 put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1370 part_mode, TREE_SIDE_EFFECTS (decl));
1371 #endif
1373 /* Change the CONCAT into a combined MEM for both parts. */
1374 PUT_CODE (reg, MEM);
1375 MEM_VOLATILE_P (reg) = MEM_VOLATILE_P (XEXP (reg, 0));
1377 /* The two parts are in memory order already.
1378 Use the lower parts address as ours. */
1379 XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1380 /* Prevent sharing of rtl that might lose. */
1381 if (GET_CODE (XEXP (reg, 0)) == PLUS)
1382 XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1386 /* Subroutine of put_var_into_stack. This puts a single pseudo reg REG
1387 into the stack frame of FUNCTION (0 means the current function).
1388 DECL_MODE is the machine mode of the user-level data type.
1389 PROMOTED_MODE is the machine mode of the register.
1390 VOLATILE_P is nonzero if this is for a "volatile" decl. */
1392 static void
1393 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p)
1394 struct function *function;
1395 rtx reg;
1396 tree type;
1397 enum machine_mode promoted_mode, decl_mode;
1398 int volatile_p;
1400 rtx new = 0;
1402 if (function)
1404 if (REGNO (reg) < function->max_parm_reg)
1405 new = function->parm_reg_stack_loc[REGNO (reg)];
1406 if (new == 0)
1407 new = assign_outer_stack_local (decl_mode, GET_MODE_SIZE (decl_mode),
1408 0, function);
1410 else
1412 if (REGNO (reg) < max_parm_reg)
1413 new = parm_reg_stack_loc[REGNO (reg)];
1414 if (new == 0)
1415 new = assign_stack_local (decl_mode, GET_MODE_SIZE (decl_mode), 0);
1418 PUT_MODE (reg, decl_mode);
1419 XEXP (reg, 0) = XEXP (new, 0);
1420 /* `volatil' bit means one thing for MEMs, another entirely for REGs. */
1421 MEM_VOLATILE_P (reg) = volatile_p;
1422 PUT_CODE (reg, MEM);
1424 /* If this is a memory ref that contains aggregate components,
1425 mark it as such for cse and loop optimize. */
1426 MEM_IN_STRUCT_P (reg) = AGGREGATE_TYPE_P (type);
1428 /* Now make sure that all refs to the variable, previously made
1429 when it was a register, are fixed up to be valid again. */
1430 if (function)
1432 struct var_refs_queue *temp;
1434 /* Variable is inherited; fix it up when we get back to its function. */
1435 push_obstacks (function->function_obstack,
1436 function->function_maybepermanent_obstack);
1438 /* See comment in restore_tree_status in tree.c for why this needs to be
1439 on saveable obstack. */
1440 temp
1441 = (struct var_refs_queue *) savealloc (sizeof (struct var_refs_queue));
1442 temp->modified = reg;
1443 temp->promoted_mode = promoted_mode;
1444 temp->unsignedp = TREE_UNSIGNED (type);
1445 temp->next = function->fixup_var_refs_queue;
1446 function->fixup_var_refs_queue = temp;
1447 pop_obstacks ();
1449 else
1450 /* Variable is local; fix it up now. */
1451 fixup_var_refs (reg, promoted_mode, TREE_UNSIGNED (type));
1454 static void
1455 fixup_var_refs (var, promoted_mode, unsignedp)
1456 rtx var;
1457 enum machine_mode promoted_mode;
1458 int unsignedp;
1460 tree pending;
1461 rtx first_insn = get_insns ();
1462 struct sequence_stack *stack = sequence_stack;
1463 tree rtl_exps = rtl_expr_chain;
1465 /* Must scan all insns for stack-refs that exceed the limit. */
1466 fixup_var_refs_insns (var, promoted_mode, unsignedp, first_insn, stack == 0);
1468 /* Scan all pending sequences too. */
1469 for (; stack; stack = stack->next)
1471 push_to_sequence (stack->first);
1472 fixup_var_refs_insns (var, promoted_mode, unsignedp,
1473 stack->first, stack->next != 0);
1474 /* Update remembered end of sequence
1475 in case we added an insn at the end. */
1476 stack->last = get_last_insn ();
1477 end_sequence ();
1480 /* Scan all waiting RTL_EXPRs too. */
1481 for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1483 rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1484 if (seq != const0_rtx && seq != 0)
1486 push_to_sequence (seq);
1487 fixup_var_refs_insns (var, promoted_mode, unsignedp, seq, 0);
1488 end_sequence ();
1493 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1494 some part of an insn. Return a struct fixup_replacement whose OLD
1495 value is equal to X. Allocate a new structure if no such entry exists. */
1497 static struct fixup_replacement *
1498 find_fixup_replacement (replacements, x)
1499 struct fixup_replacement **replacements;
1500 rtx x;
1502 struct fixup_replacement *p;
1504 /* See if we have already replaced this. */
1505 for (p = *replacements; p && p->old != x; p = p->next)
1508 if (p == 0)
1510 p = (struct fixup_replacement *) oballoc (sizeof (struct fixup_replacement));
1511 p->old = x;
1512 p->new = 0;
1513 p->next = *replacements;
1514 *replacements = p;
1517 return p;
1520 /* Scan the insn-chain starting with INSN for refs to VAR
1521 and fix them up. TOPLEVEL is nonzero if this chain is the
1522 main chain of insns for the current function. */
1524 static void
1525 fixup_var_refs_insns (var, promoted_mode, unsignedp, insn, toplevel)
1526 rtx var;
1527 enum machine_mode promoted_mode;
1528 int unsignedp;
1529 rtx insn;
1530 int toplevel;
1532 rtx call_dest = 0;
1534 while (insn)
1536 rtx next = NEXT_INSN (insn);
1537 rtx note;
1538 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1540 /* If this is a CLOBBER of VAR, delete it.
1542 If it has a REG_LIBCALL note, delete the REG_LIBCALL
1543 and REG_RETVAL notes too. */
1544 if (GET_CODE (PATTERN (insn)) == CLOBBER
1545 && XEXP (PATTERN (insn), 0) == var)
1547 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1548 /* The REG_LIBCALL note will go away since we are going to
1549 turn INSN into a NOTE, so just delete the
1550 corresponding REG_RETVAL note. */
1551 remove_note (XEXP (note, 0),
1552 find_reg_note (XEXP (note, 0), REG_RETVAL,
1553 NULL_RTX));
1555 /* In unoptimized compilation, we shouldn't call delete_insn
1556 except in jump.c doing warnings. */
1557 PUT_CODE (insn, NOTE);
1558 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1559 NOTE_SOURCE_FILE (insn) = 0;
1562 /* The insn to load VAR from a home in the arglist
1563 is now a no-op. When we see it, just delete it. */
1564 else if (toplevel
1565 && GET_CODE (PATTERN (insn)) == SET
1566 && SET_DEST (PATTERN (insn)) == var
1567 /* If this represents the result of an insn group,
1568 don't delete the insn. */
1569 && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1570 && rtx_equal_p (SET_SRC (PATTERN (insn)), var))
1572 /* In unoptimized compilation, we shouldn't call delete_insn
1573 except in jump.c doing warnings. */
1574 PUT_CODE (insn, NOTE);
1575 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1576 NOTE_SOURCE_FILE (insn) = 0;
1577 if (insn == last_parm_insn)
1578 last_parm_insn = PREV_INSN (next);
1580 else
1582 struct fixup_replacement *replacements = 0;
1583 rtx next_insn = NEXT_INSN (insn);
1585 #ifdef SMALL_REGISTER_CLASSES
1586 /* If the insn that copies the results of a CALL_INSN
1587 into a pseudo now references VAR, we have to use an
1588 intermediate pseudo since we want the life of the
1589 return value register to be only a single insn.
1591 If we don't use an intermediate pseudo, such things as
1592 address computations to make the address of VAR valid
1593 if it is not can be placed between the CALL_INSN and INSN.
1595 To make sure this doesn't happen, we record the destination
1596 of the CALL_INSN and see if the next insn uses both that
1597 and VAR. */
1599 if (SMALL_REGISTER_CLASSES)
1601 if (call_dest != 0 && GET_CODE (insn) == INSN
1602 && reg_mentioned_p (var, PATTERN (insn))
1603 && reg_mentioned_p (call_dest, PATTERN (insn)))
1605 rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1607 emit_insn_before (gen_move_insn (temp, call_dest), insn);
1609 PATTERN (insn) = replace_rtx (PATTERN (insn),
1610 call_dest, temp);
1613 if (GET_CODE (insn) == CALL_INSN
1614 && GET_CODE (PATTERN (insn)) == SET)
1615 call_dest = SET_DEST (PATTERN (insn));
1616 else if (GET_CODE (insn) == CALL_INSN
1617 && GET_CODE (PATTERN (insn)) == PARALLEL
1618 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1619 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1620 else
1621 call_dest = 0;
1623 #endif
1625 /* See if we have to do anything to INSN now that VAR is in
1626 memory. If it needs to be loaded into a pseudo, use a single
1627 pseudo for the entire insn in case there is a MATCH_DUP
1628 between two operands. We pass a pointer to the head of
1629 a list of struct fixup_replacements. If fixup_var_refs_1
1630 needs to allocate pseudos or replacement MEMs (for SUBREGs),
1631 it will record them in this list.
1633 If it allocated a pseudo for any replacement, we copy into
1634 it here. */
1636 fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1637 &replacements);
1639 /* If this is last_parm_insn, and any instructions were output
1640 after it to fix it up, then we must set last_parm_insn to
1641 the last such instruction emitted. */
1642 if (insn == last_parm_insn)
1643 last_parm_insn = PREV_INSN (next_insn);
1645 while (replacements)
1647 if (GET_CODE (replacements->new) == REG)
1649 rtx insert_before;
1650 rtx seq;
1652 /* OLD might be a (subreg (mem)). */
1653 if (GET_CODE (replacements->old) == SUBREG)
1654 replacements->old
1655 = fixup_memory_subreg (replacements->old, insn, 0);
1656 else
1657 replacements->old
1658 = fixup_stack_1 (replacements->old, insn);
1660 insert_before = insn;
1662 /* If we are changing the mode, do a conversion.
1663 This might be wasteful, but combine.c will
1664 eliminate much of the waste. */
1666 if (GET_MODE (replacements->new)
1667 != GET_MODE (replacements->old))
1669 start_sequence ();
1670 convert_move (replacements->new,
1671 replacements->old, unsignedp);
1672 seq = gen_sequence ();
1673 end_sequence ();
1675 else
1676 seq = gen_move_insn (replacements->new,
1677 replacements->old);
1679 emit_insn_before (seq, insert_before);
1682 replacements = replacements->next;
1686 /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1687 But don't touch other insns referred to by reg-notes;
1688 we will get them elsewhere. */
1689 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1690 if (GET_CODE (note) != INSN_LIST)
1691 XEXP (note, 0)
1692 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
1694 insn = next;
1698 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1699 See if the rtx expression at *LOC in INSN needs to be changed.
1701 REPLACEMENTS is a pointer to a list head that starts out zero, but may
1702 contain a list of original rtx's and replacements. If we find that we need
1703 to modify this insn by replacing a memory reference with a pseudo or by
1704 making a new MEM to implement a SUBREG, we consult that list to see if
1705 we have already chosen a replacement. If none has already been allocated,
1706 we allocate it and update the list. fixup_var_refs_insns will copy VAR
1707 or the SUBREG, as appropriate, to the pseudo. */
1709 static void
1710 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
1711 register rtx var;
1712 enum machine_mode promoted_mode;
1713 register rtx *loc;
1714 rtx insn;
1715 struct fixup_replacement **replacements;
1717 register int i;
1718 register rtx x = *loc;
1719 RTX_CODE code = GET_CODE (x);
1720 register char *fmt;
1721 register rtx tem, tem1;
1722 struct fixup_replacement *replacement;
1724 switch (code)
1726 case MEM:
1727 if (var == x)
1729 /* If we already have a replacement, use it. Otherwise,
1730 try to fix up this address in case it is invalid. */
1732 replacement = find_fixup_replacement (replacements, var);
1733 if (replacement->new)
1735 *loc = replacement->new;
1736 return;
1739 *loc = replacement->new = x = fixup_stack_1 (x, insn);
1741 /* Unless we are forcing memory to register or we changed the mode,
1742 we can leave things the way they are if the insn is valid. */
1744 INSN_CODE (insn) = -1;
1745 if (! flag_force_mem && GET_MODE (x) == promoted_mode
1746 && recog_memoized (insn) >= 0)
1747 return;
1749 *loc = replacement->new = gen_reg_rtx (promoted_mode);
1750 return;
1753 /* If X contains VAR, we need to unshare it here so that we update
1754 each occurrence separately. But all identical MEMs in one insn
1755 must be replaced with the same rtx because of the possibility of
1756 MATCH_DUPs. */
1758 if (reg_mentioned_p (var, x))
1760 replacement = find_fixup_replacement (replacements, x);
1761 if (replacement->new == 0)
1762 replacement->new = copy_most_rtx (x, var);
1764 *loc = x = replacement->new;
1766 break;
1768 case REG:
1769 case CC0:
1770 case PC:
1771 case CONST_INT:
1772 case CONST:
1773 case SYMBOL_REF:
1774 case LABEL_REF:
1775 case CONST_DOUBLE:
1776 return;
1778 case SIGN_EXTRACT:
1779 case ZERO_EXTRACT:
1780 /* Note that in some cases those types of expressions are altered
1781 by optimize_bit_field, and do not survive to get here. */
1782 if (XEXP (x, 0) == var
1783 || (GET_CODE (XEXP (x, 0)) == SUBREG
1784 && SUBREG_REG (XEXP (x, 0)) == var))
1786 /* Get TEM as a valid MEM in the mode presently in the insn.
1788 We don't worry about the possibility of MATCH_DUP here; it
1789 is highly unlikely and would be tricky to handle. */
1791 tem = XEXP (x, 0);
1792 if (GET_CODE (tem) == SUBREG)
1794 if (GET_MODE_BITSIZE (GET_MODE (tem))
1795 > GET_MODE_BITSIZE (GET_MODE (var)))
1797 replacement = find_fixup_replacement (replacements, var);
1798 if (replacement->new == 0)
1799 replacement->new = gen_reg_rtx (GET_MODE (var));
1800 SUBREG_REG (tem) = replacement->new;
1802 else
1803 tem = fixup_memory_subreg (tem, insn, 0);
1805 else
1806 tem = fixup_stack_1 (tem, insn);
1808 /* Unless we want to load from memory, get TEM into the proper mode
1809 for an extract from memory. This can only be done if the
1810 extract is at a constant position and length. */
1812 if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
1813 && GET_CODE (XEXP (x, 2)) == CONST_INT
1814 && ! mode_dependent_address_p (XEXP (tem, 0))
1815 && ! MEM_VOLATILE_P (tem))
1817 enum machine_mode wanted_mode = VOIDmode;
1818 enum machine_mode is_mode = GET_MODE (tem);
1819 int width = INTVAL (XEXP (x, 1));
1820 int pos = INTVAL (XEXP (x, 2));
1822 #ifdef HAVE_extzv
1823 if (GET_CODE (x) == ZERO_EXTRACT)
1824 wanted_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
1825 #endif
1826 #ifdef HAVE_extv
1827 if (GET_CODE (x) == SIGN_EXTRACT)
1828 wanted_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
1829 #endif
1830 /* If we have a narrower mode, we can do something. */
1831 if (wanted_mode != VOIDmode
1832 && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1834 int offset = pos / BITS_PER_UNIT;
1835 rtx old_pos = XEXP (x, 2);
1836 rtx newmem;
1838 /* If the bytes and bits are counted differently, we
1839 must adjust the offset. */
1840 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1841 offset = (GET_MODE_SIZE (is_mode)
1842 - GET_MODE_SIZE (wanted_mode) - offset);
1844 pos %= GET_MODE_BITSIZE (wanted_mode);
1846 newmem = gen_rtx (MEM, wanted_mode,
1847 plus_constant (XEXP (tem, 0), offset));
1848 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1849 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1850 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1852 /* Make the change and see if the insn remains valid. */
1853 INSN_CODE (insn) = -1;
1854 XEXP (x, 0) = newmem;
1855 XEXP (x, 2) = GEN_INT (pos);
1857 if (recog_memoized (insn) >= 0)
1858 return;
1860 /* Otherwise, restore old position. XEXP (x, 0) will be
1861 restored later. */
1862 XEXP (x, 2) = old_pos;
1866 /* If we get here, the bitfield extract insn can't accept a memory
1867 reference. Copy the input into a register. */
1869 tem1 = gen_reg_rtx (GET_MODE (tem));
1870 emit_insn_before (gen_move_insn (tem1, tem), insn);
1871 XEXP (x, 0) = tem1;
1872 return;
1874 break;
1876 case SUBREG:
1877 if (SUBREG_REG (x) == var)
1879 /* If this is a special SUBREG made because VAR was promoted
1880 from a wider mode, replace it with VAR and call ourself
1881 recursively, this time saying that the object previously
1882 had its current mode (by virtue of the SUBREG). */
1884 if (SUBREG_PROMOTED_VAR_P (x))
1886 *loc = var;
1887 fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
1888 return;
1891 /* If this SUBREG makes VAR wider, it has become a paradoxical
1892 SUBREG with VAR in memory, but these aren't allowed at this
1893 stage of the compilation. So load VAR into a pseudo and take
1894 a SUBREG of that pseudo. */
1895 if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
1897 replacement = find_fixup_replacement (replacements, var);
1898 if (replacement->new == 0)
1899 replacement->new = gen_reg_rtx (GET_MODE (var));
1900 SUBREG_REG (x) = replacement->new;
1901 return;
1904 /* See if we have already found a replacement for this SUBREG.
1905 If so, use it. Otherwise, make a MEM and see if the insn
1906 is recognized. If not, or if we should force MEM into a register,
1907 make a pseudo for this SUBREG. */
1908 replacement = find_fixup_replacement (replacements, x);
1909 if (replacement->new)
1911 *loc = replacement->new;
1912 return;
1915 replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
1917 INSN_CODE (insn) = -1;
1918 if (! flag_force_mem && recog_memoized (insn) >= 0)
1919 return;
1921 *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
1922 return;
1924 break;
1926 case SET:
1927 /* First do special simplification of bit-field references. */
1928 if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
1929 || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1930 optimize_bit_field (x, insn, 0);
1931 if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
1932 || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
1933 optimize_bit_field (x, insn, NULL_PTR);
1935 /* For a paradoxical SUBREG inside a ZERO_EXTRACT, load the object
1936 into a register and then store it back out. */
1937 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
1938 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG
1939 && SUBREG_REG (XEXP (SET_DEST (x), 0)) == var
1940 && (GET_MODE_SIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
1941 > GET_MODE_SIZE (GET_MODE (var))))
1943 replacement = find_fixup_replacement (replacements, var);
1944 if (replacement->new == 0)
1945 replacement->new = gen_reg_rtx (GET_MODE (var));
1947 SUBREG_REG (XEXP (SET_DEST (x), 0)) = replacement->new;
1948 emit_insn_after (gen_move_insn (var, replacement->new), insn);
1951 /* If SET_DEST is now a paradoxical SUBREG, put the result of this
1952 insn into a pseudo and store the low part of the pseudo into VAR. */
1953 if (GET_CODE (SET_DEST (x)) == SUBREG
1954 && SUBREG_REG (SET_DEST (x)) == var
1955 && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
1956 > GET_MODE_SIZE (GET_MODE (var))))
1958 SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
1959 emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
1960 tem)),
1961 insn);
1962 break;
1966 rtx dest = SET_DEST (x);
1967 rtx src = SET_SRC (x);
1968 rtx outerdest = dest;
1970 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1971 || GET_CODE (dest) == SIGN_EXTRACT
1972 || GET_CODE (dest) == ZERO_EXTRACT)
1973 dest = XEXP (dest, 0);
1975 if (GET_CODE (src) == SUBREG)
1976 src = XEXP (src, 0);
1978 /* If VAR does not appear at the top level of the SET
1979 just scan the lower levels of the tree. */
1981 if (src != var && dest != var)
1982 break;
1984 /* We will need to rerecognize this insn. */
1985 INSN_CODE (insn) = -1;
1987 #ifdef HAVE_insv
1988 if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var)
1990 /* Since this case will return, ensure we fixup all the
1991 operands here. */
1992 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
1993 insn, replacements);
1994 fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
1995 insn, replacements);
1996 fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
1997 insn, replacements);
1999 tem = XEXP (outerdest, 0);
2001 /* Clean up (SUBREG:SI (MEM:mode ...) 0)
2002 that may appear inside a ZERO_EXTRACT.
2003 This was legitimate when the MEM was a REG. */
2004 if (GET_CODE (tem) == SUBREG
2005 && SUBREG_REG (tem) == var)
2006 tem = fixup_memory_subreg (tem, insn, 0);
2007 else
2008 tem = fixup_stack_1 (tem, insn);
2010 if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
2011 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
2012 && ! mode_dependent_address_p (XEXP (tem, 0))
2013 && ! MEM_VOLATILE_P (tem))
2015 enum machine_mode wanted_mode
2016 = insn_operand_mode[(int) CODE_FOR_insv][0];
2017 enum machine_mode is_mode = GET_MODE (tem);
2018 int width = INTVAL (XEXP (outerdest, 1));
2019 int pos = INTVAL (XEXP (outerdest, 2));
2021 /* If we have a narrower mode, we can do something. */
2022 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
2024 int offset = pos / BITS_PER_UNIT;
2025 rtx old_pos = XEXP (outerdest, 2);
2026 rtx newmem;
2028 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
2029 offset = (GET_MODE_SIZE (is_mode)
2030 - GET_MODE_SIZE (wanted_mode) - offset);
2032 pos %= GET_MODE_BITSIZE (wanted_mode);
2034 newmem = gen_rtx (MEM, wanted_mode,
2035 plus_constant (XEXP (tem, 0), offset));
2036 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
2037 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
2038 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
2040 /* Make the change and see if the insn remains valid. */
2041 INSN_CODE (insn) = -1;
2042 XEXP (outerdest, 0) = newmem;
2043 XEXP (outerdest, 2) = GEN_INT (pos);
2045 if (recog_memoized (insn) >= 0)
2046 return;
2048 /* Otherwise, restore old position. XEXP (x, 0) will be
2049 restored later. */
2050 XEXP (outerdest, 2) = old_pos;
2054 /* If we get here, the bit-field store doesn't allow memory
2055 or isn't located at a constant position. Load the value into
2056 a register, do the store, and put it back into memory. */
2058 tem1 = gen_reg_rtx (GET_MODE (tem));
2059 emit_insn_before (gen_move_insn (tem1, tem), insn);
2060 emit_insn_after (gen_move_insn (tem, tem1), insn);
2061 XEXP (outerdest, 0) = tem1;
2062 return;
2064 #endif
2066 /* STRICT_LOW_PART is a no-op on memory references
2067 and it can cause combinations to be unrecognizable,
2068 so eliminate it. */
2070 if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2071 SET_DEST (x) = XEXP (SET_DEST (x), 0);
2073 /* A valid insn to copy VAR into or out of a register
2074 must be left alone, to avoid an infinite loop here.
2075 If the reference to VAR is by a subreg, fix that up,
2076 since SUBREG is not valid for a memref.
2077 Also fix up the address of the stack slot.
2079 Note that we must not try to recognize the insn until
2080 after we know that we have valid addresses and no
2081 (subreg (mem ...) ...) constructs, since these interfere
2082 with determining the validity of the insn. */
2084 if ((SET_SRC (x) == var
2085 || (GET_CODE (SET_SRC (x)) == SUBREG
2086 && SUBREG_REG (SET_SRC (x)) == var))
2087 && (GET_CODE (SET_DEST (x)) == REG
2088 || (GET_CODE (SET_DEST (x)) == SUBREG
2089 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2090 && GET_MODE (var) == promoted_mode
2091 && x == single_set (insn))
2093 rtx pat;
2095 replacement = find_fixup_replacement (replacements, SET_SRC (x));
2096 if (replacement->new)
2097 SET_SRC (x) = replacement->new;
2098 else if (GET_CODE (SET_SRC (x)) == SUBREG)
2099 SET_SRC (x) = replacement->new
2100 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2101 else
2102 SET_SRC (x) = replacement->new
2103 = fixup_stack_1 (SET_SRC (x), insn);
2105 if (recog_memoized (insn) >= 0)
2106 return;
2108 /* INSN is not valid, but we know that we want to
2109 copy SET_SRC (x) to SET_DEST (x) in some way. So
2110 we generate the move and see whether it requires more
2111 than one insn. If it does, we emit those insns and
2112 delete INSN. Otherwise, we an just replace the pattern
2113 of INSN; we have already verified above that INSN has
2114 no other function that to do X. */
2116 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2117 if (GET_CODE (pat) == SEQUENCE)
2119 emit_insn_after (pat, insn);
2120 PUT_CODE (insn, NOTE);
2121 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2122 NOTE_SOURCE_FILE (insn) = 0;
2124 else
2125 PATTERN (insn) = pat;
2127 return;
2130 if ((SET_DEST (x) == var
2131 || (GET_CODE (SET_DEST (x)) == SUBREG
2132 && SUBREG_REG (SET_DEST (x)) == var))
2133 && (GET_CODE (SET_SRC (x)) == REG
2134 || (GET_CODE (SET_SRC (x)) == SUBREG
2135 && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2136 && GET_MODE (var) == promoted_mode
2137 && x == single_set (insn))
2139 rtx pat;
2141 if (GET_CODE (SET_DEST (x)) == SUBREG)
2142 SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2143 else
2144 SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2146 if (recog_memoized (insn) >= 0)
2147 return;
2149 pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2150 if (GET_CODE (pat) == SEQUENCE)
2152 emit_insn_after (pat, insn);
2153 PUT_CODE (insn, NOTE);
2154 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2155 NOTE_SOURCE_FILE (insn) = 0;
2157 else
2158 PATTERN (insn) = pat;
2160 return;
2163 /* Otherwise, storing into VAR must be handled specially
2164 by storing into a temporary and copying that into VAR
2165 with a new insn after this one. Note that this case
2166 will be used when storing into a promoted scalar since
2167 the insn will now have different modes on the input
2168 and output and hence will be invalid (except for the case
2169 of setting it to a constant, which does not need any
2170 change if it is valid). We generate extra code in that case,
2171 but combine.c will eliminate it. */
2173 if (dest == var)
2175 rtx temp;
2176 rtx fixeddest = SET_DEST (x);
2178 /* STRICT_LOW_PART can be discarded, around a MEM. */
2179 if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2180 fixeddest = XEXP (fixeddest, 0);
2181 /* Convert (SUBREG (MEM)) to a MEM in a changed mode. */
2182 if (GET_CODE (fixeddest) == SUBREG)
2184 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2185 promoted_mode = GET_MODE (fixeddest);
2187 else
2188 fixeddest = fixup_stack_1 (fixeddest, insn);
2190 temp = gen_reg_rtx (promoted_mode);
2192 emit_insn_after (gen_move_insn (fixeddest,
2193 gen_lowpart (GET_MODE (fixeddest),
2194 temp)),
2195 insn);
2197 SET_DEST (x) = temp;
2202 /* Nothing special about this RTX; fix its operands. */
2204 fmt = GET_RTX_FORMAT (code);
2205 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2207 if (fmt[i] == 'e')
2208 fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
2209 if (fmt[i] == 'E')
2211 register int j;
2212 for (j = 0; j < XVECLEN (x, i); j++)
2213 fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2214 insn, replacements);
2219 /* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2220 return an rtx (MEM:m1 newaddr) which is equivalent.
2221 If any insns must be emitted to compute NEWADDR, put them before INSN.
2223 UNCRITICAL nonzero means accept paradoxical subregs.
2224 This is used for subregs found inside REG_NOTES. */
2226 static rtx
2227 fixup_memory_subreg (x, insn, uncritical)
2228 rtx x;
2229 rtx insn;
2230 int uncritical;
2232 int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2233 rtx addr = XEXP (SUBREG_REG (x), 0);
2234 enum machine_mode mode = GET_MODE (x);
2235 rtx saved, result;
2237 /* Paradoxical SUBREGs are usually invalid during RTL generation. */
2238 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2239 && ! uncritical)
2240 abort ();
2242 if (BYTES_BIG_ENDIAN)
2243 offset += (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2244 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2245 addr = plus_constant (addr, offset);
2246 if (!flag_force_addr && memory_address_p (mode, addr))
2247 /* Shortcut if no insns need be emitted. */
2248 return change_address (SUBREG_REG (x), mode, addr);
2249 start_sequence ();
2250 result = change_address (SUBREG_REG (x), mode, addr);
2251 emit_insn_before (gen_sequence (), insn);
2252 end_sequence ();
2253 return result;
2256 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2257 Replace subexpressions of X in place.
2258 If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2259 Otherwise return X, with its contents possibly altered.
2261 If any insns must be emitted to compute NEWADDR, put them before INSN.
2263 UNCRITICAL is as in fixup_memory_subreg. */
2265 static rtx
2266 walk_fixup_memory_subreg (x, insn, uncritical)
2267 register rtx x;
2268 rtx insn;
2269 int uncritical;
2271 register enum rtx_code code;
2272 register char *fmt;
2273 register int i;
2275 if (x == 0)
2276 return 0;
2278 code = GET_CODE (x);
2280 if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2281 return fixup_memory_subreg (x, insn, uncritical);
2283 /* Nothing special about this RTX; fix its operands. */
2285 fmt = GET_RTX_FORMAT (code);
2286 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2288 if (fmt[i] == 'e')
2289 XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
2290 if (fmt[i] == 'E')
2292 register int j;
2293 for (j = 0; j < XVECLEN (x, i); j++)
2294 XVECEXP (x, i, j)
2295 = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
2298 return x;
2301 /* For each memory ref within X, if it refers to a stack slot
2302 with an out of range displacement, put the address in a temp register
2303 (emitting new insns before INSN to load these registers)
2304 and alter the memory ref to use that register.
2305 Replace each such MEM rtx with a copy, to avoid clobberage. */
2307 static rtx
2308 fixup_stack_1 (x, insn)
2309 rtx x;
2310 rtx insn;
2312 register int i;
2313 register RTX_CODE code = GET_CODE (x);
2314 register char *fmt;
2316 if (code == MEM)
2318 register rtx ad = XEXP (x, 0);
2319 /* If we have address of a stack slot but it's not valid
2320 (displacement is too large), compute the sum in a register. */
2321 if (GET_CODE (ad) == PLUS
2322 && GET_CODE (XEXP (ad, 0)) == REG
2323 && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2324 && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2325 || XEXP (ad, 0) == current_function_internal_arg_pointer)
2326 && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2328 rtx temp, seq;
2329 if (memory_address_p (GET_MODE (x), ad))
2330 return x;
2332 start_sequence ();
2333 temp = copy_to_reg (ad);
2334 seq = gen_sequence ();
2335 end_sequence ();
2336 emit_insn_before (seq, insn);
2337 return change_address (x, VOIDmode, temp);
2339 return x;
2342 fmt = GET_RTX_FORMAT (code);
2343 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2345 if (fmt[i] == 'e')
2346 XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2347 if (fmt[i] == 'E')
2349 register int j;
2350 for (j = 0; j < XVECLEN (x, i); j++)
2351 XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2354 return x;
2357 /* Optimization: a bit-field instruction whose field
2358 happens to be a byte or halfword in memory
2359 can be changed to a move instruction.
2361 We call here when INSN is an insn to examine or store into a bit-field.
2362 BODY is the SET-rtx to be altered.
2364 EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2365 (Currently this is called only from function.c, and EQUIV_MEM
2366 is always 0.) */
2368 static void
2369 optimize_bit_field (body, insn, equiv_mem)
2370 rtx body;
2371 rtx insn;
2372 rtx *equiv_mem;
2374 register rtx bitfield;
2375 int destflag;
2376 rtx seq = 0;
2377 enum machine_mode mode;
2379 if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2380 || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2381 bitfield = SET_DEST (body), destflag = 1;
2382 else
2383 bitfield = SET_SRC (body), destflag = 0;
2385 /* First check that the field being stored has constant size and position
2386 and is in fact a byte or halfword suitably aligned. */
2388 if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2389 && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2390 && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2391 != BLKmode)
2392 && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2394 register rtx memref = 0;
2396 /* Now check that the containing word is memory, not a register,
2397 and that it is safe to change the machine mode. */
2399 if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2400 memref = XEXP (bitfield, 0);
2401 else if (GET_CODE (XEXP (bitfield, 0)) == REG
2402 && equiv_mem != 0)
2403 memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2404 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2405 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2406 memref = SUBREG_REG (XEXP (bitfield, 0));
2407 else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2408 && equiv_mem != 0
2409 && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2410 memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2412 if (memref
2413 && ! mode_dependent_address_p (XEXP (memref, 0))
2414 && ! MEM_VOLATILE_P (memref))
2416 /* Now adjust the address, first for any subreg'ing
2417 that we are now getting rid of,
2418 and then for which byte of the word is wanted. */
2420 register int offset = INTVAL (XEXP (bitfield, 2));
2421 rtx insns;
2423 /* Adjust OFFSET to count bits from low-address byte. */
2424 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2425 offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2426 - offset - INTVAL (XEXP (bitfield, 1)));
2428 /* Adjust OFFSET to count bytes from low-address byte. */
2429 offset /= BITS_PER_UNIT;
2430 if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2432 offset += SUBREG_WORD (XEXP (bitfield, 0)) * UNITS_PER_WORD;
2433 if (BYTES_BIG_ENDIAN)
2434 offset -= (MIN (UNITS_PER_WORD,
2435 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2436 - MIN (UNITS_PER_WORD,
2437 GET_MODE_SIZE (GET_MODE (memref))));
2440 start_sequence ();
2441 memref = change_address (memref, mode,
2442 plus_constant (XEXP (memref, 0), offset));
2443 insns = get_insns ();
2444 end_sequence ();
2445 emit_insns_before (insns, insn);
2447 /* Store this memory reference where
2448 we found the bit field reference. */
2450 if (destflag)
2452 validate_change (insn, &SET_DEST (body), memref, 1);
2453 if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2455 rtx src = SET_SRC (body);
2456 while (GET_CODE (src) == SUBREG
2457 && SUBREG_WORD (src) == 0)
2458 src = SUBREG_REG (src);
2459 if (GET_MODE (src) != GET_MODE (memref))
2460 src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2461 validate_change (insn, &SET_SRC (body), src, 1);
2463 else if (GET_MODE (SET_SRC (body)) != VOIDmode
2464 && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2465 /* This shouldn't happen because anything that didn't have
2466 one of these modes should have got converted explicitly
2467 and then referenced through a subreg.
2468 This is so because the original bit-field was
2469 handled by agg_mode and so its tree structure had
2470 the same mode that memref now has. */
2471 abort ();
2473 else
2475 rtx dest = SET_DEST (body);
2477 while (GET_CODE (dest) == SUBREG
2478 && SUBREG_WORD (dest) == 0
2479 && (GET_MODE_CLASS (GET_MODE (dest))
2480 == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest)))))
2481 dest = SUBREG_REG (dest);
2483 validate_change (insn, &SET_DEST (body), dest, 1);
2485 if (GET_MODE (dest) == GET_MODE (memref))
2486 validate_change (insn, &SET_SRC (body), memref, 1);
2487 else
2489 /* Convert the mem ref to the destination mode. */
2490 rtx newreg = gen_reg_rtx (GET_MODE (dest));
2492 start_sequence ();
2493 convert_move (newreg, memref,
2494 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2495 seq = get_insns ();
2496 end_sequence ();
2498 validate_change (insn, &SET_SRC (body), newreg, 1);
2502 /* See if we can convert this extraction or insertion into
2503 a simple move insn. We might not be able to do so if this
2504 was, for example, part of a PARALLEL.
2506 If we succeed, write out any needed conversions. If we fail,
2507 it is hard to guess why we failed, so don't do anything
2508 special; just let the optimization be suppressed. */
2510 if (apply_change_group () && seq)
2511 emit_insns_before (seq, insn);
2516 /* These routines are responsible for converting virtual register references
2517 to the actual hard register references once RTL generation is complete.
2519 The following four variables are used for communication between the
2520 routines. They contain the offsets of the virtual registers from their
2521 respective hard registers. */
2523 static int in_arg_offset;
2524 static int var_offset;
2525 static int dynamic_offset;
2526 static int out_arg_offset;
2528 /* In most machines, the stack pointer register is equivalent to the bottom
2529 of the stack. */
2531 #ifndef STACK_POINTER_OFFSET
2532 #define STACK_POINTER_OFFSET 0
2533 #endif
2535 /* If not defined, pick an appropriate default for the offset of dynamically
2536 allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2537 REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE. */
2539 #ifndef STACK_DYNAMIC_OFFSET
2541 #ifdef ACCUMULATE_OUTGOING_ARGS
2542 /* The bottom of the stack points to the actual arguments. If
2543 REG_PARM_STACK_SPACE is defined, this includes the space for the register
2544 parameters. However, if OUTGOING_REG_PARM_STACK space is not defined,
2545 stack space for register parameters is not pushed by the caller, but
2546 rather part of the fixed stack areas and hence not included in
2547 `current_function_outgoing_args_size'. Nevertheless, we must allow
2548 for it when allocating stack dynamic objects. */
2550 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2551 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2552 (current_function_outgoing_args_size \
2553 + REG_PARM_STACK_SPACE (FNDECL) + (STACK_POINTER_OFFSET))
2555 #else
2556 #define STACK_DYNAMIC_OFFSET(FNDECL) \
2557 (current_function_outgoing_args_size + (STACK_POINTER_OFFSET))
2558 #endif
2560 #else
2561 #define STACK_DYNAMIC_OFFSET(FNDECL) STACK_POINTER_OFFSET
2562 #endif
2563 #endif
2565 /* Pass through the INSNS of function FNDECL and convert virtual register
2566 references to hard register references. */
2568 void
2569 instantiate_virtual_regs (fndecl, insns)
2570 tree fndecl;
2571 rtx insns;
2573 rtx insn;
2575 /* Compute the offsets to use for this function. */
2576 in_arg_offset = FIRST_PARM_OFFSET (fndecl);
2577 var_offset = STARTING_FRAME_OFFSET;
2578 dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
2579 out_arg_offset = STACK_POINTER_OFFSET;
2581 /* Scan all variables and parameters of this function. For each that is
2582 in memory, instantiate all virtual registers if the result is a valid
2583 address. If not, we do it later. That will handle most uses of virtual
2584 regs on many machines. */
2585 instantiate_decls (fndecl, 1);
2587 /* Initialize recognition, indicating that volatile is OK. */
2588 init_recog ();
2590 /* Scan through all the insns, instantiating every virtual register still
2591 present. */
2592 for (insn = insns; insn; insn = NEXT_INSN (insn))
2593 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2594 || GET_CODE (insn) == CALL_INSN)
2596 instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
2597 instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
2600 /* Now instantiate the remaining register equivalences for debugging info.
2601 These will not be valid addresses. */
2602 instantiate_decls (fndecl, 0);
2604 /* Indicate that, from now on, assign_stack_local should use
2605 frame_pointer_rtx. */
2606 virtuals_instantiated = 1;
2609 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
2610 all virtual registers in their DECL_RTL's.
2612 If VALID_ONLY, do this only if the resulting address is still valid.
2613 Otherwise, always do it. */
2615 static void
2616 instantiate_decls (fndecl, valid_only)
2617 tree fndecl;
2618 int valid_only;
2620 tree decl;
2622 if (DECL_SAVED_INSNS (fndecl))
2623 /* When compiling an inline function, the obstack used for
2624 rtl allocation is the maybepermanent_obstack. Calling
2625 `resume_temporary_allocation' switches us back to that
2626 obstack while we process this function's parameters. */
2627 resume_temporary_allocation ();
2629 /* Process all parameters of the function. */
2630 for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
2632 int size = int_size_in_bytes (TREE_TYPE (decl));
2633 instantiate_decl (DECL_RTL (decl), size, valid_only);
2635 /* If the parameter was promoted, then the incoming RTL mode may be
2636 larger than the declared type size. We must use the larger of
2637 the two sizes. */
2638 size = MAX (GET_MODE_SIZE (GET_MODE (DECL_INCOMING_RTL (decl))), size);
2639 instantiate_decl (DECL_INCOMING_RTL (decl), size, valid_only);
2642 /* Now process all variables defined in the function or its subblocks. */
2643 instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
2645 if (DECL_INLINE (fndecl) || DECL_DEFER_OUTPUT (fndecl))
2647 /* Save all rtl allocated for this function by raising the
2648 high-water mark on the maybepermanent_obstack. */
2649 preserve_data ();
2650 /* All further rtl allocation is now done in the current_obstack. */
2651 rtl_in_current_obstack ();
2655 /* Subroutine of instantiate_decls: Process all decls in the given
2656 BLOCK node and all its subblocks. */
2658 static void
2659 instantiate_decls_1 (let, valid_only)
2660 tree let;
2661 int valid_only;
2663 tree t;
2665 for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
2666 instantiate_decl (DECL_RTL (t), int_size_in_bytes (TREE_TYPE (t)),
2667 valid_only);
2669 /* Process all subblocks. */
2670 for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
2671 instantiate_decls_1 (t, valid_only);
2674 /* Subroutine of the preceding procedures: Given RTL representing a
2675 decl and the size of the object, do any instantiation required.
2677 If VALID_ONLY is non-zero, it means that the RTL should only be
2678 changed if the new address is valid. */
2680 static void
2681 instantiate_decl (x, size, valid_only)
2682 rtx x;
2683 int size;
2684 int valid_only;
2686 enum machine_mode mode;
2687 rtx addr;
2689 /* If this is not a MEM, no need to do anything. Similarly if the
2690 address is a constant or a register that is not a virtual register. */
2692 if (x == 0 || GET_CODE (x) != MEM)
2693 return;
2695 addr = XEXP (x, 0);
2696 if (CONSTANT_P (addr)
2697 || (GET_CODE (addr) == REG
2698 && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
2699 || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
2700 return;
2702 /* If we should only do this if the address is valid, copy the address.
2703 We need to do this so we can undo any changes that might make the
2704 address invalid. This copy is unfortunate, but probably can't be
2705 avoided. */
2707 if (valid_only)
2708 addr = copy_rtx (addr);
2710 instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
2712 if (valid_only)
2714 /* Now verify that the resulting address is valid for every integer or
2715 floating-point mode up to and including SIZE bytes long. We do this
2716 since the object might be accessed in any mode and frame addresses
2717 are shared. */
2719 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2720 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2721 mode = GET_MODE_WIDER_MODE (mode))
2722 if (! memory_address_p (mode, addr))
2723 return;
2725 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
2726 mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2727 mode = GET_MODE_WIDER_MODE (mode))
2728 if (! memory_address_p (mode, addr))
2729 return;
2732 /* Put back the address now that we have updated it and we either know
2733 it is valid or we don't care whether it is valid. */
2735 XEXP (x, 0) = addr;
2738 /* Given a pointer to a piece of rtx and an optional pointer to the
2739 containing object, instantiate any virtual registers present in it.
2741 If EXTRA_INSNS, we always do the replacement and generate
2742 any extra insns before OBJECT. If it zero, we do nothing if replacement
2743 is not valid.
2745 Return 1 if we either had nothing to do or if we were able to do the
2746 needed replacement. Return 0 otherwise; we only return zero if
2747 EXTRA_INSNS is zero.
2749 We first try some simple transformations to avoid the creation of extra
2750 pseudos. */
2752 static int
2753 instantiate_virtual_regs_1 (loc, object, extra_insns)
2754 rtx *loc;
2755 rtx object;
2756 int extra_insns;
2758 rtx x;
2759 RTX_CODE code;
2760 rtx new = 0;
2761 int offset;
2762 rtx temp;
2763 rtx seq;
2764 int i, j;
2765 char *fmt;
2767 /* Re-start here to avoid recursion in common cases. */
2768 restart:
2770 x = *loc;
2771 if (x == 0)
2772 return 1;
2774 code = GET_CODE (x);
2776 /* Check for some special cases. */
2777 switch (code)
2779 case CONST_INT:
2780 case CONST_DOUBLE:
2781 case CONST:
2782 case SYMBOL_REF:
2783 case CODE_LABEL:
2784 case PC:
2785 case CC0:
2786 case ASM_INPUT:
2787 case ADDR_VEC:
2788 case ADDR_DIFF_VEC:
2789 case RETURN:
2790 return 1;
2792 case SET:
2793 /* We are allowed to set the virtual registers. This means that
2794 that the actual register should receive the source minus the
2795 appropriate offset. This is used, for example, in the handling
2796 of non-local gotos. */
2797 if (SET_DEST (x) == virtual_incoming_args_rtx)
2798 new = arg_pointer_rtx, offset = - in_arg_offset;
2799 else if (SET_DEST (x) == virtual_stack_vars_rtx)
2800 new = frame_pointer_rtx, offset = - var_offset;
2801 else if (SET_DEST (x) == virtual_stack_dynamic_rtx)
2802 new = stack_pointer_rtx, offset = - dynamic_offset;
2803 else if (SET_DEST (x) == virtual_outgoing_args_rtx)
2804 new = stack_pointer_rtx, offset = - out_arg_offset;
2806 if (new)
2808 /* The only valid sources here are PLUS or REG. Just do
2809 the simplest possible thing to handle them. */
2810 if (GET_CODE (SET_SRC (x)) != REG
2811 && GET_CODE (SET_SRC (x)) != PLUS)
2812 abort ();
2814 start_sequence ();
2815 if (GET_CODE (SET_SRC (x)) != REG)
2816 temp = force_operand (SET_SRC (x), NULL_RTX);
2817 else
2818 temp = SET_SRC (x);
2819 temp = force_operand (plus_constant (temp, offset), NULL_RTX);
2820 seq = get_insns ();
2821 end_sequence ();
2823 emit_insns_before (seq, object);
2824 SET_DEST (x) = new;
2826 if (!validate_change (object, &SET_SRC (x), temp, 0)
2827 || ! extra_insns)
2828 abort ();
2830 return 1;
2833 instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
2834 loc = &SET_SRC (x);
2835 goto restart;
2837 case PLUS:
2838 /* Handle special case of virtual register plus constant. */
2839 if (CONSTANT_P (XEXP (x, 1)))
2841 rtx old, new_offset;
2843 /* Check for (plus (plus VIRT foo) (const_int)) first. */
2844 if (GET_CODE (XEXP (x, 0)) == PLUS)
2846 rtx inner = XEXP (XEXP (x, 0), 0);
2848 if (inner == virtual_incoming_args_rtx)
2849 new = arg_pointer_rtx, offset = in_arg_offset;
2850 else if (inner == virtual_stack_vars_rtx)
2851 new = frame_pointer_rtx, offset = var_offset;
2852 else if (inner == virtual_stack_dynamic_rtx)
2853 new = stack_pointer_rtx, offset = dynamic_offset;
2854 else if (inner == virtual_outgoing_args_rtx)
2855 new = stack_pointer_rtx, offset = out_arg_offset;
2856 else
2858 loc = &XEXP (x, 0);
2859 goto restart;
2862 instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
2863 extra_insns);
2864 new = gen_rtx (PLUS, Pmode, new, XEXP (XEXP (x, 0), 1));
2867 else if (XEXP (x, 0) == virtual_incoming_args_rtx)
2868 new = arg_pointer_rtx, offset = in_arg_offset;
2869 else if (XEXP (x, 0) == virtual_stack_vars_rtx)
2870 new = frame_pointer_rtx, offset = var_offset;
2871 else if (XEXP (x, 0) == virtual_stack_dynamic_rtx)
2872 new = stack_pointer_rtx, offset = dynamic_offset;
2873 else if (XEXP (x, 0) == virtual_outgoing_args_rtx)
2874 new = stack_pointer_rtx, offset = out_arg_offset;
2875 else
2877 /* We know the second operand is a constant. Unless the
2878 first operand is a REG (which has been already checked),
2879 it needs to be checked. */
2880 if (GET_CODE (XEXP (x, 0)) != REG)
2882 loc = &XEXP (x, 0);
2883 goto restart;
2885 return 1;
2888 new_offset = plus_constant (XEXP (x, 1), offset);
2890 /* If the new constant is zero, try to replace the sum with just
2891 the register. */
2892 if (new_offset == const0_rtx
2893 && validate_change (object, loc, new, 0))
2894 return 1;
2896 /* Next try to replace the register and new offset.
2897 There are two changes to validate here and we can't assume that
2898 in the case of old offset equals new just changing the register
2899 will yield a valid insn. In the interests of a little efficiency,
2900 however, we only call validate change once (we don't queue up the
2901 changes and then call apply_change_group). */
2903 old = XEXP (x, 0);
2904 if (offset == 0
2905 ? ! validate_change (object, &XEXP (x, 0), new, 0)
2906 : (XEXP (x, 0) = new,
2907 ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
2909 if (! extra_insns)
2911 XEXP (x, 0) = old;
2912 return 0;
2915 /* Otherwise copy the new constant into a register and replace
2916 constant with that register. */
2917 temp = gen_reg_rtx (Pmode);
2918 XEXP (x, 0) = new;
2919 if (validate_change (object, &XEXP (x, 1), temp, 0))
2920 emit_insn_before (gen_move_insn (temp, new_offset), object);
2921 else
2923 /* If that didn't work, replace this expression with a
2924 register containing the sum. */
2926 XEXP (x, 0) = old;
2927 new = gen_rtx (PLUS, Pmode, new, new_offset);
2929 start_sequence ();
2930 temp = force_operand (new, NULL_RTX);
2931 seq = get_insns ();
2932 end_sequence ();
2934 emit_insns_before (seq, object);
2935 if (! validate_change (object, loc, temp, 0)
2936 && ! validate_replace_rtx (x, temp, object))
2937 abort ();
2941 return 1;
2944 /* Fall through to generic two-operand expression case. */
2945 case EXPR_LIST:
2946 case CALL:
2947 case COMPARE:
2948 case MINUS:
2949 case MULT:
2950 case DIV: case UDIV:
2951 case MOD: case UMOD:
2952 case AND: case IOR: case XOR:
2953 case ROTATERT: case ROTATE:
2954 case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2955 case NE: case EQ:
2956 case GE: case GT: case GEU: case GTU:
2957 case LE: case LT: case LEU: case LTU:
2958 if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
2959 instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
2960 loc = &XEXP (x, 0);
2961 goto restart;
2963 case MEM:
2964 /* Most cases of MEM that convert to valid addresses have already been
2965 handled by our scan of decls. The only special handling we
2966 need here is to make a copy of the rtx to ensure it isn't being
2967 shared if we have to change it to a pseudo.
2969 If the rtx is a simple reference to an address via a virtual register,
2970 it can potentially be shared. In such cases, first try to make it
2971 a valid address, which can also be shared. Otherwise, copy it and
2972 proceed normally.
2974 First check for common cases that need no processing. These are
2975 usually due to instantiation already being done on a previous instance
2976 of a shared rtx. */
2978 temp = XEXP (x, 0);
2979 if (CONSTANT_ADDRESS_P (temp)
2980 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2981 || temp == arg_pointer_rtx
2982 #endif
2983 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2984 || temp == hard_frame_pointer_rtx
2985 #endif
2986 || temp == frame_pointer_rtx)
2987 return 1;
2989 if (GET_CODE (temp) == PLUS
2990 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
2991 && (XEXP (temp, 0) == frame_pointer_rtx
2992 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2993 || XEXP (temp, 0) == hard_frame_pointer_rtx
2994 #endif
2995 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2996 || XEXP (temp, 0) == arg_pointer_rtx
2997 #endif
2999 return 1;
3001 if (temp == virtual_stack_vars_rtx
3002 || temp == virtual_incoming_args_rtx
3003 || (GET_CODE (temp) == PLUS
3004 && CONSTANT_ADDRESS_P (XEXP (temp, 1))
3005 && (XEXP (temp, 0) == virtual_stack_vars_rtx
3006 || XEXP (temp, 0) == virtual_incoming_args_rtx)))
3008 /* This MEM may be shared. If the substitution can be done without
3009 the need to generate new pseudos, we want to do it in place
3010 so all copies of the shared rtx benefit. The call below will
3011 only make substitutions if the resulting address is still
3012 valid.
3014 Note that we cannot pass X as the object in the recursive call
3015 since the insn being processed may not allow all valid
3016 addresses. However, if we were not passed on object, we can
3017 only modify X without copying it if X will have a valid
3018 address.
3020 ??? Also note that this can still lose if OBJECT is an insn that
3021 has less restrictions on an address that some other insn.
3022 In that case, we will modify the shared address. This case
3023 doesn't seem very likely, though. One case where this could
3024 happen is in the case of a USE or CLOBBER reference, but we
3025 take care of that below. */
3027 if (instantiate_virtual_regs_1 (&XEXP (x, 0),
3028 object ? object : x, 0))
3029 return 1;
3031 /* Otherwise make a copy and process that copy. We copy the entire
3032 RTL expression since it might be a PLUS which could also be
3033 shared. */
3034 *loc = x = copy_rtx (x);
3037 /* Fall through to generic unary operation case. */
3038 case SUBREG:
3039 case STRICT_LOW_PART:
3040 case NEG: case NOT:
3041 case PRE_DEC: case PRE_INC: case POST_DEC: case POST_INC:
3042 case SIGN_EXTEND: case ZERO_EXTEND:
3043 case TRUNCATE: case FLOAT_EXTEND: case FLOAT_TRUNCATE:
3044 case FLOAT: case FIX:
3045 case UNSIGNED_FIX: case UNSIGNED_FLOAT:
3046 case ABS:
3047 case SQRT:
3048 case FFS:
3049 /* These case either have just one operand or we know that we need not
3050 check the rest of the operands. */
3051 loc = &XEXP (x, 0);
3052 goto restart;
3054 case USE:
3055 case CLOBBER:
3056 /* If the operand is a MEM, see if the change is a valid MEM. If not,
3057 go ahead and make the invalid one, but do it to a copy. For a REG,
3058 just make the recursive call, since there's no chance of a problem. */
3060 if ((GET_CODE (XEXP (x, 0)) == MEM
3061 && instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 0), XEXP (x, 0),
3063 || (GET_CODE (XEXP (x, 0)) == REG
3064 && instantiate_virtual_regs_1 (&XEXP (x, 0), object, 0)))
3065 return 1;
3067 XEXP (x, 0) = copy_rtx (XEXP (x, 0));
3068 loc = &XEXP (x, 0);
3069 goto restart;
3071 case REG:
3072 /* Try to replace with a PLUS. If that doesn't work, compute the sum
3073 in front of this insn and substitute the temporary. */
3074 if (x == virtual_incoming_args_rtx)
3075 new = arg_pointer_rtx, offset = in_arg_offset;
3076 else if (x == virtual_stack_vars_rtx)
3077 new = frame_pointer_rtx, offset = var_offset;
3078 else if (x == virtual_stack_dynamic_rtx)
3079 new = stack_pointer_rtx, offset = dynamic_offset;
3080 else if (x == virtual_outgoing_args_rtx)
3081 new = stack_pointer_rtx, offset = out_arg_offset;
3083 if (new)
3085 temp = plus_constant (new, offset);
3086 if (!validate_change (object, loc, temp, 0))
3088 if (! extra_insns)
3089 return 0;
3091 start_sequence ();
3092 temp = force_operand (temp, NULL_RTX);
3093 seq = get_insns ();
3094 end_sequence ();
3096 emit_insns_before (seq, object);
3097 if (! validate_change (object, loc, temp, 0)
3098 && ! validate_replace_rtx (x, temp, object))
3099 abort ();
3103 return 1;
3106 /* Scan all subexpressions. */
3107 fmt = GET_RTX_FORMAT (code);
3108 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3109 if (*fmt == 'e')
3111 if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
3112 return 0;
3114 else if (*fmt == 'E')
3115 for (j = 0; j < XVECLEN (x, i); j++)
3116 if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
3117 extra_insns))
3118 return 0;
3120 return 1;
3123 /* Optimization: assuming this function does not receive nonlocal gotos,
3124 delete the handlers for such, as well as the insns to establish
3125 and disestablish them. */
3127 static void
3128 delete_handlers ()
3130 rtx insn;
3131 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3133 /* Delete the handler by turning off the flag that would
3134 prevent jump_optimize from deleting it.
3135 Also permit deletion of the nonlocal labels themselves
3136 if nothing local refers to them. */
3137 if (GET_CODE (insn) == CODE_LABEL)
3139 tree t, last_t;
3141 LABEL_PRESERVE_P (insn) = 0;
3143 /* Remove it from the nonlocal_label list, to avoid confusing
3144 flow. */
3145 for (t = nonlocal_labels, last_t = 0; t;
3146 last_t = t, t = TREE_CHAIN (t))
3147 if (DECL_RTL (TREE_VALUE (t)) == insn)
3148 break;
3149 if (t)
3151 if (! last_t)
3152 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
3153 else
3154 TREE_CHAIN (last_t) = TREE_CHAIN (t);
3157 if (GET_CODE (insn) == INSN
3158 && ((nonlocal_goto_handler_slot != 0
3159 && reg_mentioned_p (nonlocal_goto_handler_slot, PATTERN (insn)))
3160 || (nonlocal_goto_stack_level != 0
3161 && reg_mentioned_p (nonlocal_goto_stack_level,
3162 PATTERN (insn)))))
3163 delete_insn (insn);
3167 /* Return a list (chain of EXPR_LIST nodes) for the nonlocal labels
3168 of the current function. */
3171 nonlocal_label_rtx_list ()
3173 tree t;
3174 rtx x = 0;
3176 for (t = nonlocal_labels; t; t = TREE_CHAIN (t))
3177 x = gen_rtx (EXPR_LIST, VOIDmode, label_rtx (TREE_VALUE (t)), x);
3179 return x;
3182 /* Output a USE for any register use in RTL.
3183 This is used with -noreg to mark the extent of lifespan
3184 of any registers used in a user-visible variable's DECL_RTL. */
3186 void
3187 use_variable (rtl)
3188 rtx rtl;
3190 if (GET_CODE (rtl) == REG)
3191 /* This is a register variable. */
3192 emit_insn (gen_rtx (USE, VOIDmode, rtl));
3193 else if (GET_CODE (rtl) == MEM
3194 && GET_CODE (XEXP (rtl, 0)) == REG
3195 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3196 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3197 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3198 /* This is a variable-sized structure. */
3199 emit_insn (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)));
3202 /* Like use_variable except that it outputs the USEs after INSN
3203 instead of at the end of the insn-chain. */
3205 void
3206 use_variable_after (rtl, insn)
3207 rtx rtl, insn;
3209 if (GET_CODE (rtl) == REG)
3210 /* This is a register variable. */
3211 emit_insn_after (gen_rtx (USE, VOIDmode, rtl), insn);
3212 else if (GET_CODE (rtl) == MEM
3213 && GET_CODE (XEXP (rtl, 0)) == REG
3214 && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3215 || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3216 && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3217 /* This is a variable-sized structure. */
3218 emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)), insn);
3222 max_parm_reg_num ()
3224 return max_parm_reg;
3227 /* Return the first insn following those generated by `assign_parms'. */
3230 get_first_nonparm_insn ()
3232 if (last_parm_insn)
3233 return NEXT_INSN (last_parm_insn);
3234 return get_insns ();
3237 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
3238 Crash if there is none. */
3241 get_first_block_beg ()
3243 register rtx searcher;
3244 register rtx insn = get_first_nonparm_insn ();
3246 for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
3247 if (GET_CODE (searcher) == NOTE
3248 && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
3249 return searcher;
3251 abort (); /* Invalid call to this function. (See comments above.) */
3252 return NULL_RTX;
3255 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
3256 This means a type for which function calls must pass an address to the
3257 function or get an address back from the function.
3258 EXP may be a type node or an expression (whose type is tested). */
3261 aggregate_value_p (exp)
3262 tree exp;
3264 int i, regno, nregs;
3265 rtx reg;
3266 tree type;
3267 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 't')
3268 type = exp;
3269 else
3270 type = TREE_TYPE (exp);
3272 if (RETURN_IN_MEMORY (type))
3273 return 1;
3274 /* Types that are TREE_ADDRESSABLE must be contructed in memory,
3275 and thus can't be returned in registers. */
3276 if (TREE_ADDRESSABLE (type))
3277 return 1;
3278 if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
3279 return 1;
3280 /* Make sure we have suitable call-clobbered regs to return
3281 the value in; if not, we must return it in memory. */
3282 reg = hard_function_value (type, 0);
3284 /* If we have something other than a REG (e.g. a PARALLEL), then assume
3285 it is OK. */
3286 if (GET_CODE (reg) != REG)
3287 return 0;
3289 regno = REGNO (reg);
3290 nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
3291 for (i = 0; i < nregs; i++)
3292 if (! call_used_regs[regno + i])
3293 return 1;
3294 return 0;
3297 /* Assign RTL expressions to the function's parameters.
3298 This may involve copying them into registers and using
3299 those registers as the RTL for them.
3301 If SECOND_TIME is non-zero it means that this function is being
3302 called a second time. This is done by integrate.c when a function's
3303 compilation is deferred. We need to come back here in case the
3304 FUNCTION_ARG macro computes items needed for the rest of the compilation
3305 (such as changing which registers are fixed or caller-saved). But suppress
3306 writing any insns or setting DECL_RTL of anything in this case. */
3308 void
3309 assign_parms (fndecl, second_time)
3310 tree fndecl;
3311 int second_time;
3313 register tree parm;
3314 register rtx entry_parm = 0;
3315 register rtx stack_parm = 0;
3316 CUMULATIVE_ARGS args_so_far;
3317 enum machine_mode promoted_mode, passed_mode;
3318 enum machine_mode nominal_mode, promoted_nominal_mode;
3319 int unsignedp;
3320 /* Total space needed so far for args on the stack,
3321 given as a constant and a tree-expression. */
3322 struct args_size stack_args_size;
3323 tree fntype = TREE_TYPE (fndecl);
3324 tree fnargs = DECL_ARGUMENTS (fndecl);
3325 /* This is used for the arg pointer when referring to stack args. */
3326 rtx internal_arg_pointer;
3327 /* This is a dummy PARM_DECL that we used for the function result if
3328 the function returns a structure. */
3329 tree function_result_decl = 0;
3330 int nparmregs = list_length (fnargs) + LAST_VIRTUAL_REGISTER + 1;
3331 int varargs_setup = 0;
3332 rtx conversion_insns = 0;
3334 /* Nonzero if the last arg is named `__builtin_va_alist',
3335 which is used on some machines for old-fashioned non-ANSI varargs.h;
3336 this should be stuck onto the stack as if it had arrived there. */
3337 int hide_last_arg
3338 = (current_function_varargs
3339 && fnargs
3340 && (parm = tree_last (fnargs)) != 0
3341 && DECL_NAME (parm)
3342 && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
3343 "__builtin_va_alist")));
3345 /* Nonzero if function takes extra anonymous args.
3346 This means the last named arg must be on the stack
3347 right before the anonymous ones. */
3348 int stdarg
3349 = (TYPE_ARG_TYPES (fntype) != 0
3350 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3351 != void_type_node));
3353 current_function_stdarg = stdarg;
3355 /* If the reg that the virtual arg pointer will be translated into is
3356 not a fixed reg or is the stack pointer, make a copy of the virtual
3357 arg pointer, and address parms via the copy. The frame pointer is
3358 considered fixed even though it is not marked as such.
3360 The second time through, simply use ap to avoid generating rtx. */
3362 if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
3363 || ! (fixed_regs[ARG_POINTER_REGNUM]
3364 || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM))
3365 && ! second_time)
3366 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
3367 else
3368 internal_arg_pointer = virtual_incoming_args_rtx;
3369 current_function_internal_arg_pointer = internal_arg_pointer;
3371 stack_args_size.constant = 0;
3372 stack_args_size.var = 0;
3374 /* If struct value address is treated as the first argument, make it so. */
3375 if (aggregate_value_p (DECL_RESULT (fndecl))
3376 && ! current_function_returns_pcc_struct
3377 && struct_value_incoming_rtx == 0)
3379 tree type = build_pointer_type (TREE_TYPE (fntype));
3381 function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
3383 DECL_ARG_TYPE (function_result_decl) = type;
3384 TREE_CHAIN (function_result_decl) = fnargs;
3385 fnargs = function_result_decl;
3388 parm_reg_stack_loc = (rtx *) oballoc (nparmregs * sizeof (rtx));
3389 bzero ((char *) parm_reg_stack_loc, nparmregs * sizeof (rtx));
3391 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
3392 INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
3393 #else
3394 INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX, 0);
3395 #endif
3397 /* We haven't yet found an argument that we must push and pretend the
3398 caller did. */
3399 current_function_pretend_args_size = 0;
3401 for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
3403 int aggregate = AGGREGATE_TYPE_P (TREE_TYPE (parm));
3404 struct args_size stack_offset;
3405 struct args_size arg_size;
3406 int passed_pointer = 0;
3407 int did_conversion = 0;
3408 tree passed_type = DECL_ARG_TYPE (parm);
3409 tree nominal_type = TREE_TYPE (parm);
3411 /* Set LAST_NAMED if this is last named arg before some
3412 anonymous args. We treat it as if it were anonymous too. */
3413 int last_named = ((TREE_CHAIN (parm) == 0
3414 || DECL_NAME (TREE_CHAIN (parm)) == 0)
3415 && (stdarg || current_function_varargs));
3417 if (TREE_TYPE (parm) == error_mark_node
3418 /* This can happen after weird syntax errors
3419 or if an enum type is defined among the parms. */
3420 || TREE_CODE (parm) != PARM_DECL
3421 || passed_type == NULL)
3423 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = gen_rtx (MEM, BLKmode,
3424 const0_rtx);
3425 TREE_USED (parm) = 1;
3426 continue;
3429 /* For varargs.h function, save info about regs and stack space
3430 used by the individual args, not including the va_alist arg. */
3431 if (hide_last_arg && last_named)
3432 current_function_args_info = args_so_far;
3434 /* Find mode of arg as it is passed, and mode of arg
3435 as it should be during execution of this function. */
3436 passed_mode = TYPE_MODE (passed_type);
3437 nominal_mode = TYPE_MODE (nominal_type);
3439 /* If the parm's mode is VOID, its value doesn't matter,
3440 and avoid the usual things like emit_move_insn that could crash. */
3441 if (nominal_mode == VOIDmode)
3443 DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = const0_rtx;
3444 continue;
3447 /* If the parm is to be passed as a transparent union, use the
3448 type of the first field for the tests below. We have already
3449 verified that the modes are the same. */
3450 if (DECL_TRANSPARENT_UNION (parm)
3451 || TYPE_TRANSPARENT_UNION (passed_type))
3452 passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
3454 /* See if this arg was passed by invisible reference. It is if
3455 it is an object whose size depends on the contents of the
3456 object itself or if the machine requires these objects be passed
3457 that way. */
3459 if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
3460 && contains_placeholder_p (TYPE_SIZE (passed_type)))
3461 || TREE_ADDRESSABLE (passed_type)
3462 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3463 || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
3464 passed_type, ! last_named)
3465 #endif
3468 passed_type = nominal_type = build_pointer_type (passed_type);
3469 passed_pointer = 1;
3470 passed_mode = nominal_mode = Pmode;
3473 promoted_mode = passed_mode;
3475 #ifdef PROMOTE_FUNCTION_ARGS
3476 /* Compute the mode in which the arg is actually extended to. */
3477 promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
3478 #endif
3480 /* Let machine desc say which reg (if any) the parm arrives in.
3481 0 means it arrives on the stack. */
3482 #ifdef FUNCTION_INCOMING_ARG
3483 entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3484 passed_type, ! last_named);
3485 #else
3486 entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
3487 passed_type, ! last_named);
3488 #endif
3490 if (entry_parm == 0)
3491 promoted_mode = passed_mode;
3493 #ifdef SETUP_INCOMING_VARARGS
3494 /* If this is the last named parameter, do any required setup for
3495 varargs or stdargs. We need to know about the case of this being an
3496 addressable type, in which case we skip the registers it
3497 would have arrived in.
3499 For stdargs, LAST_NAMED will be set for two parameters, the one that
3500 is actually the last named, and the dummy parameter. We only
3501 want to do this action once.
3503 Also, indicate when RTL generation is to be suppressed. */
3504 if (last_named && !varargs_setup)
3506 SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
3507 current_function_pretend_args_size,
3508 second_time);
3509 varargs_setup = 1;
3511 #endif
3513 /* Determine parm's home in the stack,
3514 in case it arrives in the stack or we should pretend it did.
3516 Compute the stack position and rtx where the argument arrives
3517 and its size.
3519 There is one complexity here: If this was a parameter that would
3520 have been passed in registers, but wasn't only because it is
3521 __builtin_va_alist, we want locate_and_pad_parm to treat it as if
3522 it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
3523 In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
3524 0 as it was the previous time. */
3526 locate_and_pad_parm (promoted_mode, passed_type,
3527 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3529 #else
3530 #ifdef FUNCTION_INCOMING_ARG
3531 FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3532 passed_type,
3533 (! last_named
3534 || varargs_setup)) != 0,
3535 #else
3536 FUNCTION_ARG (args_so_far, promoted_mode,
3537 passed_type,
3538 ! last_named || varargs_setup) != 0,
3539 #endif
3540 #endif
3541 fndecl, &stack_args_size, &stack_offset, &arg_size);
3543 if (! second_time)
3545 rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
3547 if (offset_rtx == const0_rtx)
3548 stack_parm = gen_rtx (MEM, promoted_mode, internal_arg_pointer);
3549 else
3550 stack_parm = gen_rtx (MEM, promoted_mode,
3551 gen_rtx (PLUS, Pmode,
3552 internal_arg_pointer, offset_rtx));
3554 /* If this is a memory ref that contains aggregate components,
3555 mark it as such for cse and loop optimize. Likewise if it
3556 is readonly. */
3557 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3558 RTX_UNCHANGING_P (stack_parm) = TREE_READONLY (parm);
3561 /* If this parameter was passed both in registers and in the stack,
3562 use the copy on the stack. */
3563 if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
3564 entry_parm = 0;
3566 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3567 /* If this parm was passed part in regs and part in memory,
3568 pretend it arrived entirely in memory
3569 by pushing the register-part onto the stack.
3571 In the special case of a DImode or DFmode that is split,
3572 we could put it together in a pseudoreg directly,
3573 but for now that's not worth bothering with. */
3575 if (entry_parm)
3577 int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
3578 passed_type, ! last_named);
3580 if (nregs > 0)
3582 current_function_pretend_args_size
3583 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
3584 / (PARM_BOUNDARY / BITS_PER_UNIT)
3585 * (PARM_BOUNDARY / BITS_PER_UNIT));
3587 if (! second_time)
3589 /* Handle calls that pass values in multiple non-contiguous
3590 locations. The Irix 6 ABI has examples of this. */
3591 if (GET_CODE (entry_parm) == PARALLEL)
3592 emit_group_store (validize_mem (stack_parm),
3593 entry_parm);
3594 else
3595 move_block_from_reg (REGNO (entry_parm),
3596 validize_mem (stack_parm), nregs,
3597 int_size_in_bytes (TREE_TYPE (parm)));
3599 entry_parm = stack_parm;
3602 #endif
3604 /* If we didn't decide this parm came in a register,
3605 by default it came on the stack. */
3606 if (entry_parm == 0)
3607 entry_parm = stack_parm;
3609 /* Record permanently how this parm was passed. */
3610 if (! second_time)
3611 DECL_INCOMING_RTL (parm) = entry_parm;
3613 /* If there is actually space on the stack for this parm,
3614 count it in stack_args_size; otherwise set stack_parm to 0
3615 to indicate there is no preallocated stack slot for the parm. */
3617 if (entry_parm == stack_parm
3618 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
3619 /* On some machines, even if a parm value arrives in a register
3620 there is still an (uninitialized) stack slot allocated for it.
3622 ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
3623 whether this parameter already has a stack slot allocated,
3624 because an arg block exists only if current_function_args_size
3625 is larger than some threshold, and we haven't calculated that
3626 yet. So, for now, we just assume that stack slots never exist
3627 in this case. */
3628 || REG_PARM_STACK_SPACE (fndecl) > 0
3629 #endif
3632 stack_args_size.constant += arg_size.constant;
3633 if (arg_size.var)
3634 ADD_PARM_SIZE (stack_args_size, arg_size.var);
3636 else
3637 /* No stack slot was pushed for this parm. */
3638 stack_parm = 0;
3640 /* Update info on where next arg arrives in registers. */
3642 FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
3643 passed_type, ! last_named);
3645 /* If this is our second time through, we are done with this parm. */
3646 if (second_time)
3647 continue;
3649 /* If we can't trust the parm stack slot to be aligned enough
3650 for its ultimate type, don't use that slot after entry.
3651 We'll make another stack slot, if we need one. */
3653 int thisparm_boundary
3654 = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
3656 if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
3657 stack_parm = 0;
3660 /* If parm was passed in memory, and we need to convert it on entry,
3661 don't store it back in that same slot. */
3662 if (entry_parm != 0
3663 && nominal_mode != BLKmode && nominal_mode != passed_mode)
3664 stack_parm = 0;
3666 #if 0
3667 /* Now adjust STACK_PARM to the mode and precise location
3668 where this parameter should live during execution,
3669 if we discover that it must live in the stack during execution.
3670 To make debuggers happier on big-endian machines, we store
3671 the value in the last bytes of the space available. */
3673 if (nominal_mode != BLKmode && nominal_mode != passed_mode
3674 && stack_parm != 0)
3676 rtx offset_rtx;
3678 if (BYTES_BIG_ENDIAN
3679 && GET_MODE_SIZE (nominal_mode) < UNITS_PER_WORD)
3680 stack_offset.constant += (GET_MODE_SIZE (passed_mode)
3681 - GET_MODE_SIZE (nominal_mode));
3683 offset_rtx = ARGS_SIZE_RTX (stack_offset);
3684 if (offset_rtx == const0_rtx)
3685 stack_parm = gen_rtx (MEM, nominal_mode, internal_arg_pointer);
3686 else
3687 stack_parm = gen_rtx (MEM, nominal_mode,
3688 gen_rtx (PLUS, Pmode,
3689 internal_arg_pointer, offset_rtx));
3691 /* If this is a memory ref that contains aggregate components,
3692 mark it as such for cse and loop optimize. */
3693 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3695 #endif /* 0 */
3697 #ifdef STACK_REGS
3698 /* We need this "use" info, because the gcc-register->stack-register
3699 converter in reg-stack.c needs to know which registers are active
3700 at the start of the function call. The actual parameter loading
3701 instructions are not always available then anymore, since they might
3702 have been optimised away. */
3704 if (GET_CODE (entry_parm) == REG && !(hide_last_arg && last_named))
3705 emit_insn (gen_rtx (USE, GET_MODE (entry_parm), entry_parm));
3706 #endif
3708 /* ENTRY_PARM is an RTX for the parameter as it arrives,
3709 in the mode in which it arrives.
3710 STACK_PARM is an RTX for a stack slot where the parameter can live
3711 during the function (in case we want to put it there).
3712 STACK_PARM is 0 if no stack slot was pushed for it.
3714 Now output code if necessary to convert ENTRY_PARM to
3715 the type in which this function declares it,
3716 and store that result in an appropriate place,
3717 which may be a pseudo reg, may be STACK_PARM,
3718 or may be a local stack slot if STACK_PARM is 0.
3720 Set DECL_RTL to that place. */
3722 if (nominal_mode == BLKmode || GET_CODE (entry_parm) == PARALLEL)
3724 /* If a BLKmode arrives in registers, copy it to a stack slot.
3725 Handle calls that pass values in multiple non-contiguous
3726 locations. The Irix 6 ABI has examples of this. */
3727 if (GET_CODE (entry_parm) == REG
3728 || GET_CODE (entry_parm) == PARALLEL)
3730 int size_stored
3731 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
3732 UNITS_PER_WORD);
3734 /* Note that we will be storing an integral number of words.
3735 So we have to be careful to ensure that we allocate an
3736 integral number of words. We do this below in the
3737 assign_stack_local if space was not allocated in the argument
3738 list. If it was, this will not work if PARM_BOUNDARY is not
3739 a multiple of BITS_PER_WORD. It isn't clear how to fix this
3740 if it becomes a problem. */
3742 if (stack_parm == 0)
3744 stack_parm
3745 = assign_stack_local (GET_MODE (entry_parm),
3746 size_stored, 0);
3748 /* If this is a memory ref that contains aggregate
3749 components, mark it as such for cse and loop optimize. */
3750 MEM_IN_STRUCT_P (stack_parm) = aggregate;
3753 else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
3754 abort ();
3756 if (TREE_READONLY (parm))
3757 RTX_UNCHANGING_P (stack_parm) = 1;
3759 /* Handle calls that pass values in multiple non-contiguous
3760 locations. The Irix 6 ABI has examples of this. */
3761 if (GET_CODE (entry_parm) == PARALLEL)
3762 emit_group_store (validize_mem (stack_parm), entry_parm);
3763 else
3764 move_block_from_reg (REGNO (entry_parm),
3765 validize_mem (stack_parm),
3766 size_stored / UNITS_PER_WORD,
3767 int_size_in_bytes (TREE_TYPE (parm)));
3769 DECL_RTL (parm) = stack_parm;
3771 else if (! ((obey_regdecls && ! DECL_REGISTER (parm)
3772 && ! DECL_INLINE (fndecl))
3773 /* layout_decl may set this. */
3774 || TREE_ADDRESSABLE (parm)
3775 || TREE_SIDE_EFFECTS (parm)
3776 /* If -ffloat-store specified, don't put explicit
3777 float variables into registers. */
3778 || (flag_float_store
3779 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
3780 /* Always assign pseudo to structure return or item passed
3781 by invisible reference. */
3782 || passed_pointer || parm == function_result_decl)
3784 /* Store the parm in a pseudoregister during the function, but we
3785 may need to do it in a wider mode. */
3787 register rtx parmreg;
3788 int regno, regnoi, regnor;
3790 unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
3792 promoted_nominal_mode
3793 = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
3795 parmreg = gen_reg_rtx (promoted_nominal_mode);
3796 mark_user_reg (parmreg);
3798 /* If this was an item that we received a pointer to, set DECL_RTL
3799 appropriately. */
3800 if (passed_pointer)
3802 DECL_RTL (parm)
3803 = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
3804 MEM_IN_STRUCT_P (DECL_RTL (parm)) = aggregate;
3806 else
3807 DECL_RTL (parm) = parmreg;
3809 /* Copy the value into the register. */
3810 if (nominal_mode != passed_mode
3811 || promoted_nominal_mode != promoted_mode)
3813 /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3814 mode, by the caller. We now have to convert it to
3815 NOMINAL_MODE, if different. However, PARMREG may be in
3816 a diffent mode than NOMINAL_MODE if it is being stored
3817 promoted.
3819 If ENTRY_PARM is a hard register, it might be in a register
3820 not valid for operating in its mode (e.g., an odd-numbered
3821 register for a DFmode). In that case, moves are the only
3822 thing valid, so we can't do a convert from there. This
3823 occurs when the calling sequence allow such misaligned
3824 usages.
3826 In addition, the conversion may involve a call, which could
3827 clobber parameters which haven't been copied to pseudo
3828 registers yet. Therefore, we must first copy the parm to
3829 a pseudo reg here, and save the conversion until after all
3830 parameters have been moved. */
3832 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
3834 emit_move_insn (tempreg, validize_mem (entry_parm));
3836 push_to_sequence (conversion_insns);
3837 tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
3839 expand_assignment (parm,
3840 make_tree (nominal_type, tempreg), 0, 0);
3841 conversion_insns = get_insns ();
3842 did_conversion = 1;
3843 end_sequence ();
3845 else
3846 emit_move_insn (parmreg, validize_mem (entry_parm));
3848 /* If we were passed a pointer but the actual value
3849 can safely live in a register, put it in one. */
3850 if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
3851 && ! ((obey_regdecls && ! DECL_REGISTER (parm)
3852 && ! DECL_INLINE (fndecl))
3853 /* layout_decl may set this. */
3854 || TREE_ADDRESSABLE (parm)
3855 || TREE_SIDE_EFFECTS (parm)
3856 /* If -ffloat-store specified, don't put explicit
3857 float variables into registers. */
3858 || (flag_float_store
3859 && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
3861 /* We can't use nominal_mode, because it will have been set to
3862 Pmode above. We must use the actual mode of the parm. */
3863 parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3864 mark_user_reg (parmreg);
3865 emit_move_insn (parmreg, DECL_RTL (parm));
3866 DECL_RTL (parm) = parmreg;
3867 /* STACK_PARM is the pointer, not the parm, and PARMREG is
3868 now the parm. */
3869 stack_parm = 0;
3871 #ifdef FUNCTION_ARG_CALLEE_COPIES
3872 /* If we are passed an arg by reference and it is our responsibility
3873 to make a copy, do it now.
3874 PASSED_TYPE and PASSED mode now refer to the pointer, not the
3875 original argument, so we must recreate them in the call to
3876 FUNCTION_ARG_CALLEE_COPIES. */
3877 /* ??? Later add code to handle the case that if the argument isn't
3878 modified, don't do the copy. */
3880 else if (passed_pointer
3881 && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
3882 TYPE_MODE (DECL_ARG_TYPE (parm)),
3883 DECL_ARG_TYPE (parm),
3884 ! last_named)
3885 && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
3887 rtx copy;
3888 tree type = DECL_ARG_TYPE (parm);
3890 /* This sequence may involve a library call perhaps clobbering
3891 registers that haven't been copied to pseudos yet. */
3893 push_to_sequence (conversion_insns);
3895 if (TYPE_SIZE (type) == 0
3896 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3897 /* This is a variable sized object. */
3898 copy = gen_rtx (MEM, BLKmode,
3899 allocate_dynamic_stack_space
3900 (expr_size (parm), NULL_RTX,
3901 TYPE_ALIGN (type)));
3902 else
3903 copy = assign_stack_temp (TYPE_MODE (type),
3904 int_size_in_bytes (type), 1);
3905 MEM_IN_STRUCT_P (copy) = AGGREGATE_TYPE_P (type);
3907 store_expr (parm, copy, 0);
3908 emit_move_insn (parmreg, XEXP (copy, 0));
3909 conversion_insns = get_insns ();
3910 did_conversion = 1;
3911 end_sequence ();
3913 #endif /* FUNCTION_ARG_CALLEE_COPIES */
3915 /* In any case, record the parm's desired stack location
3916 in case we later discover it must live in the stack.
3918 If it is a COMPLEX value, store the stack location for both
3919 halves. */
3921 if (GET_CODE (parmreg) == CONCAT)
3922 regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
3923 else
3924 regno = REGNO (parmreg);
3926 if (regno >= nparmregs)
3928 rtx *new;
3929 int old_nparmregs = nparmregs;
3931 nparmregs = regno + 5;
3932 new = (rtx *) oballoc (nparmregs * sizeof (rtx));
3933 bcopy ((char *) parm_reg_stack_loc, (char *) new,
3934 old_nparmregs * sizeof (rtx));
3935 bzero ((char *) (new + old_nparmregs),
3936 (nparmregs - old_nparmregs) * sizeof (rtx));
3937 parm_reg_stack_loc = new;
3940 if (GET_CODE (parmreg) == CONCAT)
3942 enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
3944 regnor = REGNO (gen_realpart (submode, parmreg));
3945 regnoi = REGNO (gen_imagpart (submode, parmreg));
3947 if (stack_parm != 0)
3949 parm_reg_stack_loc[regnor]
3950 = gen_realpart (submode, stack_parm);
3951 parm_reg_stack_loc[regnoi]
3952 = gen_imagpart (submode, stack_parm);
3954 else
3956 parm_reg_stack_loc[regnor] = 0;
3957 parm_reg_stack_loc[regnoi] = 0;
3960 else
3961 parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
3963 /* Mark the register as eliminable if we did no conversion
3964 and it was copied from memory at a fixed offset,
3965 and the arg pointer was not copied to a pseudo-reg.
3966 If the arg pointer is a pseudo reg or the offset formed
3967 an invalid address, such memory-equivalences
3968 as we make here would screw up life analysis for it. */
3969 if (nominal_mode == passed_mode
3970 && ! did_conversion
3971 && stack_parm != 0
3972 && GET_CODE (stack_parm) == MEM
3973 && stack_offset.var == 0
3974 && reg_mentioned_p (virtual_incoming_args_rtx,
3975 XEXP (stack_parm, 0)))
3977 rtx linsn = get_last_insn ();
3978 rtx sinsn, set;
3980 /* Mark complex types separately. */
3981 if (GET_CODE (parmreg) == CONCAT)
3982 /* Scan backwards for the set of the real and
3983 imaginary parts. */
3984 for (sinsn = linsn; sinsn != 0;
3985 sinsn = prev_nonnote_insn (sinsn))
3987 set = single_set (sinsn);
3988 if (set != 0
3989 && SET_DEST (set) == regno_reg_rtx [regnoi])
3990 REG_NOTES (sinsn)
3991 = gen_rtx (EXPR_LIST, REG_EQUIV,
3992 parm_reg_stack_loc[regnoi],
3993 REG_NOTES (sinsn));
3994 else if (set != 0
3995 && SET_DEST (set) == regno_reg_rtx [regnor])
3996 REG_NOTES (sinsn)
3997 = gen_rtx (EXPR_LIST, REG_EQUIV,
3998 parm_reg_stack_loc[regnor],
3999 REG_NOTES (sinsn));
4001 else if ((set = single_set (linsn)) != 0
4002 && SET_DEST (set) == parmreg)
4003 REG_NOTES (linsn)
4004 = gen_rtx (EXPR_LIST, REG_EQUIV,
4005 stack_parm, REG_NOTES (linsn));
4008 /* For pointer data type, suggest pointer register. */
4009 if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
4010 mark_reg_pointer (parmreg,
4011 (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm)))
4012 / BITS_PER_UNIT));
4014 else
4016 /* Value must be stored in the stack slot STACK_PARM
4017 during function execution. */
4019 if (promoted_mode != nominal_mode)
4021 /* Conversion is required. */
4022 rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
4024 emit_move_insn (tempreg, validize_mem (entry_parm));
4026 push_to_sequence (conversion_insns);
4027 entry_parm = convert_to_mode (nominal_mode, tempreg,
4028 TREE_UNSIGNED (TREE_TYPE (parm)));
4029 conversion_insns = get_insns ();
4030 did_conversion = 1;
4031 end_sequence ();
4034 if (entry_parm != stack_parm)
4036 if (stack_parm == 0)
4038 stack_parm
4039 = assign_stack_local (GET_MODE (entry_parm),
4040 GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
4041 /* If this is a memory ref that contains aggregate components,
4042 mark it as such for cse and loop optimize. */
4043 MEM_IN_STRUCT_P (stack_parm) = aggregate;
4046 if (promoted_mode != nominal_mode)
4048 push_to_sequence (conversion_insns);
4049 emit_move_insn (validize_mem (stack_parm),
4050 validize_mem (entry_parm));
4051 conversion_insns = get_insns ();
4052 end_sequence ();
4054 else
4055 emit_move_insn (validize_mem (stack_parm),
4056 validize_mem (entry_parm));
4059 DECL_RTL (parm) = stack_parm;
4062 /* If this "parameter" was the place where we are receiving the
4063 function's incoming structure pointer, set up the result. */
4064 if (parm == function_result_decl)
4066 tree result = DECL_RESULT (fndecl);
4067 tree restype = TREE_TYPE (result);
4069 DECL_RTL (result)
4070 = gen_rtx (MEM, DECL_MODE (result), DECL_RTL (parm));
4072 MEM_IN_STRUCT_P (DECL_RTL (result)) = AGGREGATE_TYPE_P (restype);
4075 if (TREE_THIS_VOLATILE (parm))
4076 MEM_VOLATILE_P (DECL_RTL (parm)) = 1;
4077 if (TREE_READONLY (parm))
4078 RTX_UNCHANGING_P (DECL_RTL (parm)) = 1;
4081 /* Output all parameter conversion instructions (possibly including calls)
4082 now that all parameters have been copied out of hard registers. */
4083 emit_insns (conversion_insns);
4085 max_parm_reg = max_reg_num ();
4086 last_parm_insn = get_last_insn ();
4088 current_function_args_size = stack_args_size.constant;
4090 /* Adjust function incoming argument size for alignment and
4091 minimum length. */
4093 #ifdef REG_PARM_STACK_SPACE
4094 #ifndef MAYBE_REG_PARM_STACK_SPACE
4095 current_function_args_size = MAX (current_function_args_size,
4096 REG_PARM_STACK_SPACE (fndecl));
4097 #endif
4098 #endif
4100 #ifdef STACK_BOUNDARY
4101 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
4103 current_function_args_size
4104 = ((current_function_args_size + STACK_BYTES - 1)
4105 / STACK_BYTES) * STACK_BYTES;
4106 #endif
4108 #ifdef ARGS_GROW_DOWNWARD
4109 current_function_arg_offset_rtx
4110 = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
4111 : expand_expr (size_binop (MINUS_EXPR, stack_args_size.var,
4112 size_int (-stack_args_size.constant)),
4113 NULL_RTX, VOIDmode, 0));
4114 #else
4115 current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
4116 #endif
4118 /* See how many bytes, if any, of its args a function should try to pop
4119 on return. */
4121 current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
4122 current_function_args_size);
4124 /* For stdarg.h function, save info about
4125 regs and stack space used by the named args. */
4127 if (!hide_last_arg)
4128 current_function_args_info = args_so_far;
4130 /* Set the rtx used for the function return value. Put this in its
4131 own variable so any optimizers that need this information don't have
4132 to include tree.h. Do this here so it gets done when an inlined
4133 function gets output. */
4135 current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
4138 /* Indicate whether REGNO is an incoming argument to the current function
4139 that was promoted to a wider mode. If so, return the RTX for the
4140 register (to get its mode). PMODE and PUNSIGNEDP are set to the mode
4141 that REGNO is promoted from and whether the promotion was signed or
4142 unsigned. */
4144 #ifdef PROMOTE_FUNCTION_ARGS
4147 promoted_input_arg (regno, pmode, punsignedp)
4148 int regno;
4149 enum machine_mode *pmode;
4150 int *punsignedp;
4152 tree arg;
4154 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
4155 arg = TREE_CHAIN (arg))
4156 if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
4157 && REGNO (DECL_INCOMING_RTL (arg)) == regno
4158 && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
4160 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
4161 int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
4163 mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
4164 if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
4165 && mode != DECL_MODE (arg))
4167 *pmode = DECL_MODE (arg);
4168 *punsignedp = unsignedp;
4169 return DECL_INCOMING_RTL (arg);
4173 return 0;
4176 #endif
4178 /* Compute the size and offset from the start of the stacked arguments for a
4179 parm passed in mode PASSED_MODE and with type TYPE.
4181 INITIAL_OFFSET_PTR points to the current offset into the stacked
4182 arguments.
4184 The starting offset and size for this parm are returned in *OFFSET_PTR
4185 and *ARG_SIZE_PTR, respectively.
4187 IN_REGS is non-zero if the argument will be passed in registers. It will
4188 never be set if REG_PARM_STACK_SPACE is not defined.
4190 FNDECL is the function in which the argument was defined.
4192 There are two types of rounding that are done. The first, controlled by
4193 FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
4194 list to be aligned to the specific boundary (in bits). This rounding
4195 affects the initial and starting offsets, but not the argument size.
4197 The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4198 optionally rounds the size of the parm to PARM_BOUNDARY. The
4199 initial offset is not affected by this rounding, while the size always
4200 is and the starting offset may be. */
4202 /* offset_ptr will be negative for ARGS_GROW_DOWNWARD case;
4203 initial_offset_ptr is positive because locate_and_pad_parm's
4204 callers pass in the total size of args so far as
4205 initial_offset_ptr. arg_size_ptr is always positive.*/
4207 void
4208 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
4209 initial_offset_ptr, offset_ptr, arg_size_ptr)
4210 enum machine_mode passed_mode;
4211 tree type;
4212 int in_regs;
4213 tree fndecl;
4214 struct args_size *initial_offset_ptr;
4215 struct args_size *offset_ptr;
4216 struct args_size *arg_size_ptr;
4218 tree sizetree
4219 = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4220 enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4221 int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
4222 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4223 int reg_parm_stack_space = 0;
4225 #ifdef REG_PARM_STACK_SPACE
4226 /* If we have found a stack parm before we reach the end of the
4227 area reserved for registers, skip that area. */
4228 if (! in_regs)
4230 #ifdef MAYBE_REG_PARM_STACK_SPACE
4231 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
4232 #else
4233 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4234 #endif
4235 if (reg_parm_stack_space > 0)
4237 if (initial_offset_ptr->var)
4239 initial_offset_ptr->var
4240 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4241 size_int (reg_parm_stack_space));
4242 initial_offset_ptr->constant = 0;
4244 else if (initial_offset_ptr->constant < reg_parm_stack_space)
4245 initial_offset_ptr->constant = reg_parm_stack_space;
4248 #endif /* REG_PARM_STACK_SPACE */
4250 arg_size_ptr->var = 0;
4251 arg_size_ptr->constant = 0;
4253 #ifdef ARGS_GROW_DOWNWARD
4254 if (initial_offset_ptr->var)
4256 offset_ptr->constant = 0;
4257 offset_ptr->var = size_binop (MINUS_EXPR, integer_zero_node,
4258 initial_offset_ptr->var);
4260 else
4262 offset_ptr->constant = - initial_offset_ptr->constant;
4263 offset_ptr->var = 0;
4265 if (where_pad != none
4266 && (TREE_CODE (sizetree) != INTEGER_CST
4267 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4268 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4269 SUB_PARM_SIZE (*offset_ptr, sizetree);
4270 if (where_pad != downward)
4271 pad_to_arg_alignment (offset_ptr, boundary);
4272 if (initial_offset_ptr->var)
4274 arg_size_ptr->var = size_binop (MINUS_EXPR,
4275 size_binop (MINUS_EXPR,
4276 integer_zero_node,
4277 initial_offset_ptr->var),
4278 offset_ptr->var);
4280 else
4282 arg_size_ptr->constant = (- initial_offset_ptr->constant -
4283 offset_ptr->constant);
4285 #else /* !ARGS_GROW_DOWNWARD */
4286 pad_to_arg_alignment (initial_offset_ptr, boundary);
4287 *offset_ptr = *initial_offset_ptr;
4289 #ifdef PUSH_ROUNDING
4290 if (passed_mode != BLKmode)
4291 sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4292 #endif
4294 /* Pad_below needs the pre-rounded size to know how much to pad below
4295 so this must be done before rounding up. */
4296 if (where_pad == downward
4297 /* However, BLKmode args passed in regs have their padding done elsewhere.
4298 The stack slot must be able to hold the entire register. */
4299 && !(in_regs && passed_mode == BLKmode))
4300 pad_below (offset_ptr, passed_mode, sizetree);
4302 if (where_pad != none
4303 && (TREE_CODE (sizetree) != INTEGER_CST
4304 || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4305 sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4307 ADD_PARM_SIZE (*arg_size_ptr, sizetree);
4308 #endif /* ARGS_GROW_DOWNWARD */
4311 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4312 BOUNDARY is measured in bits, but must be a multiple of a storage unit. */
4314 static void
4315 pad_to_arg_alignment (offset_ptr, boundary)
4316 struct args_size *offset_ptr;
4317 int boundary;
4319 int boundary_in_bytes = boundary / BITS_PER_UNIT;
4321 if (boundary > BITS_PER_UNIT)
4323 if (offset_ptr->var)
4325 offset_ptr->var =
4326 #ifdef ARGS_GROW_DOWNWARD
4327 round_down
4328 #else
4329 round_up
4330 #endif
4331 (ARGS_SIZE_TREE (*offset_ptr),
4332 boundary / BITS_PER_UNIT);
4333 offset_ptr->constant = 0; /*?*/
4335 else
4336 offset_ptr->constant =
4337 #ifdef ARGS_GROW_DOWNWARD
4338 FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
4339 #else
4340 CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
4341 #endif
4345 static void
4346 pad_below (offset_ptr, passed_mode, sizetree)
4347 struct args_size *offset_ptr;
4348 enum machine_mode passed_mode;
4349 tree sizetree;
4351 if (passed_mode != BLKmode)
4353 if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4354 offset_ptr->constant
4355 += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4356 / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4357 - GET_MODE_SIZE (passed_mode));
4359 else
4361 if (TREE_CODE (sizetree) != INTEGER_CST
4362 || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4364 /* Round the size up to multiple of PARM_BOUNDARY bits. */
4365 tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4366 /* Add it in. */
4367 ADD_PARM_SIZE (*offset_ptr, s2);
4368 SUB_PARM_SIZE (*offset_ptr, sizetree);
4373 static tree
4374 round_down (value, divisor)
4375 tree value;
4376 int divisor;
4378 return size_binop (MULT_EXPR,
4379 size_binop (FLOOR_DIV_EXPR, value, size_int (divisor)),
4380 size_int (divisor));
4383 /* Walk the tree of blocks describing the binding levels within a function
4384 and warn about uninitialized variables.
4385 This is done after calling flow_analysis and before global_alloc
4386 clobbers the pseudo-regs to hard regs. */
4388 void
4389 uninitialized_vars_warning (block)
4390 tree block;
4392 register tree decl, sub;
4393 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4395 if (TREE_CODE (decl) == VAR_DECL
4396 /* These warnings are unreliable for and aggregates
4397 because assigning the fields one by one can fail to convince
4398 flow.c that the entire aggregate was initialized.
4399 Unions are troublesome because members may be shorter. */
4400 && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
4401 && DECL_RTL (decl) != 0
4402 && GET_CODE (DECL_RTL (decl)) == REG
4403 && regno_uninitialized (REGNO (DECL_RTL (decl))))
4404 warning_with_decl (decl,
4405 "`%s' might be used uninitialized in this function");
4406 if (TREE_CODE (decl) == VAR_DECL
4407 && DECL_RTL (decl) != 0
4408 && GET_CODE (DECL_RTL (decl)) == REG
4409 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4410 warning_with_decl (decl,
4411 "variable `%s' might be clobbered by `longjmp' or `vfork'");
4413 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4414 uninitialized_vars_warning (sub);
4417 /* Do the appropriate part of uninitialized_vars_warning
4418 but for arguments instead of local variables. */
4420 void
4421 setjmp_args_warning ()
4423 register tree decl;
4424 for (decl = DECL_ARGUMENTS (current_function_decl);
4425 decl; decl = TREE_CHAIN (decl))
4426 if (DECL_RTL (decl) != 0
4427 && GET_CODE (DECL_RTL (decl)) == REG
4428 && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4429 warning_with_decl (decl, "argument `%s' might be clobbered by `longjmp' or `vfork'");
4432 /* If this function call setjmp, put all vars into the stack
4433 unless they were declared `register'. */
4435 void
4436 setjmp_protect (block)
4437 tree block;
4439 register tree decl, sub;
4440 for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4441 if ((TREE_CODE (decl) == VAR_DECL
4442 || TREE_CODE (decl) == PARM_DECL)
4443 && DECL_RTL (decl) != 0
4444 && GET_CODE (DECL_RTL (decl)) == REG
4445 /* If this variable came from an inline function, it must be
4446 that it's life doesn't overlap the setjmp. If there was a
4447 setjmp in the function, it would already be in memory. We
4448 must exclude such variable because their DECL_RTL might be
4449 set to strange things such as virtual_stack_vars_rtx. */
4450 && ! DECL_FROM_INLINE (decl)
4451 && (
4452 #ifdef NON_SAVING_SETJMP
4453 /* If longjmp doesn't restore the registers,
4454 don't put anything in them. */
4455 NON_SAVING_SETJMP
4457 #endif
4458 ! DECL_REGISTER (decl)))
4459 put_var_into_stack (decl);
4460 for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4461 setjmp_protect (sub);
4464 /* Like the previous function, but for args instead of local variables. */
4466 void
4467 setjmp_protect_args ()
4469 register tree decl, sub;
4470 for (decl = DECL_ARGUMENTS (current_function_decl);
4471 decl; decl = TREE_CHAIN (decl))
4472 if ((TREE_CODE (decl) == VAR_DECL
4473 || TREE_CODE (decl) == PARM_DECL)
4474 && DECL_RTL (decl) != 0
4475 && GET_CODE (DECL_RTL (decl)) == REG
4476 && (
4477 /* If longjmp doesn't restore the registers,
4478 don't put anything in them. */
4479 #ifdef NON_SAVING_SETJMP
4480 NON_SAVING_SETJMP
4482 #endif
4483 ! DECL_REGISTER (decl)))
4484 put_var_into_stack (decl);
4487 /* Return the context-pointer register corresponding to DECL,
4488 or 0 if it does not need one. */
4491 lookup_static_chain (decl)
4492 tree decl;
4494 tree context = decl_function_context (decl);
4495 tree link;
4497 if (context == 0
4498 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_NO_STATIC_CHAIN (decl)))
4499 return 0;
4501 /* We treat inline_function_decl as an alias for the current function
4502 because that is the inline function whose vars, types, etc.
4503 are being merged into the current function.
4504 See expand_inline_function. */
4505 if (context == current_function_decl || context == inline_function_decl)
4506 return virtual_stack_vars_rtx;
4508 for (link = context_display; link; link = TREE_CHAIN (link))
4509 if (TREE_PURPOSE (link) == context)
4510 return RTL_EXPR_RTL (TREE_VALUE (link));
4512 abort ();
4515 /* Convert a stack slot address ADDR for variable VAR
4516 (from a containing function)
4517 into an address valid in this function (using a static chain). */
4520 fix_lexical_addr (addr, var)
4521 rtx addr;
4522 tree var;
4524 rtx basereg;
4525 int displacement;
4526 tree context = decl_function_context (var);
4527 struct function *fp;
4528 rtx base = 0;
4530 /* If this is the present function, we need not do anything. */
4531 if (context == current_function_decl || context == inline_function_decl)
4532 return addr;
4534 for (fp = outer_function_chain; fp; fp = fp->next)
4535 if (fp->decl == context)
4536 break;
4538 if (fp == 0)
4539 abort ();
4541 /* Decode given address as base reg plus displacement. */
4542 if (GET_CODE (addr) == REG)
4543 basereg = addr, displacement = 0;
4544 else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
4545 basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
4546 else
4547 abort ();
4549 /* We accept vars reached via the containing function's
4550 incoming arg pointer and via its stack variables pointer. */
4551 if (basereg == fp->internal_arg_pointer)
4553 /* If reached via arg pointer, get the arg pointer value
4554 out of that function's stack frame.
4556 There are two cases: If a separate ap is needed, allocate a
4557 slot in the outer function for it and dereference it that way.
4558 This is correct even if the real ap is actually a pseudo.
4559 Otherwise, just adjust the offset from the frame pointer to
4560 compensate. */
4562 #ifdef NEED_SEPARATE_AP
4563 rtx addr;
4565 if (fp->arg_pointer_save_area == 0)
4566 fp->arg_pointer_save_area
4567 = assign_outer_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
4569 addr = fix_lexical_addr (XEXP (fp->arg_pointer_save_area, 0), var);
4570 addr = memory_address (Pmode, addr);
4572 base = copy_to_reg (gen_rtx (MEM, Pmode, addr));
4573 #else
4574 displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
4575 base = lookup_static_chain (var);
4576 #endif
4579 else if (basereg == virtual_stack_vars_rtx)
4581 /* This is the same code as lookup_static_chain, duplicated here to
4582 avoid an extra call to decl_function_context. */
4583 tree link;
4585 for (link = context_display; link; link = TREE_CHAIN (link))
4586 if (TREE_PURPOSE (link) == context)
4588 base = RTL_EXPR_RTL (TREE_VALUE (link));
4589 break;
4593 if (base == 0)
4594 abort ();
4596 /* Use same offset, relative to appropriate static chain or argument
4597 pointer. */
4598 return plus_constant (base, displacement);
4601 /* Return the address of the trampoline for entering nested fn FUNCTION.
4602 If necessary, allocate a trampoline (in the stack frame)
4603 and emit rtl to initialize its contents (at entry to this function). */
4606 trampoline_address (function)
4607 tree function;
4609 tree link;
4610 tree rtlexp;
4611 rtx tramp;
4612 struct function *fp;
4613 tree fn_context;
4615 /* Find an existing trampoline and return it. */
4616 for (link = trampoline_list; link; link = TREE_CHAIN (link))
4617 if (TREE_PURPOSE (link) == function)
4618 return
4619 round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
4621 for (fp = outer_function_chain; fp; fp = fp->next)
4622 for (link = fp->trampoline_list; link; link = TREE_CHAIN (link))
4623 if (TREE_PURPOSE (link) == function)
4625 tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
4626 function);
4627 return round_trampoline_addr (tramp);
4630 /* None exists; we must make one. */
4632 /* Find the `struct function' for the function containing FUNCTION. */
4633 fp = 0;
4634 fn_context = decl_function_context (function);
4635 if (fn_context != current_function_decl)
4636 for (fp = outer_function_chain; fp; fp = fp->next)
4637 if (fp->decl == fn_context)
4638 break;
4640 /* Allocate run-time space for this trampoline
4641 (usually in the defining function's stack frame). */
4642 #ifdef ALLOCATE_TRAMPOLINE
4643 tramp = ALLOCATE_TRAMPOLINE (fp);
4644 #else
4645 /* If rounding needed, allocate extra space
4646 to ensure we have TRAMPOLINE_SIZE bytes left after rounding up. */
4647 #ifdef TRAMPOLINE_ALIGNMENT
4648 #define TRAMPOLINE_REAL_SIZE \
4649 (TRAMPOLINE_SIZE + (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT) - 1)
4650 #else
4651 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
4652 #endif
4653 if (fp != 0)
4654 tramp = assign_outer_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0, fp);
4655 else
4656 tramp = assign_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0);
4657 #endif
4659 /* Record the trampoline for reuse and note it for later initialization
4660 by expand_function_end. */
4661 if (fp != 0)
4663 push_obstacks (fp->function_maybepermanent_obstack,
4664 fp->function_maybepermanent_obstack);
4665 rtlexp = make_node (RTL_EXPR);
4666 RTL_EXPR_RTL (rtlexp) = tramp;
4667 fp->trampoline_list = tree_cons (function, rtlexp, fp->trampoline_list);
4668 pop_obstacks ();
4670 else
4672 /* Make the RTL_EXPR node temporary, not momentary, so that the
4673 trampoline_list doesn't become garbage. */
4674 int momentary = suspend_momentary ();
4675 rtlexp = make_node (RTL_EXPR);
4676 resume_momentary (momentary);
4678 RTL_EXPR_RTL (rtlexp) = tramp;
4679 trampoline_list = tree_cons (function, rtlexp, trampoline_list);
4682 tramp = fix_lexical_addr (XEXP (tramp, 0), function);
4683 return round_trampoline_addr (tramp);
4686 /* Given a trampoline address,
4687 round it to multiple of TRAMPOLINE_ALIGNMENT. */
4689 static rtx
4690 round_trampoline_addr (tramp)
4691 rtx tramp;
4693 #ifdef TRAMPOLINE_ALIGNMENT
4694 /* Round address up to desired boundary. */
4695 rtx temp = gen_reg_rtx (Pmode);
4696 temp = expand_binop (Pmode, add_optab, tramp,
4697 GEN_INT (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT - 1),
4698 temp, 0, OPTAB_LIB_WIDEN);
4699 tramp = expand_binop (Pmode, and_optab, temp,
4700 GEN_INT (- TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT),
4701 temp, 0, OPTAB_LIB_WIDEN);
4702 #endif
4703 return tramp;
4706 /* The functions identify_blocks and reorder_blocks provide a way to
4707 reorder the tree of BLOCK nodes, for optimizers that reshuffle or
4708 duplicate portions of the RTL code. Call identify_blocks before
4709 changing the RTL, and call reorder_blocks after. */
4711 /* Put all this function's BLOCK nodes including those that are chained
4712 onto the first block into a vector, and return it.
4713 Also store in each NOTE for the beginning or end of a block
4714 the index of that block in the vector.
4715 The arguments are BLOCK, the chain of top-level blocks of the function,
4716 and INSNS, the insn chain of the function. */
4718 tree *
4719 identify_blocks (block, insns)
4720 tree block;
4721 rtx insns;
4723 int n_blocks;
4724 tree *block_vector;
4725 int *block_stack;
4726 int depth = 0;
4727 int next_block_number = 1;
4728 int current_block_number = 1;
4729 rtx insn;
4731 if (block == 0)
4732 return 0;
4734 n_blocks = all_blocks (block, 0);
4735 block_vector = (tree *) xmalloc (n_blocks * sizeof (tree));
4736 block_stack = (int *) alloca (n_blocks * sizeof (int));
4738 all_blocks (block, block_vector);
4740 for (insn = insns; insn; insn = NEXT_INSN (insn))
4741 if (GET_CODE (insn) == NOTE)
4743 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4745 block_stack[depth++] = current_block_number;
4746 current_block_number = next_block_number;
4747 NOTE_BLOCK_NUMBER (insn) = next_block_number++;
4749 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4751 current_block_number = block_stack[--depth];
4752 NOTE_BLOCK_NUMBER (insn) = current_block_number;
4756 if (n_blocks != next_block_number)
4757 abort ();
4759 return block_vector;
4762 /* Given BLOCK_VECTOR which was returned by identify_blocks,
4763 and a revised instruction chain, rebuild the tree structure
4764 of BLOCK nodes to correspond to the new order of RTL.
4765 The new block tree is inserted below TOP_BLOCK.
4766 Returns the current top-level block. */
4768 tree
4769 reorder_blocks (block_vector, block, insns)
4770 tree *block_vector;
4771 tree block;
4772 rtx insns;
4774 tree current_block = block;
4775 rtx insn;
4777 if (block_vector == 0)
4778 return block;
4780 /* Prune the old trees away, so that it doesn't get in the way. */
4781 BLOCK_SUBBLOCKS (current_block) = 0;
4782 BLOCK_CHAIN (current_block) = 0;
4784 for (insn = insns; insn; insn = NEXT_INSN (insn))
4785 if (GET_CODE (insn) == NOTE)
4787 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4789 tree block = block_vector[NOTE_BLOCK_NUMBER (insn)];
4790 /* If we have seen this block before, copy it. */
4791 if (TREE_ASM_WRITTEN (block))
4792 block = copy_node (block);
4793 BLOCK_SUBBLOCKS (block) = 0;
4794 TREE_ASM_WRITTEN (block) = 1;
4795 BLOCK_SUPERCONTEXT (block) = current_block;
4796 BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4797 BLOCK_SUBBLOCKS (current_block) = block;
4798 current_block = block;
4799 NOTE_SOURCE_FILE (insn) = 0;
4801 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4803 BLOCK_SUBBLOCKS (current_block)
4804 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
4805 current_block = BLOCK_SUPERCONTEXT (current_block);
4806 NOTE_SOURCE_FILE (insn) = 0;
4810 BLOCK_SUBBLOCKS (current_block)
4811 = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
4812 return current_block;
4815 /* Reverse the order of elements in the chain T of blocks,
4816 and return the new head of the chain (old last element). */
4818 static tree
4819 blocks_nreverse (t)
4820 tree t;
4822 register tree prev = 0, decl, next;
4823 for (decl = t; decl; decl = next)
4825 next = BLOCK_CHAIN (decl);
4826 BLOCK_CHAIN (decl) = prev;
4827 prev = decl;
4829 return prev;
4832 /* Count the subblocks of the list starting with BLOCK, and list them
4833 all into the vector VECTOR. Also clear TREE_ASM_WRITTEN in all
4834 blocks. */
4836 static int
4837 all_blocks (block, vector)
4838 tree block;
4839 tree *vector;
4841 int n_blocks = 0;
4843 while (block)
4845 TREE_ASM_WRITTEN (block) = 0;
4847 /* Record this block. */
4848 if (vector)
4849 vector[n_blocks] = block;
4851 ++n_blocks;
4853 /* Record the subblocks, and their subblocks... */
4854 n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4855 vector ? vector + n_blocks : 0);
4856 block = BLOCK_CHAIN (block);
4859 return n_blocks;
4862 /* Build bytecode call descriptor for function SUBR. */
4865 bc_build_calldesc (subr)
4866 tree subr;
4868 tree calldesc = 0, arg;
4869 int nargs = 0;
4871 /* Build the argument description vector in reverse order. */
4872 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4873 nargs = 0;
4875 for (arg = DECL_ARGUMENTS (subr); arg; arg = TREE_CHAIN (arg))
4877 ++nargs;
4879 calldesc = tree_cons ((tree) 0, size_in_bytes (TREE_TYPE (arg)), calldesc);
4880 calldesc = tree_cons ((tree) 0, bc_runtime_type_code (TREE_TYPE (arg)), calldesc);
4883 DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4885 /* Prepend the function's return type. */
4886 calldesc = tree_cons ((tree) 0,
4887 size_in_bytes (TREE_TYPE (TREE_TYPE (subr))),
4888 calldesc);
4890 calldesc = tree_cons ((tree) 0,
4891 bc_runtime_type_code (TREE_TYPE (TREE_TYPE (subr))),
4892 calldesc);
4894 /* Prepend the arg count. */
4895 calldesc = tree_cons ((tree) 0, build_int_2 (nargs, 0), calldesc);
4897 /* Output the call description vector and get its address. */
4898 calldesc = build_nt (CONSTRUCTOR, (tree) 0, calldesc);
4899 TREE_TYPE (calldesc) = build_array_type (integer_type_node,
4900 build_index_type (build_int_2 (nargs * 2, 0)));
4902 return output_constant_def (calldesc);
4906 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4907 and initialize static variables for generating RTL for the statements
4908 of the function. */
4910 void
4911 init_function_start (subr, filename, line)
4912 tree subr;
4913 char *filename;
4914 int line;
4916 if (output_bytecode)
4918 this_function_decl = subr;
4919 this_function_calldesc = bc_build_calldesc (subr);
4920 local_vars_size = 0;
4921 stack_depth = 0;
4922 max_stack_depth = 0;
4923 stmt_expr_depth = 0;
4924 return;
4927 init_stmt_for_function ();
4929 cse_not_expected = ! optimize;
4931 /* Caller save not needed yet. */
4932 caller_save_needed = 0;
4934 /* No stack slots have been made yet. */
4935 stack_slot_list = 0;
4937 /* There is no stack slot for handling nonlocal gotos. */
4938 nonlocal_goto_handler_slot = 0;
4939 nonlocal_goto_stack_level = 0;
4941 /* No labels have been declared for nonlocal use. */
4942 nonlocal_labels = 0;
4944 /* No function calls so far in this function. */
4945 function_call_count = 0;
4947 /* No parm regs have been allocated.
4948 (This is important for output_inline_function.) */
4949 max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4951 /* Initialize the RTL mechanism. */
4952 init_emit ();
4954 /* Initialize the queue of pending postincrement and postdecrements,
4955 and some other info in expr.c. */
4956 init_expr ();
4958 /* We haven't done register allocation yet. */
4959 reg_renumber = 0;
4961 init_const_rtx_hash_table ();
4963 current_function_name = (*decl_printable_name) (subr, 2);
4965 /* Nonzero if this is a nested function that uses a static chain. */
4967 current_function_needs_context
4968 = (decl_function_context (current_function_decl) != 0
4969 && ! DECL_NO_STATIC_CHAIN (current_function_decl));
4971 /* Set if a call to setjmp is seen. */
4972 current_function_calls_setjmp = 0;
4974 /* Set if a call to longjmp is seen. */
4975 current_function_calls_longjmp = 0;
4977 current_function_calls_alloca = 0;
4978 current_function_has_nonlocal_label = 0;
4979 current_function_has_nonlocal_goto = 0;
4980 current_function_contains_functions = 0;
4981 current_function_is_thunk = 0;
4983 current_function_returns_pcc_struct = 0;
4984 current_function_returns_struct = 0;
4985 current_function_epilogue_delay_list = 0;
4986 current_function_uses_const_pool = 0;
4987 current_function_uses_pic_offset_table = 0;
4989 /* We have not yet needed to make a label to jump to for tail-recursion. */
4990 tail_recursion_label = 0;
4992 /* We haven't had a need to make a save area for ap yet. */
4994 arg_pointer_save_area = 0;
4996 /* No stack slots allocated yet. */
4997 frame_offset = 0;
4999 /* No SAVE_EXPRs in this function yet. */
5000 save_expr_regs = 0;
5002 /* No RTL_EXPRs in this function yet. */
5003 rtl_expr_chain = 0;
5005 /* Set up to allocate temporaries. */
5006 init_temp_slots ();
5008 /* Within function body, compute a type's size as soon it is laid out. */
5009 immediate_size_expand++;
5011 /* We haven't made any trampolines for this function yet. */
5012 trampoline_list = 0;
5014 init_pending_stack_adjust ();
5015 inhibit_defer_pop = 0;
5017 current_function_outgoing_args_size = 0;
5019 /* Prevent ever trying to delete the first instruction of a function.
5020 Also tell final how to output a linenum before the function prologue. */
5021 emit_line_note (filename, line);
5023 /* Make sure first insn is a note even if we don't want linenums.
5024 This makes sure the first insn will never be deleted.
5025 Also, final expects a note to appear there. */
5026 emit_note (NULL_PTR, NOTE_INSN_DELETED);
5028 /* Set flags used by final.c. */
5029 if (aggregate_value_p (DECL_RESULT (subr)))
5031 #ifdef PCC_STATIC_STRUCT_RETURN
5032 current_function_returns_pcc_struct = 1;
5033 #endif
5034 current_function_returns_struct = 1;
5037 /* Warn if this value is an aggregate type,
5038 regardless of which calling convention we are using for it. */
5039 if (warn_aggregate_return
5040 && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
5041 warning ("function returns an aggregate");
5043 current_function_returns_pointer
5044 = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
5046 /* Indicate that we need to distinguish between the return value of the
5047 present function and the return value of a function being called. */
5048 rtx_equal_function_value_matters = 1;
5050 /* Indicate that we have not instantiated virtual registers yet. */
5051 virtuals_instantiated = 0;
5053 /* Indicate we have no need of a frame pointer yet. */
5054 frame_pointer_needed = 0;
5056 /* By default assume not varargs or stdarg. */
5057 current_function_varargs = 0;
5058 current_function_stdarg = 0;
5061 /* Indicate that the current function uses extra args
5062 not explicitly mentioned in the argument list in any fashion. */
5064 void
5065 mark_varargs ()
5067 current_function_varargs = 1;
5070 /* Expand a call to __main at the beginning of a possible main function. */
5072 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
5073 #undef HAS_INIT_SECTION
5074 #define HAS_INIT_SECTION
5075 #endif
5077 void
5078 expand_main_function ()
5080 if (!output_bytecode)
5082 /* The zero below avoids a possible parse error */
5084 #if !defined (HAS_INIT_SECTION)
5085 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, NAME__MAIN), 0,
5086 VOIDmode, 0);
5087 #endif /* not HAS_INIT_SECTION */
5091 extern struct obstack permanent_obstack;
5093 /* Expand start of bytecode function. See comment at
5094 expand_function_start below for details. */
5096 void
5097 bc_expand_function_start (subr, parms_have_cleanups)
5098 tree subr;
5099 int parms_have_cleanups;
5101 char label[20], *name;
5102 static int nlab;
5103 tree thisarg;
5104 int argsz;
5106 if (TREE_PUBLIC (subr))
5107 bc_globalize_label (IDENTIFIER_POINTER (DECL_NAME (subr)));
5109 #ifdef DEBUG_PRINT_CODE
5110 fprintf (stderr, "\n<func %s>\n", IDENTIFIER_POINTER (DECL_NAME (subr)));
5111 #endif
5113 for (argsz = 0, thisarg = DECL_ARGUMENTS (subr); thisarg; thisarg = TREE_CHAIN (thisarg))
5115 if (DECL_RTL (thisarg))
5116 abort (); /* Should be NULL here I think. */
5117 else if (TREE_CONSTANT (DECL_SIZE (thisarg)))
5119 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5120 argsz += TREE_INT_CST_LOW (DECL_SIZE (thisarg));
5122 else
5124 /* Variable-sized objects are pointers to their storage. */
5125 DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5126 argsz += POINTER_SIZE;
5130 bc_begin_function (xstrdup (IDENTIFIER_POINTER (DECL_NAME (subr))));
5132 ASM_GENERATE_INTERNAL_LABEL (label, "LX", nlab);
5134 ++nlab;
5135 name = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
5136 this_function_callinfo = bc_gen_rtx (name, 0, (struct bc_label *) 0);
5137 this_function_bytecode =
5138 bc_emit_trampoline (BYTECODE_LABEL (this_function_callinfo));
5142 /* Expand end of bytecode function. See details the comment of
5143 expand_function_end(), below. */
5145 void
5146 bc_expand_function_end ()
5148 char *ptrconsts;
5150 expand_null_return ();
5152 /* Emit any fixup code. This must be done before the call to
5153 to BC_END_FUNCTION (), since that will cause the bytecode
5154 segment to be finished off and closed. */
5156 expand_fixups (NULL_RTX);
5158 ptrconsts = bc_end_function ();
5160 bc_align_const (2 /* INT_ALIGN */);
5162 /* If this changes also make sure to change bc-interp.h! */
5164 bc_emit_const_labeldef (BYTECODE_LABEL (this_function_callinfo));
5165 bc_emit_const ((char *) &max_stack_depth, sizeof max_stack_depth);
5166 bc_emit_const ((char *) &local_vars_size, sizeof local_vars_size);
5167 bc_emit_const_labelref (this_function_bytecode, 0);
5168 bc_emit_const_labelref (ptrconsts, 0);
5169 bc_emit_const_labelref (BYTECODE_LABEL (this_function_calldesc), 0);
5173 /* Start the RTL for a new function, and set variables used for
5174 emitting RTL.
5175 SUBR is the FUNCTION_DECL node.
5176 PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5177 the function's parameters, which must be run at any return statement. */
5179 void
5180 expand_function_start (subr, parms_have_cleanups)
5181 tree subr;
5182 int parms_have_cleanups;
5184 register int i;
5185 tree tem;
5186 rtx last_ptr;
5188 if (output_bytecode)
5190 bc_expand_function_start (subr, parms_have_cleanups);
5191 return;
5194 /* Make sure volatile mem refs aren't considered
5195 valid operands of arithmetic insns. */
5196 init_recog_no_volatile ();
5198 /* If function gets a static chain arg, store it in the stack frame.
5199 Do this first, so it gets the first stack slot offset. */
5200 if (current_function_needs_context)
5202 last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5204 #ifdef SMALL_REGISTER_CLASSES
5205 /* Delay copying static chain if it is not a register to avoid
5206 conflicts with regs used for parameters. */
5207 if (! SMALL_REGISTER_CLASSES
5208 || GET_CODE (static_chain_incoming_rtx) == REG)
5209 #endif
5210 emit_move_insn (last_ptr, static_chain_incoming_rtx);
5213 /* If the parameters of this function need cleaning up, get a label
5214 for the beginning of the code which executes those cleanups. This must
5215 be done before doing anything with return_label. */
5216 if (parms_have_cleanups)
5217 cleanup_label = gen_label_rtx ();
5218 else
5219 cleanup_label = 0;
5221 /* Make the label for return statements to jump to, if this machine
5222 does not have a one-instruction return and uses an epilogue,
5223 or if it returns a structure, or if it has parm cleanups. */
5224 #ifdef HAVE_return
5225 if (cleanup_label == 0 && HAVE_return
5226 && ! current_function_returns_pcc_struct
5227 && ! (current_function_returns_struct && ! optimize))
5228 return_label = 0;
5229 else
5230 return_label = gen_label_rtx ();
5231 #else
5232 return_label = gen_label_rtx ();
5233 #endif
5235 /* Initialize rtx used to return the value. */
5236 /* Do this before assign_parms so that we copy the struct value address
5237 before any library calls that assign parms might generate. */
5239 /* Decide whether to return the value in memory or in a register. */
5240 if (aggregate_value_p (DECL_RESULT (subr)))
5242 /* Returning something that won't go in a register. */
5243 register rtx value_address = 0;
5245 #ifdef PCC_STATIC_STRUCT_RETURN
5246 if (current_function_returns_pcc_struct)
5248 int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
5249 value_address = assemble_static_space (size);
5251 else
5252 #endif
5254 /* Expect to be passed the address of a place to store the value.
5255 If it is passed as an argument, assign_parms will take care of
5256 it. */
5257 if (struct_value_incoming_rtx)
5259 value_address = gen_reg_rtx (Pmode);
5260 emit_move_insn (value_address, struct_value_incoming_rtx);
5263 if (value_address)
5265 DECL_RTL (DECL_RESULT (subr))
5266 = gen_rtx (MEM, DECL_MODE (DECL_RESULT (subr)), value_address);
5267 MEM_IN_STRUCT_P (DECL_RTL (DECL_RESULT (subr)))
5268 = AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
5271 else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5272 /* If return mode is void, this decl rtl should not be used. */
5273 DECL_RTL (DECL_RESULT (subr)) = 0;
5274 else if (parms_have_cleanups)
5276 /* If function will end with cleanup code for parms,
5277 compute the return values into a pseudo reg,
5278 which we will copy into the true return register
5279 after the cleanups are done. */
5281 enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
5283 #ifdef PROMOTE_FUNCTION_RETURN
5284 tree type = TREE_TYPE (DECL_RESULT (subr));
5285 int unsignedp = TREE_UNSIGNED (type);
5287 mode = promote_mode (type, mode, &unsignedp, 1);
5288 #endif
5290 DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
5292 else
5293 /* Scalar, returned in a register. */
5295 #ifdef FUNCTION_OUTGOING_VALUE
5296 DECL_RTL (DECL_RESULT (subr))
5297 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5298 #else
5299 DECL_RTL (DECL_RESULT (subr))
5300 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5301 #endif
5303 /* Mark this reg as the function's return value. */
5304 if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
5306 REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
5307 /* Needed because we may need to move this to memory
5308 in case it's a named return value whose address is taken. */
5309 DECL_REGISTER (DECL_RESULT (subr)) = 1;
5313 /* Initialize rtx for parameters and local variables.
5314 In some cases this requires emitting insns. */
5316 assign_parms (subr, 0);
5318 #ifdef SMALL_REGISTER_CLASSES
5319 /* Copy the static chain now if it wasn't a register. The delay is to
5320 avoid conflicts with the parameter passing registers. */
5322 if (SMALL_REGISTER_CLASSES && current_function_needs_context)
5323 if (GET_CODE (static_chain_incoming_rtx) != REG)
5324 emit_move_insn (last_ptr, static_chain_incoming_rtx);
5325 #endif
5327 /* The following was moved from init_function_start.
5328 The move is supposed to make sdb output more accurate. */
5329 /* Indicate the beginning of the function body,
5330 as opposed to parm setup. */
5331 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
5333 /* If doing stupid allocation, mark parms as born here. */
5335 if (GET_CODE (get_last_insn ()) != NOTE)
5336 emit_note (NULL_PTR, NOTE_INSN_DELETED);
5337 parm_birth_insn = get_last_insn ();
5339 if (obey_regdecls)
5341 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5342 use_variable (regno_reg_rtx[i]);
5344 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5345 use_variable (current_function_internal_arg_pointer);
5348 context_display = 0;
5349 if (current_function_needs_context)
5351 /* Fetch static chain values for containing functions. */
5352 tem = decl_function_context (current_function_decl);
5353 /* If not doing stupid register allocation copy the static chain
5354 pointer into a pseudo. If we have small register classes, copy
5355 the value from memory if static_chain_incoming_rtx is a REG. If
5356 we do stupid register allocation, we use the stack address
5357 generated above. */
5358 if (tem && ! obey_regdecls)
5360 #ifdef SMALL_REGISTER_CLASSES
5361 /* If the static chain originally came in a register, put it back
5362 there, then move it out in the next insn. The reason for
5363 this peculiar code is to satisfy function integration. */
5364 if (SMALL_REGISTER_CLASSES
5365 && GET_CODE (static_chain_incoming_rtx) == REG)
5366 emit_move_insn (static_chain_incoming_rtx, last_ptr);
5367 #endif
5369 last_ptr = copy_to_reg (static_chain_incoming_rtx);
5372 while (tem)
5374 tree rtlexp = make_node (RTL_EXPR);
5376 RTL_EXPR_RTL (rtlexp) = last_ptr;
5377 context_display = tree_cons (tem, rtlexp, context_display);
5378 tem = decl_function_context (tem);
5379 if (tem == 0)
5380 break;
5381 /* Chain thru stack frames, assuming pointer to next lexical frame
5382 is found at the place we always store it. */
5383 #ifdef FRAME_GROWS_DOWNWARD
5384 last_ptr = plus_constant (last_ptr, - GET_MODE_SIZE (Pmode));
5385 #endif
5386 last_ptr = copy_to_reg (gen_rtx (MEM, Pmode,
5387 memory_address (Pmode, last_ptr)));
5389 /* If we are not optimizing, ensure that we know that this
5390 piece of context is live over the entire function. */
5391 if (! optimize)
5392 save_expr_regs = gen_rtx (EXPR_LIST, VOIDmode, last_ptr,
5393 save_expr_regs);
5397 /* After the display initializations is where the tail-recursion label
5398 should go, if we end up needing one. Ensure we have a NOTE here
5399 since some things (like trampolines) get placed before this. */
5400 tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
5402 /* Evaluate now the sizes of any types declared among the arguments. */
5403 for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
5405 expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
5406 /* Flush the queue in case this parameter declaration has
5407 side-effects. */
5408 emit_queue ();
5411 /* Make sure there is a line number after the function entry setup code. */
5412 force_next_line_note ();
5415 /* Generate RTL for the end of the current function.
5416 FILENAME and LINE are the current position in the source file.
5418 It is up to language-specific callers to do cleanups for parameters--
5419 or else, supply 1 for END_BINDINGS and we will call expand_end_bindings. */
5421 void
5422 expand_function_end (filename, line, end_bindings)
5423 char *filename;
5424 int line;
5425 int end_bindings;
5427 register int i;
5428 tree link;
5430 #ifdef TRAMPOLINE_TEMPLATE
5431 static rtx initial_trampoline;
5432 #endif
5434 if (output_bytecode)
5436 bc_expand_function_end ();
5437 return;
5440 #ifdef NON_SAVING_SETJMP
5441 /* Don't put any variables in registers if we call setjmp
5442 on a machine that fails to restore the registers. */
5443 if (NON_SAVING_SETJMP && current_function_calls_setjmp)
5445 if (DECL_INITIAL (current_function_decl) != error_mark_node)
5446 setjmp_protect (DECL_INITIAL (current_function_decl));
5448 setjmp_protect_args ();
5450 #endif
5452 /* Save the argument pointer if a save area was made for it. */
5453 if (arg_pointer_save_area)
5455 rtx x = gen_move_insn (arg_pointer_save_area, virtual_incoming_args_rtx);
5456 emit_insn_before (x, tail_recursion_reentry);
5459 /* Initialize any trampolines required by this function. */
5460 for (link = trampoline_list; link; link = TREE_CHAIN (link))
5462 tree function = TREE_PURPOSE (link);
5463 rtx context = lookup_static_chain (function);
5464 rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
5465 rtx blktramp;
5466 rtx seq;
5468 #ifdef TRAMPOLINE_TEMPLATE
5469 /* First make sure this compilation has a template for
5470 initializing trampolines. */
5471 if (initial_trampoline == 0)
5473 end_temporary_allocation ();
5474 initial_trampoline
5475 = gen_rtx (MEM, BLKmode, assemble_trampoline_template ());
5476 resume_temporary_allocation ();
5478 #endif
5480 /* Generate insns to initialize the trampoline. */
5481 start_sequence ();
5482 tramp = round_trampoline_addr (XEXP (tramp, 0));
5483 #ifdef TRAMPOLINE_TEMPLATE
5484 blktramp = change_address (initial_trampoline, BLKmode, tramp);
5485 emit_block_move (blktramp, initial_trampoline,
5486 GEN_INT (TRAMPOLINE_SIZE),
5487 TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
5488 #endif
5489 INITIALIZE_TRAMPOLINE (tramp, XEXP (DECL_RTL (function), 0), context);
5490 seq = get_insns ();
5491 end_sequence ();
5493 /* Put those insns at entry to the containing function (this one). */
5494 emit_insns_before (seq, tail_recursion_reentry);
5497 /* Warn about unused parms if extra warnings were specified. */
5498 if (warn_unused && extra_warnings)
5500 tree decl;
5502 for (decl = DECL_ARGUMENTS (current_function_decl);
5503 decl; decl = TREE_CHAIN (decl))
5504 if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
5505 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
5506 warning_with_decl (decl, "unused parameter `%s'");
5509 /* Delete handlers for nonlocal gotos if nothing uses them. */
5510 if (nonlocal_goto_handler_slot != 0 && !current_function_has_nonlocal_label)
5511 delete_handlers ();
5513 /* End any sequences that failed to be closed due to syntax errors. */
5514 while (in_sequence_p ())
5515 end_sequence ();
5517 /* Outside function body, can't compute type's actual size
5518 until next function's body starts. */
5519 immediate_size_expand--;
5521 /* If doing stupid register allocation,
5522 mark register parms as dying here. */
5524 if (obey_regdecls)
5526 rtx tem;
5527 for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5528 use_variable (regno_reg_rtx[i]);
5530 /* Likewise for the regs of all the SAVE_EXPRs in the function. */
5532 for (tem = save_expr_regs; tem; tem = XEXP (tem, 1))
5534 use_variable (XEXP (tem, 0));
5535 use_variable_after (XEXP (tem, 0), parm_birth_insn);
5538 if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5539 use_variable (current_function_internal_arg_pointer);
5542 clear_pending_stack_adjust ();
5543 do_pending_stack_adjust ();
5545 /* Mark the end of the function body.
5546 If control reaches this insn, the function can drop through
5547 without returning a value. */
5548 emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
5550 /* Must mark the last line number note in the function, so that the test
5551 coverage code can avoid counting the last line twice. This just tells
5552 the code to ignore the immediately following line note, since there
5553 already exists a copy of this note somewhere above. This line number
5554 note is still needed for debugging though, so we can't delete it. */
5555 if (flag_test_coverage)
5556 emit_note (NULL_PTR, NOTE_REPEATED_LINE_NUMBER);
5558 /* Output a linenumber for the end of the function.
5559 SDB depends on this. */
5560 emit_line_note_force (filename, line);
5562 /* Output the label for the actual return from the function,
5563 if one is expected. This happens either because a function epilogue
5564 is used instead of a return instruction, or because a return was done
5565 with a goto in order to run local cleanups, or because of pcc-style
5566 structure returning. */
5568 if (return_label)
5569 emit_label (return_label);
5571 /* C++ uses this. */
5572 if (end_bindings)
5573 expand_end_bindings (0, 0, 0);
5575 /* If we had calls to alloca, and this machine needs
5576 an accurate stack pointer to exit the function,
5577 insert some code to save and restore the stack pointer. */
5578 #ifdef EXIT_IGNORE_STACK
5579 if (! EXIT_IGNORE_STACK)
5580 #endif
5581 if (current_function_calls_alloca)
5583 rtx tem = 0;
5585 emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5586 emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
5589 /* If scalar return value was computed in a pseudo-reg,
5590 copy that to the hard return register. */
5591 if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0
5592 && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG
5593 && (REGNO (DECL_RTL (DECL_RESULT (current_function_decl)))
5594 >= FIRST_PSEUDO_REGISTER))
5596 rtx real_decl_result;
5598 #ifdef FUNCTION_OUTGOING_VALUE
5599 real_decl_result
5600 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5601 current_function_decl);
5602 #else
5603 real_decl_result
5604 = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5605 current_function_decl);
5606 #endif
5607 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
5608 /* If this is a BLKmode structure being returned in registers, then use
5609 the mode computed in expand_return. */
5610 if (GET_MODE (real_decl_result) == BLKmode)
5611 PUT_MODE (real_decl_result,
5612 GET_MODE (DECL_RTL (DECL_RESULT (current_function_decl))));
5613 emit_move_insn (real_decl_result,
5614 DECL_RTL (DECL_RESULT (current_function_decl)));
5615 emit_insn (gen_rtx (USE, VOIDmode, real_decl_result));
5618 /* If returning a structure, arrange to return the address of the value
5619 in a place where debuggers expect to find it.
5621 If returning a structure PCC style,
5622 the caller also depends on this value.
5623 And current_function_returns_pcc_struct is not necessarily set. */
5624 if (current_function_returns_struct
5625 || current_function_returns_pcc_struct)
5627 rtx value_address = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5628 tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5629 #ifdef FUNCTION_OUTGOING_VALUE
5630 rtx outgoing
5631 = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
5632 current_function_decl);
5633 #else
5634 rtx outgoing
5635 = FUNCTION_VALUE (build_pointer_type (type),
5636 current_function_decl);
5637 #endif
5639 /* Mark this as a function return value so integrate will delete the
5640 assignment and USE below when inlining this function. */
5641 REG_FUNCTION_VALUE_P (outgoing) = 1;
5643 emit_move_insn (outgoing, value_address);
5644 use_variable (outgoing);
5647 /* Output a return insn if we are using one.
5648 Otherwise, let the rtl chain end here, to drop through
5649 into the epilogue. */
5651 #ifdef HAVE_return
5652 if (HAVE_return)
5654 emit_jump_insn (gen_return ());
5655 emit_barrier ();
5657 #endif
5659 /* Fix up any gotos that jumped out to the outermost
5660 binding level of the function.
5661 Must follow emitting RETURN_LABEL. */
5663 /* If you have any cleanups to do at this point,
5664 and they need to create temporary variables,
5665 then you will lose. */
5666 expand_fixups (get_insns ());
5669 /* These arrays record the INSN_UIDs of the prologue and epilogue insns. */
5671 static int *prologue;
5672 static int *epilogue;
5674 /* Create an array that records the INSN_UIDs of INSNS (either a sequence
5675 or a single insn). */
5677 static int *
5678 record_insns (insns)
5679 rtx insns;
5681 int *vec;
5683 if (GET_CODE (insns) == SEQUENCE)
5685 int len = XVECLEN (insns, 0);
5686 vec = (int *) oballoc ((len + 1) * sizeof (int));
5687 vec[len] = 0;
5688 while (--len >= 0)
5689 vec[len] = INSN_UID (XVECEXP (insns, 0, len));
5691 else
5693 vec = (int *) oballoc (2 * sizeof (int));
5694 vec[0] = INSN_UID (insns);
5695 vec[1] = 0;
5697 return vec;
5700 /* Determine how many INSN_UIDs in VEC are part of INSN. */
5702 static int
5703 contains (insn, vec)
5704 rtx insn;
5705 int *vec;
5707 register int i, j;
5709 if (GET_CODE (insn) == INSN
5710 && GET_CODE (PATTERN (insn)) == SEQUENCE)
5712 int count = 0;
5713 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5714 for (j = 0; vec[j]; j++)
5715 if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == vec[j])
5716 count++;
5717 return count;
5719 else
5721 for (j = 0; vec[j]; j++)
5722 if (INSN_UID (insn) == vec[j])
5723 return 1;
5725 return 0;
5728 /* Generate the prologue and epilogue RTL if the machine supports it. Thread
5729 this into place with notes indicating where the prologue ends and where
5730 the epilogue begins. Update the basic block information when possible. */
5732 void
5733 thread_prologue_and_epilogue_insns (f)
5734 rtx f;
5736 #ifdef HAVE_prologue
5737 if (HAVE_prologue)
5739 rtx head, seq, insn;
5741 /* The first insn (a NOTE_INSN_DELETED) is followed by zero or more
5742 prologue insns and a NOTE_INSN_PROLOGUE_END. */
5743 emit_note_after (NOTE_INSN_PROLOGUE_END, f);
5744 seq = gen_prologue ();
5745 head = emit_insn_after (seq, f);
5747 /* Include the new prologue insns in the first block. Ignore them
5748 if they form a basic block unto themselves. */
5749 if (basic_block_head && n_basic_blocks
5750 && GET_CODE (basic_block_head[0]) != CODE_LABEL)
5751 basic_block_head[0] = NEXT_INSN (f);
5753 /* Retain a map of the prologue insns. */
5754 prologue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : head);
5756 else
5757 #endif
5758 prologue = 0;
5760 #ifdef HAVE_epilogue
5761 if (HAVE_epilogue)
5763 rtx insn = get_last_insn ();
5764 rtx prev = prev_nonnote_insn (insn);
5766 /* If we end with a BARRIER, we don't need an epilogue. */
5767 if (! (prev && GET_CODE (prev) == BARRIER))
5769 rtx tail, seq, tem;
5770 rtx first_use = 0;
5771 rtx last_use = 0;
5773 /* The last basic block ends with a NOTE_INSN_EPILOGUE_BEG, the
5774 epilogue insns, the USE insns at the end of a function,
5775 the jump insn that returns, and then a BARRIER. */
5777 /* Move the USE insns at the end of a function onto a list. */
5778 while (prev
5779 && GET_CODE (prev) == INSN
5780 && GET_CODE (PATTERN (prev)) == USE)
5782 tem = prev;
5783 prev = prev_nonnote_insn (prev);
5785 NEXT_INSN (PREV_INSN (tem)) = NEXT_INSN (tem);
5786 PREV_INSN (NEXT_INSN (tem)) = PREV_INSN (tem);
5787 if (first_use)
5789 NEXT_INSN (tem) = first_use;
5790 PREV_INSN (first_use) = tem;
5792 first_use = tem;
5793 if (!last_use)
5794 last_use = tem;
5797 emit_barrier_after (insn);
5799 seq = gen_epilogue ();
5800 tail = emit_jump_insn_after (seq, insn);
5802 /* Insert the USE insns immediately before the return insn, which
5803 must be the first instruction before the final barrier. */
5804 if (first_use)
5806 tem = prev_nonnote_insn (get_last_insn ());
5807 NEXT_INSN (PREV_INSN (tem)) = first_use;
5808 PREV_INSN (first_use) = PREV_INSN (tem);
5809 PREV_INSN (tem) = last_use;
5810 NEXT_INSN (last_use) = tem;
5813 emit_note_after (NOTE_INSN_EPILOGUE_BEG, insn);
5815 /* Include the new epilogue insns in the last block. Ignore
5816 them if they form a basic block unto themselves. */
5817 if (basic_block_end && n_basic_blocks
5818 && GET_CODE (basic_block_end[n_basic_blocks - 1]) != JUMP_INSN)
5819 basic_block_end[n_basic_blocks - 1] = tail;
5821 /* Retain a map of the epilogue insns. */
5822 epilogue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : tail);
5823 return;
5826 #endif
5827 epilogue = 0;
5830 /* Reposition the prologue-end and epilogue-begin notes after instruction
5831 scheduling and delayed branch scheduling. */
5833 void
5834 reposition_prologue_and_epilogue_notes (f)
5835 rtx f;
5837 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
5838 /* Reposition the prologue and epilogue notes. */
5839 if (n_basic_blocks)
5841 rtx next, prev;
5842 int len;
5844 if (prologue)
5846 register rtx insn, note = 0;
5848 /* Scan from the beginning until we reach the last prologue insn.
5849 We apparently can't depend on basic_block_{head,end} after
5850 reorg has run. */
5851 for (len = 0; prologue[len]; len++)
5853 for (insn = f; len && insn; insn = NEXT_INSN (insn))
5855 if (GET_CODE (insn) == NOTE)
5857 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
5858 note = insn;
5860 else if ((len -= contains (insn, prologue)) == 0)
5862 /* Find the prologue-end note if we haven't already, and
5863 move it to just after the last prologue insn. */
5864 if (note == 0)
5866 for (note = insn; note = NEXT_INSN (note);)
5867 if (GET_CODE (note) == NOTE
5868 && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
5869 break;
5871 next = NEXT_INSN (note);
5872 prev = PREV_INSN (note);
5873 if (prev)
5874 NEXT_INSN (prev) = next;
5875 if (next)
5876 PREV_INSN (next) = prev;
5877 add_insn_after (note, insn);
5882 if (epilogue)
5884 register rtx insn, note = 0;
5886 /* Scan from the end until we reach the first epilogue insn.
5887 We apparently can't depend on basic_block_{head,end} after
5888 reorg has run. */
5889 for (len = 0; epilogue[len]; len++)
5891 for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
5893 if (GET_CODE (insn) == NOTE)
5895 if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
5896 note = insn;
5898 else if ((len -= contains (insn, epilogue)) == 0)
5900 /* Find the epilogue-begin note if we haven't already, and
5901 move it to just before the first epilogue insn. */
5902 if (note == 0)
5904 for (note = insn; note = PREV_INSN (note);)
5905 if (GET_CODE (note) == NOTE
5906 && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
5907 break;
5909 next = NEXT_INSN (note);
5910 prev = PREV_INSN (note);
5911 if (prev)
5912 NEXT_INSN (prev) = next;
5913 if (next)
5914 PREV_INSN (next) = prev;
5915 add_insn_after (note, PREV_INSN (insn));
5920 #endif /* HAVE_prologue or HAVE_epilogue */