OSDN Git Service

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