OSDN Git Service

(instantiate_decls): Check DECL_SAVED_INSNS to see if obstack change
[pf3gnuchains/gcc-fork.git] / gcc / function.c
1 /* Expands front end tree to back end RTL for GNU C-Compiler
2    Copyright (C) 1987, 88, 89, 91-94, 1995 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
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.
26
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.
30
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.
35
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.  */
40
41 #include "config.h"
42
43 #include <stdio.h>
44
45 #include "rtl.h"
46 #include "tree.h"
47 #include "flags.h"
48 #include "function.h"
49 #include "insn-flags.h"
50 #include "expr.h"
51 #include "insn-codes.h"
52 #include "regs.h"
53 #include "hard-reg-set.h"
54 #include "insn-config.h"
55 #include "recog.h"
56 #include "output.h"
57 #include "basic-block.h"
58 #include "obstack.h"
59 #include "bytecode.h"
60
61 /* Some systems use __main in a way incompatible with its use in gcc, in these
62    cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
63    give the same symbol without quotes for an alternative entry point.  You
64    must define both, or neither. */
65 #ifndef NAME__MAIN
66 #define NAME__MAIN "__main"
67 #define SYMBOL__MAIN __main
68 #endif
69
70 /* Round a value to the lowest integer less than it that is a multiple of
71    the required alignment.  Avoid using division in case the value is
72    negative.  Assume the alignment is a power of two.  */
73 #define FLOOR_ROUND(VALUE,ALIGN) ((VALUE) & ~((ALIGN) - 1))
74
75 /* Similar, but round to the next highest integer that meets the
76    alignment.  */
77 #define CEIL_ROUND(VALUE,ALIGN) (((VALUE) + (ALIGN) - 1) & ~((ALIGN)- 1))
78
79 /* NEED_SEPARATE_AP means that we cannot derive ap from the value of fp
80    during rtl generation.  If they are different register numbers, this is
81    always true.  It may also be true if
82    FIRST_PARM_OFFSET - STARTING_FRAME_OFFSET is not a constant during rtl
83    generation.  See fix_lexical_addr for details.  */
84
85 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
86 #define NEED_SEPARATE_AP
87 #endif
88
89 /* Number of bytes of args popped by function being compiled on its return.
90    Zero if no bytes are to be popped.
91    May affect compilation of return insn or of function epilogue.  */
92
93 int current_function_pops_args;
94
95 /* Nonzero if function being compiled needs to be given an address
96    where the value should be stored.  */
97
98 int current_function_returns_struct;
99
100 /* Nonzero if function being compiled needs to
101    return the address of where it has put a structure value.  */
102
103 int current_function_returns_pcc_struct;
104
105 /* Nonzero if function being compiled needs to be passed a static chain.  */
106
107 int current_function_needs_context;
108
109 /* Nonzero if function being compiled can call setjmp.  */
110
111 int current_function_calls_setjmp;
112
113 /* Nonzero if function being compiled can call longjmp.  */
114
115 int current_function_calls_longjmp;
116
117 /* Nonzero if function being compiled receives nonlocal gotos
118    from nested functions.  */
119
120 int current_function_has_nonlocal_label;
121
122 /* Nonzero if function being compiled has nonlocal gotos to parent
123    function.  */
124
125 int current_function_has_nonlocal_goto;
126
127 /* Nonzero if function being compiled contains nested functions.  */
128
129 int current_function_contains_functions;
130
131 /* Nonzero if function being compiled can call alloca,
132    either as a subroutine or builtin.  */
133
134 int current_function_calls_alloca;
135
136 /* Nonzero if the current function returns a pointer type */
137
138 int current_function_returns_pointer;
139
140 /* If some insns can be deferred to the delay slots of the epilogue, the
141    delay list for them is recorded here.  */
142
143 rtx current_function_epilogue_delay_list;
144
145 /* If function's args have a fixed size, this is that size, in bytes.
146    Otherwise, it is -1.
147    May affect compilation of return insn or of function epilogue.  */
148
149 int current_function_args_size;
150
151 /* # bytes the prologue should push and pretend that the caller pushed them.
152    The prologue must do this, but only if parms can be passed in registers.  */
153
154 int current_function_pretend_args_size;
155
156 /* # of bytes of outgoing arguments.  If ACCUMULATE_OUTGOING_ARGS is
157    defined, the needed space is pushed by the prologue. */
158
159 int current_function_outgoing_args_size;
160
161 /* This is the offset from the arg pointer to the place where the first
162    anonymous arg can be found, if there is one.  */
163
164 rtx current_function_arg_offset_rtx;
165
166 /* Nonzero if current function uses varargs.h or equivalent.
167    Zero for functions that use stdarg.h.  */
168
169 int current_function_varargs;
170
171 /* Nonzero if current function uses stdarg.h or equivalent.
172    Zero for functions that use varargs.h.  */
173
174 int current_function_stdarg;
175
176 /* Quantities of various kinds of registers
177    used for the current function's args.  */
178
179 CUMULATIVE_ARGS current_function_args_info;
180
181 /* Name of function now being compiled.  */
182
183 char *current_function_name;
184
185 /* If non-zero, an RTL expression for that location at which the current
186    function returns its result.  Always equal to
187    DECL_RTL (DECL_RESULT (current_function_decl)), but provided
188    independently of the tree structures.  */
189
190 rtx current_function_return_rtx;
191
192 /* Nonzero if the current function uses the constant pool.  */
193
194 int current_function_uses_const_pool;
195
196 /* Nonzero if the current function uses pic_offset_table_rtx.  */
197 int current_function_uses_pic_offset_table;
198
199 /* The arg pointer hard register, or the pseudo into which it was copied.  */
200 rtx current_function_internal_arg_pointer;
201
202 /* The FUNCTION_DECL for an inline function currently being expanded.  */
203 tree inline_function_decl;
204
205 /* Number of function calls seen so far in current function.  */
206
207 int function_call_count;
208
209 /* List (chain of TREE_LIST) of LABEL_DECLs for all nonlocal labels
210    (labels to which there can be nonlocal gotos from nested functions)
211    in this function.  */
212
213 tree nonlocal_labels;
214
215 /* RTX for stack slot that holds the current handler for nonlocal gotos.
216    Zero when function does not have nonlocal labels.  */
217
218 rtx nonlocal_goto_handler_slot;
219
220 /* RTX for stack slot that holds the stack pointer value to restore
221    for a nonlocal goto.
222    Zero when function does not have nonlocal labels.  */
223
224 rtx nonlocal_goto_stack_level;
225
226 /* Label that will go on parm cleanup code, if any.
227    Jumping to this label runs cleanup code for parameters, if
228    such code must be run.  Following this code is the logical return label.  */
229
230 rtx cleanup_label;
231
232 /* Label that will go on function epilogue.
233    Jumping to this label serves as a "return" instruction
234    on machines which require execution of the epilogue on all returns.  */
235
236 rtx return_label;
237
238 /* List (chain of EXPR_LISTs) of pseudo-regs of SAVE_EXPRs.
239    So we can mark them all live at the end of the function, if nonopt.  */
240 rtx save_expr_regs;
241
242 /* List (chain of EXPR_LISTs) of all stack slots in this function.
243    Made for the sake of unshare_all_rtl.  */
244 rtx stack_slot_list;
245
246 /* Chain of all RTL_EXPRs that have insns in them.  */
247 tree rtl_expr_chain;
248
249 /* Label to jump back to for tail recursion, or 0 if we have
250    not yet needed one for this function.  */
251 rtx tail_recursion_label;
252
253 /* Place after which to insert the tail_recursion_label if we need one.  */
254 rtx tail_recursion_reentry;
255
256 /* Location at which to save the argument pointer if it will need to be
257    referenced.  There are two cases where this is done: if nonlocal gotos
258    exist, or if vars stored at an offset from the argument pointer will be
259    needed by inner routines.  */
260
261 rtx arg_pointer_save_area;
262
263 /* Offset to end of allocated area of stack frame.
264    If stack grows down, this is the address of the last stack slot allocated.
265    If stack grows up, this is the address for the next slot.  */
266 int frame_offset;
267
268 /* List (chain of TREE_LISTs) of static chains for containing functions.
269    Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
270    in an RTL_EXPR in the TREE_VALUE.  */
271 static tree context_display;
272
273 /* List (chain of TREE_LISTs) of trampolines for nested functions.
274    The trampoline sets up the static chain and jumps to the function.
275    We supply the trampoline's address when the function's address is requested.
276
277    Each link has a FUNCTION_DECL in the TREE_PURPOSE and a reg rtx
278    in an RTL_EXPR in the TREE_VALUE.  */
279 static tree trampoline_list;
280
281 /* Insn after which register parms and SAVE_EXPRs are born, if nonopt.  */
282 static rtx parm_birth_insn;
283
284 #if 0
285 /* Nonzero if a stack slot has been generated whose address is not
286    actually valid.  It means that the generated rtl must all be scanned
287    to detect and correct the invalid addresses where they occur.  */
288 static int invalid_stack_slot;
289 #endif
290
291 /* Last insn of those whose job was to put parms into their nominal homes.  */
292 static rtx last_parm_insn;
293
294 /* 1 + last pseudo register number used for loading a copy
295    of a parameter of this function.  */
296 static int max_parm_reg;
297
298 /* Vector indexed by REGNO, containing location on stack in which
299    to put the parm which is nominally in pseudo register REGNO,
300    if we discover that that parm must go in the stack.  */
301 static rtx *parm_reg_stack_loc;
302
303 #if 0  /* Turned off because 0 seems to work just as well.  */
304 /* Cleanup lists are required for binding levels regardless of whether
305    that binding level has cleanups or not.  This node serves as the
306    cleanup list whenever an empty list is required.  */
307 static tree empty_cleanup_list;
308 #endif
309
310 /* Nonzero once virtual register instantiation has been done.
311    assign_stack_local uses frame_pointer_rtx when this is nonzero.  */
312 static int virtuals_instantiated;
313
314 /* These variables hold pointers to functions to
315    save and restore machine-specific data,
316    in push_function_context and pop_function_context.  */
317 void (*save_machine_status) ();
318 void (*restore_machine_status) ();
319
320 /* Nonzero if we need to distinguish between the return value of this function
321    and the return value of a function called by this function.  This helps
322    integrate.c  */
323
324 extern int rtx_equal_function_value_matters;
325 extern tree sequence_rtl_expr;
326 extern tree bc_runtime_type_code ();
327 extern rtx bc_build_calldesc ();
328 extern char *bc_emit_trampoline ();
329 extern char *bc_end_function ();
330 \f
331 /* In order to evaluate some expressions, such as function calls returning
332    structures in memory, we need to temporarily allocate stack locations.
333    We record each allocated temporary in the following structure.
334
335    Associated with each temporary slot is a nesting level.  When we pop up
336    one level, all temporaries associated with the previous level are freed.
337    Normally, all temporaries are freed after the execution of the statement
338    in which they were created.  However, if we are inside a ({...}) grouping,
339    the result may be in a temporary and hence must be preserved.  If the
340    result could be in a temporary, we preserve it if we can determine which
341    one it is in.  If we cannot determine which temporary may contain the
342    result, all temporaries are preserved.  A temporary is preserved by
343    pretending it was allocated at the previous nesting level.
344
345    Automatic variables are also assigned temporary slots, at the nesting
346    level where they are defined.  They are marked a "kept" so that
347    free_temp_slots will not free them.  */
348
349 struct temp_slot
350 {
351   /* Points to next temporary slot.  */
352   struct temp_slot *next;
353   /* The rtx to used to reference the slot. */
354   rtx slot;
355   /* The rtx used to represent the address if not the address of the
356      slot above.  May be an EXPR_LIST if multiple addresses exist.  */
357   rtx address;
358   /* The size, in units, of the slot.  */
359   int size;
360   /* The value of `sequence_rtl_expr' when this temporary is allocated.  */
361   tree rtl_expr;
362   /* Non-zero if this temporary is currently in use.  */
363   char in_use;
364   /* Non-zero if this temporary has its address taken.  */
365   char addr_taken;
366   /* Nesting level at which this slot is being used.  */
367   int level;
368   /* Non-zero if this should survive a call to free_temp_slots.  */
369   int keep;
370   /* The offset of the slot from the frame_pointer, including extra space
371      for alignment.  This info is for combine_temp_slots.  */
372   int base_offset;
373   /* The size of the slot, including extra space for alignment.  This
374      info is for combine_temp_slots.  */
375   int full_size;
376 };
377
378 /* List of all temporaries allocated, both available and in use.  */
379
380 struct temp_slot *temp_slots;
381
382 /* Current nesting level for temporaries.  */
383
384 int temp_slot_level;
385 \f
386 /* The FUNCTION_DECL node for the current function.  */
387 static tree this_function_decl;
388
389 /* Callinfo pointer for the current function.  */
390 static rtx this_function_callinfo;
391
392 /* The label in the bytecode file of this function's actual bytecode.
393    Not an rtx.  */
394 static char *this_function_bytecode;
395
396 /* The call description vector for the current function.  */
397 static rtx this_function_calldesc;
398
399 /* Size of the local variables allocated for the current function.  */
400 int local_vars_size;
401
402 /* Current depth of the bytecode evaluation stack.  */
403 int stack_depth;
404
405 /* Maximum depth of the evaluation stack in this function.  */
406 int max_stack_depth;
407
408 /* Current depth in statement expressions.  */
409 static int stmt_expr_depth;
410
411 /* This structure is used to record MEMs or pseudos used to replace VAR, any
412    SUBREGs of VAR, and any MEMs containing VAR as an address.  We need to
413    maintain this list in case two operands of an insn were required to match;
414    in that case we must ensure we use the same replacement.  */
415
416 struct fixup_replacement
417 {
418   rtx old;
419   rtx new;
420   struct fixup_replacement *next;
421 };
422    
423 /* Forward declarations.  */
424
425 static struct temp_slot *find_temp_slot_from_address  PROTO((rtx));
426 static void put_reg_into_stack  PROTO((struct function *, rtx, tree,
427                                        enum machine_mode, enum machine_mode,
428                                        int));
429 static void fixup_var_refs      PROTO((rtx, enum machine_mode, int));
430 static struct fixup_replacement
431   *find_fixup_replacement       PROTO((struct fixup_replacement **, rtx));
432 static void fixup_var_refs_insns PROTO((rtx, enum machine_mode, int,
433                                         rtx, int));
434 static void fixup_var_refs_1    PROTO((rtx, enum machine_mode, rtx *, rtx,
435                                        struct fixup_replacement **));
436 static rtx fixup_memory_subreg  PROTO((rtx, rtx, int));
437 static rtx walk_fixup_memory_subreg  PROTO((rtx, rtx, int));
438 static rtx fixup_stack_1        PROTO((rtx, rtx));
439 static void optimize_bit_field  PROTO((rtx, rtx, rtx *));
440 static void instantiate_decls   PROTO((tree, int));
441 static void instantiate_decls_1 PROTO((tree, int));
442 static void instantiate_decl    PROTO((rtx, int, int));
443 static int instantiate_virtual_regs_1 PROTO((rtx *, rtx, int));
444 static void delete_handlers     PROTO((void));
445 static void pad_to_arg_alignment PROTO((struct args_size *, int));
446 static void pad_below           PROTO((struct args_size *, enum  machine_mode,
447                                        tree));
448 static tree round_down          PROTO((tree, int));
449 static rtx round_trampoline_addr PROTO((rtx));
450 static tree blocks_nreverse     PROTO((tree));
451 static int all_blocks           PROTO((tree, tree *));
452 static int *record_insns        PROTO((rtx));
453 static int contains             PROTO((rtx, int *));
454 \f
455 /* Pointer to chain of `struct function' for containing functions.  */
456 struct function *outer_function_chain;
457
458 /* Given a function decl for a containing function,
459    return the `struct function' for it.  */
460
461 struct function *
462 find_function_data (decl)
463      tree decl;
464 {
465   struct function *p;
466   for (p = outer_function_chain; p; p = p->next)
467     if (p->decl == decl)
468       return p;
469   abort ();
470 }
471
472 /* Save the current context for compilation of a nested function.
473    This is called from language-specific code.
474    The caller is responsible for saving any language-specific status,
475    since this function knows only about language-independent variables.  */
476
477 void
478 push_function_context_to (context)
479      tree context;
480 {
481   struct function *p = (struct function *) xmalloc (sizeof (struct function));
482
483   p->next = outer_function_chain;
484   outer_function_chain = p;
485
486   p->name = current_function_name;
487   p->decl = current_function_decl;
488   p->pops_args = current_function_pops_args;
489   p->returns_struct = current_function_returns_struct;
490   p->returns_pcc_struct = current_function_returns_pcc_struct;
491   p->needs_context = current_function_needs_context;
492   p->calls_setjmp = current_function_calls_setjmp;
493   p->calls_longjmp = current_function_calls_longjmp;
494   p->calls_alloca = current_function_calls_alloca;
495   p->has_nonlocal_label = current_function_has_nonlocal_label;
496   p->has_nonlocal_goto = current_function_has_nonlocal_goto;
497   p->contains_functions = current_function_contains_functions;
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
532   save_tree_status (p, context);
533   save_storage_status (p);
534   save_emit_status (p);
535   init_emit ();
536   save_expr_status (p);
537   save_stmt_status (p);
538   save_varasm_status (p);
539
540   if (save_machine_status)
541     (*save_machine_status) (p);
542 }
543
544 void
545 push_function_context ()
546 {
547   push_function_context_to (current_function_decl);
548 }
549
550 /* Restore the last saved context, at the end of a nested function.
551    This function is called from language-specific code.  */
552
553 void
554 pop_function_context_from (context)
555      tree context;
556 {
557   struct function *p = outer_function_chain;
558
559   outer_function_chain = p->next;
560
561   current_function_contains_functions
562     = p->contains_functions || p->inline_obstacks
563       || context == current_function_decl;
564   current_function_name = p->name;
565   current_function_decl = p->decl;
566   current_function_pops_args = p->pops_args;
567   current_function_returns_struct = p->returns_struct;
568   current_function_returns_pcc_struct = p->returns_pcc_struct;
569   current_function_needs_context = p->needs_context;
570   current_function_calls_setjmp = p->calls_setjmp;
571   current_function_calls_longjmp = p->calls_longjmp;
572   current_function_calls_alloca = p->calls_alloca;
573   current_function_has_nonlocal_label = p->has_nonlocal_label;
574   current_function_has_nonlocal_goto = p->has_nonlocal_goto;
575   current_function_args_size = p->args_size;
576   current_function_pretend_args_size = p->pretend_args_size;
577   current_function_arg_offset_rtx = p->arg_offset_rtx;
578   current_function_varargs = p->varargs;
579   current_function_stdarg = p->stdarg;
580   current_function_uses_const_pool = p->uses_const_pool;
581   current_function_uses_pic_offset_table = p->uses_pic_offset_table;
582   current_function_internal_arg_pointer = p->internal_arg_pointer;
583   max_parm_reg = p->max_parm_reg;
584   parm_reg_stack_loc = p->parm_reg_stack_loc;
585   current_function_outgoing_args_size = p->outgoing_args_size;
586   current_function_return_rtx = p->return_rtx;
587   nonlocal_goto_handler_slot = p->nonlocal_goto_handler_slot;
588   nonlocal_goto_stack_level = p->nonlocal_goto_stack_level;
589   nonlocal_labels = p->nonlocal_labels;
590   cleanup_label = p->cleanup_label;
591   return_label = p->return_label;
592   save_expr_regs = p->save_expr_regs;
593   stack_slot_list = p->stack_slot_list;
594   parm_birth_insn = p->parm_birth_insn;
595   frame_offset = p->frame_offset;
596   tail_recursion_label = p->tail_recursion_label;
597   tail_recursion_reentry = p->tail_recursion_reentry;
598   arg_pointer_save_area = p->arg_pointer_save_area;
599   rtl_expr_chain = p->rtl_expr_chain;
600   last_parm_insn = p->last_parm_insn;
601   context_display = p->context_display;
602   trampoline_list = p->trampoline_list;
603   function_call_count = p->function_call_count;
604   temp_slots = p->temp_slots;
605   temp_slot_level = p->temp_slot_level;
606   current_function_epilogue_delay_list = p->epilogue_delay_list;
607   reg_renumber = 0;
608
609   restore_tree_status (p);
610   restore_storage_status (p);
611   restore_expr_status (p);
612   restore_emit_status (p);
613   restore_stmt_status (p);
614   restore_varasm_status (p);
615
616   if (restore_machine_status)
617     (*restore_machine_status) (p);
618
619   /* Finish doing put_var_into_stack for any of our variables
620      which became addressable during the nested function.  */
621   {
622     struct var_refs_queue *queue = p->fixup_var_refs_queue;
623     for (; queue; queue = queue->next)
624       fixup_var_refs (queue->modified, queue->promoted_mode, queue->unsignedp);
625   }
626
627   free (p);
628
629   /* Reset variables that have known state during rtx generation.  */
630   rtx_equal_function_value_matters = 1;
631   virtuals_instantiated = 0;
632 }
633
634 void pop_function_context ()
635 {
636   pop_function_context_from (current_function_decl);
637 }
638 \f
639 /* Allocate fixed slots in the stack frame of the current function.  */
640
641 /* Return size needed for stack frame based on slots so far allocated.
642    This size counts from zero.  It is not rounded to STACK_BOUNDARY;
643    the caller may have to do that.  */
644
645 int
646 get_frame_size ()
647 {
648 #ifdef FRAME_GROWS_DOWNWARD
649   return -frame_offset;
650 #else
651   return frame_offset;
652 #endif
653 }
654
655 /* Allocate a stack slot of SIZE bytes and return a MEM rtx for it
656    with machine mode MODE.
657    
658    ALIGN controls the amount of alignment for the address of the slot:
659    0 means according to MODE,
660    -1 means use BIGGEST_ALIGNMENT and round size to multiple of that,
661    positive specifies alignment boundary in bits.
662
663    We do not round to stack_boundary here.  */
664
665 rtx
666 assign_stack_local (mode, size, align)
667      enum machine_mode mode;
668      int size;
669      int align;
670 {
671   register rtx x, addr;
672   int bigend_correction = 0;
673   int alignment;
674
675   if (align == 0)
676     {
677       alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
678       if (mode == BLKmode)
679         alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
680     }
681   else if (align == -1)
682     {
683       alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
684       size = CEIL_ROUND (size, alignment);
685     }
686   else
687     alignment = align / BITS_PER_UNIT;
688
689   /* Round frame offset to that alignment.
690      We must be careful here, since FRAME_OFFSET might be negative and
691      division with a negative dividend isn't as well defined as we might
692      like.  So we instead assume that ALIGNMENT is a power of two and
693      use logical operations which are unambiguous.  */
694 #ifdef FRAME_GROWS_DOWNWARD
695   frame_offset = FLOOR_ROUND (frame_offset, alignment);
696 #else
697   frame_offset = CEIL_ROUND (frame_offset, alignment);
698 #endif
699
700   /* On a big-endian machine, if we are allocating more space than we will use,
701      use the least significant bytes of those that are allocated.  */
702   if (BYTES_BIG_ENDIAN && mode != BLKmode)
703     bigend_correction = size - GET_MODE_SIZE (mode);
704
705 #ifdef FRAME_GROWS_DOWNWARD
706   frame_offset -= size;
707 #endif
708
709   /* If we have already instantiated virtual registers, return the actual
710      address relative to the frame pointer.  */
711   if (virtuals_instantiated)
712     addr = plus_constant (frame_pointer_rtx,
713                           (frame_offset + bigend_correction
714                            + STARTING_FRAME_OFFSET));
715   else
716     addr = plus_constant (virtual_stack_vars_rtx,
717                           frame_offset + bigend_correction);
718
719 #ifndef FRAME_GROWS_DOWNWARD
720   frame_offset += size;
721 #endif
722
723   x = gen_rtx (MEM, mode, addr);
724
725   stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, x, stack_slot_list);
726
727   return x;
728 }
729
730 /* Assign a stack slot in a containing function.
731    First three arguments are same as in preceding function.
732    The last argument specifies the function to allocate in.  */
733
734 rtx
735 assign_outer_stack_local (mode, size, align, function)
736      enum machine_mode mode;
737      int size;
738      int align;
739      struct function *function;
740 {
741   register rtx x, addr;
742   int bigend_correction = 0;
743   int alignment;
744
745   /* Allocate in the memory associated with the function in whose frame
746      we are assigning.  */
747   push_obstacks (function->function_obstack,
748                  function->function_maybepermanent_obstack);
749
750   if (align == 0)
751     {
752       alignment = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
753       if (mode == BLKmode)
754         alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
755     }
756   else if (align == -1)
757     {
758       alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
759       size = CEIL_ROUND (size, alignment);
760     }
761   else
762     alignment = align / BITS_PER_UNIT;
763
764   /* Round frame offset to that alignment.  */
765 #ifdef FRAME_GROWS_DOWNWARD
766   function->frame_offset = FLOOR_ROUND (function->frame_offset, alignment);
767 #else
768   function->frame_offset = CEIL_ROUND (function->frame_offset, alignment);
769 #endif
770
771   /* On a big-endian machine, if we are allocating more space than we will use,
772      use the least significant bytes of those that are allocated.  */
773   if (BYTES_BIG_ENDIAN && mode != BLKmode)
774     bigend_correction = size - GET_MODE_SIZE (mode);
775
776 #ifdef FRAME_GROWS_DOWNWARD
777   function->frame_offset -= size;
778 #endif
779   addr = plus_constant (virtual_stack_vars_rtx,
780                         function->frame_offset + bigend_correction);
781 #ifndef FRAME_GROWS_DOWNWARD
782   function->frame_offset += size;
783 #endif
784
785   x = gen_rtx (MEM, mode, addr);
786
787   function->stack_slot_list
788     = gen_rtx (EXPR_LIST, VOIDmode, x, function->stack_slot_list);
789
790   pop_obstacks ();
791
792   return x;
793 }
794 \f
795 /* Allocate a temporary stack slot and record it for possible later
796    reuse.
797
798    MODE is the machine mode to be given to the returned rtx.
799
800    SIZE is the size in units of the space required.  We do no rounding here
801    since assign_stack_local will do any required rounding.
802
803    KEEP is 1 if this slot is to be retained after a call to
804    free_temp_slots.  Automatic variables for a block are allocated
805    with this flag.  KEEP is 2, if we allocate a longer term temporary,
806    whose lifetime is controlled by CLEANUP_POINT_EXPRs.  */
807
808 rtx
809 assign_stack_temp (mode, size, keep)
810      enum machine_mode mode;
811      int size;
812      int keep;
813 {
814   struct temp_slot *p, *best_p = 0;
815
816   /* If SIZE is -1 it means that somebody tried to allocate a temporary
817      of a variable size.  */
818   if (size == -1)
819     abort ();
820
821   /* First try to find an available, already-allocated temporary that is the
822      exact size we require.  */
823   for (p = temp_slots; p; p = p->next)
824     if (p->size == size && GET_MODE (p->slot) == mode && ! p->in_use)
825       break;
826
827   /* If we didn't find, one, try one that is larger than what we want.  We
828      find the smallest such.  */
829   if (p == 0)
830     for (p = temp_slots; p; p = p->next)
831       if (p->size > size && GET_MODE (p->slot) == mode && ! p->in_use
832           && (best_p == 0 || best_p->size > p->size))
833         best_p = p;
834
835   /* Make our best, if any, the one to use.  */
836   if (best_p)
837     {
838       /* If there are enough aligned bytes left over, make them into a new
839          temp_slot so that the extra bytes don't get wasted.  Do this only
840          for BLKmode slots, so that we can be sure of the alignment.  */
841       if (GET_MODE (best_p->slot) == BLKmode)
842         {
843           int alignment = BIGGEST_ALIGNMENT / BITS_PER_UNIT;
844           int rounded_size = CEIL_ROUND (size, alignment);
845
846           if (best_p->size - rounded_size >= alignment)
847             {
848               p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
849               p->in_use = p->addr_taken = 0;
850               p->size = best_p->size - rounded_size;
851               p->base_offset = best_p->base_offset + rounded_size;
852               p->full_size = best_p->full_size - rounded_size;
853               p->slot = gen_rtx (MEM, BLKmode,
854                                  plus_constant (XEXP (best_p->slot, 0),
855                                                 rounded_size));
856               p->address = 0;
857               p->rtl_expr = 0;
858               p->next = temp_slots;
859               temp_slots = p;
860
861               stack_slot_list = gen_rtx (EXPR_LIST, VOIDmode, p->slot,
862                                          stack_slot_list);
863
864               best_p->size = rounded_size;
865               best_p->full_size = rounded_size;
866             }
867         }
868
869       p = best_p;
870     }
871               
872   /* If we still didn't find one, make a new temporary.  */
873   if (p == 0)
874     {
875       int frame_offset_old = frame_offset;
876       p = (struct temp_slot *) oballoc (sizeof (struct temp_slot));
877       /* If the temp slot mode doesn't indicate the alignment,
878          use the largest possible, so no one will be disappointed.  */
879       p->slot = assign_stack_local (mode, size, mode == BLKmode ? -1 : 0);
880       /* The following slot size computation is necessary because we don't
881          know the actual size of the temporary slot until assign_stack_local
882          has performed all the frame alignment and size rounding for the
883          requested temporary.  Note that extra space added for alignment
884          can be either above or below this stack slot depending on which
885          way the frame grows.  We include the extra space if and only if it
886          is above this slot.  */
887 #ifdef FRAME_GROWS_DOWNWARD
888       p->size = frame_offset_old - frame_offset;
889 #else
890       p->size = size;
891 #endif
892       /* Now define the fields used by combine_temp_slots.  */
893 #ifdef FRAME_GROWS_DOWNWARD
894       p->base_offset = frame_offset;
895       p->full_size = frame_offset_old - frame_offset;
896 #else
897       p->base_offset = frame_offset_old;
898       p->full_size = frame_offset - frame_offset_old;
899 #endif
900       p->address = 0;
901       p->next = temp_slots;
902       temp_slots = p;
903     }
904
905   p->in_use = 1;
906   p->addr_taken = 0;
907   p->rtl_expr = sequence_rtl_expr;
908
909   if (keep == 2)
910     {
911       p->level = target_temp_slot_level;
912       p->keep = 0;
913     }
914   else
915     {
916       p->level = temp_slot_level;
917       p->keep = keep;
918     }
919   return p->slot;
920 }
921 \f
922 /* Assign a temporary of given TYPE.
923    KEEP is as for assign_stack_temp.
924    MEMORY_REQUIRED is 1 if the result must be addressable stack memory;
925    it is 0 if a register is OK. */
926
927 rtx
928 assign_temp (type, keep, memory_required)
929      tree type;
930      int keep;
931      int memory_required;
932 {
933   enum machine_mode mode = TYPE_MODE (type);
934   int unsignedp = TREE_UNSIGNED (type);
935
936   if (mode == BLKmode || memory_required)
937     {
938       int size = int_size_in_bytes (type);
939       rtx tmp;
940
941       /* Unfortunately, we don't yet know how to allocate variable-sized
942          temporaries.  However, sometimes we have a fixed upper limit on
943          the size (which is stored in TYPE_ARRAY_MAX_SIZE) and can use that
944          instead.  This is the case for Chill variable-sized strings. */
945       if (size == -1 && TREE_CODE (type) == ARRAY_TYPE
946           && TYPE_ARRAY_MAX_SIZE (type) != NULL_TREE
947           && TREE_CODE (TYPE_ARRAY_MAX_SIZE (type)) == INTEGER_CST)
948         size = TREE_INT_CST_LOW (TYPE_ARRAY_MAX_SIZE (type));
949
950       tmp = assign_stack_temp (mode, size, keep);
951       MEM_IN_STRUCT_P (tmp) = AGGREGATE_TYPE_P (type);
952       return tmp;
953     }
954
955   if (mode == VOIDmode)
956     return const0_rtx;
957
958 #ifndef PROMOTE_FOR_CALL_ONLY
959   mode = promote_mode (type, mode, &unsignedp, 0);
960 #endif
961
962   return gen_reg_rtx (mode);
963 }
964 \f
965 /* Combine temporary stack slots which are adjacent on the stack.
966
967    This allows for better use of already allocated stack space.  This is only
968    done for BLKmode slots because we can be sure that we won't have alignment
969    problems in this case.  */
970
971 void
972 combine_temp_slots ()
973 {
974   struct temp_slot *p, *q;
975   struct temp_slot *prev_p, *prev_q;
976   /* Determine where to free back to after this function.  */
977   rtx free_pointer = rtx_alloc (CONST_INT);
978
979   for (p = temp_slots, prev_p = 0; p; p = prev_p ? prev_p->next : temp_slots)
980     {
981       int delete_p = 0;
982       if (! p->in_use && GET_MODE (p->slot) == BLKmode)
983         for (q = p->next, prev_q = p; q; q = prev_q->next)
984           {
985             int delete_q = 0;
986             if (! q->in_use && GET_MODE (q->slot) == BLKmode)
987               {
988                 if (p->base_offset + p->full_size == q->base_offset)
989                   {
990                     /* Q comes after P; combine Q into P.  */
991                     p->size += q->size;
992                     p->full_size += q->full_size;
993                     delete_q = 1;
994                   }
995                 else if (q->base_offset + q->full_size == p->base_offset)
996                   {
997                     /* P comes after Q; combine P into Q.  */
998                     q->size += p->size;
999                     q->full_size += p->full_size;
1000                     delete_p = 1;
1001                     break;
1002                   }
1003               }
1004             /* Either delete Q or advance past it.  */
1005             if (delete_q)
1006               prev_q->next = q->next;
1007             else
1008               prev_q = q;
1009           }
1010       /* Either delete P or advance past it.  */
1011       if (delete_p)
1012         {
1013           if (prev_p)
1014             prev_p->next = p->next;
1015           else
1016             temp_slots = p->next;
1017         }
1018       else
1019         prev_p = p;
1020     }
1021
1022   /* Free all the RTL made by plus_constant.  */ 
1023   rtx_free (free_pointer);
1024 }
1025 \f
1026 /* Find the temp slot corresponding to the object at address X.  */
1027
1028 static struct temp_slot *
1029 find_temp_slot_from_address (x)
1030      rtx x;
1031 {
1032   struct temp_slot *p;
1033   rtx next;
1034
1035   for (p = temp_slots; p; p = p->next)
1036     {
1037       if (! p->in_use)
1038         continue;
1039       else if (XEXP (p->slot, 0) == x
1040                || p->address == x)
1041         return p;
1042
1043       else if (p->address != 0 && GET_CODE (p->address) == EXPR_LIST)
1044         for (next = p->address; next; next = XEXP (next, 1))
1045           if (XEXP (next, 0) == x)
1046             return p;
1047     }
1048
1049   return 0;
1050 }
1051       
1052 /* Indicate that NEW is an alternate way of referring to the temp slot
1053    that previous was known by OLD.  */
1054
1055 void
1056 update_temp_slot_address (old, new)
1057      rtx old, new;
1058 {
1059   struct temp_slot *p = find_temp_slot_from_address (old);
1060
1061   /* If none, return.  Else add NEW as an alias.  */
1062   if (p == 0)
1063     return;
1064   else if (p->address == 0)
1065     p->address = new;
1066   else
1067     {
1068       if (GET_CODE (p->address) != EXPR_LIST)
1069         p->address = gen_rtx (EXPR_LIST, VOIDmode, p->address, NULL_RTX);
1070
1071       p->address = gen_rtx (EXPR_LIST, VOIDmode, new, p->address);
1072     }
1073 }
1074
1075 /* If X could be a reference to a temporary slot, mark the fact that its
1076    address was taken.  */
1077
1078 void
1079 mark_temp_addr_taken (x)
1080      rtx x;
1081 {
1082   struct temp_slot *p;
1083
1084   if (x == 0)
1085     return;
1086
1087   /* If X is not in memory or is at a constant address, it cannot be in
1088      a temporary slot.  */
1089   if (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1090     return;
1091
1092   p = find_temp_slot_from_address (XEXP (x, 0));
1093   if (p != 0)
1094     p->addr_taken = 1;
1095 }
1096
1097 /* If X could be a reference to a temporary slot, mark that slot as belonging
1098    to the to one level higher.  If X matched one of our slots, just mark that
1099    one.  Otherwise, we can't easily predict which it is, so upgrade all of
1100    them.  Kept slots need not be touched.
1101
1102    This is called when an ({...}) construct occurs and a statement
1103    returns a value in memory.  */
1104
1105 void
1106 preserve_temp_slots (x)
1107      rtx x;
1108 {
1109   struct temp_slot *p = 0;
1110
1111   /* If there is no result, we still might have some objects whose address
1112      were taken, so we need to make sure they stay around.  */
1113   if (x == 0)
1114     {
1115       for (p = temp_slots; p; p = p->next)
1116         if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1117           p->level--;
1118
1119       return;
1120     }
1121
1122   /* If X is a register that is being used as a pointer, see if we have
1123      a temporary slot we know it points to.  To be consistent with
1124      the code below, we really should preserve all non-kept slots
1125      if we can't find a match, but that seems to be much too costly.  */
1126   if (GET_CODE (x) == REG && REGNO_POINTER_FLAG (REGNO (x)))
1127     p = find_temp_slot_from_address (x);
1128
1129   /* If X is not in memory or is at a constant address, it cannot be in
1130      a temporary slot, but it can contain something whose address was
1131      taken.  */
1132   if (p == 0 && (GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0))))
1133     {
1134       for (p = temp_slots; p; p = p->next)
1135         if (p->in_use && p->level == temp_slot_level && p->addr_taken)
1136           p->level--;
1137
1138       return;
1139     }
1140
1141   /* First see if we can find a match.  */
1142   if (p == 0)
1143     p = find_temp_slot_from_address (XEXP (x, 0));
1144
1145   if (p != 0)
1146     {
1147       /* Move everything at our level whose address was taken to our new
1148          level in case we used its address.  */
1149       struct temp_slot *q;
1150
1151       for (q = temp_slots; q; q = q->next)
1152         if (q != p && q->addr_taken && q->level == p->level)
1153           q->level--;
1154
1155       p->level--;
1156       p->addr_taken = 0;
1157       return;
1158     }
1159
1160   /* Otherwise, preserve all non-kept slots at this level.  */
1161   for (p = temp_slots; p; p = p->next)
1162     if (p->in_use && p->level == temp_slot_level && ! p->keep)
1163       p->level--;
1164 }
1165
1166 /* X is the result of an RTL_EXPR.  If it is a temporary slot associated
1167    with that RTL_EXPR, promote it into a temporary slot at the present
1168    level so it will not be freed when we free slots made in the
1169    RTL_EXPR.  */
1170
1171 void
1172 preserve_rtl_expr_result (x)
1173      rtx x;
1174 {
1175   struct temp_slot *p;
1176
1177   /* If X is not in memory or is at a constant address, it cannot be in
1178      a temporary slot.  */
1179   if (x == 0 || GET_CODE (x) != MEM || CONSTANT_P (XEXP (x, 0)))
1180     return;
1181
1182   /* If we can find a match, move it to our level unless it is already at
1183      an upper level.  */
1184   p = find_temp_slot_from_address (XEXP (x, 0));
1185   if (p != 0)
1186     {
1187       p->level = MIN (p->level, temp_slot_level);
1188       p->rtl_expr = 0;
1189     }
1190
1191   return;
1192 }
1193
1194 /* Free all temporaries used so far.  This is normally called at the end
1195    of generating code for a statement.  Don't free any temporaries
1196    currently in use for an RTL_EXPR that hasn't yet been emitted.
1197    We could eventually do better than this since it can be reused while
1198    generating the same RTL_EXPR, but this is complex and probably not
1199    worthwhile.  */
1200
1201 void
1202 free_temp_slots ()
1203 {
1204   struct temp_slot *p;
1205
1206   for (p = temp_slots; p; p = p->next)
1207     if (p->in_use && p->level == temp_slot_level && ! p->keep
1208         && p->rtl_expr == 0)
1209       p->in_use = 0;
1210
1211   combine_temp_slots ();
1212 }
1213
1214 /* Free all temporary slots used in T, an RTL_EXPR node.  */
1215
1216 void
1217 free_temps_for_rtl_expr (t)
1218      tree t;
1219 {
1220   struct temp_slot *p;
1221
1222   for (p = temp_slots; p; p = p->next)
1223     if (p->rtl_expr == t)
1224       p->in_use = 0;
1225
1226   combine_temp_slots ();
1227 }
1228
1229 /* Push deeper into the nesting level for stack temporaries.  */
1230
1231 void
1232 push_temp_slots ()
1233 {
1234   temp_slot_level++;
1235 }
1236
1237 /* Pop a temporary nesting level.  All slots in use in the current level
1238    are freed.  */
1239
1240 void
1241 pop_temp_slots ()
1242 {
1243   struct temp_slot *p;
1244
1245   for (p = temp_slots; p; p = p->next)
1246     if (p->in_use && p->level == temp_slot_level && p->rtl_expr == 0)
1247       p->in_use = 0;
1248
1249   combine_temp_slots ();
1250
1251   temp_slot_level--;
1252 }
1253
1254 /* Initialize temporary slots.  */
1255
1256 void
1257 init_temp_slots ()
1258 {
1259   /* We have not allocated any temporaries yet.  */
1260   temp_slots = 0;
1261   temp_slot_level = 0;
1262   target_temp_slot_level = 0;
1263 }
1264 \f
1265 /* Retroactively move an auto variable from a register to a stack slot.
1266    This is done when an address-reference to the variable is seen.  */
1267
1268 void
1269 put_var_into_stack (decl)
1270      tree decl;
1271 {
1272   register rtx reg;
1273   enum machine_mode promoted_mode, decl_mode;
1274   struct function *function = 0;
1275   tree context;
1276
1277   if (output_bytecode)
1278     return;
1279   
1280   context = decl_function_context (decl);
1281
1282   /* Get the current rtl used for this object and it's original mode.  */
1283   reg = TREE_CODE (decl) == SAVE_EXPR ? SAVE_EXPR_RTL (decl) : DECL_RTL (decl);
1284
1285   /* No need to do anything if decl has no rtx yet
1286      since in that case caller is setting TREE_ADDRESSABLE
1287      and a stack slot will be assigned when the rtl is made.  */
1288   if (reg == 0)
1289     return;
1290
1291   /* Get the declared mode for this object.  */
1292   decl_mode = (TREE_CODE (decl) == SAVE_EXPR ? TYPE_MODE (TREE_TYPE (decl))
1293                : DECL_MODE (decl));
1294   /* Get the mode it's actually stored in.  */
1295   promoted_mode = GET_MODE (reg);
1296
1297   /* If this variable comes from an outer function,
1298      find that function's saved context.  */
1299   if (context != current_function_decl)
1300     for (function = outer_function_chain; function; function = function->next)
1301       if (function->decl == context)
1302         break;
1303
1304   /* If this is a variable-size object with a pseudo to address it,
1305      put that pseudo into the stack, if the var is nonlocal.  */
1306   if (DECL_NONLOCAL (decl)
1307       && GET_CODE (reg) == MEM
1308       && GET_CODE (XEXP (reg, 0)) == REG
1309       && REGNO (XEXP (reg, 0)) > LAST_VIRTUAL_REGISTER)
1310     {
1311       reg = XEXP (reg, 0);
1312       decl_mode = promoted_mode = GET_MODE (reg);
1313     }
1314
1315   /* Now we should have a value that resides in one or more pseudo regs.  */
1316
1317   if (GET_CODE (reg) == REG)
1318     put_reg_into_stack (function, reg, TREE_TYPE (decl),
1319                         promoted_mode, decl_mode, TREE_SIDE_EFFECTS (decl));
1320   else if (GET_CODE (reg) == CONCAT)
1321     {
1322       /* A CONCAT contains two pseudos; put them both in the stack.
1323          We do it so they end up consecutive.  */
1324       enum machine_mode part_mode = GET_MODE (XEXP (reg, 0));
1325       tree part_type = TREE_TYPE (TREE_TYPE (decl));
1326 #ifdef FRAME_GROWS_DOWNWARD
1327       /* Since part 0 should have a lower address, do it second.  */
1328       put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1329                           part_mode, TREE_SIDE_EFFECTS (decl));
1330       put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1331                           part_mode, TREE_SIDE_EFFECTS (decl));
1332 #else
1333       put_reg_into_stack (function, XEXP (reg, 0), part_type, part_mode,
1334                           part_mode, TREE_SIDE_EFFECTS (decl));
1335       put_reg_into_stack (function, XEXP (reg, 1), part_type, part_mode,
1336                           part_mode, TREE_SIDE_EFFECTS (decl));
1337 #endif
1338
1339       /* Change the CONCAT into a combined MEM for both parts.  */
1340       PUT_CODE (reg, MEM);
1341       MEM_VOLATILE_P (reg) = MEM_VOLATILE_P (XEXP (reg, 0));
1342
1343       /* The two parts are in memory order already.
1344          Use the lower parts address as ours.  */
1345       XEXP (reg, 0) = XEXP (XEXP (reg, 0), 0);
1346       /* Prevent sharing of rtl that might lose.  */
1347       if (GET_CODE (XEXP (reg, 0)) == PLUS)
1348         XEXP (reg, 0) = copy_rtx (XEXP (reg, 0));
1349     }
1350 }
1351
1352 /* Subroutine of put_var_into_stack.  This puts a single pseudo reg REG
1353    into the stack frame of FUNCTION (0 means the current function).
1354    DECL_MODE is the machine mode of the user-level data type.
1355    PROMOTED_MODE is the machine mode of the register.
1356    VOLATILE_P is nonzero if this is for a "volatile" decl.  */
1357
1358 static void
1359 put_reg_into_stack (function, reg, type, promoted_mode, decl_mode, volatile_p)
1360      struct function *function;
1361      rtx reg;
1362      tree type;
1363      enum machine_mode promoted_mode, decl_mode;
1364      int volatile_p;
1365 {
1366   rtx new = 0;
1367
1368   if (function)
1369     {
1370       if (REGNO (reg) < function->max_parm_reg)
1371         new = function->parm_reg_stack_loc[REGNO (reg)];
1372       if (new == 0)
1373         new = assign_outer_stack_local (decl_mode, GET_MODE_SIZE (decl_mode),
1374                                         0, function);
1375     }
1376   else
1377     {
1378       if (REGNO (reg) < max_parm_reg)
1379         new = parm_reg_stack_loc[REGNO (reg)];
1380       if (new == 0)
1381         new = assign_stack_local (decl_mode, GET_MODE_SIZE (decl_mode), 0);
1382     }
1383
1384   PUT_MODE (reg, decl_mode);
1385   XEXP (reg, 0) = XEXP (new, 0);
1386   /* `volatil' bit means one thing for MEMs, another entirely for REGs.  */
1387   MEM_VOLATILE_P (reg) = volatile_p;
1388   PUT_CODE (reg, MEM);
1389
1390   /* If this is a memory ref that contains aggregate components,
1391      mark it as such for cse and loop optimize.  */
1392   MEM_IN_STRUCT_P (reg) = AGGREGATE_TYPE_P (type);
1393
1394   /* Now make sure that all refs to the variable, previously made
1395      when it was a register, are fixed up to be valid again.  */
1396   if (function)
1397     {
1398       struct var_refs_queue *temp;
1399
1400       /* Variable is inherited; fix it up when we get back to its function.  */
1401       push_obstacks (function->function_obstack,
1402                      function->function_maybepermanent_obstack);
1403
1404       /* See comment in restore_tree_status in tree.c for why this needs to be
1405          on saveable obstack.  */
1406       temp
1407         = (struct var_refs_queue *) savealloc (sizeof (struct var_refs_queue));
1408       temp->modified = reg;
1409       temp->promoted_mode = promoted_mode;
1410       temp->unsignedp = TREE_UNSIGNED (type);
1411       temp->next = function->fixup_var_refs_queue;
1412       function->fixup_var_refs_queue = temp;
1413       pop_obstacks ();
1414     }
1415   else
1416     /* Variable is local; fix it up now.  */
1417     fixup_var_refs (reg, promoted_mode, TREE_UNSIGNED (type));
1418 }
1419 \f
1420 static void
1421 fixup_var_refs (var, promoted_mode, unsignedp)
1422      rtx var;
1423      enum machine_mode promoted_mode;
1424      int unsignedp;
1425 {
1426   tree pending;
1427   rtx first_insn = get_insns ();
1428   struct sequence_stack *stack = sequence_stack;
1429   tree rtl_exps = rtl_expr_chain;
1430
1431   /* Must scan all insns for stack-refs that exceed the limit.  */
1432   fixup_var_refs_insns (var, promoted_mode, unsignedp, first_insn, stack == 0);
1433
1434   /* Scan all pending sequences too.  */
1435   for (; stack; stack = stack->next)
1436     {
1437       push_to_sequence (stack->first);
1438       fixup_var_refs_insns (var, promoted_mode, unsignedp,
1439                             stack->first, stack->next != 0);
1440       /* Update remembered end of sequence
1441          in case we added an insn at the end.  */
1442       stack->last = get_last_insn ();
1443       end_sequence ();
1444     }
1445
1446   /* Scan all waiting RTL_EXPRs too.  */
1447   for (pending = rtl_exps; pending; pending = TREE_CHAIN (pending))
1448     {
1449       rtx seq = RTL_EXPR_SEQUENCE (TREE_VALUE (pending));
1450       if (seq != const0_rtx && seq != 0)
1451         {
1452           push_to_sequence (seq);
1453           fixup_var_refs_insns (var, promoted_mode, unsignedp, seq, 0);
1454           end_sequence ();
1455         }
1456     }
1457 }
1458 \f
1459 /* REPLACEMENTS is a pointer to a list of the struct fixup_replacement and X is
1460    some part of an insn.  Return a struct fixup_replacement whose OLD
1461    value is equal to X.  Allocate a new structure if no such entry exists. */
1462
1463 static struct fixup_replacement *
1464 find_fixup_replacement (replacements, x)
1465      struct fixup_replacement **replacements;
1466      rtx x;
1467 {
1468   struct fixup_replacement *p;
1469
1470   /* See if we have already replaced this.  */
1471   for (p = *replacements; p && p->old != x; p = p->next)
1472     ;
1473
1474   if (p == 0)
1475     {
1476       p = (struct fixup_replacement *) oballoc (sizeof (struct fixup_replacement));
1477       p->old = x;
1478       p->new = 0;
1479       p->next = *replacements;
1480       *replacements = p;
1481     }
1482
1483   return p;
1484 }
1485
1486 /* Scan the insn-chain starting with INSN for refs to VAR
1487    and fix them up.  TOPLEVEL is nonzero if this chain is the
1488    main chain of insns for the current function.  */
1489
1490 static void
1491 fixup_var_refs_insns (var, promoted_mode, unsignedp, insn, toplevel)
1492      rtx var;
1493      enum machine_mode promoted_mode;
1494      int unsignedp;
1495      rtx insn;
1496      int toplevel;
1497 {
1498   rtx call_dest = 0;
1499
1500   while (insn)
1501     {
1502       rtx next = NEXT_INSN (insn);
1503       rtx note;
1504       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1505         {
1506           /* If this is a CLOBBER of VAR, delete it.
1507
1508              If it has a REG_LIBCALL note, delete the REG_LIBCALL
1509              and REG_RETVAL notes too.  */
1510           if (GET_CODE (PATTERN (insn)) == CLOBBER
1511               && XEXP (PATTERN (insn), 0) == var)
1512             {
1513               if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX)) != 0)
1514                 /* The REG_LIBCALL note will go away since we are going to
1515                    turn INSN into a NOTE, so just delete the
1516                    corresponding REG_RETVAL note.  */
1517                 remove_note (XEXP (note, 0),
1518                              find_reg_note (XEXP (note, 0), REG_RETVAL,
1519                                             NULL_RTX));
1520
1521               /* In unoptimized compilation, we shouldn't call delete_insn
1522                  except in jump.c doing warnings.  */
1523               PUT_CODE (insn, NOTE);
1524               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1525               NOTE_SOURCE_FILE (insn) = 0;
1526             }
1527
1528           /* The insn to load VAR from a home in the arglist
1529              is now a no-op.  When we see it, just delete it.  */
1530           else if (toplevel
1531                    && GET_CODE (PATTERN (insn)) == SET
1532                    && SET_DEST (PATTERN (insn)) == var
1533                    /* If this represents the result of an insn group,
1534                       don't delete the insn.  */
1535                    && find_reg_note (insn, REG_RETVAL, NULL_RTX) == 0
1536                    && rtx_equal_p (SET_SRC (PATTERN (insn)), var))
1537             {
1538               /* In unoptimized compilation, we shouldn't call delete_insn
1539                  except in jump.c doing warnings.  */
1540               PUT_CODE (insn, NOTE);
1541               NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1542               NOTE_SOURCE_FILE (insn) = 0;
1543               if (insn == last_parm_insn)
1544                 last_parm_insn = PREV_INSN (next);
1545             }
1546           else
1547             {
1548               struct fixup_replacement *replacements = 0;
1549               rtx next_insn = NEXT_INSN (insn);
1550
1551 #ifdef SMALL_REGISTER_CLASSES
1552               /* If the insn that copies the results of a CALL_INSN
1553                  into a pseudo now references VAR, we have to use an
1554                  intermediate pseudo since we want the life of the
1555                  return value register to be only a single insn.
1556
1557                  If we don't use an intermediate pseudo, such things as
1558                  address computations to make the address of VAR valid
1559                  if it is not can be placed between the CALL_INSN and INSN.
1560
1561                  To make sure this doesn't happen, we record the destination
1562                  of the CALL_INSN and see if the next insn uses both that
1563                  and VAR.  */
1564
1565               if (call_dest != 0 && GET_CODE (insn) == INSN
1566                   && reg_mentioned_p (var, PATTERN (insn))
1567                   && reg_mentioned_p (call_dest, PATTERN (insn)))
1568                 {
1569                   rtx temp = gen_reg_rtx (GET_MODE (call_dest));
1570
1571                   emit_insn_before (gen_move_insn (temp, call_dest), insn);
1572
1573                   PATTERN (insn) = replace_rtx (PATTERN (insn),
1574                                                 call_dest, temp);
1575                 }
1576               
1577               if (GET_CODE (insn) == CALL_INSN
1578                   && GET_CODE (PATTERN (insn)) == SET)
1579                 call_dest = SET_DEST (PATTERN (insn));
1580               else if (GET_CODE (insn) == CALL_INSN
1581                        && GET_CODE (PATTERN (insn)) == PARALLEL
1582                        && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1583                 call_dest = SET_DEST (XVECEXP (PATTERN (insn), 0, 0));
1584               else
1585                 call_dest = 0;
1586 #endif
1587
1588               /* See if we have to do anything to INSN now that VAR is in
1589                  memory.  If it needs to be loaded into a pseudo, use a single
1590                  pseudo for the entire insn in case there is a MATCH_DUP
1591                  between two operands.  We pass a pointer to the head of
1592                  a list of struct fixup_replacements.  If fixup_var_refs_1
1593                  needs to allocate pseudos or replacement MEMs (for SUBREGs),
1594                  it will record them in this list.
1595                  
1596                  If it allocated a pseudo for any replacement, we copy into
1597                  it here.  */
1598
1599               fixup_var_refs_1 (var, promoted_mode, &PATTERN (insn), insn,
1600                                 &replacements);
1601
1602               /* If this is last_parm_insn, and any instructions were output
1603                  after it to fix it up, then we must set last_parm_insn to
1604                  the last such instruction emitted.  */
1605               if (insn == last_parm_insn)
1606                 last_parm_insn = PREV_INSN (next_insn);
1607
1608               while (replacements)
1609                 {
1610                   if (GET_CODE (replacements->new) == REG)
1611                     {
1612                       rtx insert_before;
1613                       rtx seq;
1614
1615                       /* OLD might be a (subreg (mem)).  */
1616                       if (GET_CODE (replacements->old) == SUBREG)
1617                         replacements->old
1618                           = fixup_memory_subreg (replacements->old, insn, 0);
1619                       else
1620                         replacements->old
1621                           = fixup_stack_1 (replacements->old, insn);
1622
1623                       insert_before = insn;
1624
1625                       /* If we are changing the mode, do a conversion.
1626                          This might be wasteful, but combine.c will
1627                          eliminate much of the waste.  */
1628
1629                       if (GET_MODE (replacements->new)
1630                           != GET_MODE (replacements->old))
1631                         {
1632                           start_sequence ();
1633                           convert_move (replacements->new,
1634                                         replacements->old, unsignedp);
1635                           seq = gen_sequence ();
1636                           end_sequence ();
1637                         }
1638                       else
1639                         seq = gen_move_insn (replacements->new,
1640                                              replacements->old);
1641
1642                       emit_insn_before (seq, insert_before);
1643                     }
1644
1645                   replacements = replacements->next;
1646                 }
1647             }
1648
1649           /* Also fix up any invalid exprs in the REG_NOTES of this insn.
1650              But don't touch other insns referred to by reg-notes;
1651              we will get them elsewhere.  */
1652           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1653             if (GET_CODE (note) != INSN_LIST)
1654               XEXP (note, 0)
1655                 = walk_fixup_memory_subreg (XEXP (note, 0), insn, 1);
1656         }
1657       insn = next;
1658     }
1659 }
1660 \f
1661 /* VAR is a MEM that used to be a pseudo register with mode PROMOTED_MODE.
1662    See if the rtx expression at *LOC in INSN needs to be changed.  
1663
1664    REPLACEMENTS is a pointer to a list head that starts out zero, but may
1665    contain a list of original rtx's and replacements. If we find that we need
1666    to modify this insn by replacing a memory reference with a pseudo or by
1667    making a new MEM to implement a SUBREG, we consult that list to see if
1668    we have already chosen a replacement. If none has already been allocated,
1669    we allocate it and update the list.  fixup_var_refs_insns will copy VAR
1670    or the SUBREG, as appropriate, to the pseudo.  */
1671
1672 static void
1673 fixup_var_refs_1 (var, promoted_mode, loc, insn, replacements)
1674      register rtx var;
1675      enum machine_mode promoted_mode;
1676      register rtx *loc;
1677      rtx insn;
1678      struct fixup_replacement **replacements;
1679 {
1680   register int i;
1681   register rtx x = *loc;
1682   RTX_CODE code = GET_CODE (x);
1683   register char *fmt;
1684   register rtx tem, tem1;
1685   struct fixup_replacement *replacement;
1686
1687   switch (code)
1688     {
1689     case MEM:
1690       if (var == x)
1691         {
1692           /* If we already have a replacement, use it.  Otherwise, 
1693              try to fix up this address in case it is invalid.  */
1694
1695           replacement = find_fixup_replacement (replacements, var);
1696           if (replacement->new)
1697             {
1698               *loc = replacement->new;
1699               return;
1700             }
1701
1702           *loc = replacement->new = x = fixup_stack_1 (x, insn);
1703
1704           /* Unless we are forcing memory to register or we changed the mode,
1705              we can leave things the way they are if the insn is valid.  */
1706              
1707           INSN_CODE (insn) = -1;
1708           if (! flag_force_mem && GET_MODE (x) == promoted_mode
1709               && recog_memoized (insn) >= 0)
1710             return;
1711
1712           *loc = replacement->new = gen_reg_rtx (promoted_mode);
1713           return;
1714         }
1715
1716       /* If X contains VAR, we need to unshare it here so that we update
1717          each occurrence separately.  But all identical MEMs in one insn
1718          must be replaced with the same rtx because of the possibility of
1719          MATCH_DUPs.  */
1720
1721       if (reg_mentioned_p (var, x))
1722         {
1723           replacement = find_fixup_replacement (replacements, x);
1724           if (replacement->new == 0)
1725             replacement->new = copy_most_rtx (x, var);
1726
1727           *loc = x = replacement->new;
1728         }
1729       break;
1730
1731     case REG:
1732     case CC0:
1733     case PC:
1734     case CONST_INT:
1735     case CONST:
1736     case SYMBOL_REF:
1737     case LABEL_REF:
1738     case CONST_DOUBLE:
1739       return;
1740
1741     case SIGN_EXTRACT:
1742     case ZERO_EXTRACT:
1743       /* Note that in some cases those types of expressions are altered
1744          by optimize_bit_field, and do not survive to get here.  */
1745       if (XEXP (x, 0) == var
1746           || (GET_CODE (XEXP (x, 0)) == SUBREG
1747               && SUBREG_REG (XEXP (x, 0)) == var))
1748         {
1749           /* Get TEM as a valid MEM in the mode presently in the insn.
1750
1751              We don't worry about the possibility of MATCH_DUP here; it
1752              is highly unlikely and would be tricky to handle.  */
1753
1754           tem = XEXP (x, 0);
1755           if (GET_CODE (tem) == SUBREG)
1756             tem = fixup_memory_subreg (tem, insn, 1);
1757           tem = fixup_stack_1 (tem, insn);
1758
1759           /* Unless we want to load from memory, get TEM into the proper mode
1760              for an extract from memory.  This can only be done if the
1761              extract is at a constant position and length.  */
1762
1763           if (! flag_force_mem && GET_CODE (XEXP (x, 1)) == CONST_INT
1764               && GET_CODE (XEXP (x, 2)) == CONST_INT
1765               && ! mode_dependent_address_p (XEXP (tem, 0))
1766               && ! MEM_VOLATILE_P (tem))
1767             {
1768               enum machine_mode wanted_mode = VOIDmode;
1769               enum machine_mode is_mode = GET_MODE (tem);
1770               int width = INTVAL (XEXP (x, 1));
1771               int pos = INTVAL (XEXP (x, 2));
1772
1773 #ifdef HAVE_extzv
1774               if (GET_CODE (x) == ZERO_EXTRACT)
1775                 wanted_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
1776 #endif
1777 #ifdef HAVE_extv
1778               if (GET_CODE (x) == SIGN_EXTRACT)
1779                 wanted_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
1780 #endif
1781               /* If we have a narrower mode, we can do something.  */
1782               if (wanted_mode != VOIDmode
1783                   && GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1784                 {
1785                   int offset = pos / BITS_PER_UNIT;
1786                   rtx old_pos = XEXP (x, 2);
1787                   rtx newmem;
1788
1789                   /* If the bytes and bits are counted differently, we
1790                      must adjust the offset.  */
1791                   if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1792                     offset = (GET_MODE_SIZE (is_mode)
1793                               - GET_MODE_SIZE (wanted_mode) - offset);
1794
1795                   pos %= GET_MODE_BITSIZE (wanted_mode);
1796
1797                   newmem = gen_rtx (MEM, wanted_mode,
1798                                     plus_constant (XEXP (tem, 0), offset));
1799                   RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1800                   MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1801                   MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1802
1803                   /* Make the change and see if the insn remains valid.  */
1804                   INSN_CODE (insn) = -1;
1805                   XEXP (x, 0) = newmem;
1806                   XEXP (x, 2) = GEN_INT (pos);
1807
1808                   if (recog_memoized (insn) >= 0)
1809                     return;
1810
1811                   /* Otherwise, restore old position.  XEXP (x, 0) will be
1812                      restored later.  */
1813                   XEXP (x, 2) = old_pos;
1814                 }
1815             }
1816
1817           /* If we get here, the bitfield extract insn can't accept a memory
1818              reference.  Copy the input into a register.  */
1819
1820           tem1 = gen_reg_rtx (GET_MODE (tem));
1821           emit_insn_before (gen_move_insn (tem1, tem), insn);
1822           XEXP (x, 0) = tem1;
1823           return;
1824         }
1825       break;
1826               
1827     case SUBREG:
1828       if (SUBREG_REG (x) == var)
1829         {
1830           /* If this is a special SUBREG made because VAR was promoted
1831              from a wider mode, replace it with VAR and call ourself
1832              recursively, this time saying that the object previously
1833              had its current mode (by virtue of the SUBREG).  */
1834
1835           if (SUBREG_PROMOTED_VAR_P (x))
1836             {
1837               *loc = var;
1838               fixup_var_refs_1 (var, GET_MODE (var), loc, insn, replacements);
1839               return;
1840             }
1841
1842           /* If this SUBREG makes VAR wider, it has become a paradoxical
1843              SUBREG with VAR in memory, but these aren't allowed at this 
1844              stage of the compilation.  So load VAR into a pseudo and take
1845              a SUBREG of that pseudo.  */
1846           if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (var)))
1847             {
1848               replacement = find_fixup_replacement (replacements, var);
1849               if (replacement->new == 0)
1850                 replacement->new = gen_reg_rtx (GET_MODE (var));
1851               SUBREG_REG (x) = replacement->new;
1852               return;
1853             }
1854
1855           /* See if we have already found a replacement for this SUBREG.
1856              If so, use it.  Otherwise, make a MEM and see if the insn
1857              is recognized.  If not, or if we should force MEM into a register,
1858              make a pseudo for this SUBREG.  */
1859           replacement = find_fixup_replacement (replacements, x);
1860           if (replacement->new)
1861             {
1862               *loc = replacement->new;
1863               return;
1864             }
1865           
1866           replacement->new = *loc = fixup_memory_subreg (x, insn, 0);
1867
1868           INSN_CODE (insn) = -1;
1869           if (! flag_force_mem && recog_memoized (insn) >= 0)
1870             return;
1871
1872           *loc = replacement->new = gen_reg_rtx (GET_MODE (x));
1873           return;
1874         }
1875       break;
1876
1877     case SET:
1878       /* First do special simplification of bit-field references.  */
1879       if (GET_CODE (SET_DEST (x)) == SIGN_EXTRACT
1880           || GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
1881         optimize_bit_field (x, insn, 0);
1882       if (GET_CODE (SET_SRC (x)) == SIGN_EXTRACT
1883           || GET_CODE (SET_SRC (x)) == ZERO_EXTRACT)
1884         optimize_bit_field (x, insn, NULL_PTR);
1885
1886       /* If SET_DEST is now a paradoxical SUBREG, put the result of this
1887          insn into a pseudo and store the low part of the pseudo into VAR. */
1888       if (GET_CODE (SET_DEST (x)) == SUBREG
1889           && SUBREG_REG (SET_DEST (x)) == var
1890           && (GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
1891               > GET_MODE_SIZE (GET_MODE (var))))
1892         {
1893           SET_DEST (x) = tem = gen_reg_rtx (GET_MODE (SET_DEST (x)));
1894           emit_insn_after (gen_move_insn (var, gen_lowpart (GET_MODE (var),
1895                                                             tem)),
1896                            insn);
1897           break;
1898         }
1899           
1900       {
1901         rtx dest = SET_DEST (x);
1902         rtx src = SET_SRC (x);
1903         rtx outerdest = dest;
1904
1905         while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART
1906                || GET_CODE (dest) == SIGN_EXTRACT
1907                || GET_CODE (dest) == ZERO_EXTRACT)
1908           dest = XEXP (dest, 0);
1909
1910         if (GET_CODE (src) == SUBREG)
1911           src = XEXP (src, 0);
1912
1913         /* If VAR does not appear at the top level of the SET
1914            just scan the lower levels of the tree.  */
1915
1916         if (src != var && dest != var)
1917           break;
1918
1919         /* We will need to rerecognize this insn.  */
1920         INSN_CODE (insn) = -1;
1921
1922 #ifdef HAVE_insv
1923         if (GET_CODE (outerdest) == ZERO_EXTRACT && dest == var)
1924           {
1925             /* Since this case will return, ensure we fixup all the
1926                operands here.  */
1927             fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 1),
1928                               insn, replacements);
1929             fixup_var_refs_1 (var, promoted_mode, &XEXP (outerdest, 2),
1930                               insn, replacements);
1931             fixup_var_refs_1 (var, promoted_mode, &SET_SRC (x),
1932                               insn, replacements);
1933
1934             tem = XEXP (outerdest, 0);
1935
1936             /* Clean up (SUBREG:SI (MEM:mode ...) 0)
1937                that may appear inside a ZERO_EXTRACT.
1938                This was legitimate when the MEM was a REG.  */
1939             if (GET_CODE (tem) == SUBREG
1940                 && SUBREG_REG (tem) == var)
1941               tem = fixup_memory_subreg (tem, insn, 1);
1942             else
1943               tem = fixup_stack_1 (tem, insn);
1944
1945             if (GET_CODE (XEXP (outerdest, 1)) == CONST_INT
1946                 && GET_CODE (XEXP (outerdest, 2)) == CONST_INT
1947                 && ! mode_dependent_address_p (XEXP (tem, 0))
1948                 && ! MEM_VOLATILE_P (tem))
1949               {
1950                 enum machine_mode wanted_mode
1951                   = insn_operand_mode[(int) CODE_FOR_insv][0];
1952                 enum machine_mode is_mode = GET_MODE (tem);
1953                 int width = INTVAL (XEXP (outerdest, 1));
1954                 int pos = INTVAL (XEXP (outerdest, 2));
1955
1956                 /* If we have a narrower mode, we can do something.  */
1957                 if (GET_MODE_SIZE (wanted_mode) < GET_MODE_SIZE (is_mode))
1958                   {
1959                     int offset = pos / BITS_PER_UNIT;
1960                     rtx old_pos = XEXP (outerdest, 2);
1961                     rtx newmem;
1962
1963                     if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN)
1964                       offset = (GET_MODE_SIZE (is_mode)
1965                                 - GET_MODE_SIZE (wanted_mode) - offset);
1966
1967                     pos %= GET_MODE_BITSIZE (wanted_mode);
1968
1969                     newmem = gen_rtx (MEM, wanted_mode,
1970                                       plus_constant (XEXP (tem, 0), offset));
1971                     RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (tem);
1972                     MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (tem);
1973                     MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (tem);
1974
1975                     /* Make the change and see if the insn remains valid.  */
1976                     INSN_CODE (insn) = -1;
1977                     XEXP (outerdest, 0) = newmem;
1978                     XEXP (outerdest, 2) = GEN_INT (pos);
1979                     
1980                     if (recog_memoized (insn) >= 0)
1981                       return;
1982                     
1983                     /* Otherwise, restore old position.  XEXP (x, 0) will be
1984                        restored later.  */
1985                     XEXP (outerdest, 2) = old_pos;
1986                   }
1987               }
1988
1989             /* If we get here, the bit-field store doesn't allow memory
1990                or isn't located at a constant position.  Load the value into
1991                a register, do the store, and put it back into memory.  */
1992
1993             tem1 = gen_reg_rtx (GET_MODE (tem));
1994             emit_insn_before (gen_move_insn (tem1, tem), insn);
1995             emit_insn_after (gen_move_insn (tem, tem1), insn);
1996             XEXP (outerdest, 0) = tem1;
1997             return;
1998           }
1999 #endif
2000
2001         /* STRICT_LOW_PART is a no-op on memory references
2002            and it can cause combinations to be unrecognizable,
2003            so eliminate it.  */
2004
2005         if (dest == var && GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2006           SET_DEST (x) = XEXP (SET_DEST (x), 0);
2007
2008         /* A valid insn to copy VAR into or out of a register
2009            must be left alone, to avoid an infinite loop here.
2010            If the reference to VAR is by a subreg, fix that up,
2011            since SUBREG is not valid for a memref.
2012            Also fix up the address of the stack slot.
2013
2014            Note that we must not try to recognize the insn until
2015            after we know that we have valid addresses and no
2016            (subreg (mem ...) ...) constructs, since these interfere
2017            with determining the validity of the insn.  */
2018
2019         if ((SET_SRC (x) == var
2020              || (GET_CODE (SET_SRC (x)) == SUBREG
2021                  && SUBREG_REG (SET_SRC (x)) == var))
2022             && (GET_CODE (SET_DEST (x)) == REG
2023                 || (GET_CODE (SET_DEST (x)) == SUBREG
2024                     && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG))
2025             && GET_MODE (var) == promoted_mode
2026             && x == single_set (insn))
2027           {
2028             rtx pat;
2029
2030             replacement = find_fixup_replacement (replacements, SET_SRC (x));
2031             if (replacement->new)
2032               SET_SRC (x) = replacement->new;
2033             else if (GET_CODE (SET_SRC (x)) == SUBREG)
2034               SET_SRC (x) = replacement->new
2035                 = fixup_memory_subreg (SET_SRC (x), insn, 0);
2036             else
2037               SET_SRC (x) = replacement->new
2038                 = fixup_stack_1 (SET_SRC (x), insn);
2039
2040             if (recog_memoized (insn) >= 0)
2041               return;
2042
2043             /* INSN is not valid, but we know that we want to
2044                copy SET_SRC (x) to SET_DEST (x) in some way.  So
2045                we generate the move and see whether it requires more
2046                than one insn.  If it does, we emit those insns and
2047                delete INSN.  Otherwise, we an just replace the pattern 
2048                of INSN; we have already verified above that INSN has
2049                no other function that to do X.  */
2050
2051             pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2052             if (GET_CODE (pat) == SEQUENCE)
2053               {
2054                 emit_insn_after (pat, insn);
2055                 PUT_CODE (insn, NOTE);
2056                 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2057                 NOTE_SOURCE_FILE (insn) = 0;
2058               }
2059             else
2060               PATTERN (insn) = pat;
2061
2062             return;
2063           }
2064
2065         if ((SET_DEST (x) == var
2066              || (GET_CODE (SET_DEST (x)) == SUBREG
2067                  && SUBREG_REG (SET_DEST (x)) == var))
2068             && (GET_CODE (SET_SRC (x)) == REG
2069                 || (GET_CODE (SET_SRC (x)) == SUBREG
2070                     && GET_CODE (SUBREG_REG (SET_SRC (x))) == REG))
2071             && GET_MODE (var) == promoted_mode
2072             && x == single_set (insn))
2073           {
2074             rtx pat;
2075
2076             if (GET_CODE (SET_DEST (x)) == SUBREG)
2077               SET_DEST (x) = fixup_memory_subreg (SET_DEST (x), insn, 0);
2078             else
2079               SET_DEST (x) = fixup_stack_1 (SET_DEST (x), insn);
2080
2081             if (recog_memoized (insn) >= 0)
2082               return;
2083
2084             pat = gen_move_insn (SET_DEST (x), SET_SRC (x));
2085             if (GET_CODE (pat) == SEQUENCE)
2086               {
2087                 emit_insn_after (pat, insn);
2088                 PUT_CODE (insn, NOTE);
2089                 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
2090                 NOTE_SOURCE_FILE (insn) = 0;
2091               }
2092             else
2093               PATTERN (insn) = pat;
2094
2095             return;
2096           }
2097
2098         /* Otherwise, storing into VAR must be handled specially
2099            by storing into a temporary and copying that into VAR
2100            with a new insn after this one.  Note that this case
2101            will be used when storing into a promoted scalar since
2102            the insn will now have different modes on the input
2103            and output and hence will be invalid (except for the case
2104            of setting it to a constant, which does not need any
2105            change if it is valid).  We generate extra code in that case,
2106            but combine.c will eliminate it.  */
2107
2108         if (dest == var)
2109           {
2110             rtx temp;
2111             rtx fixeddest = SET_DEST (x);
2112
2113             /* STRICT_LOW_PART can be discarded, around a MEM.  */
2114             if (GET_CODE (fixeddest) == STRICT_LOW_PART)
2115               fixeddest = XEXP (fixeddest, 0);
2116             /* Convert (SUBREG (MEM)) to a MEM in a changed mode.  */
2117             if (GET_CODE (fixeddest) == SUBREG)
2118               {
2119                 fixeddest = fixup_memory_subreg (fixeddest, insn, 0);
2120                 promoted_mode = GET_MODE (fixeddest);
2121               }
2122             else
2123               fixeddest = fixup_stack_1 (fixeddest, insn);
2124
2125             temp = gen_reg_rtx (promoted_mode);
2126
2127             emit_insn_after (gen_move_insn (fixeddest,
2128                                             gen_lowpart (GET_MODE (fixeddest),
2129                                                          temp)),
2130                              insn);
2131
2132             SET_DEST (x) = temp;
2133           }
2134       }
2135     }
2136
2137   /* Nothing special about this RTX; fix its operands.  */
2138
2139   fmt = GET_RTX_FORMAT (code);
2140   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2141     {
2142       if (fmt[i] == 'e')
2143         fixup_var_refs_1 (var, promoted_mode, &XEXP (x, i), insn, replacements);
2144       if (fmt[i] == 'E')
2145         {
2146           register int j;
2147           for (j = 0; j < XVECLEN (x, i); j++)
2148             fixup_var_refs_1 (var, promoted_mode, &XVECEXP (x, i, j),
2149                               insn, replacements);
2150         }
2151     }
2152 }
2153 \f
2154 /* Given X, an rtx of the form (SUBREG:m1 (MEM:m2 addr)),
2155    return an rtx (MEM:m1 newaddr) which is equivalent.
2156    If any insns must be emitted to compute NEWADDR, put them before INSN.
2157
2158    UNCRITICAL nonzero means accept paradoxical subregs.
2159    This is used for subregs found inside of ZERO_EXTRACTs and in REG_NOTES. */
2160
2161 static rtx
2162 fixup_memory_subreg (x, insn, uncritical)
2163      rtx x;
2164      rtx insn;
2165      int uncritical;
2166 {
2167   int offset = SUBREG_WORD (x) * UNITS_PER_WORD;
2168   rtx addr = XEXP (SUBREG_REG (x), 0);
2169   enum machine_mode mode = GET_MODE (x);
2170   rtx saved, result;
2171
2172   /* Paradoxical SUBREGs are usually invalid during RTL generation.  */
2173   if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
2174       && ! uncritical)
2175     abort ();
2176
2177   if (BYTES_BIG_ENDIAN)
2178     offset += (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
2179                - MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode)));
2180   addr = plus_constant (addr, offset);
2181   if (!flag_force_addr && memory_address_p (mode, addr))
2182     /* Shortcut if no insns need be emitted.  */
2183     return change_address (SUBREG_REG (x), mode, addr);
2184   start_sequence ();
2185   result = change_address (SUBREG_REG (x), mode, addr);
2186   emit_insn_before (gen_sequence (), insn);
2187   end_sequence ();
2188   return result;
2189 }
2190
2191 /* Do fixup_memory_subreg on all (SUBREG (MEM ...) ...) contained in X.
2192    Replace subexpressions of X in place.
2193    If X itself is a (SUBREG (MEM ...) ...), return the replacement expression.
2194    Otherwise return X, with its contents possibly altered.
2195
2196    If any insns must be emitted to compute NEWADDR, put them before INSN. 
2197
2198    UNCRITICAL is as in fixup_memory_subreg.  */
2199
2200 static rtx
2201 walk_fixup_memory_subreg (x, insn, uncritical)
2202      register rtx x;
2203      rtx insn;
2204      int uncritical;
2205 {
2206   register enum rtx_code code;
2207   register char *fmt;
2208   register int i;
2209
2210   if (x == 0)
2211     return 0;
2212
2213   code = GET_CODE (x);
2214
2215   if (code == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
2216     return fixup_memory_subreg (x, insn, uncritical);
2217
2218   /* Nothing special about this RTX; fix its operands.  */
2219
2220   fmt = GET_RTX_FORMAT (code);
2221   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2222     {
2223       if (fmt[i] == 'e')
2224         XEXP (x, i) = walk_fixup_memory_subreg (XEXP (x, i), insn, uncritical);
2225       if (fmt[i] == 'E')
2226         {
2227           register int j;
2228           for (j = 0; j < XVECLEN (x, i); j++)
2229             XVECEXP (x, i, j)
2230               = walk_fixup_memory_subreg (XVECEXP (x, i, j), insn, uncritical);
2231         }
2232     }
2233   return x;
2234 }
2235 \f
2236 /* For each memory ref within X, if it refers to a stack slot
2237    with an out of range displacement, put the address in a temp register
2238    (emitting new insns before INSN to load these registers)
2239    and alter the memory ref to use that register.
2240    Replace each such MEM rtx with a copy, to avoid clobberage.  */
2241
2242 static rtx
2243 fixup_stack_1 (x, insn)
2244      rtx x;
2245      rtx insn;
2246 {
2247   register int i;
2248   register RTX_CODE code = GET_CODE (x);
2249   register char *fmt;
2250
2251   if (code == MEM)
2252     {
2253       register rtx ad = XEXP (x, 0);
2254       /* If we have address of a stack slot but it's not valid
2255          (displacement is too large), compute the sum in a register.  */
2256       if (GET_CODE (ad) == PLUS
2257           && GET_CODE (XEXP (ad, 0)) == REG
2258           && ((REGNO (XEXP (ad, 0)) >= FIRST_VIRTUAL_REGISTER
2259                && REGNO (XEXP (ad, 0)) <= LAST_VIRTUAL_REGISTER)
2260               || XEXP (ad, 0) == current_function_internal_arg_pointer)
2261           && GET_CODE (XEXP (ad, 1)) == CONST_INT)
2262         {
2263           rtx temp, seq;
2264           if (memory_address_p (GET_MODE (x), ad))
2265             return x;
2266
2267           start_sequence ();
2268           temp = copy_to_reg (ad);
2269           seq = gen_sequence ();
2270           end_sequence ();
2271           emit_insn_before (seq, insn);
2272           return change_address (x, VOIDmode, temp);
2273         }
2274       return x;
2275     }
2276
2277   fmt = GET_RTX_FORMAT (code);
2278   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2279     {
2280       if (fmt[i] == 'e')
2281         XEXP (x, i) = fixup_stack_1 (XEXP (x, i), insn);
2282       if (fmt[i] == 'E')
2283         {
2284           register int j;
2285           for (j = 0; j < XVECLEN (x, i); j++)
2286             XVECEXP (x, i, j) = fixup_stack_1 (XVECEXP (x, i, j), insn);
2287         }
2288     }
2289   return x;
2290 }
2291 \f
2292 /* Optimization: a bit-field instruction whose field
2293    happens to be a byte or halfword in memory
2294    can be changed to a move instruction.
2295
2296    We call here when INSN is an insn to examine or store into a bit-field.
2297    BODY is the SET-rtx to be altered.
2298
2299    EQUIV_MEM is the table `reg_equiv_mem' if that is available; else 0.
2300    (Currently this is called only from function.c, and EQUIV_MEM
2301    is always 0.)  */
2302
2303 static void
2304 optimize_bit_field (body, insn, equiv_mem)
2305      rtx body;
2306      rtx insn;
2307      rtx *equiv_mem;
2308 {
2309   register rtx bitfield;
2310   int destflag;
2311   rtx seq = 0;
2312   enum machine_mode mode;
2313
2314   if (GET_CODE (SET_DEST (body)) == SIGN_EXTRACT
2315       || GET_CODE (SET_DEST (body)) == ZERO_EXTRACT)
2316     bitfield = SET_DEST (body), destflag = 1;
2317   else
2318     bitfield = SET_SRC (body), destflag = 0;
2319
2320   /* First check that the field being stored has constant size and position
2321      and is in fact a byte or halfword suitably aligned.  */
2322
2323   if (GET_CODE (XEXP (bitfield, 1)) == CONST_INT
2324       && GET_CODE (XEXP (bitfield, 2)) == CONST_INT
2325       && ((mode = mode_for_size (INTVAL (XEXP (bitfield, 1)), MODE_INT, 1))
2326           != BLKmode)
2327       && INTVAL (XEXP (bitfield, 2)) % INTVAL (XEXP (bitfield, 1)) == 0)
2328     {
2329       register rtx memref = 0;
2330
2331       /* Now check that the containing word is memory, not a register,
2332          and that it is safe to change the machine mode.  */
2333
2334       if (GET_CODE (XEXP (bitfield, 0)) == MEM)
2335         memref = XEXP (bitfield, 0);
2336       else if (GET_CODE (XEXP (bitfield, 0)) == REG
2337                && equiv_mem != 0)
2338         memref = equiv_mem[REGNO (XEXP (bitfield, 0))];
2339       else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2340                && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == MEM)
2341         memref = SUBREG_REG (XEXP (bitfield, 0));
2342       else if (GET_CODE (XEXP (bitfield, 0)) == SUBREG
2343                && equiv_mem != 0
2344                && GET_CODE (SUBREG_REG (XEXP (bitfield, 0))) == REG)
2345         memref = equiv_mem[REGNO (SUBREG_REG (XEXP (bitfield, 0)))];
2346
2347       if (memref
2348           && ! mode_dependent_address_p (XEXP (memref, 0))
2349           && ! MEM_VOLATILE_P (memref))
2350         {
2351           /* Now adjust the address, first for any subreg'ing
2352              that we are now getting rid of,
2353              and then for which byte of the word is wanted.  */
2354
2355           register int offset = INTVAL (XEXP (bitfield, 2));
2356           rtx insns;
2357
2358           /* Adjust OFFSET to count bits from low-address byte.  */
2359           if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
2360             offset = (GET_MODE_BITSIZE (GET_MODE (XEXP (bitfield, 0)))
2361                       - offset - INTVAL (XEXP (bitfield, 1)));
2362
2363           /* Adjust OFFSET to count bytes from low-address byte.  */
2364           offset /= BITS_PER_UNIT;
2365           if (GET_CODE (XEXP (bitfield, 0)) == SUBREG)
2366             {
2367               offset += SUBREG_WORD (XEXP (bitfield, 0)) * UNITS_PER_WORD;
2368               if (BYTES_BIG_ENDIAN)
2369                 offset -= (MIN (UNITS_PER_WORD,
2370                                 GET_MODE_SIZE (GET_MODE (XEXP (bitfield, 0))))
2371                            - MIN (UNITS_PER_WORD,
2372                                   GET_MODE_SIZE (GET_MODE (memref))));
2373             }
2374
2375           start_sequence ();
2376           memref = change_address (memref, mode,
2377                                    plus_constant (XEXP (memref, 0), offset));
2378           insns = get_insns ();
2379           end_sequence ();
2380           emit_insns_before (insns, insn);
2381
2382           /* Store this memory reference where
2383              we found the bit field reference.  */
2384
2385           if (destflag)
2386             {
2387               validate_change (insn, &SET_DEST (body), memref, 1);
2388               if (! CONSTANT_ADDRESS_P (SET_SRC (body)))
2389                 {
2390                   rtx src = SET_SRC (body);
2391                   while (GET_CODE (src) == SUBREG
2392                          && SUBREG_WORD (src) == 0)
2393                     src = SUBREG_REG (src);
2394                   if (GET_MODE (src) != GET_MODE (memref))
2395                     src = gen_lowpart (GET_MODE (memref), SET_SRC (body));
2396                   validate_change (insn, &SET_SRC (body), src, 1);
2397                 }
2398               else if (GET_MODE (SET_SRC (body)) != VOIDmode
2399                        && GET_MODE (SET_SRC (body)) != GET_MODE (memref))
2400                 /* This shouldn't happen because anything that didn't have
2401                    one of these modes should have got converted explicitly
2402                    and then referenced through a subreg.
2403                    This is so because the original bit-field was
2404                    handled by agg_mode and so its tree structure had
2405                    the same mode that memref now has.  */
2406                 abort ();
2407             }
2408           else
2409             {
2410               rtx dest = SET_DEST (body);
2411
2412               while (GET_CODE (dest) == SUBREG
2413                      && SUBREG_WORD (dest) == 0
2414                      && (GET_MODE_CLASS (GET_MODE (dest))
2415                          == GET_MODE_CLASS (GET_MODE (SUBREG_REG (dest)))))
2416                 dest = SUBREG_REG (dest);
2417
2418               validate_change (insn, &SET_DEST (body), dest, 1);
2419
2420               if (GET_MODE (dest) == GET_MODE (memref))
2421                 validate_change (insn, &SET_SRC (body), memref, 1);
2422               else
2423                 {
2424                   /* Convert the mem ref to the destination mode.  */
2425                   rtx newreg = gen_reg_rtx (GET_MODE (dest));
2426
2427                   start_sequence ();
2428                   convert_move (newreg, memref,
2429                                 GET_CODE (SET_SRC (body)) == ZERO_EXTRACT);
2430                   seq = get_insns ();
2431                   end_sequence ();
2432
2433                   validate_change (insn, &SET_SRC (body), newreg, 1);
2434                 }
2435             }
2436
2437           /* See if we can convert this extraction or insertion into
2438              a simple move insn.  We might not be able to do so if this
2439              was, for example, part of a PARALLEL.
2440
2441              If we succeed, write out any needed conversions.  If we fail,
2442              it is hard to guess why we failed, so don't do anything
2443              special; just let the optimization be suppressed.  */
2444
2445           if (apply_change_group () && seq)
2446             emit_insns_before (seq, insn);
2447         }
2448     }
2449 }
2450 \f
2451 /* These routines are responsible for converting virtual register references
2452    to the actual hard register references once RTL generation is complete.
2453
2454    The following four variables are used for communication between the
2455    routines.  They contain the offsets of the virtual registers from their
2456    respective hard registers.  */
2457
2458 static int in_arg_offset;
2459 static int var_offset;
2460 static int dynamic_offset;
2461 static int out_arg_offset;
2462
2463 /* In most machines, the stack pointer register is equivalent to the bottom
2464    of the stack.  */
2465
2466 #ifndef STACK_POINTER_OFFSET
2467 #define STACK_POINTER_OFFSET    0
2468 #endif
2469
2470 /* If not defined, pick an appropriate default for the offset of dynamically
2471    allocated memory depending on the value of ACCUMULATE_OUTGOING_ARGS,
2472    REG_PARM_STACK_SPACE, and OUTGOING_REG_PARM_STACK_SPACE.  */
2473
2474 #ifndef STACK_DYNAMIC_OFFSET
2475
2476 #ifdef ACCUMULATE_OUTGOING_ARGS
2477 /* The bottom of the stack points to the actual arguments.  If
2478    REG_PARM_STACK_SPACE is defined, this includes the space for the register
2479    parameters.  However, if OUTGOING_REG_PARM_STACK space is not defined,
2480    stack space for register parameters is not pushed by the caller, but 
2481    rather part of the fixed stack areas and hence not included in
2482    `current_function_outgoing_args_size'.  Nevertheless, we must allow
2483    for it when allocating stack dynamic objects.  */
2484
2485 #if defined(REG_PARM_STACK_SPACE) && ! defined(OUTGOING_REG_PARM_STACK_SPACE)
2486 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
2487 (current_function_outgoing_args_size    \
2488  + REG_PARM_STACK_SPACE (FNDECL) + (STACK_POINTER_OFFSET))
2489
2490 #else
2491 #define STACK_DYNAMIC_OFFSET(FNDECL)    \
2492 (current_function_outgoing_args_size + (STACK_POINTER_OFFSET))
2493 #endif
2494
2495 #else
2496 #define STACK_DYNAMIC_OFFSET(FNDECL) STACK_POINTER_OFFSET
2497 #endif
2498 #endif
2499
2500 /* Pass through the INSNS of function FNDECL and convert virtual register
2501    references to hard register references.  */
2502
2503 void
2504 instantiate_virtual_regs (fndecl, insns)
2505      tree fndecl;
2506      rtx insns;
2507 {
2508   rtx insn;
2509
2510   /* Compute the offsets to use for this function.  */
2511   in_arg_offset = FIRST_PARM_OFFSET (fndecl);
2512   var_offset = STARTING_FRAME_OFFSET;
2513   dynamic_offset = STACK_DYNAMIC_OFFSET (fndecl);
2514   out_arg_offset = STACK_POINTER_OFFSET;
2515
2516   /* Scan all variables and parameters of this function.  For each that is
2517      in memory, instantiate all virtual registers if the result is a valid
2518      address.  If not, we do it later.  That will handle most uses of virtual
2519      regs on many machines.  */
2520   instantiate_decls (fndecl, 1);
2521
2522   /* Initialize recognition, indicating that volatile is OK.  */
2523   init_recog ();
2524
2525   /* Scan through all the insns, instantiating every virtual register still
2526      present.  */
2527   for (insn = insns; insn; insn = NEXT_INSN (insn))
2528     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
2529         || GET_CODE (insn) == CALL_INSN)
2530       {
2531         instantiate_virtual_regs_1 (&PATTERN (insn), insn, 1);
2532         instantiate_virtual_regs_1 (&REG_NOTES (insn), NULL_RTX, 0);
2533       }
2534
2535   /* Now instantiate the remaining register equivalences for debugging info.
2536      These will not be valid addresses.  */
2537   instantiate_decls (fndecl, 0);
2538
2539   /* Indicate that, from now on, assign_stack_local should use
2540      frame_pointer_rtx.  */
2541   virtuals_instantiated = 1;
2542 }
2543
2544 /* Scan all decls in FNDECL (both variables and parameters) and instantiate
2545    all virtual registers in their DECL_RTL's.
2546
2547    If VALID_ONLY, do this only if the resulting address is still valid.
2548    Otherwise, always do it.  */
2549
2550 static void
2551 instantiate_decls (fndecl, valid_only)
2552      tree fndecl;
2553      int valid_only;
2554 {
2555   tree decl;
2556
2557   if (DECL_SAVED_INSNS (fndecl))
2558     /* When compiling an inline function, the obstack used for
2559        rtl allocation is the maybepermanent_obstack.  Calling
2560        `resume_temporary_allocation' switches us back to that
2561        obstack while we process this function's parameters.  */
2562     resume_temporary_allocation ();
2563
2564   /* Process all parameters of the function.  */
2565   for (decl = DECL_ARGUMENTS (fndecl); decl; decl = TREE_CHAIN (decl))
2566     {
2567       instantiate_decl (DECL_RTL (decl), int_size_in_bytes (TREE_TYPE (decl)),
2568                         valid_only);    
2569       instantiate_decl (DECL_INCOMING_RTL (decl),
2570                         int_size_in_bytes (TREE_TYPE (decl)), valid_only);
2571     }
2572
2573   /* Now process all variables defined in the function or its subblocks. */
2574   instantiate_decls_1 (DECL_INITIAL (fndecl), valid_only);
2575
2576   if (DECL_INLINE (fndecl) || DECL_DEFER_OUTPUT (fndecl))
2577     {
2578       /* Save all rtl allocated for this function by raising the
2579          high-water mark on the maybepermanent_obstack.  */
2580       preserve_data ();
2581       /* All further rtl allocation is now done in the current_obstack.  */
2582       rtl_in_current_obstack ();
2583     }
2584 }
2585
2586 /* Subroutine of instantiate_decls: Process all decls in the given
2587    BLOCK node and all its subblocks.  */
2588
2589 static void
2590 instantiate_decls_1 (let, valid_only)
2591      tree let;
2592      int valid_only;
2593 {
2594   tree t;
2595
2596   for (t = BLOCK_VARS (let); t; t = TREE_CHAIN (t))
2597     instantiate_decl (DECL_RTL (t), int_size_in_bytes (TREE_TYPE (t)),
2598                       valid_only);
2599
2600   /* Process all subblocks.  */
2601   for (t = BLOCK_SUBBLOCKS (let); t; t = TREE_CHAIN (t))
2602     instantiate_decls_1 (t, valid_only);
2603 }
2604
2605 /* Subroutine of the preceding procedures: Given RTL representing a
2606    decl and the size of the object, do any instantiation required.
2607
2608    If VALID_ONLY is non-zero, it means that the RTL should only be
2609    changed if the new address is valid.  */
2610
2611 static void
2612 instantiate_decl (x, size, valid_only)
2613      rtx x;
2614      int size;
2615      int valid_only;
2616 {
2617   enum machine_mode mode;
2618   rtx addr;
2619
2620   /* If this is not a MEM, no need to do anything.  Similarly if the
2621      address is a constant or a register that is not a virtual register.  */
2622
2623   if (x == 0 || GET_CODE (x) != MEM)
2624     return;
2625
2626   addr = XEXP (x, 0);
2627   if (CONSTANT_P (addr)
2628       || (GET_CODE (addr) == REG
2629           && (REGNO (addr) < FIRST_VIRTUAL_REGISTER
2630               || REGNO (addr) > LAST_VIRTUAL_REGISTER)))
2631     return;
2632
2633   /* If we should only do this if the address is valid, copy the address.
2634      We need to do this so we can undo any changes that might make the
2635      address invalid.  This copy is unfortunate, but probably can't be
2636      avoided.  */
2637
2638   if (valid_only)
2639     addr = copy_rtx (addr);
2640
2641   instantiate_virtual_regs_1 (&addr, NULL_RTX, 0);
2642
2643   if (! valid_only)
2644     return;
2645
2646   /* Now verify that the resulting address is valid for every integer or
2647      floating-point mode up to and including SIZE bytes long.  We do this
2648      since the object might be accessed in any mode and frame addresses
2649      are shared.  */
2650
2651   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2652        mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2653        mode = GET_MODE_WIDER_MODE (mode))
2654     if (! memory_address_p (mode, addr))
2655       return;
2656
2657   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
2658        mode != VOIDmode && GET_MODE_SIZE (mode) <= size;
2659        mode = GET_MODE_WIDER_MODE (mode))
2660     if (! memory_address_p (mode, addr))
2661       return;
2662
2663   /* Otherwise, put back the address, now that we have updated it and we
2664      know it is valid.  */
2665
2666   XEXP (x, 0) = addr;
2667 }
2668 \f
2669 /* Given a pointer to a piece of rtx and an optional pointer to the
2670    containing object, instantiate any virtual registers present in it.
2671
2672    If EXTRA_INSNS, we always do the replacement and generate
2673    any extra insns before OBJECT.  If it zero, we do nothing if replacement
2674    is not valid.
2675
2676    Return 1 if we either had nothing to do or if we were able to do the
2677    needed replacement.  Return 0 otherwise; we only return zero if 
2678    EXTRA_INSNS is zero.
2679
2680    We first try some simple transformations to avoid the creation of extra
2681    pseudos.  */
2682
2683 static int
2684 instantiate_virtual_regs_1 (loc, object, extra_insns)
2685      rtx *loc;
2686      rtx object;
2687      int extra_insns;
2688 {
2689   rtx x;
2690   RTX_CODE code;
2691   rtx new = 0;
2692   int offset;
2693   rtx temp;
2694   rtx seq;
2695   int i, j;
2696   char *fmt;
2697
2698   /* Re-start here to avoid recursion in common cases.  */
2699  restart:
2700
2701   x = *loc;
2702   if (x == 0)
2703     return 1;
2704
2705   code = GET_CODE (x);
2706
2707   /* Check for some special cases.  */
2708   switch (code)
2709     {
2710     case CONST_INT:
2711     case CONST_DOUBLE:
2712     case CONST:
2713     case SYMBOL_REF:
2714     case CODE_LABEL:
2715     case PC:
2716     case CC0:
2717     case ASM_INPUT:
2718     case ADDR_VEC:
2719     case ADDR_DIFF_VEC:
2720     case RETURN:
2721       return 1;
2722
2723     case SET:
2724       /* We are allowed to set the virtual registers.  This means that
2725          that the actual register should receive the source minus the
2726          appropriate offset.  This is used, for example, in the handling
2727          of non-local gotos.  */
2728       if (SET_DEST (x) == virtual_incoming_args_rtx)
2729         new = arg_pointer_rtx, offset = - in_arg_offset;
2730       else if (SET_DEST (x) == virtual_stack_vars_rtx)
2731         new = frame_pointer_rtx, offset = - var_offset;
2732       else if (SET_DEST (x) == virtual_stack_dynamic_rtx)
2733         new = stack_pointer_rtx, offset = - dynamic_offset;
2734       else if (SET_DEST (x) == virtual_outgoing_args_rtx)
2735         new = stack_pointer_rtx, offset = - out_arg_offset;
2736
2737       if (new)
2738         {
2739           /* The only valid sources here are PLUS or REG.  Just do
2740              the simplest possible thing to handle them.  */
2741           if (GET_CODE (SET_SRC (x)) != REG
2742               && GET_CODE (SET_SRC (x)) != PLUS)
2743             abort ();
2744
2745           start_sequence ();
2746           if (GET_CODE (SET_SRC (x)) != REG)
2747             temp = force_operand (SET_SRC (x), NULL_RTX);
2748           else
2749             temp = SET_SRC (x);
2750           temp = force_operand (plus_constant (temp, offset), NULL_RTX);
2751           seq = get_insns ();
2752           end_sequence ();
2753
2754           emit_insns_before (seq, object);
2755           SET_DEST (x) = new;
2756
2757           if (!validate_change (object, &SET_SRC (x), temp, 0)
2758               || ! extra_insns)
2759             abort ();
2760
2761           return 1;
2762         }
2763
2764       instantiate_virtual_regs_1 (&SET_DEST (x), object, extra_insns);
2765       loc = &SET_SRC (x);
2766       goto restart;
2767
2768     case PLUS:
2769       /* Handle special case of virtual register plus constant.  */
2770       if (CONSTANT_P (XEXP (x, 1)))
2771         {
2772           rtx old, new_offset;
2773
2774           /* Check for (plus (plus VIRT foo) (const_int)) first.  */
2775           if (GET_CODE (XEXP (x, 0)) == PLUS)
2776             {
2777               rtx inner = XEXP (XEXP (x, 0), 0);
2778
2779               if (inner == virtual_incoming_args_rtx)
2780                 new = arg_pointer_rtx, offset = in_arg_offset;
2781               else if (inner == virtual_stack_vars_rtx)
2782                 new = frame_pointer_rtx, offset = var_offset;
2783               else if (inner == virtual_stack_dynamic_rtx)
2784                 new = stack_pointer_rtx, offset = dynamic_offset;
2785               else if (inner == virtual_outgoing_args_rtx)
2786                 new = stack_pointer_rtx, offset = out_arg_offset;
2787               else
2788                 {
2789                   loc = &XEXP (x, 0);
2790                   goto restart;
2791                 }
2792
2793               instantiate_virtual_regs_1 (&XEXP (XEXP (x, 0), 1), object,
2794                                           extra_insns);
2795               new = gen_rtx (PLUS, Pmode, new, XEXP (XEXP (x, 0), 1));
2796             }
2797
2798           else if (XEXP (x, 0) == virtual_incoming_args_rtx)
2799             new = arg_pointer_rtx, offset = in_arg_offset;
2800           else if (XEXP (x, 0) == virtual_stack_vars_rtx)
2801             new = frame_pointer_rtx, offset = var_offset;
2802           else if (XEXP (x, 0) == virtual_stack_dynamic_rtx)
2803             new = stack_pointer_rtx, offset = dynamic_offset;
2804           else if (XEXP (x, 0) == virtual_outgoing_args_rtx)
2805             new = stack_pointer_rtx, offset = out_arg_offset;
2806           else
2807             {
2808               /* We know the second operand is a constant.  Unless the
2809                  first operand is a REG (which has been already checked),
2810                  it needs to be checked.  */
2811               if (GET_CODE (XEXP (x, 0)) != REG)
2812                 {
2813                   loc = &XEXP (x, 0);
2814                   goto restart;
2815                 }
2816               return 1;
2817             }
2818
2819           new_offset = plus_constant (XEXP (x, 1), offset);
2820
2821           /* If the new constant is zero, try to replace the sum with just
2822              the register.  */
2823           if (new_offset == const0_rtx
2824               && validate_change (object, loc, new, 0))
2825             return 1;
2826
2827           /* Next try to replace the register and new offset.
2828              There are two changes to validate here and we can't assume that
2829              in the case of old offset equals new just changing the register
2830              will yield a valid insn.  In the interests of a little efficiency,
2831              however, we only call validate change once (we don't queue up the
2832              changes and then call apply_change_group). */
2833
2834           old = XEXP (x, 0);
2835           if (offset == 0
2836               ? ! validate_change (object, &XEXP (x, 0), new, 0)
2837               : (XEXP (x, 0) = new,
2838                  ! validate_change (object, &XEXP (x, 1), new_offset, 0)))
2839             {
2840               if (! extra_insns)
2841                 {
2842                   XEXP (x, 0) = old;
2843                   return 0;
2844                 }
2845
2846               /* Otherwise copy the new constant into a register and replace
2847                  constant with that register.  */
2848               temp = gen_reg_rtx (Pmode);
2849               XEXP (x, 0) = new;
2850               if (validate_change (object, &XEXP (x, 1), temp, 0))
2851                 emit_insn_before (gen_move_insn (temp, new_offset), object);
2852               else
2853                 {
2854                   /* If that didn't work, replace this expression with a
2855                      register containing the sum.  */
2856
2857                   XEXP (x, 0) = old;
2858                   new = gen_rtx (PLUS, Pmode, new, new_offset);
2859
2860                   start_sequence ();
2861                   temp = force_operand (new, NULL_RTX);
2862                   seq = get_insns ();
2863                   end_sequence ();
2864
2865                   emit_insns_before (seq, object);
2866                   if (! validate_change (object, loc, temp, 0)
2867                       && ! validate_replace_rtx (x, temp, object))
2868                     abort ();
2869                 }
2870             }
2871
2872           return 1;
2873         }
2874
2875       /* Fall through to generic two-operand expression case.  */
2876     case EXPR_LIST:
2877     case CALL:
2878     case COMPARE:
2879     case MINUS:
2880     case MULT:
2881     case DIV:      case UDIV:
2882     case MOD:      case UMOD:
2883     case AND:      case IOR:      case XOR:
2884     case ROTATERT: case ROTATE:
2885     case ASHIFTRT: case LSHIFTRT: case ASHIFT:
2886     case NE:       case EQ:
2887     case GE:       case GT:       case GEU:    case GTU:
2888     case LE:       case LT:       case LEU:    case LTU:
2889       if (XEXP (x, 1) && ! CONSTANT_P (XEXP (x, 1)))
2890         instantiate_virtual_regs_1 (&XEXP (x, 1), object, extra_insns);
2891       loc = &XEXP (x, 0);
2892       goto restart;
2893
2894     case MEM:
2895       /* Most cases of MEM that convert to valid addresses have already been
2896          handled by our scan of regno_reg_rtx.  The only special handling we
2897          need here is to make a copy of the rtx to ensure it isn't being
2898          shared if we have to change it to a pseudo. 
2899
2900          If the rtx is a simple reference to an address via a virtual register,
2901          it can potentially be shared.  In such cases, first try to make it
2902          a valid address, which can also be shared.  Otherwise, copy it and
2903          proceed normally. 
2904
2905          First check for common cases that need no processing.  These are
2906          usually due to instantiation already being done on a previous instance
2907          of a shared rtx.  */
2908
2909       temp = XEXP (x, 0);
2910       if (CONSTANT_ADDRESS_P (temp)
2911 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2912           || temp == arg_pointer_rtx
2913 #endif
2914 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2915           || temp == hard_frame_pointer_rtx
2916 #endif
2917           || temp == frame_pointer_rtx)
2918         return 1;
2919
2920       if (GET_CODE (temp) == PLUS
2921           && CONSTANT_ADDRESS_P (XEXP (temp, 1))
2922           && (XEXP (temp, 0) == frame_pointer_rtx
2923 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2924               || XEXP (temp, 0) == hard_frame_pointer_rtx
2925 #endif
2926 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
2927               || XEXP (temp, 0) == arg_pointer_rtx
2928 #endif
2929               ))
2930         return 1;
2931
2932       if (temp == virtual_stack_vars_rtx
2933           || temp == virtual_incoming_args_rtx
2934           || (GET_CODE (temp) == PLUS
2935               && CONSTANT_ADDRESS_P (XEXP (temp, 1))
2936               && (XEXP (temp, 0) == virtual_stack_vars_rtx
2937                   || XEXP (temp, 0) == virtual_incoming_args_rtx)))
2938         {
2939           /* This MEM may be shared.  If the substitution can be done without
2940              the need to generate new pseudos, we want to do it in place
2941              so all copies of the shared rtx benefit.  The call below will
2942              only make substitutions if the resulting address is still
2943              valid.
2944
2945              Note that we cannot pass X as the object in the recursive call
2946              since the insn being processed may not allow all valid
2947              addresses.  However, if we were not passed on object, we can
2948              only modify X without copying it if X will have a valid
2949              address.
2950
2951              ??? Also note that this can still lose if OBJECT is an insn that
2952              has less restrictions on an address that some other insn.
2953              In that case, we will modify the shared address.  This case
2954              doesn't seem very likely, though.  */
2955
2956           if (instantiate_virtual_regs_1 (&XEXP (x, 0),
2957                                           object ? object : x, 0))
2958             return 1;
2959
2960           /* Otherwise make a copy and process that copy.  We copy the entire
2961              RTL expression since it might be a PLUS which could also be
2962              shared.  */
2963           *loc = x = copy_rtx (x);
2964         }
2965
2966       /* Fall through to generic unary operation case.  */
2967     case USE:
2968     case CLOBBER:
2969     case SUBREG:
2970     case STRICT_LOW_PART:
2971     case NEG:          case NOT:
2972     case PRE_DEC:      case PRE_INC:      case POST_DEC:    case POST_INC:
2973     case SIGN_EXTEND:  case ZERO_EXTEND:
2974     case TRUNCATE:     case FLOAT_EXTEND: case FLOAT_TRUNCATE:
2975     case FLOAT:        case FIX:
2976     case UNSIGNED_FIX: case UNSIGNED_FLOAT:
2977     case ABS:
2978     case SQRT:
2979     case FFS:
2980       /* These case either have just one operand or we know that we need not
2981          check the rest of the operands.  */
2982       loc = &XEXP (x, 0);
2983       goto restart;
2984
2985     case REG:
2986       /* Try to replace with a PLUS.  If that doesn't work, compute the sum
2987          in front of this insn and substitute the temporary.  */
2988       if (x == virtual_incoming_args_rtx)
2989         new = arg_pointer_rtx, offset = in_arg_offset;
2990       else if (x == virtual_stack_vars_rtx)
2991         new = frame_pointer_rtx, offset = var_offset;
2992       else if (x == virtual_stack_dynamic_rtx)
2993         new = stack_pointer_rtx, offset = dynamic_offset;
2994       else if (x == virtual_outgoing_args_rtx)
2995         new = stack_pointer_rtx, offset = out_arg_offset;
2996
2997       if (new)
2998         {
2999           temp = plus_constant (new, offset);
3000           if (!validate_change (object, loc, temp, 0))
3001             {
3002               if (! extra_insns)
3003                 return 0;
3004
3005               start_sequence ();
3006               temp = force_operand (temp, NULL_RTX);
3007               seq = get_insns ();
3008               end_sequence ();
3009
3010               emit_insns_before (seq, object);
3011               if (! validate_change (object, loc, temp, 0)
3012                   && ! validate_replace_rtx (x, temp, object))
3013                 abort ();
3014             }
3015         }
3016
3017       return 1;
3018     }
3019
3020   /* Scan all subexpressions.  */
3021   fmt = GET_RTX_FORMAT (code);
3022   for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
3023     if (*fmt == 'e')
3024       {
3025         if (!instantiate_virtual_regs_1 (&XEXP (x, i), object, extra_insns))
3026           return 0;
3027       }
3028     else if (*fmt == 'E')
3029       for (j = 0; j < XVECLEN (x, i); j++)
3030         if (! instantiate_virtual_regs_1 (&XVECEXP (x, i, j), object,
3031                                           extra_insns))
3032           return 0;
3033
3034   return 1;
3035 }
3036 \f
3037 /* Optimization: assuming this function does not receive nonlocal gotos,
3038    delete the handlers for such, as well as the insns to establish
3039    and disestablish them.  */
3040
3041 static void
3042 delete_handlers ()
3043 {
3044   rtx insn;
3045   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3046     {
3047       /* Delete the handler by turning off the flag that would
3048          prevent jump_optimize from deleting it.
3049          Also permit deletion of the nonlocal labels themselves
3050          if nothing local refers to them.  */
3051       if (GET_CODE (insn) == CODE_LABEL)
3052         {
3053           tree t, last_t;
3054
3055           LABEL_PRESERVE_P (insn) = 0;
3056
3057           /* Remove it from the nonlocal_label list, to avoid confusing
3058              flow.  */
3059           for (t = nonlocal_labels, last_t = 0; t;
3060                last_t = t, t = TREE_CHAIN (t))
3061             if (DECL_RTL (TREE_VALUE (t)) == insn)
3062               break;
3063           if (t)
3064             {
3065               if (! last_t)
3066                 nonlocal_labels = TREE_CHAIN (nonlocal_labels);
3067               else
3068                 TREE_CHAIN (last_t) = TREE_CHAIN (t);
3069             }
3070         }
3071       if (GET_CODE (insn) == INSN
3072           && ((nonlocal_goto_handler_slot != 0
3073                && reg_mentioned_p (nonlocal_goto_handler_slot, PATTERN (insn)))
3074               || (nonlocal_goto_stack_level != 0
3075                   && reg_mentioned_p (nonlocal_goto_stack_level,
3076                                       PATTERN (insn)))))
3077         delete_insn (insn);
3078     }
3079 }
3080
3081 /* Return a list (chain of EXPR_LIST nodes) for the nonlocal labels
3082    of the current function.  */
3083
3084 rtx
3085 nonlocal_label_rtx_list ()
3086 {
3087   tree t;
3088   rtx x = 0;
3089
3090   for (t = nonlocal_labels; t; t = TREE_CHAIN (t))
3091     x = gen_rtx (EXPR_LIST, VOIDmode, label_rtx (TREE_VALUE (t)), x);
3092
3093   return x;
3094 }
3095 \f
3096 /* Output a USE for any register use in RTL.
3097    This is used with -noreg to mark the extent of lifespan
3098    of any registers used in a user-visible variable's DECL_RTL.  */
3099
3100 void
3101 use_variable (rtl)
3102      rtx rtl;
3103 {
3104   if (GET_CODE (rtl) == REG)
3105     /* This is a register variable.  */
3106     emit_insn (gen_rtx (USE, VOIDmode, rtl));
3107   else if (GET_CODE (rtl) == MEM
3108            && GET_CODE (XEXP (rtl, 0)) == REG
3109            && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3110                || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3111            && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3112     /* This is a variable-sized structure.  */
3113     emit_insn (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)));
3114 }
3115
3116 /* Like use_variable except that it outputs the USEs after INSN
3117    instead of at the end of the insn-chain.  */
3118
3119 void
3120 use_variable_after (rtl, insn)
3121      rtx rtl, insn;
3122 {
3123   if (GET_CODE (rtl) == REG)
3124     /* This is a register variable.  */
3125     emit_insn_after (gen_rtx (USE, VOIDmode, rtl), insn);
3126   else if (GET_CODE (rtl) == MEM
3127            && GET_CODE (XEXP (rtl, 0)) == REG
3128            && (REGNO (XEXP (rtl, 0)) < FIRST_VIRTUAL_REGISTER
3129                || REGNO (XEXP (rtl, 0)) > LAST_VIRTUAL_REGISTER)
3130            && XEXP (rtl, 0) != current_function_internal_arg_pointer)
3131     /* This is a variable-sized structure.  */
3132     emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (rtl, 0)), insn);
3133 }
3134 \f
3135 int
3136 max_parm_reg_num ()
3137 {
3138   return max_parm_reg;
3139 }
3140
3141 /* Return the first insn following those generated by `assign_parms'.  */
3142
3143 rtx
3144 get_first_nonparm_insn ()
3145 {
3146   if (last_parm_insn)
3147     return NEXT_INSN (last_parm_insn);
3148   return get_insns ();
3149 }
3150
3151 /* Return the first NOTE_INSN_BLOCK_BEG note in the function.
3152    Crash if there is none.  */
3153
3154 rtx
3155 get_first_block_beg ()
3156 {
3157   register rtx searcher;
3158   register rtx insn = get_first_nonparm_insn ();
3159
3160   for (searcher = insn; searcher; searcher = NEXT_INSN (searcher))
3161     if (GET_CODE (searcher) == NOTE
3162         && NOTE_LINE_NUMBER (searcher) == NOTE_INSN_BLOCK_BEG)
3163       return searcher;
3164
3165   abort ();     /* Invalid call to this function.  (See comments above.)  */
3166   return NULL_RTX;
3167 }
3168
3169 /* Return 1 if EXP is an aggregate type (or a value with aggregate type).
3170    This means a type for which function calls must pass an address to the
3171    function or get an address back from the function.
3172    EXP may be a type node or an expression (whose type is tested).  */
3173
3174 int
3175 aggregate_value_p (exp)
3176      tree exp;
3177 {
3178   int i, regno, nregs;
3179   rtx reg;
3180   tree type;
3181   if (TREE_CODE_CLASS (TREE_CODE (exp)) == 't')
3182     type = exp;
3183   else
3184     type = TREE_TYPE (exp);
3185
3186   if (RETURN_IN_MEMORY (type))
3187     return 1;
3188   if (flag_pcc_struct_return && AGGREGATE_TYPE_P (type))
3189     return 1;
3190   /* Make sure we have suitable call-clobbered regs to return
3191      the value in; if not, we must return it in memory.  */
3192   reg = hard_function_value (type, 0);
3193   regno = REGNO (reg);
3194   nregs = HARD_REGNO_NREGS (regno, TYPE_MODE (type));
3195   for (i = 0; i < nregs; i++)
3196     if (! call_used_regs[regno + i])
3197       return 1;
3198   return 0;
3199 }
3200 \f
3201 /* Assign RTL expressions to the function's parameters.
3202    This may involve copying them into registers and using
3203    those registers as the RTL for them.
3204
3205    If SECOND_TIME is non-zero it means that this function is being
3206    called a second time.  This is done by integrate.c when a function's
3207    compilation is deferred.  We need to come back here in case the
3208    FUNCTION_ARG macro computes items needed for the rest of the compilation
3209    (such as changing which registers are fixed or caller-saved).  But suppress
3210    writing any insns or setting DECL_RTL of anything in this case.  */
3211
3212 void
3213 assign_parms (fndecl, second_time)
3214      tree fndecl;
3215      int second_time;
3216 {
3217   register tree parm;
3218   register rtx entry_parm = 0;
3219   register rtx stack_parm = 0;
3220   CUMULATIVE_ARGS args_so_far;
3221   enum machine_mode promoted_mode, passed_mode;
3222   enum machine_mode nominal_mode, promoted_nominal_mode;
3223   int unsignedp;
3224   /* Total space needed so far for args on the stack,
3225      given as a constant and a tree-expression.  */
3226   struct args_size stack_args_size;
3227   tree fntype = TREE_TYPE (fndecl);
3228   tree fnargs = DECL_ARGUMENTS (fndecl);
3229   /* This is used for the arg pointer when referring to stack args.  */
3230   rtx internal_arg_pointer;
3231   /* This is a dummy PARM_DECL that we used for the function result if 
3232      the function returns a structure.  */
3233   tree function_result_decl = 0;
3234   int nparmregs = list_length (fnargs) + LAST_VIRTUAL_REGISTER + 1;
3235   int varargs_setup = 0;
3236   rtx conversion_insns = 0;
3237   /* FUNCTION_ARG may look at this variable.  Since this is not
3238      expanding a call it will always be zero in this function.  */
3239   int current_call_is_indirect = 0;
3240
3241   /* Nonzero if the last arg is named `__builtin_va_alist',
3242      which is used on some machines for old-fashioned non-ANSI varargs.h;
3243      this should be stuck onto the stack as if it had arrived there.  */
3244   int hide_last_arg
3245     = (current_function_varargs
3246        && fnargs
3247        && (parm = tree_last (fnargs)) != 0
3248        && DECL_NAME (parm)
3249        && (! strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
3250                      "__builtin_va_alist")));
3251
3252   /* Nonzero if function takes extra anonymous args.
3253      This means the last named arg must be on the stack
3254      right before the anonymous ones. */
3255   int stdarg
3256     = (TYPE_ARG_TYPES (fntype) != 0
3257        && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3258            != void_type_node));
3259
3260   current_function_stdarg = stdarg;
3261
3262   /* If the reg that the virtual arg pointer will be translated into is
3263      not a fixed reg or is the stack pointer, make a copy of the virtual
3264      arg pointer, and address parms via the copy.  The frame pointer is
3265      considered fixed even though it is not marked as such.
3266
3267      The second time through, simply use ap to avoid generating rtx.  */
3268
3269   if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
3270        || ! (fixed_regs[ARG_POINTER_REGNUM]
3271              || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM))
3272       && ! second_time)
3273     internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
3274   else
3275     internal_arg_pointer = virtual_incoming_args_rtx;
3276   current_function_internal_arg_pointer = internal_arg_pointer;
3277
3278   stack_args_size.constant = 0;
3279   stack_args_size.var = 0;
3280
3281   /* If struct value address is treated as the first argument, make it so.  */
3282   if (aggregate_value_p (DECL_RESULT (fndecl))
3283       && ! current_function_returns_pcc_struct
3284       && struct_value_incoming_rtx == 0)
3285     {
3286       tree type = build_pointer_type (TREE_TYPE (fntype));
3287
3288       function_result_decl = build_decl (PARM_DECL, NULL_TREE, type);
3289
3290       DECL_ARG_TYPE (function_result_decl) = type;
3291       TREE_CHAIN (function_result_decl) = fnargs;
3292       fnargs = function_result_decl;
3293     }
3294                                
3295   parm_reg_stack_loc = (rtx *) oballoc (nparmregs * sizeof (rtx));
3296   bzero ((char *) parm_reg_stack_loc, nparmregs * sizeof (rtx));
3297
3298 #ifdef INIT_CUMULATIVE_INCOMING_ARGS
3299   INIT_CUMULATIVE_INCOMING_ARGS (args_so_far, fntype, NULL_RTX);
3300 #else
3301   INIT_CUMULATIVE_ARGS (args_so_far, fntype, NULL_RTX);
3302 #endif
3303
3304   /* We haven't yet found an argument that we must push and pretend the
3305      caller did.  */
3306   current_function_pretend_args_size = 0;
3307
3308   for (parm = fnargs; parm; parm = TREE_CHAIN (parm))
3309     {
3310       int aggregate = AGGREGATE_TYPE_P (TREE_TYPE (parm));
3311       struct args_size stack_offset;
3312       struct args_size arg_size;
3313       int passed_pointer = 0;
3314       int did_conversion = 0;
3315       tree passed_type = DECL_ARG_TYPE (parm);
3316       tree nominal_type = TREE_TYPE (parm);
3317
3318       /* Set LAST_NAMED if this is last named arg before some
3319          anonymous args.  We treat it as if it were anonymous too.  */
3320       int last_named = ((TREE_CHAIN (parm) == 0
3321                          || DECL_NAME (TREE_CHAIN (parm)) == 0)
3322                         && (stdarg || current_function_varargs));
3323
3324       if (TREE_TYPE (parm) == error_mark_node
3325           /* This can happen after weird syntax errors
3326              or if an enum type is defined among the parms.  */
3327           || TREE_CODE (parm) != PARM_DECL
3328           || passed_type == NULL)
3329         {
3330           DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = gen_rtx (MEM, BLKmode,
3331                                                                 const0_rtx);
3332           TREE_USED (parm) = 1;
3333           continue;
3334         }
3335
3336       /* For varargs.h function, save info about regs and stack space
3337          used by the individual args, not including the va_alist arg.  */
3338       if (hide_last_arg && last_named)
3339         current_function_args_info = args_so_far;
3340
3341       /* Find mode of arg as it is passed, and mode of arg
3342          as it should be during execution of this function.  */
3343       passed_mode = TYPE_MODE (passed_type);
3344       nominal_mode = TYPE_MODE (nominal_type);
3345
3346       /* If the parm's mode is VOID, its value doesn't matter,
3347          and avoid the usual things like emit_move_insn that could crash.  */
3348       if (nominal_mode == VOIDmode)
3349         {
3350           DECL_INCOMING_RTL (parm) = DECL_RTL (parm) = const0_rtx;
3351           continue;
3352         }
3353
3354       /* If the parm is to be passed as a transparent union, use the
3355          type of the first field for the tests below.  We have already
3356          verified that the modes are the same.  */
3357       if (DECL_TRANSPARENT_UNION (parm)
3358           || TYPE_TRANSPARENT_UNION (passed_type))
3359         passed_type = TREE_TYPE (TYPE_FIELDS (passed_type));
3360
3361       /* See if this arg was passed by invisible reference.  It is if
3362          it is an object whose size depends on the contents of the
3363          object itself or if the machine requires these objects be passed
3364          that way.  */
3365
3366       if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
3367            && contains_placeholder_p (TYPE_SIZE (passed_type)))
3368           || TREE_ADDRESSABLE (passed_type)
3369 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3370           || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, passed_mode,
3371                                               passed_type, ! last_named)
3372 #endif
3373           )
3374         {
3375           passed_type = nominal_type = build_pointer_type (passed_type);
3376           passed_pointer = 1;
3377           passed_mode = nominal_mode = Pmode;
3378         }
3379
3380       promoted_mode = passed_mode;
3381
3382 #ifdef PROMOTE_FUNCTION_ARGS
3383       /* Compute the mode in which the arg is actually extended to.  */
3384       promoted_mode = promote_mode (passed_type, promoted_mode, &unsignedp, 1);
3385 #endif
3386
3387       /* Let machine desc say which reg (if any) the parm arrives in.
3388          0 means it arrives on the stack.  */
3389 #ifdef FUNCTION_INCOMING_ARG
3390       entry_parm = FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3391                                           passed_type, ! last_named);
3392 #else
3393       entry_parm = FUNCTION_ARG (args_so_far, promoted_mode,
3394                                  passed_type, ! last_named);
3395 #endif
3396
3397       if (entry_parm == 0)
3398         promoted_mode = passed_mode;
3399
3400 #ifdef SETUP_INCOMING_VARARGS
3401       /* If this is the last named parameter, do any required setup for
3402          varargs or stdargs.  We need to know about the case of this being an
3403          addressable type, in which case we skip the registers it
3404          would have arrived in.
3405
3406          For stdargs, LAST_NAMED will be set for two parameters, the one that
3407          is actually the last named, and the dummy parameter.  We only
3408          want to do this action once.
3409
3410          Also, indicate when RTL generation is to be suppressed.  */
3411       if (last_named && !varargs_setup)
3412         {
3413           SETUP_INCOMING_VARARGS (args_so_far, promoted_mode, passed_type,
3414                                   current_function_pretend_args_size,
3415                                   second_time);
3416           varargs_setup = 1;
3417         }
3418 #endif
3419
3420       /* Determine parm's home in the stack,
3421          in case it arrives in the stack or we should pretend it did.
3422
3423          Compute the stack position and rtx where the argument arrives
3424          and its size.
3425
3426          There is one complexity here:  If this was a parameter that would
3427          have been passed in registers, but wasn't only because it is
3428          __builtin_va_alist, we want locate_and_pad_parm to treat it as if
3429          it came in a register so that REG_PARM_STACK_SPACE isn't skipped.
3430          In this case, we call FUNCTION_ARG with NAMED set to 1 instead of
3431          0 as it was the previous time.  */
3432
3433       locate_and_pad_parm (promoted_mode, passed_type,
3434 #ifdef STACK_PARMS_IN_REG_PARM_AREA
3435                            1,
3436 #else
3437 #ifdef FUNCTION_INCOMING_ARG
3438                            FUNCTION_INCOMING_ARG (args_so_far, promoted_mode,
3439                                                   passed_type,
3440                                                   (! last_named
3441                                                    || varargs_setup)) != 0,
3442 #else
3443                            FUNCTION_ARG (args_so_far, promoted_mode,
3444                                          passed_type,
3445                                          ! last_named || varargs_setup) != 0,
3446 #endif
3447 #endif
3448                            fndecl, &stack_args_size, &stack_offset, &arg_size);
3449
3450       if (! second_time)
3451         {
3452           rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
3453
3454           if (offset_rtx == const0_rtx)
3455             stack_parm = gen_rtx (MEM, promoted_mode, internal_arg_pointer);
3456           else
3457             stack_parm = gen_rtx (MEM, promoted_mode,
3458                                   gen_rtx (PLUS, Pmode,
3459                                            internal_arg_pointer, offset_rtx));
3460
3461           /* If this is a memory ref that contains aggregate components,
3462              mark it as such for cse and loop optimize.  */
3463           MEM_IN_STRUCT_P (stack_parm) = aggregate;
3464         }
3465
3466       /* If this parameter was passed both in registers and in the stack,
3467          use the copy on the stack.  */
3468       if (MUST_PASS_IN_STACK (promoted_mode, passed_type))
3469         entry_parm = 0;
3470
3471 #ifdef FUNCTION_ARG_PARTIAL_NREGS
3472       /* If this parm was passed part in regs and part in memory,
3473          pretend it arrived entirely in memory
3474          by pushing the register-part onto the stack.
3475
3476          In the special case of a DImode or DFmode that is split,
3477          we could put it together in a pseudoreg directly,
3478          but for now that's not worth bothering with.  */
3479
3480       if (entry_parm)
3481         {
3482           int nregs = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, promoted_mode,
3483                                                   passed_type, ! last_named);
3484
3485           if (nregs > 0)
3486             {
3487               current_function_pretend_args_size
3488                 = (((nregs * UNITS_PER_WORD) + (PARM_BOUNDARY / BITS_PER_UNIT) - 1)
3489                    / (PARM_BOUNDARY / BITS_PER_UNIT)
3490                    * (PARM_BOUNDARY / BITS_PER_UNIT));
3491
3492               if (! second_time)
3493                 move_block_from_reg (REGNO (entry_parm),
3494                                      validize_mem (stack_parm), nregs,
3495                                      int_size_in_bytes (TREE_TYPE (parm)));
3496               entry_parm = stack_parm;
3497             }
3498         }
3499 #endif
3500
3501       /* If we didn't decide this parm came in a register,
3502          by default it came on the stack.  */
3503       if (entry_parm == 0)
3504         entry_parm = stack_parm;
3505
3506       /* Record permanently how this parm was passed.  */
3507       if (! second_time)
3508         DECL_INCOMING_RTL (parm) = entry_parm;
3509
3510       /* If there is actually space on the stack for this parm,
3511          count it in stack_args_size; otherwise set stack_parm to 0
3512          to indicate there is no preallocated stack slot for the parm.  */
3513
3514       if (entry_parm == stack_parm
3515 #if defined (REG_PARM_STACK_SPACE) && ! defined (MAYBE_REG_PARM_STACK_SPACE)
3516           /* On some machines, even if a parm value arrives in a register
3517              there is still an (uninitialized) stack slot allocated for it.
3518
3519              ??? When MAYBE_REG_PARM_STACK_SPACE is defined, we can't tell
3520              whether this parameter already has a stack slot allocated,
3521              because an arg block exists only if current_function_args_size
3522              is larger than some threshold, and we haven't calculated that
3523              yet.  So, for now, we just assume that stack slots never exist
3524              in this case.  */
3525           || REG_PARM_STACK_SPACE (fndecl) > 0
3526 #endif
3527           )
3528         {
3529           stack_args_size.constant += arg_size.constant;
3530           if (arg_size.var)
3531             ADD_PARM_SIZE (stack_args_size, arg_size.var);
3532         }
3533       else
3534         /* No stack slot was pushed for this parm.  */
3535         stack_parm = 0;
3536
3537       /* Update info on where next arg arrives in registers.  */
3538
3539       FUNCTION_ARG_ADVANCE (args_so_far, promoted_mode,
3540                             passed_type, ! last_named);
3541
3542       /* If this is our second time through, we are done with this parm. */
3543       if (second_time)
3544         continue;
3545
3546       /* If we can't trust the parm stack slot to be aligned enough
3547          for its ultimate type, don't use that slot after entry.
3548          We'll make another stack slot, if we need one.  */
3549       {
3550         int thisparm_boundary
3551           = FUNCTION_ARG_BOUNDARY (promoted_mode, passed_type);
3552
3553         if (GET_MODE_ALIGNMENT (nominal_mode) > thisparm_boundary)
3554           stack_parm = 0;
3555       }
3556
3557       /* If parm was passed in memory, and we need to convert it on entry,
3558          don't store it back in that same slot.  */
3559       if (entry_parm != 0
3560           && nominal_mode != BLKmode && nominal_mode != passed_mode)
3561         stack_parm = 0;
3562
3563 #if 0
3564       /* Now adjust STACK_PARM to the mode and precise location
3565          where this parameter should live during execution,
3566          if we discover that it must live in the stack during execution.
3567          To make debuggers happier on big-endian machines, we store
3568          the value in the last bytes of the space available.  */
3569
3570       if (nominal_mode != BLKmode && nominal_mode != passed_mode
3571           && stack_parm != 0)
3572         {
3573           rtx offset_rtx;
3574
3575           if (BYTES_BIG_ENDIAN
3576               && GET_MODE_SIZE (nominal_mode) < UNITS_PER_WORD)
3577             stack_offset.constant += (GET_MODE_SIZE (passed_mode)
3578                                       - GET_MODE_SIZE (nominal_mode));
3579
3580           offset_rtx = ARGS_SIZE_RTX (stack_offset);
3581           if (offset_rtx == const0_rtx)
3582             stack_parm = gen_rtx (MEM, nominal_mode, internal_arg_pointer);
3583           else
3584             stack_parm = gen_rtx (MEM, nominal_mode,
3585                                   gen_rtx (PLUS, Pmode,
3586                                            internal_arg_pointer, offset_rtx));
3587
3588           /* If this is a memory ref that contains aggregate components,
3589              mark it as such for cse and loop optimize.  */
3590           MEM_IN_STRUCT_P (stack_parm) = aggregate;
3591         }
3592 #endif /* 0 */
3593
3594 #ifdef STACK_REGS
3595       /* We need this "use" info, because the gcc-register->stack-register
3596          converter in reg-stack.c needs to know which registers are active
3597          at the start of the function call.  The actual parameter loading
3598          instructions are not always available then anymore, since they might
3599          have been optimised away.  */
3600
3601       if (GET_CODE (entry_parm) == REG && !(hide_last_arg && last_named))
3602           emit_insn (gen_rtx (USE, GET_MODE (entry_parm), entry_parm));
3603 #endif
3604
3605       /* ENTRY_PARM is an RTX for the parameter as it arrives,
3606          in the mode in which it arrives.
3607          STACK_PARM is an RTX for a stack slot where the parameter can live
3608          during the function (in case we want to put it there).
3609          STACK_PARM is 0 if no stack slot was pushed for it.
3610
3611          Now output code if necessary to convert ENTRY_PARM to
3612          the type in which this function declares it,
3613          and store that result in an appropriate place,
3614          which may be a pseudo reg, may be STACK_PARM,
3615          or may be a local stack slot if STACK_PARM is 0.
3616
3617          Set DECL_RTL to that place.  */
3618
3619       if (nominal_mode == BLKmode)
3620         {
3621           /* If a BLKmode arrives in registers, copy it to a stack slot.  */
3622           if (GET_CODE (entry_parm) == REG)
3623             {
3624               int size_stored
3625                 = CEIL_ROUND (int_size_in_bytes (TREE_TYPE (parm)),
3626                               UNITS_PER_WORD);
3627
3628               /* Note that we will be storing an integral number of words.
3629                  So we have to be careful to ensure that we allocate an
3630                  integral number of words.  We do this below in the
3631                  assign_stack_local if space was not allocated in the argument
3632                  list.  If it was, this will not work if PARM_BOUNDARY is not
3633                  a multiple of BITS_PER_WORD.  It isn't clear how to fix this
3634                  if it becomes a problem.  */
3635
3636               if (stack_parm == 0)
3637                 {
3638                   stack_parm
3639                     = assign_stack_local (GET_MODE (entry_parm),
3640                                           size_stored, 0);
3641
3642                   /* If this is a memory ref that contains aggregate
3643                      components, mark it as such for cse and loop optimize.  */
3644                   MEM_IN_STRUCT_P (stack_parm) = aggregate;
3645                 }
3646
3647               else if (PARM_BOUNDARY % BITS_PER_WORD != 0)
3648                 abort ();
3649
3650               if (TREE_READONLY (parm))
3651                 RTX_UNCHANGING_P (stack_parm) = 1;
3652
3653               move_block_from_reg (REGNO (entry_parm),
3654                                    validize_mem (stack_parm),
3655                                    size_stored / UNITS_PER_WORD,
3656                                    int_size_in_bytes (TREE_TYPE (parm)));
3657             }
3658           DECL_RTL (parm) = stack_parm;
3659         }
3660       else if (! ((obey_regdecls && ! DECL_REGISTER (parm)
3661                    && ! DECL_INLINE (fndecl))
3662                   /* layout_decl may set this.  */
3663                   || TREE_ADDRESSABLE (parm)
3664                   || TREE_SIDE_EFFECTS (parm)
3665                   /* If -ffloat-store specified, don't put explicit
3666                      float variables into registers.  */
3667                   || (flag_float_store
3668                       && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE))
3669                /* Always assign pseudo to structure return or item passed
3670                   by invisible reference.  */
3671                || passed_pointer || parm == function_result_decl)
3672         {
3673           /* Store the parm in a pseudoregister during the function, but we
3674              may need to do it in a wider mode.  */
3675
3676           register rtx parmreg;
3677           int regno, regnoi, regnor;
3678
3679           unsignedp = TREE_UNSIGNED (TREE_TYPE (parm));
3680
3681           promoted_nominal_mode
3682             = promote_mode (TREE_TYPE (parm), nominal_mode, &unsignedp, 0);
3683
3684           parmreg = gen_reg_rtx (promoted_nominal_mode);
3685           REG_USERVAR_P (parmreg) = 1;
3686
3687           /* If this was an item that we received a pointer to, set DECL_RTL
3688              appropriately.  */
3689           if (passed_pointer)
3690             {
3691               DECL_RTL (parm)
3692                 = gen_rtx (MEM, TYPE_MODE (TREE_TYPE (passed_type)), parmreg);
3693               MEM_IN_STRUCT_P (DECL_RTL (parm)) = aggregate;
3694             }
3695           else
3696             DECL_RTL (parm) = parmreg;
3697
3698           /* Copy the value into the register.  */
3699           if (nominal_mode != passed_mode
3700               || promoted_nominal_mode != promoted_mode)
3701             {
3702               /* ENTRY_PARM has been converted to PROMOTED_MODE, its
3703                  mode, by the caller.  We now have to convert it to 
3704                  NOMINAL_MODE, if different.  However, PARMREG may be in
3705                  a diffent mode than NOMINAL_MODE if it is being stored
3706                  promoted.
3707
3708                  If ENTRY_PARM is a hard register, it might be in a register
3709                  not valid for operating in its mode (e.g., an odd-numbered
3710                  register for a DFmode).  In that case, moves are the only
3711                  thing valid, so we can't do a convert from there.  This
3712                  occurs when the calling sequence allow such misaligned
3713                  usages.
3714
3715                  In addition, the conversion may involve a call, which could
3716                  clobber parameters which haven't been copied to pseudo
3717                  registers yet.  Therefore, we must first copy the parm to
3718                  a pseudo reg here, and save the conversion until after all
3719                  parameters have been moved.  */
3720
3721               rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
3722
3723               emit_move_insn (tempreg, validize_mem (entry_parm));
3724
3725               push_to_sequence (conversion_insns);
3726               tempreg = convert_to_mode (nominal_mode, tempreg, unsignedp);
3727
3728               expand_assignment (parm,
3729                                  make_tree (nominal_type, tempreg), 0, 0);
3730               conversion_insns = get_insns ();
3731               did_conversion = 1;
3732               end_sequence ();
3733             }
3734           else
3735             emit_move_insn (parmreg, validize_mem (entry_parm));
3736
3737           /* If we were passed a pointer but the actual value
3738              can safely live in a register, put it in one.  */
3739           if (passed_pointer && TYPE_MODE (TREE_TYPE (parm)) != BLKmode
3740               && ! ((obey_regdecls && ! DECL_REGISTER (parm)
3741                      && ! DECL_INLINE (fndecl))
3742                     /* layout_decl may set this.  */
3743                     || TREE_ADDRESSABLE (parm)
3744                     || TREE_SIDE_EFFECTS (parm)
3745                     /* If -ffloat-store specified, don't put explicit
3746                        float variables into registers.  */
3747                     || (flag_float_store
3748                         && TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE)))
3749             {
3750               /* We can't use nominal_mode, because it will have been set to
3751                  Pmode above.  We must use the actual mode of the parm.  */
3752               parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm)));
3753               REG_USERVAR_P (parmreg) = 1;
3754               emit_move_insn (parmreg, DECL_RTL (parm));
3755               DECL_RTL (parm) = parmreg;
3756               /* STACK_PARM is the pointer, not the parm, and PARMREG is
3757                  now the parm.  */
3758               stack_parm = 0;
3759             }
3760 #ifdef FUNCTION_ARG_CALLEE_COPIES
3761           /* If we are passed an arg by reference and it is our responsibility
3762              to make a copy, do it now.
3763              PASSED_TYPE and PASSED mode now refer to the pointer, not the
3764              original argument, so we must recreate them in the call to
3765              FUNCTION_ARG_CALLEE_COPIES.  */
3766           /* ??? Later add code to handle the case that if the argument isn't
3767              modified, don't do the copy.  */
3768
3769           else if (passed_pointer
3770                    && FUNCTION_ARG_CALLEE_COPIES (args_so_far,
3771                                                   TYPE_MODE (DECL_ARG_TYPE (parm)),
3772                                                   DECL_ARG_TYPE (parm),
3773                                                   ! last_named)
3774                    && ! TREE_ADDRESSABLE (DECL_ARG_TYPE (parm)))
3775             {
3776               rtx copy;
3777               tree type = DECL_ARG_TYPE (parm);
3778
3779               /* This sequence may involve a library call perhaps clobbering
3780                  registers that haven't been copied to pseudos yet.  */
3781
3782               push_to_sequence (conversion_insns);
3783
3784               if (TYPE_SIZE (type) == 0
3785                   || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
3786                 /* This is a variable sized object.  */
3787                 copy = gen_rtx (MEM, BLKmode,
3788                                 allocate_dynamic_stack_space
3789                                 (expr_size (parm), NULL_RTX,
3790                                  TYPE_ALIGN (type)));
3791               else
3792                 copy = assign_stack_temp (TYPE_MODE (type),
3793                                           int_size_in_bytes (type), 1);
3794               MEM_IN_STRUCT_P (copy) = AGGREGATE_TYPE_P (type);
3795
3796               store_expr (parm, copy, 0);
3797               emit_move_insn (parmreg, XEXP (copy, 0));
3798               conversion_insns = get_insns ();
3799               did_conversion = 1;
3800               end_sequence ();
3801             }
3802 #endif /* FUNCTION_ARG_CALLEE_COPIES */
3803
3804           /* In any case, record the parm's desired stack location
3805              in case we later discover it must live in the stack. 
3806
3807              If it is a COMPLEX value, store the stack location for both
3808              halves.  */
3809
3810           if (GET_CODE (parmreg) == CONCAT)
3811             regno = MAX (REGNO (XEXP (parmreg, 0)), REGNO (XEXP (parmreg, 1)));
3812           else
3813             regno = REGNO (parmreg);
3814
3815           if (regno >= nparmregs)
3816             {
3817               rtx *new;
3818               int old_nparmregs = nparmregs;
3819
3820               nparmregs = regno + 5;
3821               new = (rtx *) oballoc (nparmregs * sizeof (rtx));
3822               bcopy ((char *) parm_reg_stack_loc, (char *) new,
3823                      old_nparmregs * sizeof (rtx));
3824               bzero ((char *) (new + old_nparmregs),
3825                      (nparmregs - old_nparmregs) * sizeof (rtx));
3826               parm_reg_stack_loc = new;
3827             }
3828
3829           if (GET_CODE (parmreg) == CONCAT)
3830             {
3831               enum machine_mode submode = GET_MODE (XEXP (parmreg, 0));
3832
3833               regnor = REGNO (gen_realpart (submode, parmreg));
3834               regnoi = REGNO (gen_imagpart (submode, parmreg));
3835
3836               if (stack_parm != 0)
3837                 {
3838                   parm_reg_stack_loc[regnor]
3839                     = gen_realpart (submode, stack_parm);
3840                   parm_reg_stack_loc[regnoi]
3841                     = gen_imagpart (submode, stack_parm);
3842                 }
3843               else
3844                 {
3845                   parm_reg_stack_loc[regnor] = 0;
3846                   parm_reg_stack_loc[regnoi] = 0;
3847                 }
3848             }
3849           else
3850             parm_reg_stack_loc[REGNO (parmreg)] = stack_parm;
3851
3852           /* Mark the register as eliminable if we did no conversion
3853              and it was copied from memory at a fixed offset,
3854              and the arg pointer was not copied to a pseudo-reg.
3855              If the arg pointer is a pseudo reg or the offset formed
3856              an invalid address, such memory-equivalences
3857              as we make here would screw up life analysis for it.  */
3858           if (nominal_mode == passed_mode
3859               && ! did_conversion
3860               && GET_CODE (entry_parm) == MEM
3861               && entry_parm == stack_parm
3862               && stack_offset.var == 0
3863               && reg_mentioned_p (virtual_incoming_args_rtx,
3864                                   XEXP (entry_parm, 0)))
3865             {
3866               rtx linsn = get_last_insn ();
3867
3868               /* Mark complex types separately.  */
3869               if (GET_CODE (parmreg) == CONCAT)
3870                 {
3871                   REG_NOTES (linsn)
3872                       = gen_rtx (EXPR_LIST, REG_EQUIV,
3873                                  parm_reg_stack_loc[regnoi], REG_NOTES (linsn));
3874
3875                   /* Now search backward for where we set the real part.  */
3876                   for (; linsn != 0
3877                        && ! reg_referenced_p (parm_reg_stack_loc[regnor],
3878                                               PATTERN (linsn));
3879                        linsn = prev_nonnote_insn (linsn))
3880                     ;
3881
3882                   REG_NOTES (linsn)
3883                       = gen_rtx (EXPR_LIST, REG_EQUIV,
3884                                  parm_reg_stack_loc[regnor], REG_NOTES (linsn));
3885                 }
3886               else
3887                 REG_NOTES (linsn)
3888                  = gen_rtx (EXPR_LIST, REG_EQUIV,
3889                             entry_parm, REG_NOTES (linsn));
3890             }
3891
3892           /* For pointer data type, suggest pointer register.  */
3893           if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
3894             mark_reg_pointer (parmreg,
3895                               (TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm)))
3896                                / BITS_PER_UNIT));
3897         }
3898       else
3899         {
3900           /* Value must be stored in the stack slot STACK_PARM
3901              during function execution.  */
3902
3903           if (promoted_mode != nominal_mode)
3904             {
3905               /* Conversion is required.   */
3906               rtx tempreg = gen_reg_rtx (GET_MODE (entry_parm));
3907
3908               emit_move_insn (tempreg, validize_mem (entry_parm));
3909
3910               push_to_sequence (conversion_insns);
3911               entry_parm = convert_to_mode (nominal_mode, tempreg,
3912                                             TREE_UNSIGNED (TREE_TYPE (parm)));
3913               conversion_insns = get_insns ();
3914               did_conversion = 1;
3915               end_sequence ();
3916             }
3917
3918           if (entry_parm != stack_parm)
3919             {
3920               if (stack_parm == 0)
3921                 {
3922                   stack_parm
3923                     = assign_stack_local (GET_MODE (entry_parm),
3924                                           GET_MODE_SIZE (GET_MODE (entry_parm)), 0);
3925                   /* If this is a memory ref that contains aggregate components,
3926                      mark it as such for cse and loop optimize.  */
3927                   MEM_IN_STRUCT_P (stack_parm) = aggregate;
3928                 }
3929
3930               if (promoted_mode != nominal_mode)
3931                 {
3932                   push_to_sequence (conversion_insns);
3933                   emit_move_insn (validize_mem (stack_parm),
3934                                   validize_mem (entry_parm));
3935                   conversion_insns = get_insns ();
3936                   end_sequence ();
3937                 }
3938               else
3939                 emit_move_insn (validize_mem (stack_parm),
3940                                 validize_mem (entry_parm));
3941             }
3942
3943           DECL_RTL (parm) = stack_parm;
3944         }
3945       
3946       /* If this "parameter" was the place where we are receiving the
3947          function's incoming structure pointer, set up the result.  */
3948       if (parm == function_result_decl)
3949         {
3950           tree result = DECL_RESULT (fndecl);
3951           tree restype = TREE_TYPE (result);
3952
3953           DECL_RTL (result)
3954             = gen_rtx (MEM, DECL_MODE (result), DECL_RTL (parm));
3955
3956           MEM_IN_STRUCT_P (DECL_RTL (result)) = AGGREGATE_TYPE_P (restype);
3957         }
3958
3959       if (TREE_THIS_VOLATILE (parm))
3960         MEM_VOLATILE_P (DECL_RTL (parm)) = 1;
3961       if (TREE_READONLY (parm))
3962         RTX_UNCHANGING_P (DECL_RTL (parm)) = 1;
3963     }
3964
3965   /* Output all parameter conversion instructions (possibly including calls)
3966      now that all parameters have been copied out of hard registers.  */
3967   emit_insns (conversion_insns);
3968
3969   max_parm_reg = max_reg_num ();
3970   last_parm_insn = get_last_insn ();
3971
3972   current_function_args_size = stack_args_size.constant;
3973
3974   /* Adjust function incoming argument size for alignment and
3975      minimum length.  */
3976
3977 #ifdef REG_PARM_STACK_SPACE
3978 #ifndef MAYBE_REG_PARM_STACK_SPACE
3979   current_function_args_size = MAX (current_function_args_size,
3980                                     REG_PARM_STACK_SPACE (fndecl));
3981 #endif
3982 #endif
3983
3984 #ifdef STACK_BOUNDARY
3985 #define STACK_BYTES (STACK_BOUNDARY / BITS_PER_UNIT)
3986
3987   current_function_args_size
3988     = ((current_function_args_size + STACK_BYTES - 1)
3989        / STACK_BYTES) * STACK_BYTES;
3990 #endif  
3991
3992 #ifdef ARGS_GROW_DOWNWARD
3993   current_function_arg_offset_rtx
3994     = (stack_args_size.var == 0 ? GEN_INT (-stack_args_size.constant)
3995        : expand_expr (size_binop (MINUS_EXPR, stack_args_size.var,      
3996                                   size_int (-stack_args_size.constant)),   
3997                       NULL_RTX, VOIDmode, 0));
3998 #else
3999   current_function_arg_offset_rtx = ARGS_SIZE_RTX (stack_args_size);
4000 #endif
4001
4002   /* See how many bytes, if any, of its args a function should try to pop
4003      on return.  */
4004
4005   current_function_pops_args = RETURN_POPS_ARGS (fndecl, TREE_TYPE (fndecl),
4006                                                  current_function_args_size);
4007
4008   /* For stdarg.h function, save info about
4009      regs and stack space used by the named args.  */
4010
4011   if (!hide_last_arg)
4012     current_function_args_info = args_so_far;
4013
4014   /* Set the rtx used for the function return value.  Put this in its
4015      own variable so any optimizers that need this information don't have
4016      to include tree.h.  Do this here so it gets done when an inlined
4017      function gets output.  */
4018
4019   current_function_return_rtx = DECL_RTL (DECL_RESULT (fndecl));
4020 }
4021 \f
4022 /* Indicate whether REGNO is an incoming argument to the current function
4023    that was promoted to a wider mode.  If so, return the RTX for the
4024    register (to get its mode).  PMODE and PUNSIGNEDP are set to the mode
4025    that REGNO is promoted from and whether the promotion was signed or
4026    unsigned.  */
4027
4028 #ifdef PROMOTE_FUNCTION_ARGS
4029
4030 rtx
4031 promoted_input_arg (regno, pmode, punsignedp)
4032      int regno;
4033      enum machine_mode *pmode;
4034      int *punsignedp;
4035 {
4036   tree arg;
4037
4038   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
4039        arg = TREE_CHAIN (arg))
4040     if (GET_CODE (DECL_INCOMING_RTL (arg)) == REG
4041         && REGNO (DECL_INCOMING_RTL (arg)) == regno
4042         && TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
4043       {
4044         enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
4045         int unsignedp = TREE_UNSIGNED (TREE_TYPE (arg));
4046
4047         mode = promote_mode (TREE_TYPE (arg), mode, &unsignedp, 1);
4048         if (mode == GET_MODE (DECL_INCOMING_RTL (arg))
4049             && mode != DECL_MODE (arg))
4050           {
4051             *pmode = DECL_MODE (arg);
4052             *punsignedp = unsignedp;
4053             return DECL_INCOMING_RTL (arg);
4054           }
4055       }
4056
4057   return 0;
4058 }
4059
4060 #endif
4061 \f
4062 /* Compute the size and offset from the start of the stacked arguments for a
4063    parm passed in mode PASSED_MODE and with type TYPE.
4064
4065    INITIAL_OFFSET_PTR points to the current offset into the stacked
4066    arguments.
4067
4068    The starting offset and size for this parm are returned in *OFFSET_PTR
4069    and *ARG_SIZE_PTR, respectively.
4070
4071    IN_REGS is non-zero if the argument will be passed in registers.  It will
4072    never be set if REG_PARM_STACK_SPACE is not defined.
4073
4074    FNDECL is the function in which the argument was defined.
4075
4076    There are two types of rounding that are done.  The first, controlled by
4077    FUNCTION_ARG_BOUNDARY, forces the offset from the start of the argument
4078    list to be aligned to the specific boundary (in bits).  This rounding
4079    affects the initial and starting offsets, but not the argument size.
4080
4081    The second, controlled by FUNCTION_ARG_PADDING and PARM_BOUNDARY,
4082    optionally rounds the size of the parm to PARM_BOUNDARY.  The
4083    initial offset is not affected by this rounding, while the size always
4084    is and the starting offset may be.  */
4085
4086 /*  offset_ptr will be negative for ARGS_GROW_DOWNWARD case; 
4087     initial_offset_ptr is positive because locate_and_pad_parm's
4088     callers pass in the total size of args so far as
4089     initial_offset_ptr. arg_size_ptr is always positive.*/
4090
4091 void
4092 locate_and_pad_parm (passed_mode, type, in_regs, fndecl,
4093                      initial_offset_ptr, offset_ptr, arg_size_ptr)
4094      enum machine_mode passed_mode;
4095      tree type;
4096      int in_regs;
4097      tree fndecl;
4098      struct args_size *initial_offset_ptr;
4099      struct args_size *offset_ptr;
4100      struct args_size *arg_size_ptr;
4101 {
4102   tree sizetree
4103     = type ? size_in_bytes (type) : size_int (GET_MODE_SIZE (passed_mode));
4104   enum direction where_pad = FUNCTION_ARG_PADDING (passed_mode, type);
4105   int boundary = FUNCTION_ARG_BOUNDARY (passed_mode, type);
4106   int boundary_in_bytes = boundary / BITS_PER_UNIT;
4107   int reg_parm_stack_space = 0;
4108
4109 #ifdef REG_PARM_STACK_SPACE
4110   /* If we have found a stack parm before we reach the end of the
4111      area reserved for registers, skip that area.  */
4112   if (! in_regs)
4113     {
4114 #ifdef MAYBE_REG_PARM_STACK_SPACE
4115       reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
4116 #else
4117       reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
4118 #endif
4119       if (reg_parm_stack_space > 0)
4120         {
4121           if (initial_offset_ptr->var)
4122             {
4123               initial_offset_ptr->var
4124                 = size_binop (MAX_EXPR, ARGS_SIZE_TREE (*initial_offset_ptr),
4125                               size_int (reg_parm_stack_space));
4126               initial_offset_ptr->constant = 0;
4127             }
4128           else if (initial_offset_ptr->constant < reg_parm_stack_space)
4129             initial_offset_ptr->constant = reg_parm_stack_space;
4130         }
4131     }
4132 #endif /* REG_PARM_STACK_SPACE */
4133
4134   arg_size_ptr->var = 0;
4135   arg_size_ptr->constant = 0;
4136
4137 #ifdef ARGS_GROW_DOWNWARD
4138   if (initial_offset_ptr->var)
4139     {
4140       offset_ptr->constant = 0;
4141       offset_ptr->var = size_binop (MINUS_EXPR, integer_zero_node,
4142                                     initial_offset_ptr->var);
4143     }
4144   else
4145     {
4146       offset_ptr->constant = - initial_offset_ptr->constant;
4147       offset_ptr->var = 0;
4148     }
4149   if (where_pad != none
4150       && (TREE_CODE (sizetree) != INTEGER_CST
4151           || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4152     sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4153   SUB_PARM_SIZE (*offset_ptr, sizetree);
4154   if (where_pad != downward)
4155     pad_to_arg_alignment (offset_ptr, boundary);
4156   if (initial_offset_ptr->var)
4157     {
4158       arg_size_ptr->var = size_binop (MINUS_EXPR,
4159                                       size_binop (MINUS_EXPR,
4160                                                   integer_zero_node,
4161                                                   initial_offset_ptr->var),
4162                                       offset_ptr->var);
4163     }
4164   else
4165     {
4166       arg_size_ptr->constant = (- initial_offset_ptr->constant -
4167                                 offset_ptr->constant); 
4168     }
4169 #else /* !ARGS_GROW_DOWNWARD */
4170   pad_to_arg_alignment (initial_offset_ptr, boundary);
4171   *offset_ptr = *initial_offset_ptr;
4172
4173 #ifdef PUSH_ROUNDING
4174   if (passed_mode != BLKmode)
4175     sizetree = size_int (PUSH_ROUNDING (TREE_INT_CST_LOW (sizetree)));
4176 #endif
4177
4178   /* Pad_below needs the pre-rounded size to know how much to pad below
4179      so this must be done before rounding up.  */
4180   if (where_pad == downward
4181     /* However, BLKmode args passed in regs have their padding done elsewhere.
4182        The stack slot must be able to hold the entire register.  */
4183       && !(in_regs && passed_mode == BLKmode))
4184     pad_below (offset_ptr, passed_mode, sizetree);
4185
4186   if (where_pad != none
4187       && (TREE_CODE (sizetree) != INTEGER_CST
4188           || ((TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)))
4189     sizetree = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4190
4191   ADD_PARM_SIZE (*arg_size_ptr, sizetree);
4192 #endif /* ARGS_GROW_DOWNWARD */
4193 }
4194
4195 /* Round the stack offset in *OFFSET_PTR up to a multiple of BOUNDARY.
4196    BOUNDARY is measured in bits, but must be a multiple of a storage unit.  */
4197
4198 static void
4199 pad_to_arg_alignment (offset_ptr, boundary)
4200      struct args_size *offset_ptr;
4201      int boundary;
4202 {
4203   int boundary_in_bytes = boundary / BITS_PER_UNIT;
4204   
4205   if (boundary > BITS_PER_UNIT)
4206     {
4207       if (offset_ptr->var)
4208         {
4209           offset_ptr->var  =
4210 #ifdef ARGS_GROW_DOWNWARD
4211             round_down 
4212 #else
4213             round_up
4214 #endif
4215               (ARGS_SIZE_TREE (*offset_ptr),
4216                boundary / BITS_PER_UNIT);
4217           offset_ptr->constant = 0; /*?*/
4218         }
4219       else
4220         offset_ptr->constant =
4221 #ifdef ARGS_GROW_DOWNWARD
4222           FLOOR_ROUND (offset_ptr->constant, boundary_in_bytes);
4223 #else
4224           CEIL_ROUND (offset_ptr->constant, boundary_in_bytes);
4225 #endif
4226     }
4227 }
4228
4229 static void
4230 pad_below (offset_ptr, passed_mode, sizetree)
4231      struct args_size *offset_ptr;
4232      enum machine_mode passed_mode;
4233      tree sizetree;
4234 {
4235   if (passed_mode != BLKmode)
4236     {
4237       if (GET_MODE_BITSIZE (passed_mode) % PARM_BOUNDARY)
4238         offset_ptr->constant
4239           += (((GET_MODE_BITSIZE (passed_mode) + PARM_BOUNDARY - 1)
4240                / PARM_BOUNDARY * PARM_BOUNDARY / BITS_PER_UNIT)
4241               - GET_MODE_SIZE (passed_mode));
4242     }
4243   else
4244     {
4245       if (TREE_CODE (sizetree) != INTEGER_CST
4246           || (TREE_INT_CST_LOW (sizetree) * BITS_PER_UNIT) % PARM_BOUNDARY)
4247         {
4248           /* Round the size up to multiple of PARM_BOUNDARY bits.  */
4249           tree s2 = round_up (sizetree, PARM_BOUNDARY / BITS_PER_UNIT);
4250           /* Add it in.  */
4251           ADD_PARM_SIZE (*offset_ptr, s2);
4252           SUB_PARM_SIZE (*offset_ptr, sizetree);
4253         }
4254     }
4255 }
4256
4257 static tree
4258 round_down (value, divisor)
4259      tree value;
4260      int divisor;
4261 {
4262   return size_binop (MULT_EXPR,
4263                      size_binop (FLOOR_DIV_EXPR, value, size_int (divisor)),
4264                      size_int (divisor));
4265 }
4266 \f
4267 /* Walk the tree of blocks describing the binding levels within a function
4268    and warn about uninitialized variables.
4269    This is done after calling flow_analysis and before global_alloc
4270    clobbers the pseudo-regs to hard regs.  */
4271
4272 void
4273 uninitialized_vars_warning (block)
4274      tree block;
4275 {
4276   register tree decl, sub;
4277   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4278     {
4279       if (TREE_CODE (decl) == VAR_DECL
4280           /* These warnings are unreliable for and aggregates
4281              because assigning the fields one by one can fail to convince
4282              flow.c that the entire aggregate was initialized.
4283              Unions are troublesome because members may be shorter.  */
4284           && ! AGGREGATE_TYPE_P (TREE_TYPE (decl))
4285           && DECL_RTL (decl) != 0
4286           && GET_CODE (DECL_RTL (decl)) == REG
4287           && regno_uninitialized (REGNO (DECL_RTL (decl))))
4288         warning_with_decl (decl,
4289                            "`%s' might be used uninitialized in this function");
4290       if (TREE_CODE (decl) == VAR_DECL
4291           && DECL_RTL (decl) != 0
4292           && GET_CODE (DECL_RTL (decl)) == REG
4293           && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4294         warning_with_decl (decl,
4295                            "variable `%s' might be clobbered by `longjmp' or `vfork'");
4296     }
4297   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4298     uninitialized_vars_warning (sub);
4299 }
4300
4301 /* Do the appropriate part of uninitialized_vars_warning
4302    but for arguments instead of local variables.  */
4303
4304 void
4305 setjmp_args_warning ()
4306 {
4307   register tree decl;
4308   for (decl = DECL_ARGUMENTS (current_function_decl);
4309        decl; decl = TREE_CHAIN (decl))
4310     if (DECL_RTL (decl) != 0
4311         && GET_CODE (DECL_RTL (decl)) == REG
4312         && regno_clobbered_at_setjmp (REGNO (DECL_RTL (decl))))
4313       warning_with_decl (decl, "argument `%s' might be clobbered by `longjmp' or `vfork'");
4314 }
4315
4316 /* If this function call setjmp, put all vars into the stack
4317    unless they were declared `register'.  */
4318
4319 void
4320 setjmp_protect (block)
4321      tree block;
4322 {
4323   register tree decl, sub;
4324   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
4325     if ((TREE_CODE (decl) == VAR_DECL
4326          || TREE_CODE (decl) == PARM_DECL)
4327         && DECL_RTL (decl) != 0
4328         && GET_CODE (DECL_RTL (decl)) == REG
4329         /* If this variable came from an inline function, it must be
4330            that it's life doesn't overlap the setjmp.  If there was a
4331            setjmp in the function, it would already be in memory.  We
4332            must exclude such variable because their DECL_RTL might be
4333            set to strange things such as virtual_stack_vars_rtx.  */
4334         && ! DECL_FROM_INLINE (decl)
4335         && (
4336 #ifdef NON_SAVING_SETJMP
4337             /* If longjmp doesn't restore the registers,
4338                don't put anything in them.  */
4339             NON_SAVING_SETJMP
4340             ||
4341 #endif
4342             ! DECL_REGISTER (decl)))
4343       put_var_into_stack (decl);
4344   for (sub = BLOCK_SUBBLOCKS (block); sub; sub = TREE_CHAIN (sub))
4345     setjmp_protect (sub);
4346 }
4347 \f
4348 /* Like the previous function, but for args instead of local variables.  */
4349
4350 void
4351 setjmp_protect_args ()
4352 {
4353   register tree decl, sub;
4354   for (decl = DECL_ARGUMENTS (current_function_decl);
4355        decl; decl = TREE_CHAIN (decl))
4356     if ((TREE_CODE (decl) == VAR_DECL
4357          || TREE_CODE (decl) == PARM_DECL)
4358         && DECL_RTL (decl) != 0
4359         && GET_CODE (DECL_RTL (decl)) == REG
4360         && (
4361             /* If longjmp doesn't restore the registers,
4362                don't put anything in them.  */
4363 #ifdef NON_SAVING_SETJMP
4364             NON_SAVING_SETJMP
4365             ||
4366 #endif
4367             ! DECL_REGISTER (decl)))
4368       put_var_into_stack (decl);
4369 }
4370 \f
4371 /* Return the context-pointer register corresponding to DECL,
4372    or 0 if it does not need one.  */
4373
4374 rtx
4375 lookup_static_chain (decl)
4376      tree decl;
4377 {
4378   tree context = decl_function_context (decl);
4379   tree link;
4380
4381   if (context == 0)
4382     return 0;
4383   
4384   /* We treat inline_function_decl as an alias for the current function
4385      because that is the inline function whose vars, types, etc.
4386      are being merged into the current function.
4387      See expand_inline_function.  */
4388   if (context == current_function_decl || context == inline_function_decl)
4389     return virtual_stack_vars_rtx;
4390
4391   for (link = context_display; link; link = TREE_CHAIN (link))
4392     if (TREE_PURPOSE (link) == context)
4393       return RTL_EXPR_RTL (TREE_VALUE (link));
4394
4395   abort ();
4396 }
4397 \f
4398 /* Convert a stack slot address ADDR for variable VAR
4399    (from a containing function)
4400    into an address valid in this function (using a static chain).  */
4401
4402 rtx
4403 fix_lexical_addr (addr, var)
4404      rtx addr;
4405      tree var;
4406 {
4407   rtx basereg;
4408   int displacement;
4409   tree context = decl_function_context (var);
4410   struct function *fp;
4411   rtx base = 0;
4412
4413   /* If this is the present function, we need not do anything.  */
4414   if (context == current_function_decl || context == inline_function_decl)
4415     return addr;
4416
4417   for (fp = outer_function_chain; fp; fp = fp->next)
4418     if (fp->decl == context)
4419       break;
4420
4421   if (fp == 0)
4422     abort ();
4423
4424   /* Decode given address as base reg plus displacement.  */
4425   if (GET_CODE (addr) == REG)
4426     basereg = addr, displacement = 0;
4427   else if (GET_CODE (addr) == PLUS && GET_CODE (XEXP (addr, 1)) == CONST_INT)
4428     basereg = XEXP (addr, 0), displacement = INTVAL (XEXP (addr, 1));
4429   else
4430     abort ();
4431
4432   /* We accept vars reached via the containing function's
4433      incoming arg pointer and via its stack variables pointer.  */
4434   if (basereg == fp->internal_arg_pointer)
4435     {
4436       /* If reached via arg pointer, get the arg pointer value
4437          out of that function's stack frame.
4438
4439          There are two cases:  If a separate ap is needed, allocate a
4440          slot in the outer function for it and dereference it that way.
4441          This is correct even if the real ap is actually a pseudo.
4442          Otherwise, just adjust the offset from the frame pointer to
4443          compensate.  */
4444
4445 #ifdef NEED_SEPARATE_AP
4446       rtx addr;
4447
4448       if (fp->arg_pointer_save_area == 0)
4449         fp->arg_pointer_save_area
4450           = assign_outer_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0, fp);
4451
4452       addr = fix_lexical_addr (XEXP (fp->arg_pointer_save_area, 0), var);
4453       addr = memory_address (Pmode, addr);
4454
4455       base = copy_to_reg (gen_rtx (MEM, Pmode, addr));
4456 #else
4457       displacement += (FIRST_PARM_OFFSET (context) - STARTING_FRAME_OFFSET);
4458       base = lookup_static_chain (var);
4459 #endif
4460     }
4461
4462   else if (basereg == virtual_stack_vars_rtx)
4463     {
4464       /* This is the same code as lookup_static_chain, duplicated here to
4465          avoid an extra call to decl_function_context.  */
4466       tree link;
4467
4468       for (link = context_display; link; link = TREE_CHAIN (link))
4469         if (TREE_PURPOSE (link) == context)
4470           {
4471             base = RTL_EXPR_RTL (TREE_VALUE (link));
4472             break;
4473           }
4474     }
4475
4476   if (base == 0)
4477     abort ();
4478
4479   /* Use same offset, relative to appropriate static chain or argument
4480      pointer.  */
4481   return plus_constant (base, displacement);
4482 }
4483 \f
4484 /* Return the address of the trampoline for entering nested fn FUNCTION.
4485    If necessary, allocate a trampoline (in the stack frame)
4486    and emit rtl to initialize its contents (at entry to this function).  */
4487
4488 rtx
4489 trampoline_address (function)
4490      tree function;
4491 {
4492   tree link;
4493   tree rtlexp;
4494   rtx tramp;
4495   struct function *fp;
4496   tree fn_context;
4497
4498   /* Find an existing trampoline and return it.  */
4499   for (link = trampoline_list; link; link = TREE_CHAIN (link))
4500     if (TREE_PURPOSE (link) == function)
4501       return
4502         round_trampoline_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0));
4503
4504   for (fp = outer_function_chain; fp; fp = fp->next)
4505     for (link = fp->trampoline_list; link; link = TREE_CHAIN (link))
4506       if (TREE_PURPOSE (link) == function)
4507         {
4508           tramp = fix_lexical_addr (XEXP (RTL_EXPR_RTL (TREE_VALUE (link)), 0),
4509                                     function);
4510           return round_trampoline_addr (tramp);
4511         }
4512
4513   /* None exists; we must make one.  */
4514
4515   /* Find the `struct function' for the function containing FUNCTION.  */
4516   fp = 0;
4517   fn_context = decl_function_context (function);
4518   if (fn_context != current_function_decl)
4519     for (fp = outer_function_chain; fp; fp = fp->next)
4520       if (fp->decl == fn_context)
4521         break;
4522
4523   /* Allocate run-time space for this trampoline
4524      (usually in the defining function's stack frame).  */
4525 #ifdef ALLOCATE_TRAMPOLINE
4526   tramp = ALLOCATE_TRAMPOLINE (fp);
4527 #else
4528   /* If rounding needed, allocate extra space
4529      to ensure we have TRAMPOLINE_SIZE bytes left after rounding up.  */
4530 #ifdef TRAMPOLINE_ALIGNMENT
4531 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE + TRAMPOLINE_ALIGNMENT - 1)
4532 #else
4533 #define TRAMPOLINE_REAL_SIZE (TRAMPOLINE_SIZE)
4534 #endif
4535   if (fp != 0)
4536     tramp = assign_outer_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0, fp);
4537   else
4538     tramp = assign_stack_local (BLKmode, TRAMPOLINE_REAL_SIZE, 0);
4539 #endif
4540
4541   /* Record the trampoline for reuse and note it for later initialization
4542      by expand_function_end.  */
4543   if (fp != 0)
4544     {
4545       push_obstacks (fp->function_maybepermanent_obstack,
4546                      fp->function_maybepermanent_obstack);
4547       rtlexp = make_node (RTL_EXPR);
4548       RTL_EXPR_RTL (rtlexp) = tramp;
4549       fp->trampoline_list = tree_cons (function, rtlexp, fp->trampoline_list);
4550       pop_obstacks ();
4551     }
4552   else
4553     {
4554       /* Make the RTL_EXPR node temporary, not momentary, so that the
4555          trampoline_list doesn't become garbage.  */
4556       int momentary = suspend_momentary ();
4557       rtlexp = make_node (RTL_EXPR);
4558       resume_momentary (momentary);
4559
4560       RTL_EXPR_RTL (rtlexp) = tramp;
4561       trampoline_list = tree_cons (function, rtlexp, trampoline_list);
4562     }
4563
4564   tramp = fix_lexical_addr (XEXP (tramp, 0), function);
4565   return round_trampoline_addr (tramp);
4566 }
4567
4568 /* Given a trampoline address,
4569    round it to multiple of TRAMPOLINE_ALIGNMENT.  */
4570
4571 static rtx
4572 round_trampoline_addr (tramp)
4573      rtx tramp;
4574 {
4575 #ifdef TRAMPOLINE_ALIGNMENT
4576   /* Round address up to desired boundary.  */
4577   rtx temp = gen_reg_rtx (Pmode);
4578   temp = expand_binop (Pmode, add_optab, tramp,
4579                        GEN_INT (TRAMPOLINE_ALIGNMENT - 1),
4580                        temp, 0, OPTAB_LIB_WIDEN);
4581   tramp = expand_binop (Pmode, and_optab, temp,
4582                         GEN_INT (- TRAMPOLINE_ALIGNMENT),
4583                         temp, 0, OPTAB_LIB_WIDEN);
4584 #endif
4585   return tramp;
4586 }
4587 \f
4588 /* The functions identify_blocks and reorder_blocks provide a way to
4589    reorder the tree of BLOCK nodes, for optimizers that reshuffle or
4590    duplicate portions of the RTL code.  Call identify_blocks before
4591    changing the RTL, and call reorder_blocks after.  */
4592
4593 /* Put all this function's BLOCK nodes including those that are chained
4594    onto the first block into a vector, and return it.
4595    Also store in each NOTE for the beginning or end of a block
4596    the index of that block in the vector.
4597    The arguments are BLOCK, the chain of top-level blocks of the function,
4598    and INSNS, the insn chain of the function.  */
4599
4600 tree *
4601 identify_blocks (block, insns)
4602      tree block;
4603      rtx insns;
4604 {
4605   int n_blocks;
4606   tree *block_vector;
4607   int *block_stack;
4608   int depth = 0;
4609   int next_block_number = 1;
4610   int current_block_number = 1;
4611   rtx insn;
4612
4613   if (block == 0)
4614     return 0;
4615
4616   n_blocks = all_blocks (block, 0);
4617   block_vector = (tree *) xmalloc (n_blocks * sizeof (tree));
4618   block_stack = (int *) alloca (n_blocks * sizeof (int));
4619
4620   all_blocks (block, block_vector);
4621
4622   for (insn = insns; insn; insn = NEXT_INSN (insn))
4623     if (GET_CODE (insn) == NOTE)
4624       {
4625         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4626           {
4627             block_stack[depth++] = current_block_number;
4628             current_block_number = next_block_number;
4629             NOTE_BLOCK_NUMBER (insn) =  next_block_number++;
4630           }
4631         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4632           {
4633             current_block_number = block_stack[--depth];
4634             NOTE_BLOCK_NUMBER (insn) = current_block_number;
4635           }
4636       }
4637
4638   if (n_blocks != next_block_number)
4639     abort ();
4640
4641   return block_vector;
4642 }
4643
4644 /* Given BLOCK_VECTOR which was returned by identify_blocks,
4645    and a revised instruction chain, rebuild the tree structure
4646    of BLOCK nodes to correspond to the new order of RTL.
4647    The new block tree is inserted below TOP_BLOCK.
4648    Returns the current top-level block.  */
4649
4650 tree
4651 reorder_blocks (block_vector, block, insns)
4652      tree *block_vector;
4653      tree block;
4654      rtx insns;
4655 {
4656   tree current_block = block;
4657   rtx insn;
4658
4659   if (block_vector == 0)
4660     return block;
4661
4662   /* Prune the old trees away, so that it doesn't get in the way.  */
4663   BLOCK_SUBBLOCKS (current_block) = 0;
4664   BLOCK_CHAIN (current_block) = 0;
4665
4666   for (insn = insns; insn; insn = NEXT_INSN (insn))
4667     if (GET_CODE (insn) == NOTE)
4668       {
4669         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_BEG)
4670           {
4671             tree block = block_vector[NOTE_BLOCK_NUMBER (insn)];
4672             /* If we have seen this block before, copy it.  */
4673             if (TREE_ASM_WRITTEN (block))
4674               block = copy_node (block);
4675             BLOCK_SUBBLOCKS (block) = 0;
4676             TREE_ASM_WRITTEN (block) = 1;
4677             BLOCK_SUPERCONTEXT (block) = current_block; 
4678             BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (current_block);
4679             BLOCK_SUBBLOCKS (current_block) = block;
4680             current_block = block;
4681             NOTE_SOURCE_FILE (insn) = 0;
4682           }
4683         if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_BLOCK_END)
4684           {
4685             BLOCK_SUBBLOCKS (current_block)
4686               = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
4687             current_block = BLOCK_SUPERCONTEXT (current_block);
4688             NOTE_SOURCE_FILE (insn) = 0;
4689           }
4690       }
4691
4692   BLOCK_SUBBLOCKS (current_block)
4693     = blocks_nreverse (BLOCK_SUBBLOCKS (current_block));
4694   return current_block;
4695 }
4696
4697 /* Reverse the order of elements in the chain T of blocks,
4698    and return the new head of the chain (old last element).  */
4699
4700 static tree
4701 blocks_nreverse (t)
4702      tree t;
4703 {
4704   register tree prev = 0, decl, next;
4705   for (decl = t; decl; decl = next)
4706     {
4707       next = BLOCK_CHAIN (decl);
4708       BLOCK_CHAIN (decl) = prev;
4709       prev = decl;
4710     }
4711   return prev;
4712 }
4713
4714 /* Count the subblocks of the list starting with BLOCK, and list them
4715    all into the vector VECTOR.  Also clear TREE_ASM_WRITTEN in all
4716    blocks.  */
4717
4718 static int
4719 all_blocks (block, vector)
4720      tree block;
4721      tree *vector;
4722 {
4723   int n_blocks = 0;
4724
4725   while (block)
4726     {
4727       TREE_ASM_WRITTEN (block) = 0;
4728
4729       /* Record this block.  */
4730       if (vector)
4731         vector[n_blocks] = block;
4732
4733       ++n_blocks;
4734       
4735       /* Record the subblocks, and their subblocks...  */
4736       n_blocks += all_blocks (BLOCK_SUBBLOCKS (block),
4737                               vector ? vector + n_blocks : 0);
4738       block = BLOCK_CHAIN (block);
4739     }
4740
4741   return n_blocks;
4742 }
4743 \f
4744 /* Build bytecode call descriptor for function SUBR. */
4745
4746 rtx
4747 bc_build_calldesc (subr)
4748   tree subr;
4749 {
4750   tree calldesc = 0, arg;
4751   int nargs = 0;
4752
4753   /* Build the argument description vector in reverse order.  */
4754   DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4755   nargs = 0;
4756
4757   for (arg = DECL_ARGUMENTS (subr); arg; arg = TREE_CHAIN (arg))
4758     {
4759       ++nargs;
4760
4761       calldesc = tree_cons ((tree) 0, size_in_bytes (TREE_TYPE (arg)), calldesc);
4762       calldesc = tree_cons ((tree) 0, bc_runtime_type_code (TREE_TYPE (arg)), calldesc);
4763     }
4764
4765   DECL_ARGUMENTS (subr) = nreverse (DECL_ARGUMENTS (subr));
4766
4767   /* Prepend the function's return type.  */
4768   calldesc = tree_cons ((tree) 0,
4769                         size_in_bytes (TREE_TYPE (TREE_TYPE (subr))),
4770                         calldesc);
4771
4772   calldesc = tree_cons ((tree) 0,
4773                         bc_runtime_type_code (TREE_TYPE (TREE_TYPE (subr))),
4774                         calldesc);
4775
4776   /* Prepend the arg count.  */
4777   calldesc = tree_cons ((tree) 0, build_int_2 (nargs, 0), calldesc);
4778
4779   /* Output the call description vector and get its address.  */
4780   calldesc = build_nt (CONSTRUCTOR, (tree) 0, calldesc);
4781   TREE_TYPE (calldesc) = build_array_type (integer_type_node,
4782                                            build_index_type (build_int_2 (nargs * 2, 0)));
4783
4784   return output_constant_def (calldesc);
4785 }
4786
4787
4788 /* Generate RTL for the start of the function SUBR (a FUNCTION_DECL tree node)
4789    and initialize static variables for generating RTL for the statements
4790    of the function.  */
4791
4792 void
4793 init_function_start (subr, filename, line)
4794      tree subr;
4795      char *filename;
4796      int line;
4797 {
4798   char *junk;
4799
4800   if (output_bytecode)
4801     {
4802       this_function_decl = subr;
4803       this_function_calldesc = bc_build_calldesc (subr);
4804       local_vars_size = 0;
4805       stack_depth = 0;
4806       max_stack_depth = 0;
4807       stmt_expr_depth = 0;
4808       return;
4809     }
4810
4811   init_stmt_for_function ();
4812
4813   cse_not_expected = ! optimize;
4814
4815   /* Caller save not needed yet.  */
4816   caller_save_needed = 0;
4817
4818   /* No stack slots have been made yet.  */
4819   stack_slot_list = 0;
4820
4821   /* There is no stack slot for handling nonlocal gotos.  */
4822   nonlocal_goto_handler_slot = 0;
4823   nonlocal_goto_stack_level = 0;
4824
4825   /* No labels have been declared for nonlocal use.  */
4826   nonlocal_labels = 0;
4827
4828   /* No function calls so far in this function.  */
4829   function_call_count = 0;
4830
4831   /* No parm regs have been allocated.
4832      (This is important for output_inline_function.)  */
4833   max_parm_reg = LAST_VIRTUAL_REGISTER + 1;
4834
4835   /* Initialize the RTL mechanism.  */
4836   init_emit ();
4837
4838   /* Initialize the queue of pending postincrement and postdecrements,
4839      and some other info in expr.c.  */
4840   init_expr ();
4841
4842   /* We haven't done register allocation yet.  */
4843   reg_renumber = 0;
4844
4845   init_const_rtx_hash_table ();
4846
4847   current_function_name = (*decl_printable_name) (subr, &junk);
4848
4849   /* Nonzero if this is a nested function that uses a static chain.  */
4850
4851   current_function_needs_context
4852     = (decl_function_context (current_function_decl) != 0);
4853
4854   /* Set if a call to setjmp is seen.  */
4855   current_function_calls_setjmp = 0;
4856
4857   /* Set if a call to longjmp is seen.  */
4858   current_function_calls_longjmp = 0;
4859
4860   current_function_calls_alloca = 0;
4861   current_function_has_nonlocal_label = 0;
4862   current_function_has_nonlocal_goto = 0;
4863   current_function_contains_functions = 0;
4864
4865   current_function_returns_pcc_struct = 0;
4866   current_function_returns_struct = 0;
4867   current_function_epilogue_delay_list = 0;
4868   current_function_uses_const_pool = 0;
4869   current_function_uses_pic_offset_table = 0;
4870
4871   /* We have not yet needed to make a label to jump to for tail-recursion.  */
4872   tail_recursion_label = 0;
4873
4874   /* We haven't had a need to make a save area for ap yet.  */
4875
4876   arg_pointer_save_area = 0;
4877
4878   /* No stack slots allocated yet.  */
4879   frame_offset = 0;
4880
4881   /* No SAVE_EXPRs in this function yet.  */
4882   save_expr_regs = 0;
4883
4884   /* No RTL_EXPRs in this function yet.  */
4885   rtl_expr_chain = 0;
4886
4887   /* Set up to allocate temporaries.  */
4888   init_temp_slots ();
4889
4890   /* Within function body, compute a type's size as soon it is laid out.  */
4891   immediate_size_expand++;
4892
4893   /* We haven't made any trampolines for this function yet.  */
4894   trampoline_list = 0;
4895
4896   init_pending_stack_adjust ();
4897   inhibit_defer_pop = 0;
4898
4899   current_function_outgoing_args_size = 0;
4900
4901   /* Prevent ever trying to delete the first instruction of a function.
4902      Also tell final how to output a linenum before the function prologue.  */
4903   emit_line_note (filename, line);
4904
4905   /* Make sure first insn is a note even if we don't want linenums.
4906      This makes sure the first insn will never be deleted.
4907      Also, final expects a note to appear there.  */
4908   emit_note (NULL_PTR, NOTE_INSN_DELETED);
4909
4910   /* Set flags used by final.c.  */
4911   if (aggregate_value_p (DECL_RESULT (subr)))
4912     {
4913 #ifdef PCC_STATIC_STRUCT_RETURN
4914       current_function_returns_pcc_struct = 1;
4915 #endif
4916       current_function_returns_struct = 1;
4917     }
4918
4919   /* Warn if this value is an aggregate type,
4920      regardless of which calling convention we are using for it.  */
4921   if (warn_aggregate_return
4922       && AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
4923     warning ("function returns an aggregate");
4924
4925   current_function_returns_pointer
4926     = POINTER_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
4927
4928   /* Indicate that we need to distinguish between the return value of the
4929      present function and the return value of a function being called.  */
4930   rtx_equal_function_value_matters = 1;
4931
4932   /* Indicate that we have not instantiated virtual registers yet.  */
4933   virtuals_instantiated = 0;
4934
4935   /* Indicate we have no need of a frame pointer yet.  */
4936   frame_pointer_needed = 0;
4937
4938   /* By default assume not varargs or stdarg.  */
4939   current_function_varargs = 0;
4940   current_function_stdarg = 0;
4941 }
4942
4943 /* Indicate that the current function uses extra args
4944    not explicitly mentioned in the argument list in any fashion.  */
4945
4946 void
4947 mark_varargs ()
4948 {
4949   current_function_varargs = 1;
4950 }
4951
4952 /* Expand a call to __main at the beginning of a possible main function.  */
4953
4954 #if defined(INIT_SECTION_ASM_OP) && !defined(INVOKE__main)
4955 #undef HAS_INIT_SECTION
4956 #define HAS_INIT_SECTION
4957 #endif
4958
4959 void
4960 expand_main_function ()
4961 {
4962   if (!output_bytecode)
4963     {
4964       /* The zero below avoids a possible parse error */
4965       0;
4966 #if !defined (HAS_INIT_SECTION)
4967       emit_library_call (gen_rtx (SYMBOL_REF, Pmode, NAME__MAIN), 0,
4968                          VOIDmode, 0);
4969 #endif /* not HAS_INIT_SECTION */
4970     }
4971 }
4972 \f
4973 extern struct obstack permanent_obstack;
4974
4975 /* Expand start of bytecode function. See comment at
4976    expand_function_start below for details. */
4977
4978 void
4979 bc_expand_function_start (subr, parms_have_cleanups)
4980   tree subr;
4981   int parms_have_cleanups;
4982 {
4983   char label[20], *name;
4984   static int nlab;
4985   tree thisarg;
4986   int argsz;
4987
4988   if (TREE_PUBLIC (subr))
4989     bc_globalize_label (IDENTIFIER_POINTER (DECL_NAME (subr)));
4990
4991 #ifdef DEBUG_PRINT_CODE
4992   fprintf (stderr, "\n<func %s>\n", IDENTIFIER_POINTER (DECL_NAME (subr)));
4993 #endif
4994
4995   for (argsz = 0, thisarg = DECL_ARGUMENTS (subr); thisarg; thisarg = TREE_CHAIN (thisarg))
4996     {
4997       if (DECL_RTL (thisarg))
4998         abort ();               /* Should be NULL here I think.  */
4999       else if (TREE_CONSTANT (DECL_SIZE (thisarg)))
5000         {
5001           DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5002           argsz += TREE_INT_CST_LOW (DECL_SIZE (thisarg));
5003         }
5004       else
5005         {
5006           /* Variable-sized objects are pointers to their storage. */
5007           DECL_RTL (thisarg) = bc_gen_rtx ((char *) 0, argsz, (struct bc_label *) 0);
5008           argsz += POINTER_SIZE;
5009         }
5010     }
5011
5012   bc_begin_function (bc_xstrdup (IDENTIFIER_POINTER (DECL_NAME (subr))));
5013
5014   ASM_GENERATE_INTERNAL_LABEL (label, "LX", nlab);
5015
5016   ++nlab;
5017   name = (char *) obstack_copy0 (&permanent_obstack, label, strlen (label));
5018   this_function_callinfo = bc_gen_rtx (name, 0, (struct bc_label *) 0);
5019   this_function_bytecode =
5020     bc_emit_trampoline (BYTECODE_LABEL (this_function_callinfo));
5021 }
5022
5023
5024 /* Expand end of bytecode function. See details the comment of
5025    expand_function_end(), below. */
5026
5027 void
5028 bc_expand_function_end ()
5029 {
5030   char *ptrconsts;
5031
5032   expand_null_return ();
5033
5034   /* Emit any fixup code. This must be done before the call to
5035      to BC_END_FUNCTION (), since that will cause the bytecode
5036      segment to be finished off and closed. */
5037
5038   expand_fixups (NULL_RTX);
5039
5040   ptrconsts = bc_end_function ();
5041
5042   bc_align_const (2 /* INT_ALIGN */);
5043
5044   /* If this changes also make sure to change bc-interp.h!  */
5045
5046   bc_emit_const_labeldef (BYTECODE_LABEL (this_function_callinfo));
5047   bc_emit_const ((char *) &max_stack_depth, sizeof max_stack_depth);
5048   bc_emit_const ((char *) &local_vars_size, sizeof local_vars_size);
5049   bc_emit_const_labelref (this_function_bytecode, 0);
5050   bc_emit_const_labelref (ptrconsts, 0);
5051   bc_emit_const_labelref (BYTECODE_LABEL (this_function_calldesc), 0);
5052 }
5053
5054
5055 /* Start the RTL for a new function, and set variables used for
5056    emitting RTL.
5057    SUBR is the FUNCTION_DECL node.
5058    PARMS_HAVE_CLEANUPS is nonzero if there are cleanups associated with
5059    the function's parameters, which must be run at any return statement.  */
5060
5061 void
5062 expand_function_start (subr, parms_have_cleanups)
5063      tree subr;
5064      int parms_have_cleanups;
5065 {
5066   register int i;
5067   tree tem;
5068   rtx last_ptr;
5069
5070   if (output_bytecode)
5071     {
5072       bc_expand_function_start (subr, parms_have_cleanups);
5073       return;
5074     }
5075
5076   /* Make sure volatile mem refs aren't considered
5077      valid operands of arithmetic insns.  */
5078   init_recog_no_volatile ();
5079
5080   /* If function gets a static chain arg, store it in the stack frame.
5081      Do this first, so it gets the first stack slot offset.  */
5082   if (current_function_needs_context)
5083     {
5084       last_ptr = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
5085
5086 #ifdef SMALL_REGISTER_CLASSES
5087       /* Delay copying static chain if it is not a register to avoid
5088          conflicts with regs used for parameters.  */
5089       if (GET_CODE (static_chain_incoming_rtx) == REG)
5090 #endif
5091         emit_move_insn (last_ptr, static_chain_incoming_rtx);
5092     }
5093
5094   /* If the parameters of this function need cleaning up, get a label
5095      for the beginning of the code which executes those cleanups.  This must
5096      be done before doing anything with return_label.  */
5097   if (parms_have_cleanups)
5098     cleanup_label = gen_label_rtx ();
5099   else
5100     cleanup_label = 0;
5101
5102   /* Make the label for return statements to jump to, if this machine
5103      does not have a one-instruction return and uses an epilogue,
5104      or if it returns a structure, or if it has parm cleanups.  */
5105 #ifdef HAVE_return
5106   if (cleanup_label == 0 && HAVE_return
5107       && ! current_function_returns_pcc_struct
5108       && ! (current_function_returns_struct && ! optimize))
5109     return_label = 0;
5110   else
5111     return_label = gen_label_rtx ();
5112 #else
5113   return_label = gen_label_rtx ();
5114 #endif
5115
5116   /* Initialize rtx used to return the value.  */
5117   /* Do this before assign_parms so that we copy the struct value address
5118      before any library calls that assign parms might generate.  */
5119
5120   /* Decide whether to return the value in memory or in a register.  */
5121   if (aggregate_value_p (DECL_RESULT (subr)))
5122     {
5123       /* Returning something that won't go in a register.  */
5124       register rtx value_address = 0;
5125
5126 #ifdef PCC_STATIC_STRUCT_RETURN
5127       if (current_function_returns_pcc_struct)
5128         {
5129           int size = int_size_in_bytes (TREE_TYPE (DECL_RESULT (subr)));
5130           value_address = assemble_static_space (size);
5131         }
5132       else
5133 #endif
5134         {
5135           /* Expect to be passed the address of a place to store the value.
5136              If it is passed as an argument, assign_parms will take care of
5137              it.  */
5138           if (struct_value_incoming_rtx)
5139             {
5140               value_address = gen_reg_rtx (Pmode);
5141               emit_move_insn (value_address, struct_value_incoming_rtx);
5142             }
5143         }
5144       if (value_address)
5145         {
5146           DECL_RTL (DECL_RESULT (subr))
5147             = gen_rtx (MEM, DECL_MODE (DECL_RESULT (subr)), value_address);
5148           MEM_IN_STRUCT_P (DECL_RTL (DECL_RESULT (subr)))
5149             = AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr)));
5150         }
5151     }
5152   else if (DECL_MODE (DECL_RESULT (subr)) == VOIDmode)
5153     /* If return mode is void, this decl rtl should not be used.  */
5154     DECL_RTL (DECL_RESULT (subr)) = 0;
5155   else if (parms_have_cleanups)
5156     {
5157       /* If function will end with cleanup code for parms,
5158          compute the return values into a pseudo reg,
5159          which we will copy into the true return register
5160          after the cleanups are done.  */
5161
5162       enum machine_mode mode = DECL_MODE (DECL_RESULT (subr));
5163
5164 #ifdef PROMOTE_FUNCTION_RETURN
5165       tree type = TREE_TYPE (DECL_RESULT (subr));
5166       int unsignedp = TREE_UNSIGNED (type);
5167
5168       mode = promote_mode (type, mode, &unsignedp, 1);
5169 #endif
5170
5171       DECL_RTL (DECL_RESULT (subr)) = gen_reg_rtx (mode);
5172     }
5173   else
5174     /* Scalar, returned in a register.  */
5175     {
5176 #ifdef FUNCTION_OUTGOING_VALUE
5177       DECL_RTL (DECL_RESULT (subr))
5178         = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5179 #else
5180       DECL_RTL (DECL_RESULT (subr))
5181         = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (subr)), subr);
5182 #endif
5183
5184       /* Mark this reg as the function's return value.  */
5185       if (GET_CODE (DECL_RTL (DECL_RESULT (subr))) == REG)
5186         {
5187           REG_FUNCTION_VALUE_P (DECL_RTL (DECL_RESULT (subr))) = 1;
5188           /* Needed because we may need to move this to memory
5189              in case it's a named return value whose address is taken.  */
5190           DECL_REGISTER (DECL_RESULT (subr)) = 1;
5191         }
5192     }
5193
5194   /* Initialize rtx for parameters and local variables.
5195      In some cases this requires emitting insns.  */
5196
5197   assign_parms (subr, 0);
5198
5199 #ifdef SMALL_REGISTER_CLASSES
5200   /* Copy the static chain now if it wasn't a register.  The delay is to
5201      avoid conflicts with the parameter passing registers.  */
5202
5203   if (current_function_needs_context)
5204       if (GET_CODE (static_chain_incoming_rtx) != REG)
5205         emit_move_insn (last_ptr, static_chain_incoming_rtx);
5206 #endif
5207
5208   /* The following was moved from init_function_start.
5209      The move is supposed to make sdb output more accurate.  */
5210   /* Indicate the beginning of the function body,
5211      as opposed to parm setup.  */
5212   emit_note (NULL_PTR, NOTE_INSN_FUNCTION_BEG);
5213
5214   /* If doing stupid allocation, mark parms as born here.  */
5215
5216   if (GET_CODE (get_last_insn ()) != NOTE)
5217     emit_note (NULL_PTR, NOTE_INSN_DELETED);
5218   parm_birth_insn = get_last_insn ();
5219
5220   if (obey_regdecls)
5221     {
5222       for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5223         use_variable (regno_reg_rtx[i]);
5224
5225       if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5226         use_variable (current_function_internal_arg_pointer);
5227     }
5228
5229   /* Fetch static chain values for containing functions.  */
5230   tem = decl_function_context (current_function_decl);
5231   /* If not doing stupid register allocation copy the static chain
5232      pointer into a pseudo.  If we have small register classes, copy the
5233      value from memory if static_chain_incoming_rtx is a REG.  If we do
5234      stupid register allocation, we use the stack address generated above.  */
5235   if (tem && ! obey_regdecls)
5236     {
5237 #ifdef SMALL_REGISTER_CLASSES
5238       /* If the static chain originally came in a register, put it back
5239          there, then move it out in the next insn.  The reason for
5240          this peculiar code is to satisfy function integration.  */
5241       if (GET_CODE (static_chain_incoming_rtx) == REG)
5242         emit_move_insn (static_chain_incoming_rtx, last_ptr);
5243 #endif
5244
5245       last_ptr = copy_to_reg (static_chain_incoming_rtx);
5246     }
5247
5248   context_display = 0;
5249   while (tem)
5250     {
5251       tree rtlexp = make_node (RTL_EXPR);
5252
5253       RTL_EXPR_RTL (rtlexp) = last_ptr;
5254       context_display = tree_cons (tem, rtlexp, context_display);
5255       tem = decl_function_context (tem);
5256       if (tem == 0)
5257         break;
5258       /* Chain thru stack frames, assuming pointer to next lexical frame
5259          is found at the place we always store it.  */
5260 #ifdef FRAME_GROWS_DOWNWARD
5261       last_ptr = plus_constant (last_ptr, - GET_MODE_SIZE (Pmode));
5262 #endif
5263       last_ptr = copy_to_reg (gen_rtx (MEM, Pmode,
5264                                        memory_address (Pmode, last_ptr)));
5265
5266       /* If we are not optimizing, ensure that we know that this
5267          piece of context is live over the entire function.  */
5268       if (! optimize)
5269         save_expr_regs = gen_rtx (EXPR_LIST, VOIDmode, last_ptr,
5270                                   save_expr_regs);
5271     }
5272
5273   /* After the display initializations is where the tail-recursion label
5274      should go, if we end up needing one.   Ensure we have a NOTE here
5275      since some things (like trampolines) get placed before this.  */
5276   tail_recursion_reentry = emit_note (NULL_PTR, NOTE_INSN_DELETED);
5277
5278   /* Evaluate now the sizes of any types declared among the arguments.  */
5279   for (tem = nreverse (get_pending_sizes ()); tem; tem = TREE_CHAIN (tem))
5280     expand_expr (TREE_VALUE (tem), const0_rtx, VOIDmode, 0);
5281
5282   /* Make sure there is a line number after the function entry setup code.  */
5283   force_next_line_note ();
5284 }
5285 \f
5286 /* Generate RTL for the end of the current function.
5287    FILENAME and LINE are the current position in the source file. 
5288
5289    It is up to language-specific callers to do cleanups for parameters--
5290    or else, supply 1 for END_BINDINGS and we will call expand_end_bindings.  */
5291
5292 void
5293 expand_function_end (filename, line, end_bindings)
5294      char *filename;
5295      int line;
5296      int end_bindings;
5297 {
5298   register int i;
5299   tree link;
5300
5301   static rtx initial_trampoline;
5302
5303   if (output_bytecode)
5304     {
5305       bc_expand_function_end ();
5306       return;
5307     }
5308
5309 #ifdef NON_SAVING_SETJMP
5310   /* Don't put any variables in registers if we call setjmp
5311      on a machine that fails to restore the registers.  */
5312   if (NON_SAVING_SETJMP && current_function_calls_setjmp)
5313     {
5314       if (DECL_INITIAL (current_function_decl) != error_mark_node)
5315         setjmp_protect (DECL_INITIAL (current_function_decl));
5316
5317       setjmp_protect_args ();
5318     }
5319 #endif
5320
5321   /* Save the argument pointer if a save area was made for it.  */
5322   if (arg_pointer_save_area)
5323     {
5324       rtx x = gen_move_insn (arg_pointer_save_area, virtual_incoming_args_rtx);
5325       emit_insn_before (x, tail_recursion_reentry);
5326     }
5327
5328   /* Initialize any trampolines required by this function.  */
5329   for (link = trampoline_list; link; link = TREE_CHAIN (link))
5330     {
5331       tree function = TREE_PURPOSE (link);
5332       rtx context = lookup_static_chain (function);
5333       rtx tramp = RTL_EXPR_RTL (TREE_VALUE (link));
5334       rtx seq;
5335
5336       /* First make sure this compilation has a template for
5337          initializing trampolines.  */
5338       if (initial_trampoline == 0)
5339         {
5340           end_temporary_allocation ();
5341           initial_trampoline
5342             = gen_rtx (MEM, BLKmode, assemble_trampoline_template ());
5343           resume_temporary_allocation ();
5344         }
5345
5346       /* Generate insns to initialize the trampoline.  */
5347       start_sequence ();
5348       tramp = change_address (initial_trampoline, BLKmode,
5349                               round_trampoline_addr (XEXP (tramp, 0)));
5350       emit_block_move (tramp, initial_trampoline, GEN_INT (TRAMPOLINE_SIZE),
5351                        FUNCTION_BOUNDARY / BITS_PER_UNIT);
5352       INITIALIZE_TRAMPOLINE (XEXP (tramp, 0),
5353                              XEXP (DECL_RTL (function), 0), context);
5354       seq = get_insns ();
5355       end_sequence ();
5356
5357       /* Put those insns at entry to the containing function (this one).  */
5358       emit_insns_before (seq, tail_recursion_reentry);
5359     }
5360
5361   /* Warn about unused parms if extra warnings were specified.  */
5362   if (warn_unused && extra_warnings)
5363     {
5364       tree decl;
5365
5366       for (decl = DECL_ARGUMENTS (current_function_decl);
5367            decl; decl = TREE_CHAIN (decl))
5368         if (! TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
5369             && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
5370           warning_with_decl (decl, "unused parameter `%s'");
5371     }
5372
5373   /* Delete handlers for nonlocal gotos if nothing uses them.  */
5374   if (nonlocal_goto_handler_slot != 0 && !current_function_has_nonlocal_label)
5375     delete_handlers ();
5376
5377   /* End any sequences that failed to be closed due to syntax errors.  */
5378   while (in_sequence_p ())
5379     end_sequence ();
5380
5381   /* Outside function body, can't compute type's actual size
5382      until next function's body starts.  */
5383   immediate_size_expand--;
5384
5385   /* If doing stupid register allocation,
5386      mark register parms as dying here.  */
5387
5388   if (obey_regdecls)
5389     {
5390       rtx tem;
5391       for (i = LAST_VIRTUAL_REGISTER + 1; i < max_parm_reg; i++)
5392         use_variable (regno_reg_rtx[i]);
5393
5394       /* Likewise for the regs of all the SAVE_EXPRs in the function.  */
5395
5396       for (tem = save_expr_regs; tem; tem = XEXP (tem, 1))
5397         {
5398           use_variable (XEXP (tem, 0));
5399           use_variable_after (XEXP (tem, 0), parm_birth_insn);
5400         }
5401
5402       if (current_function_internal_arg_pointer != virtual_incoming_args_rtx)
5403         use_variable (current_function_internal_arg_pointer);
5404     }
5405
5406   clear_pending_stack_adjust ();
5407   do_pending_stack_adjust ();
5408
5409   /* Mark the end of the function body.
5410      If control reaches this insn, the function can drop through
5411      without returning a value.  */
5412   emit_note (NULL_PTR, NOTE_INSN_FUNCTION_END);
5413
5414   /* Output a linenumber for the end of the function.
5415      SDB depends on this.  */
5416   emit_line_note_force (filename, line);
5417
5418   /* Output the label for the actual return from the function,
5419      if one is expected.  This happens either because a function epilogue
5420      is used instead of a return instruction, or because a return was done
5421      with a goto in order to run local cleanups, or because of pcc-style
5422      structure returning.  */
5423
5424   if (return_label)
5425     emit_label (return_label);
5426
5427   /* C++ uses this.  */
5428   if (end_bindings)
5429     expand_end_bindings (0, 0, 0);
5430
5431   /* If we had calls to alloca, and this machine needs
5432      an accurate stack pointer to exit the function,
5433      insert some code to save and restore the stack pointer.  */
5434 #ifdef EXIT_IGNORE_STACK
5435   if (! EXIT_IGNORE_STACK)
5436 #endif
5437     if (current_function_calls_alloca)
5438       {
5439         rtx tem = 0;
5440
5441         emit_stack_save (SAVE_FUNCTION, &tem, parm_birth_insn);
5442         emit_stack_restore (SAVE_FUNCTION, tem, NULL_RTX);
5443       }
5444
5445   /* If scalar return value was computed in a pseudo-reg,
5446      copy that to the hard return register.  */
5447   if (DECL_RTL (DECL_RESULT (current_function_decl)) != 0
5448       && GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) == REG
5449       && (REGNO (DECL_RTL (DECL_RESULT (current_function_decl)))
5450           >= FIRST_PSEUDO_REGISTER))
5451     {
5452       rtx real_decl_result;
5453
5454 #ifdef FUNCTION_OUTGOING_VALUE
5455       real_decl_result
5456         = FUNCTION_OUTGOING_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5457                                    current_function_decl);
5458 #else
5459       real_decl_result
5460         = FUNCTION_VALUE (TREE_TYPE (DECL_RESULT (current_function_decl)),
5461                           current_function_decl);
5462 #endif
5463       REG_FUNCTION_VALUE_P (real_decl_result) = 1;
5464       emit_move_insn (real_decl_result,
5465                       DECL_RTL (DECL_RESULT (current_function_decl)));
5466       emit_insn (gen_rtx (USE, VOIDmode, real_decl_result));
5467     }
5468
5469   /* If returning a structure, arrange to return the address of the value
5470      in a place where debuggers expect to find it.
5471
5472      If returning a structure PCC style,
5473      the caller also depends on this value.
5474      And current_function_returns_pcc_struct is not necessarily set.  */
5475   if (current_function_returns_struct
5476       || current_function_returns_pcc_struct)
5477     {
5478       rtx value_address = XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
5479       tree type = TREE_TYPE (DECL_RESULT (current_function_decl));
5480 #ifdef FUNCTION_OUTGOING_VALUE
5481       rtx outgoing
5482         = FUNCTION_OUTGOING_VALUE (build_pointer_type (type),
5483                                    current_function_decl);
5484 #else
5485       rtx outgoing
5486         = FUNCTION_VALUE (build_pointer_type (type),
5487                           current_function_decl);
5488 #endif
5489
5490       /* Mark this as a function return value so integrate will delete the
5491          assignment and USE below when inlining this function.  */
5492       REG_FUNCTION_VALUE_P (outgoing) = 1;
5493
5494       emit_move_insn (outgoing, value_address);
5495       use_variable (outgoing);
5496     }
5497
5498   /* Output a return insn if we are using one.
5499      Otherwise, let the rtl chain end here, to drop through
5500      into the epilogue.  */
5501
5502 #ifdef HAVE_return
5503   if (HAVE_return)
5504     {
5505       emit_jump_insn (gen_return ());
5506       emit_barrier ();
5507     }
5508 #endif
5509
5510   /* Fix up any gotos that jumped out to the outermost
5511      binding level of the function.
5512      Must follow emitting RETURN_LABEL.  */
5513
5514   /* If you have any cleanups to do at this point,
5515      and they need to create temporary variables,
5516      then you will lose.  */
5517   expand_fixups (get_insns ());
5518 }
5519 \f
5520 /* These arrays record the INSN_UIDs of the prologue and epilogue insns.  */
5521
5522 static int *prologue;
5523 static int *epilogue;
5524
5525 /* Create an array that records the INSN_UIDs of INSNS (either a sequence
5526    or a single insn).  */
5527
5528 static int *
5529 record_insns (insns)
5530      rtx insns;
5531 {
5532   int *vec;
5533
5534   if (GET_CODE (insns) == SEQUENCE)
5535     {
5536       int len = XVECLEN (insns, 0);
5537       vec = (int *) oballoc ((len + 1) * sizeof (int));
5538       vec[len] = 0;
5539       while (--len >= 0)
5540         vec[len] = INSN_UID (XVECEXP (insns, 0, len));
5541     }
5542   else
5543     {
5544       vec = (int *) oballoc (2 * sizeof (int));
5545       vec[0] = INSN_UID (insns);
5546       vec[1] = 0;
5547     }
5548   return vec;
5549 }
5550
5551 /* Determine how many INSN_UIDs in VEC are part of INSN.  */
5552
5553 static int
5554 contains (insn, vec)
5555      rtx insn;
5556      int *vec;
5557 {
5558   register int i, j;
5559
5560   if (GET_CODE (insn) == INSN
5561       && GET_CODE (PATTERN (insn)) == SEQUENCE)
5562     {
5563       int count = 0;
5564       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
5565         for (j = 0; vec[j]; j++)
5566           if (INSN_UID (XVECEXP (PATTERN (insn), 0, i)) == vec[j])
5567             count++;
5568       return count;
5569     }
5570   else
5571     {
5572       for (j = 0; vec[j]; j++)
5573         if (INSN_UID (insn) == vec[j])
5574           return 1;
5575     }
5576   return 0;
5577 }
5578
5579 /* Generate the prologue and epilogue RTL if the machine supports it.  Thread
5580    this into place with notes indicating where the prologue ends and where
5581    the epilogue begins.  Update the basic block information when possible.  */
5582
5583 void
5584 thread_prologue_and_epilogue_insns (f)
5585      rtx f;
5586 {
5587 #ifdef HAVE_prologue
5588   if (HAVE_prologue)
5589     {
5590       rtx head, seq, insn;
5591
5592       /* The first insn (a NOTE_INSN_DELETED) is followed by zero or more
5593          prologue insns and a NOTE_INSN_PROLOGUE_END.  */
5594       emit_note_after (NOTE_INSN_PROLOGUE_END, f);
5595       seq = gen_prologue ();
5596       head = emit_insn_after (seq, f);
5597
5598       /* Include the new prologue insns in the first block.  Ignore them
5599          if they form a basic block unto themselves.  */
5600       if (basic_block_head && n_basic_blocks
5601           && GET_CODE (basic_block_head[0]) != CODE_LABEL)
5602         basic_block_head[0] = NEXT_INSN (f);
5603
5604       /* Retain a map of the prologue insns.  */
5605       prologue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : head);
5606     }
5607   else
5608 #endif
5609     prologue = 0;
5610
5611 #ifdef HAVE_epilogue
5612   if (HAVE_epilogue)
5613     {
5614       rtx insn = get_last_insn ();
5615       rtx prev = prev_nonnote_insn (insn);
5616
5617       /* If we end with a BARRIER, we don't need an epilogue.  */
5618       if (! (prev && GET_CODE (prev) == BARRIER))
5619         {
5620           rtx tail, seq, tem;
5621           rtx first_use = 0;
5622           rtx last_use = 0;
5623
5624           /* The last basic block ends with a NOTE_INSN_EPILOGUE_BEG, the
5625              epilogue insns, the USE insns at the end of a function,
5626              the jump insn that returns, and then a BARRIER.  */
5627
5628           /* Move the USE insns at the end of a function onto a list.  */
5629           while (prev
5630                  && GET_CODE (prev) == INSN
5631                  && GET_CODE (PATTERN (prev)) == USE)
5632             {
5633               tem = prev;
5634               prev = prev_nonnote_insn (prev);
5635
5636               NEXT_INSN (PREV_INSN (tem)) = NEXT_INSN (tem);
5637               PREV_INSN (NEXT_INSN (tem)) = PREV_INSN (tem);
5638               if (first_use)
5639                 {
5640                   NEXT_INSN (tem) = first_use;
5641                   PREV_INSN (first_use) = tem;
5642                 }
5643               first_use = tem;
5644               if (!last_use)
5645                 last_use = tem;
5646             }
5647
5648           emit_barrier_after (insn);
5649
5650           seq = gen_epilogue ();
5651           tail = emit_jump_insn_after (seq, insn);
5652
5653           /* Insert the USE insns immediately before the return insn, which
5654              must be the first instruction before the final barrier.  */
5655           if (first_use)
5656             {
5657               tem = prev_nonnote_insn (get_last_insn ());
5658               NEXT_INSN (PREV_INSN (tem)) = first_use;
5659               PREV_INSN (first_use) = PREV_INSN (tem);
5660               PREV_INSN (tem) = last_use;
5661               NEXT_INSN (last_use) = tem;
5662             }
5663
5664           emit_note_after (NOTE_INSN_EPILOGUE_BEG, insn);
5665
5666           /* Include the new epilogue insns in the last block.  Ignore
5667              them if they form a basic block unto themselves.  */
5668           if (basic_block_end && n_basic_blocks
5669               && GET_CODE (basic_block_end[n_basic_blocks - 1]) != JUMP_INSN)
5670             basic_block_end[n_basic_blocks - 1] = tail;
5671
5672           /* Retain a map of the epilogue insns.  */
5673           epilogue = record_insns (GET_CODE (seq) == SEQUENCE ? seq : tail);
5674           return;
5675         }
5676     }
5677 #endif
5678   epilogue = 0;
5679 }
5680
5681 /* Reposition the prologue-end and epilogue-begin notes after instruction
5682    scheduling and delayed branch scheduling.  */
5683
5684 void
5685 reposition_prologue_and_epilogue_notes (f)
5686      rtx f;
5687 {
5688 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
5689   /* Reposition the prologue and epilogue notes.  */
5690   if (n_basic_blocks)
5691     {
5692       rtx next, prev;
5693       int len;
5694
5695       if (prologue)
5696         {
5697           register rtx insn, note = 0;
5698
5699           /* Scan from the beginning until we reach the last prologue insn.
5700              We apparently can't depend on basic_block_{head,end} after
5701              reorg has run.  */
5702           for (len = 0; prologue[len]; len++)
5703             ;
5704           for (insn = f; len && insn; insn = NEXT_INSN (insn))
5705             {
5706               if (GET_CODE (insn) == NOTE)
5707                 {
5708                   if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_PROLOGUE_END)
5709                     note = insn;
5710                 }
5711               else if ((len -= contains (insn, prologue)) == 0)
5712                 {
5713                   /* Find the prologue-end note if we haven't already, and
5714                      move it to just after the last prologue insn.  */
5715                   if (note == 0)
5716                     {
5717                       for (note = insn; note = NEXT_INSN (note);)
5718                         if (GET_CODE (note) == NOTE
5719                             && NOTE_LINE_NUMBER (note) == NOTE_INSN_PROLOGUE_END)
5720                           break;
5721                     }
5722                   next = NEXT_INSN (note);
5723                   prev = PREV_INSN (note);
5724                   if (prev)
5725                     NEXT_INSN (prev) = next;
5726                   if (next)
5727                     PREV_INSN (next) = prev;
5728                   add_insn_after (note, insn);
5729                 }
5730             }
5731         }
5732
5733       if (epilogue)
5734         {
5735           register rtx insn, note = 0;
5736
5737           /* Scan from the end until we reach the first epilogue insn.
5738              We apparently can't depend on basic_block_{head,end} after
5739              reorg has run.  */
5740           for (len = 0; epilogue[len]; len++)
5741             ;
5742           for (insn = get_last_insn (); len && insn; insn = PREV_INSN (insn))
5743             {
5744               if (GET_CODE (insn) == NOTE)
5745                 {
5746                   if (NOTE_LINE_NUMBER (insn) == NOTE_INSN_EPILOGUE_BEG)
5747                     note = insn;
5748                 }
5749               else if ((len -= contains (insn, epilogue)) == 0)
5750                 {
5751                   /* Find the epilogue-begin note if we haven't already, and
5752                      move it to just before the first epilogue insn.  */
5753                   if (note == 0)
5754                     {
5755                       for (note = insn; note = PREV_INSN (note);)
5756                         if (GET_CODE (note) == NOTE
5757                             && NOTE_LINE_NUMBER (note) == NOTE_INSN_EPILOGUE_BEG)
5758                           break;
5759                     }
5760                   next = NEXT_INSN (note);
5761                   prev = PREV_INSN (note);
5762                   if (prev)
5763                     NEXT_INSN (prev) = next;
5764                   if (next)
5765                     PREV_INSN (next) = prev;
5766                   add_insn_after (note, PREV_INSN (insn));
5767                 }
5768             }
5769         }
5770     }
5771 #endif /* HAVE_prologue or HAVE_epilogue */
5772 }