OSDN Git Service

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