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