OSDN Git Service

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