OSDN Git Service

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