]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/calls.c
* gcc.c-torture/compile/20000511-1.c: New test.
[thirdparty/gcc.git] / gcc / calls.c
CommitLineData
51bbfa0c 1/* Convert function calls to rtl insns, for GNU C compiler.
3c71940f
JL
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 1999, 2000 Free Software Foundation, Inc.
51bbfa0c
RS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
51bbfa0c
RS
21
22#include "config.h"
670ee920
KG
23#include "system.h"
24#include "rtl.h"
25#include "tree.h"
26#include "flags.h"
27#include "expr.h"
49ad7cfa 28#include "function.h"
670ee920 29#include "regs.h"
51bbfa0c 30#include "insn-flags.h"
5f6da302 31#include "toplev.h"
d6f4ec51 32#include "output.h"
b1474bb7 33#include "tm_p.h"
51bbfa0c 34
f73ad30e
JH
35#ifndef ACCUMULATE_OUTGOING_ARGS
36#define ACCUMULATE_OUTGOING_ARGS 0
37#endif
38
39/* Supply a default definition for PUSH_ARGS. */
40#ifndef PUSH_ARGS
41#ifdef PUSH_ROUNDING
42#define PUSH_ARGS !ACCUMULATE_OUTGOING_ARGS
43#else
44#define PUSH_ARGS 0
45#endif
46#endif
47
0a1c58a2
JL
48#if !defined FUNCTION_OK_FOR_SIBCALL
49#define FUNCTION_OK_FOR_SIBCALL(DECL) 1
50#endif
51
c795bca9
BS
52#if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
53#define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
54#endif
55
51bbfa0c 56/* Decide whether a function's arguments should be processed
bbc8a071
RK
57 from first to last or from last to first.
58
59 They should if the stack and args grow in opposite directions, but
60 only if we have push insns. */
51bbfa0c 61
51bbfa0c 62#ifdef PUSH_ROUNDING
bbc8a071 63
40083ddf 64#if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
f73ad30e 65#define PUSH_ARGS_REVERSED PUSH_ARGS
51bbfa0c 66#endif
bbc8a071 67
51bbfa0c
RS
68#endif
69
f73ad30e
JH
70#ifndef PUSH_ARGS_REVERSED
71#define PUSH_ARGS_REVERSED 0
72#endif
73
c795bca9
BS
74/* Like PREFERRED_STACK_BOUNDARY but in units of bytes, not bits. */
75#define STACK_BYTES (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)
51bbfa0c
RS
76
77/* Data structure and subroutines used within expand_call. */
78
79struct arg_data
80{
81 /* Tree node for this argument. */
82 tree tree_value;
1efe6448
RK
83 /* Mode for value; TYPE_MODE unless promoted. */
84 enum machine_mode mode;
51bbfa0c
RS
85 /* Current RTL value for argument, or 0 if it isn't precomputed. */
86 rtx value;
87 /* Initially-compute RTL value for argument; only for const functions. */
88 rtx initial_value;
89 /* Register to pass this argument in, 0 if passed on stack, or an
cacbd532 90 PARALLEL if the arg is to be copied into multiple non-contiguous
51bbfa0c
RS
91 registers. */
92 rtx reg;
84b55618
RK
93 /* If REG was promoted from the actual mode of the argument expression,
94 indicates whether the promotion is sign- or zero-extended. */
95 int unsignedp;
51bbfa0c
RS
96 /* Number of registers to use. 0 means put the whole arg in registers.
97 Also 0 if not passed in registers. */
98 int partial;
d64f5a78
RS
99 /* Non-zero if argument must be passed on stack.
100 Note that some arguments may be passed on the stack
101 even though pass_on_stack is zero, just because FUNCTION_ARG says so.
102 pass_on_stack identifies arguments that *cannot* go in registers. */
51bbfa0c
RS
103 int pass_on_stack;
104 /* Offset of this argument from beginning of stack-args. */
105 struct args_size offset;
106 /* Similar, but offset to the start of the stack slot. Different from
107 OFFSET if this arg pads downward. */
108 struct args_size slot_offset;
109 /* Size of this argument on the stack, rounded up for any padding it gets,
110 parts of the argument passed in registers do not count.
111 If REG_PARM_STACK_SPACE is defined, then register parms
112 are counted here as well. */
113 struct args_size size;
114 /* Location on the stack at which parameter should be stored. The store
115 has already been done if STACK == VALUE. */
116 rtx stack;
117 /* Location on the stack of the start of this argument slot. This can
118 differ from STACK if this arg pads downward. This location is known
119 to be aligned to FUNCTION_ARG_BOUNDARY. */
120 rtx stack_slot;
51bbfa0c
RS
121 /* Place that this stack area has been saved, if needed. */
122 rtx save_area;
4ab56118
RK
123 /* If an argument's alignment does not permit direct copying into registers,
124 copy in smaller-sized pieces into pseudos. These are stored in a
125 block pointed to by this field. The next field says how many
126 word-sized pseudos we made. */
127 rtx *aligned_regs;
128 int n_aligned_regs;
4fc026cd
CM
129 /* The amount that the stack pointer needs to be adjusted to
130 force alignment for the next argument. */
131 struct args_size alignment_pad;
51bbfa0c
RS
132};
133
b94301c2 134/* A vector of one char per byte of stack space. A byte if non-zero if
51bbfa0c
RS
135 the corresponding stack location has been used.
136 This vector is used to prevent a function call within an argument from
137 clobbering any stack already set up. */
138static char *stack_usage_map;
139
140/* Size of STACK_USAGE_MAP. */
141static int highest_outgoing_arg_in_use;
2f4aa534
RS
142
143/* stack_arg_under_construction is nonzero when an argument may be
144 initialized with a constructor call (including a C function that
145 returns a BLKmode struct) and expand_call must take special action
146 to make sure the object being constructed does not overlap the
147 argument list for the constructor call. */
148int stack_arg_under_construction;
51bbfa0c 149
3d994c6b
KG
150static int calls_function PARAMS ((tree, int));
151static int calls_function_1 PARAMS ((tree, int));
0a1c58a2 152
f2d33f13
JH
153/* Nonzero if this is a call to a `const' function. */
154#define ECF_CONST 1
155/* Nonzero if this is a call to a `volatile' function. */
156#define ECF_NORETURN 2
157/* Nonzero if this is a call to malloc or a related function. */
158#define ECF_MALLOC 4
159/* Nonzero if it is plausible that this is a call to alloca. */
160#define ECF_MAY_BE_ALLOCA 8
161/* Nonzero if this is a call to a function that won't throw an exception. */
162#define ECF_NOTHROW 16
163/* Nonzero if this is a call to setjmp or a related function. */
164#define ECF_RETURNS_TWICE 32
165/* Nonzero if this is a call to `longjmp'. */
166#define ECF_LONGJMP 64
167/* Nonzero if this is a syscall that makes a new process in the image of
168 the current one. */
169#define ECF_FORK_OR_EXEC 128
170#define ECF_SIBCALL 256
2a8f6b90
JH
171/* Nonzero if this is a call to "pure" function (like const function,
172 but may read memory. */
173#define ECF_PURE 512
f2d33f13 174
3d994c6b
KG
175static void emit_call_1 PARAMS ((rtx, tree, tree, HOST_WIDE_INT,
176 HOST_WIDE_INT, HOST_WIDE_INT, rtx,
0a1c58a2 177 rtx, int, rtx, int));
3d994c6b
KG
178static void precompute_register_parameters PARAMS ((int,
179 struct arg_data *,
180 int *));
181static void store_one_arg PARAMS ((struct arg_data *, rtx, int, int,
182 int));
183static void store_unaligned_arguments_into_pseudos PARAMS ((struct arg_data *,
184 int));
185static int finalize_must_preallocate PARAMS ((int, int,
186 struct arg_data *,
187 struct args_size *));
40d6e956
JH
188static void precompute_arguments PARAMS ((int, int,
189 struct arg_data *));
3d994c6b 190static int compute_argument_block_size PARAMS ((int,
c2f8b491
JH
191 struct args_size *,
192 int));
3d994c6b
KG
193static void initialize_argument_information PARAMS ((int,
194 struct arg_data *,
195 struct args_size *,
196 int, tree, tree,
197 CUMULATIVE_ARGS *,
198 int, rtx *, int *,
f2d33f13 199 int *, int *));
3d994c6b
KG
200static void compute_argument_addresses PARAMS ((struct arg_data *,
201 rtx, int));
202static rtx rtx_for_function_call PARAMS ((tree, tree));
203static void load_register_parameters PARAMS ((struct arg_data *,
204 int, rtx *));
12a22e76 205static int libfunc_nothrow PARAMS ((rtx));
de76b467
JH
206static rtx emit_library_call_value_1 PARAMS ((int, rtx, rtx, int,
207 enum machine_mode,
208 int, va_list));
f2d33f13
JH
209static int special_function_p PARAMS ((tree, int));
210static int flags_from_decl_or_type PARAMS ((tree));
211static rtx try_to_integrate PARAMS ((tree, tree, rtx,
212 int, tree, rtx));
ce48579b 213static int combine_pending_stack_adjustment_and_call
739fb049 214 PARAMS ((int, struct args_size *, int));
21a3b983 215
f73ad30e 216#ifdef REG_PARM_STACK_SPACE
3d994c6b
KG
217static rtx save_fixed_argument_area PARAMS ((int, rtx, int *, int *));
218static void restore_fixed_argument_area PARAMS ((rtx, rtx, int, int));
20efdf74 219#endif
51bbfa0c 220\f
1ce0cb53
JW
221/* If WHICH is 1, return 1 if EXP contains a call to the built-in function
222 `alloca'.
223
224 If WHICH is 0, return 1 if EXP contains a call to any function.
225 Actually, we only need return 1 if evaluating EXP would require pushing
226 arguments on the stack, but that is too difficult to compute, so we just
227 assume any function call might require the stack. */
51bbfa0c 228
1c8d7aef
RS
229static tree calls_function_save_exprs;
230
51bbfa0c 231static int
1ce0cb53 232calls_function (exp, which)
51bbfa0c 233 tree exp;
1ce0cb53 234 int which;
1c8d7aef
RS
235{
236 int val;
237 calls_function_save_exprs = 0;
238 val = calls_function_1 (exp, which);
239 calls_function_save_exprs = 0;
240 return val;
241}
242
243static int
244calls_function_1 (exp, which)
245 tree exp;
246 int which;
51bbfa0c
RS
247{
248 register int i;
0207efa2
RK
249 enum tree_code code = TREE_CODE (exp);
250 int type = TREE_CODE_CLASS (code);
251 int length = tree_code_length[(int) code];
51bbfa0c 252
ddd5a7c1 253 /* If this code is language-specific, we don't know what it will do. */
0207efa2
RK
254 if ((int) code >= NUM_TREE_CODES)
255 return 1;
51bbfa0c 256
0207efa2 257 switch (code)
51bbfa0c
RS
258 {
259 case CALL_EXPR:
1ce0cb53
JW
260 if (which == 0)
261 return 1;
262 else if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
263 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
0207efa2
RK
264 == FUNCTION_DECL))
265 {
266 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
0c4c16df
JH
267 int flags = special_function_p (fndecl, 0);
268 if (flags & ECF_MAY_BE_ALLOCA)
0207efa2
RK
269 return 1;
270 }
51bbfa0c
RS
271
272 /* Third operand is RTL. */
273 length = 2;
274 break;
275
276 case SAVE_EXPR:
277 if (SAVE_EXPR_RTL (exp) != 0)
278 return 0;
1c8d7aef
RS
279 if (value_member (exp, calls_function_save_exprs))
280 return 0;
281 calls_function_save_exprs = tree_cons (NULL_TREE, exp,
282 calls_function_save_exprs);
283 return (TREE_OPERAND (exp, 0) != 0
284 && calls_function_1 (TREE_OPERAND (exp, 0), which));
51bbfa0c
RS
285
286 case BLOCK:
ef03bc85
CH
287 {
288 register tree local;
289
290 for (local = BLOCK_VARS (exp); local; local = TREE_CHAIN (local))
1ce0cb53 291 if (DECL_INITIAL (local) != 0
1c8d7aef 292 && calls_function_1 (DECL_INITIAL (local), which))
ef03bc85
CH
293 return 1;
294 }
295 {
296 register tree subblock;
297
298 for (subblock = BLOCK_SUBBLOCKS (exp);
299 subblock;
300 subblock = TREE_CHAIN (subblock))
1c8d7aef 301 if (calls_function_1 (subblock, which))
ef03bc85
CH
302 return 1;
303 }
304 return 0;
0c4c16df
JH
305 case TREE_LIST:
306 for (; exp != 0; exp = TREE_CHAIN (exp))
307 if (calls_function_1 (TREE_VALUE (exp), which))
308 return 1;
309 return 0;
51bbfa0c
RS
310
311 case METHOD_CALL_EXPR:
312 length = 3;
313 break;
314
315 case WITH_CLEANUP_EXPR:
316 length = 1;
317 break;
318
319 case RTL_EXPR:
320 return 0;
0c4c16df 321
e9a25f70
JL
322 default:
323 break;
51bbfa0c
RS
324 }
325
0c4c16df
JH
326 /* Only expressions and references can contain calls. */
327 if (type != 'e' && type != '<' && type != '1' && type != '2' && type != 'r'
328 && type != 'b')
329 return 0;
330
51bbfa0c
RS
331 for (i = 0; i < length; i++)
332 if (TREE_OPERAND (exp, i) != 0
1c8d7aef 333 && calls_function_1 (TREE_OPERAND (exp, i), which))
51bbfa0c
RS
334 return 1;
335
336 return 0;
337}
338\f
339/* Force FUNEXP into a form suitable for the address of a CALL,
340 and return that as an rtx. Also load the static chain register
341 if FNDECL is a nested function.
342
77cac2f2
RK
343 CALL_FUSAGE points to a variable holding the prospective
344 CALL_INSN_FUNCTION_USAGE information. */
51bbfa0c 345
03dacb02 346rtx
77cac2f2 347prepare_call_address (funexp, fndecl, call_fusage, reg_parm_seen)
51bbfa0c
RS
348 rtx funexp;
349 tree fndecl;
77cac2f2 350 rtx *call_fusage;
01368078 351 int reg_parm_seen;
51bbfa0c
RS
352{
353 rtx static_chain_value = 0;
354
355 funexp = protect_from_queue (funexp, 0);
356
357 if (fndecl != 0)
0f41302f 358 /* Get possible static chain value for nested function in C. */
51bbfa0c
RS
359 static_chain_value = lookup_static_chain (fndecl);
360
361 /* Make a valid memory address and copy constants thru pseudo-regs,
362 but not for a constant address if -fno-function-cse. */
363 if (GET_CODE (funexp) != SYMBOL_REF)
01368078 364 /* If we are using registers for parameters, force the
e9a25f70
JL
365 function address into a register now. */
366 funexp = ((SMALL_REGISTER_CLASSES && reg_parm_seen)
367 ? force_not_mem (memory_address (FUNCTION_MODE, funexp))
368 : memory_address (FUNCTION_MODE, funexp));
51bbfa0c
RS
369 else
370 {
371#ifndef NO_FUNCTION_CSE
372 if (optimize && ! flag_no_function_cse)
373#ifdef NO_RECURSIVE_FUNCTION_CSE
374 if (fndecl != current_function_decl)
375#endif
376 funexp = force_reg (Pmode, funexp);
377#endif
378 }
379
380 if (static_chain_value != 0)
381 {
382 emit_move_insn (static_chain_rtx, static_chain_value);
383
f991a240
RK
384 if (GET_CODE (static_chain_rtx) == REG)
385 use_reg (call_fusage, static_chain_rtx);
51bbfa0c
RS
386 }
387
388 return funexp;
389}
390
391/* Generate instructions to call function FUNEXP,
392 and optionally pop the results.
393 The CALL_INSN is the first insn generated.
394
607ea900 395 FNDECL is the declaration node of the function. This is given to the
2c8da025
RK
396 macro RETURN_POPS_ARGS to determine whether this function pops its own args.
397
334c4f0f
RK
398 FUNTYPE is the data type of the function. This is given to the macro
399 RETURN_POPS_ARGS to determine whether this function pops its own args.
400 We used to allow an identifier for library functions, but that doesn't
401 work when the return type is an aggregate type and the calling convention
402 says that the pointer to this aggregate is to be popped by the callee.
51bbfa0c
RS
403
404 STACK_SIZE is the number of bytes of arguments on the stack,
c2732da3
JM
405 ROUNDED_STACK_SIZE is that number rounded up to
406 PREFERRED_STACK_BOUNDARY; zero if the size is variable. This is
407 both to put into the call insn and to generate explicit popping
408 code if necessary.
51bbfa0c
RS
409
410 STRUCT_VALUE_SIZE is the number of bytes wanted in a structure value.
411 It is zero if this call doesn't want a structure value.
412
413 NEXT_ARG_REG is the rtx that results from executing
414 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1)
415 just after all the args have had their registers assigned.
416 This could be whatever you like, but normally it is the first
417 arg-register beyond those used for args in this call,
418 or 0 if all the arg-registers are used in this call.
419 It is passed on to `gen_call' so you can put this info in the call insn.
420
421 VALREG is a hard register in which a value is returned,
422 or 0 if the call does not return a value.
423
424 OLD_INHIBIT_DEFER_POP is the value that `inhibit_defer_pop' had before
425 the args to this call were processed.
426 We restore `inhibit_defer_pop' to that value.
427
94b25f81 428 CALL_FUSAGE is either empty or an EXPR_LIST of USE expressions that
f2d33f13 429 denote registers used by the called function. */
51bbfa0c 430
322e3e34 431static void
fb5eebb9
RH
432emit_call_1 (funexp, fndecl, funtype, stack_size, rounded_stack_size,
433 struct_value_size, next_arg_reg, valreg, old_inhibit_defer_pop,
0a1c58a2 434 call_fusage, ecf_flags)
51bbfa0c 435 rtx funexp;
c84e2712
KG
436 tree fndecl ATTRIBUTE_UNUSED;
437 tree funtype ATTRIBUTE_UNUSED;
6a651371 438 HOST_WIDE_INT stack_size ATTRIBUTE_UNUSED;
fb5eebb9 439 HOST_WIDE_INT rounded_stack_size;
962f1324 440 HOST_WIDE_INT struct_value_size ATTRIBUTE_UNUSED;
51bbfa0c
RS
441 rtx next_arg_reg;
442 rtx valreg;
443 int old_inhibit_defer_pop;
77cac2f2 444 rtx call_fusage;
0a1c58a2 445 int ecf_flags;
51bbfa0c 446{
062e7fd8 447 rtx rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
57bed152 448#if defined (HAVE_call) && defined (HAVE_call_value)
e5d70561 449 rtx struct_value_size_rtx = GEN_INT (struct_value_size);
57bed152 450#endif
51bbfa0c
RS
451 rtx call_insn;
452 int already_popped = 0;
fb5eebb9 453 HOST_WIDE_INT n_popped = RETURN_POPS_ARGS (fndecl, funtype, stack_size);
51bbfa0c
RS
454
455 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
456 and we don't want to load it into a register as an optimization,
457 because prepare_call_address already did it if it should be done. */
458 if (GET_CODE (funexp) != SYMBOL_REF)
459 funexp = memory_address (FUNCTION_MODE, funexp);
460
0a1c58a2
JL
461#if defined (HAVE_sibcall_pop) && defined (HAVE_sibcall_value_pop)
462 if ((ecf_flags & ECF_SIBCALL)
463 && HAVE_sibcall_pop && HAVE_sibcall_value_pop
464 && (RETURN_POPS_ARGS (fndecl, funtype, stack_size) > 0
465 || stack_size == 0))
466 {
467 rtx n_pop = GEN_INT (RETURN_POPS_ARGS (fndecl, funtype, stack_size));
468 rtx pat;
469
470 /* If this subroutine pops its own args, record that in the call insn
471 if possible, for the sake of frame pointer elimination. */
472
473 if (valreg)
474 pat = gen_sibcall_value_pop (valreg,
475 gen_rtx_MEM (FUNCTION_MODE, funexp),
476 rounded_stack_size_rtx, next_arg_reg,
477 n_pop);
478 else
479 pat = gen_sibcall_pop (gen_rtx_MEM (FUNCTION_MODE, funexp),
480 rounded_stack_size_rtx, next_arg_reg, n_pop);
481
482 emit_call_insn (pat);
483 already_popped = 1;
484 }
485 else
486#endif
487
51bbfa0c 488#if defined (HAVE_call_pop) && defined (HAVE_call_value_pop)
8bcafee3
JDA
489/* If the target has "call" or "call_value" insns, then prefer them
490 if no arguments are actually popped. If the target does not have
491 "call" or "call_value" insns, then we must use the popping versions
492 even if the call has no arguments to pop. */
493#if defined (HAVE_call) && defined (HAVE_call_value)
494 if (HAVE_call && HAVE_call_value && HAVE_call_pop && HAVE_call_value_pop
f73ad30e 495 && n_popped > 0)
8bcafee3
JDA
496#else
497 if (HAVE_call_pop && HAVE_call_value_pop)
498#endif
51bbfa0c 499 {
fb5eebb9 500 rtx n_pop = GEN_INT (n_popped);
51bbfa0c
RS
501 rtx pat;
502
503 /* If this subroutine pops its own args, record that in the call insn
504 if possible, for the sake of frame pointer elimination. */
2c8da025 505
51bbfa0c
RS
506 if (valreg)
507 pat = gen_call_value_pop (valreg,
38a448ca 508 gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 509 rounded_stack_size_rtx, next_arg_reg, n_pop);
51bbfa0c 510 else
38a448ca 511 pat = gen_call_pop (gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 512 rounded_stack_size_rtx, next_arg_reg, n_pop);
51bbfa0c
RS
513
514 emit_call_insn (pat);
515 already_popped = 1;
516 }
517 else
518#endif
51bbfa0c 519
0a1c58a2
JL
520#if defined (HAVE_sibcall) && defined (HAVE_sibcall_value)
521 if ((ecf_flags & ECF_SIBCALL)
522 && HAVE_sibcall && HAVE_sibcall_value)
523 {
524 if (valreg)
525 emit_call_insn (gen_sibcall_value (valreg,
526 gen_rtx_MEM (FUNCTION_MODE, funexp),
527 rounded_stack_size_rtx,
528 next_arg_reg, NULL_RTX));
529 else
530 emit_call_insn (gen_sibcall (gen_rtx_MEM (FUNCTION_MODE, funexp),
531 rounded_stack_size_rtx, next_arg_reg,
532 struct_value_size_rtx));
533 }
534 else
535#endif
536
51bbfa0c
RS
537#if defined (HAVE_call) && defined (HAVE_call_value)
538 if (HAVE_call && HAVE_call_value)
539 {
540 if (valreg)
541 emit_call_insn (gen_call_value (valreg,
38a448ca 542 gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 543 rounded_stack_size_rtx, next_arg_reg,
e992302c 544 NULL_RTX));
51bbfa0c 545 else
38a448ca 546 emit_call_insn (gen_call (gen_rtx_MEM (FUNCTION_MODE, funexp),
062e7fd8 547 rounded_stack_size_rtx, next_arg_reg,
51bbfa0c
RS
548 struct_value_size_rtx));
549 }
550 else
551#endif
552 abort ();
553
77cac2f2 554 /* Find the CALL insn we just emitted. */
51bbfa0c
RS
555 for (call_insn = get_last_insn ();
556 call_insn && GET_CODE (call_insn) != CALL_INSN;
557 call_insn = PREV_INSN (call_insn))
558 ;
559
560 if (! call_insn)
561 abort ();
562
2a8f6b90
JH
563 /* Mark memory as used for "pure" function call. */
564 if (ecf_flags & ECF_PURE)
565 {
566 call_fusage = gen_rtx_EXPR_LIST (VOIDmode,
567 gen_rtx_USE (VOIDmode,
568 gen_rtx_MEM (BLKmode,
569 gen_rtx_SCRATCH (VOIDmode))), call_fusage);
570 }
571
e59e60a7
RK
572 /* Put the register usage information on the CALL. If there is already
573 some usage information, put ours at the end. */
574 if (CALL_INSN_FUNCTION_USAGE (call_insn))
575 {
576 rtx link;
577
578 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
579 link = XEXP (link, 1))
580 ;
581
582 XEXP (link, 1) = call_fusage;
583 }
584 else
585 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
51bbfa0c
RS
586
587 /* If this is a const call, then set the insn's unchanging bit. */
2a8f6b90 588 if (ecf_flags & (ECF_CONST | ECF_PURE))
51bbfa0c
RS
589 CONST_CALL_P (call_insn) = 1;
590
12a22e76
JM
591 /* If this call can't throw, attach a REG_EH_REGION reg note to that
592 effect. */
0a1c58a2 593 if (ecf_flags & ECF_NOTHROW)
54cea123 594 REG_NOTES (call_insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, const0_rtx,
12a22e76
JM
595 REG_NOTES (call_insn));
596
0a1c58a2
JL
597 SIBLING_CALL_P (call_insn) = ((ecf_flags & ECF_SIBCALL) != 0);
598
b1e64e0d
RS
599 /* Restore this now, so that we do defer pops for this call's args
600 if the context of the call as a whole permits. */
601 inhibit_defer_pop = old_inhibit_defer_pop;
602
fb5eebb9 603 if (n_popped > 0)
51bbfa0c
RS
604 {
605 if (!already_popped)
e3da301d 606 CALL_INSN_FUNCTION_USAGE (call_insn)
38a448ca
RH
607 = gen_rtx_EXPR_LIST (VOIDmode,
608 gen_rtx_CLOBBER (VOIDmode, stack_pointer_rtx),
609 CALL_INSN_FUNCTION_USAGE (call_insn));
fb5eebb9 610 rounded_stack_size -= n_popped;
062e7fd8 611 rounded_stack_size_rtx = GEN_INT (rounded_stack_size);
1503a7ec 612 stack_pointer_delta -= n_popped;
51bbfa0c
RS
613 }
614
f73ad30e 615 if (!ACCUMULATE_OUTGOING_ARGS)
51bbfa0c 616 {
f73ad30e
JH
617 /* If returning from the subroutine does not automatically pop the args,
618 we need an instruction to pop them sooner or later.
619 Perhaps do it now; perhaps just record how much space to pop later.
620
621 If returning from the subroutine does pop the args, indicate that the
622 stack pointer will be changed. */
623
f73ad30e
JH
624 if (rounded_stack_size != 0)
625 {
626 if (flag_defer_pop && inhibit_defer_pop == 0
2a8f6b90 627 && !(ecf_flags & (ECF_CONST | ECF_PURE)))
f73ad30e
JH
628 pending_stack_adjust += rounded_stack_size;
629 else
630 adjust_stack (rounded_stack_size_rtx);
631 }
51bbfa0c 632 }
f73ad30e
JH
633 /* When we accumulate outgoing args, we must avoid any stack manipulations.
634 Restore the stack pointer to its original value now. Usually
635 ACCUMULATE_OUTGOING_ARGS targets don't get here, but there are exceptions.
636 On i386 ACCUMULATE_OUTGOING_ARGS can be enabled on demand, and
637 popping variants of functions exist as well.
638
639 ??? We may optimize similar to defer_pop above, but it is
640 probably not worthwhile.
641
642 ??? It will be worthwhile to enable combine_stack_adjustments even for
643 such machines. */
644 else if (n_popped)
645 anti_adjust_stack (GEN_INT (n_popped));
51bbfa0c
RS
646}
647
20efdf74
JL
648/* Determine if the function identified by NAME and FNDECL is one with
649 special properties we wish to know about.
650
651 For example, if the function might return more than one time (setjmp), then
652 set RETURNS_TWICE to a nonzero value.
653
f2d33f13 654 Similarly set LONGJMP for if the function is in the longjmp family.
20efdf74 655
f2d33f13 656 Set MALLOC for any of the standard memory allocation functions which
20efdf74
JL
657 allocate from the heap.
658
659 Set MAY_BE_ALLOCA for any memory allocation function that might allocate
660 space from the stack such as alloca. */
661
f2d33f13
JH
662static int
663special_function_p (fndecl, flags)
20efdf74 664 tree fndecl;
f2d33f13 665 int flags;
20efdf74 666{
f2d33f13 667 if (! (flags & ECF_MALLOC)
3a8c995b 668 && fndecl && DECL_NAME (fndecl)
140592a0 669 && IDENTIFIER_LENGTH (DECL_NAME (fndecl)) <= 17
20efdf74
JL
670 /* Exclude functions not at the file scope, or not `extern',
671 since they are not the magic functions we would otherwise
672 think they are. */
673 && DECL_CONTEXT (fndecl) == NULL_TREE && TREE_PUBLIC (fndecl))
674 {
3a8c995b 675 char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
20efdf74
JL
676 char *tname = name;
677
ca54603f
JL
678 /* We assume that alloca will always be called by name. It
679 makes no sense to pass it as a pointer-to-function to
680 anything that does not understand its behavior. */
f2d33f13
JH
681 if (((IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 6
682 && name[0] == 'a'
683 && ! strcmp (name, "alloca"))
684 || (IDENTIFIER_LENGTH (DECL_NAME (fndecl)) == 16
685 && name[0] == '_'
686 && ! strcmp (name, "__builtin_alloca"))))
687 flags |= ECF_MAY_BE_ALLOCA;
ca54603f 688
20efdf74
JL
689 /* Disregard prefix _, __ or __x. */
690 if (name[0] == '_')
691 {
692 if (name[1] == '_' && name[2] == 'x')
693 tname += 3;
694 else if (name[1] == '_')
695 tname += 2;
696 else
697 tname += 1;
698 }
699
700 if (tname[0] == 's')
701 {
f2d33f13
JH
702 if ((tname[1] == 'e'
703 && (! strcmp (tname, "setjmp")
704 || ! strcmp (tname, "setjmp_syscall")))
705 || (tname[1] == 'i'
706 && ! strcmp (tname, "sigsetjmp"))
707 || (tname[1] == 'a'
708 && ! strcmp (tname, "savectx")))
709 flags |= ECF_RETURNS_TWICE;
710
20efdf74
JL
711 if (tname[1] == 'i'
712 && ! strcmp (tname, "siglongjmp"))
f2d33f13 713 flags |= ECF_LONGJMP;
20efdf74
JL
714 }
715 else if ((tname[0] == 'q' && tname[1] == 's'
716 && ! strcmp (tname, "qsetjmp"))
717 || (tname[0] == 'v' && tname[1] == 'f'
718 && ! strcmp (tname, "vfork")))
f2d33f13 719 flags |= ECF_RETURNS_TWICE;
20efdf74
JL
720
721 else if (tname[0] == 'l' && tname[1] == 'o'
722 && ! strcmp (tname, "longjmp"))
f2d33f13 723 flags |= ECF_LONGJMP;
fa76d9e0
JR
724
725 else if ((tname[0] == 'f' && tname[1] == 'o'
726 && ! strcmp (tname, "fork"))
727 /* Linux specific: __clone. check NAME to insist on the
728 leading underscores, to avoid polluting the ISO / POSIX
729 namespace. */
730 || (name[0] == '_' && name[1] == '_'
731 && ! strcmp (tname, "clone"))
732 || (tname[0] == 'e' && tname[1] == 'x' && tname[2] == 'e'
733 && tname[3] == 'c' && (tname[4] == 'l' || tname[4] == 'v')
734 && (tname[5] == '\0'
735 || ((tname[5] == 'p' || tname[5] == 'e')
736 && tname[6] == '\0'))))
f2d33f13 737 flags |= ECF_FORK_OR_EXEC;
fa76d9e0 738
140592a0 739 /* Do not add any more malloc-like functions to this list,
82514696
KG
740 instead mark them as malloc functions using the malloc attribute.
741 Note, realloc is not suitable for attribute malloc since
1e5a1107
JM
742 it may return the same address across multiple calls.
743 C++ operator new is not suitable because it is not required
744 to return a unique pointer; indeed, the standard placement new
745 just returns its argument. */
91d024d5
ML
746 else if (TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl))) == Pmode
747 && (! strcmp (tname, "malloc")
748 || ! strcmp (tname, "calloc")
749 || ! strcmp (tname, "strdup")))
f2d33f13 750 flags |= ECF_MALLOC;
20efdf74 751 }
f2d33f13 752 return flags;
20efdf74
JL
753}
754
f2d33f13
JH
755/* Return nonzero when tree represent call to longjmp. */
756int
757setjmp_call_p (fndecl)
758 tree fndecl;
759{
760 return special_function_p (fndecl, 0) & ECF_RETURNS_TWICE;
761}
762
763/* Detect flags (function attributes) from the function type node. */
764static int
765flags_from_decl_or_type (exp)
766 tree exp;
767{
768 int flags = 0;
769 /* ??? We can't set IS_MALLOC for function types? */
770 if (DECL_P (exp))
771 {
772 /* The function exp may have the `malloc' attribute. */
773 if (DECL_P (exp) && DECL_IS_MALLOC (exp))
774 flags |= ECF_MALLOC;
775
2a8f6b90
JH
776 /* The function exp may have the `pure' attribute. */
777 if (DECL_P (exp) && DECL_IS_PURE (exp))
778 flags |= ECF_PURE;
779
f2d33f13
JH
780 if (TREE_NOTHROW (exp))
781 flags |= ECF_NOTHROW;
782 }
783
784 if (TREE_READONLY (exp) && !TREE_THIS_VOLATILE (exp))
785 flags |= ECF_CONST;
786
787 if (TREE_THIS_VOLATILE (exp))
788 flags |= ECF_NORETURN;
789
790 return flags;
791}
792
793
20efdf74
JL
794/* Precompute all register parameters as described by ARGS, storing values
795 into fields within the ARGS array.
796
797 NUM_ACTUALS indicates the total number elements in the ARGS array.
798
799 Set REG_PARM_SEEN if we encounter a register parameter. */
800
801static void
802precompute_register_parameters (num_actuals, args, reg_parm_seen)
803 int num_actuals;
804 struct arg_data *args;
805 int *reg_parm_seen;
806{
807 int i;
808
809 *reg_parm_seen = 0;
810
811 for (i = 0; i < num_actuals; i++)
812 if (args[i].reg != 0 && ! args[i].pass_on_stack)
813 {
814 *reg_parm_seen = 1;
815
816 if (args[i].value == 0)
817 {
818 push_temp_slots ();
819 args[i].value = expand_expr (args[i].tree_value, NULL_RTX,
820 VOIDmode, 0);
821 preserve_temp_slots (args[i].value);
822 pop_temp_slots ();
823
824 /* ANSI doesn't require a sequence point here,
825 but PCC has one, so this will avoid some problems. */
826 emit_queue ();
827 }
828
829 /* If we are to promote the function arg to a wider mode,
830 do it now. */
831
832 if (args[i].mode != TYPE_MODE (TREE_TYPE (args[i].tree_value)))
833 args[i].value
834 = convert_modes (args[i].mode,
835 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
836 args[i].value, args[i].unsignedp);
837
838 /* If the value is expensive, and we are inside an appropriately
839 short loop, put the value into a pseudo and then put the pseudo
840 into the hard reg.
841
842 For small register classes, also do this if this call uses
843 register parameters. This is to avoid reload conflicts while
844 loading the parameters registers. */
845
846 if ((! (GET_CODE (args[i].value) == REG
847 || (GET_CODE (args[i].value) == SUBREG
848 && GET_CODE (SUBREG_REG (args[i].value)) == REG)))
849 && args[i].mode != BLKmode
850 && rtx_cost (args[i].value, SET) > 2
851 && ((SMALL_REGISTER_CLASSES && *reg_parm_seen)
852 || preserve_subexpressions_p ()))
853 args[i].value = copy_to_mode_reg (args[i].mode, args[i].value);
854 }
855}
856
f73ad30e 857#ifdef REG_PARM_STACK_SPACE
20efdf74
JL
858
859 /* The argument list is the property of the called routine and it
860 may clobber it. If the fixed area has been used for previous
861 parameters, we must save and restore it. */
862static rtx
863save_fixed_argument_area (reg_parm_stack_space, argblock,
864 low_to_save, high_to_save)
865 int reg_parm_stack_space;
866 rtx argblock;
867 int *low_to_save;
868 int *high_to_save;
869{
870 int i;
871 rtx save_area = NULL_RTX;
872
873 /* Compute the boundary of the that needs to be saved, if any. */
874#ifdef ARGS_GROW_DOWNWARD
875 for (i = 0; i < reg_parm_stack_space + 1; i++)
876#else
877 for (i = 0; i < reg_parm_stack_space; i++)
878#endif
879 {
880 if (i >= highest_outgoing_arg_in_use
881 || stack_usage_map[i] == 0)
882 continue;
883
884 if (*low_to_save == -1)
885 *low_to_save = i;
886
887 *high_to_save = i;
888 }
889
890 if (*low_to_save >= 0)
891 {
892 int num_to_save = *high_to_save - *low_to_save + 1;
893 enum machine_mode save_mode
894 = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
895 rtx stack_area;
896
897 /* If we don't have the required alignment, must do this in BLKmode. */
898 if ((*low_to_save & (MIN (GET_MODE_SIZE (save_mode),
899 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
900 save_mode = BLKmode;
901
902#ifdef ARGS_GROW_DOWNWARD
903 stack_area = gen_rtx_MEM (save_mode,
904 memory_address (save_mode,
905 plus_constant (argblock,
906 - *high_to_save)));
907#else
908 stack_area = gen_rtx_MEM (save_mode,
909 memory_address (save_mode,
910 plus_constant (argblock,
911 *low_to_save)));
912#endif
913 if (save_mode == BLKmode)
914 {
915 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
19caa751
RK
916 /* Cannot use emit_block_move here because it can be done by a
917 library call which in turn gets into this place again and deadly
918 infinite recursion happens. */
04572513 919 move_by_pieces (validize_mem (save_area), stack_area, num_to_save,
19caa751 920 PARM_BOUNDARY);
20efdf74
JL
921 }
922 else
923 {
924 save_area = gen_reg_rtx (save_mode);
925 emit_move_insn (save_area, stack_area);
926 }
927 }
928 return save_area;
929}
930
931static void
932restore_fixed_argument_area (save_area, argblock, high_to_save, low_to_save)
933 rtx save_area;
934 rtx argblock;
935 int high_to_save;
936 int low_to_save;
937{
938 enum machine_mode save_mode = GET_MODE (save_area);
939#ifdef ARGS_GROW_DOWNWARD
940 rtx stack_area
941 = gen_rtx_MEM (save_mode,
942 memory_address (save_mode,
943 plus_constant (argblock,
944 - high_to_save)));
945#else
946 rtx stack_area
947 = gen_rtx_MEM (save_mode,
948 memory_address (save_mode,
949 plus_constant (argblock,
950 low_to_save)));
951#endif
952
953 if (save_mode != BLKmode)
954 emit_move_insn (stack_area, save_area);
955 else
04572513
JJ
956 /* Cannot use emit_block_move here because it can be done by a library
957 call which in turn gets into this place again and deadly infinite
958 recursion happens. */
959 move_by_pieces (stack_area, validize_mem (save_area),
19caa751 960 high_to_save - low_to_save + 1, PARM_BOUNDARY);
20efdf74
JL
961}
962#endif
963
964/* If any elements in ARGS refer to parameters that are to be passed in
965 registers, but not in memory, and whose alignment does not permit a
966 direct copy into registers. Copy the values into a group of pseudos
8e6a59fe
MM
967 which we will later copy into the appropriate hard registers.
968
969 Pseudos for each unaligned argument will be stored into the array
970 args[argnum].aligned_regs. The caller is responsible for deallocating
971 the aligned_regs array if it is nonzero. */
972
20efdf74
JL
973static void
974store_unaligned_arguments_into_pseudos (args, num_actuals)
975 struct arg_data *args;
976 int num_actuals;
977{
978 int i, j;
979
980 for (i = 0; i < num_actuals; i++)
981 if (args[i].reg != 0 && ! args[i].pass_on_stack
982 && args[i].mode == BLKmode
983 && (TYPE_ALIGN (TREE_TYPE (args[i].tree_value))
984 < (unsigned int) MIN (BIGGEST_ALIGNMENT, BITS_PER_WORD)))
985 {
986 int bytes = int_size_in_bytes (TREE_TYPE (args[i].tree_value));
987 int big_endian_correction = 0;
988
989 args[i].n_aligned_regs
990 = args[i].partial ? args[i].partial
991 : (bytes + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
992
8e6a59fe
MM
993 args[i].aligned_regs = (rtx *) xmalloc (sizeof (rtx)
994 * args[i].n_aligned_regs);
20efdf74
JL
995
996 /* Structures smaller than a word are aligned to the least
997 significant byte (to the right). On a BYTES_BIG_ENDIAN machine,
998 this means we must skip the empty high order bytes when
999 calculating the bit offset. */
1000 if (BYTES_BIG_ENDIAN && bytes < UNITS_PER_WORD)
1001 big_endian_correction = (BITS_PER_WORD - (bytes * BITS_PER_UNIT));
1002
1003 for (j = 0; j < args[i].n_aligned_regs; j++)
1004 {
1005 rtx reg = gen_reg_rtx (word_mode);
1006 rtx word = operand_subword_force (args[i].value, j, BLKmode);
1007 int bitsize = MIN (bytes * BITS_PER_UNIT, BITS_PER_WORD);
1008 int bitalign = TYPE_ALIGN (TREE_TYPE (args[i].tree_value));
1009
1010 args[i].aligned_regs[j] = reg;
1011
1012 /* There is no need to restrict this code to loading items
1013 in TYPE_ALIGN sized hunks. The bitfield instructions can
1014 load up entire word sized registers efficiently.
1015
1016 ??? This may not be needed anymore.
1017 We use to emit a clobber here but that doesn't let later
1018 passes optimize the instructions we emit. By storing 0 into
1019 the register later passes know the first AND to zero out the
1020 bitfield being set in the register is unnecessary. The store
1021 of 0 will be deleted as will at least the first AND. */
1022
1023 emit_move_insn (reg, const0_rtx);
1024
1025 bytes -= bitsize / BITS_PER_UNIT;
1026 store_bit_field (reg, bitsize, big_endian_correction, word_mode,
19caa751
RK
1027 extract_bit_field (word, bitsize, 0, 1, NULL_RTX,
1028 word_mode, word_mode, bitalign,
20efdf74 1029 BITS_PER_WORD),
19caa751 1030 bitalign, BITS_PER_WORD);
20efdf74
JL
1031 }
1032 }
1033}
1034
d7cdf113
JL
1035/* Fill in ARGS_SIZE and ARGS array based on the parameters found in
1036 ACTPARMS.
1037
1038 NUM_ACTUALS is the total number of parameters.
1039
1040 N_NAMED_ARGS is the total number of named arguments.
1041
1042 FNDECL is the tree code for the target of this call (if known)
1043
1044 ARGS_SO_FAR holds state needed by the target to know where to place
1045 the next argument.
1046
1047 REG_PARM_STACK_SPACE is the number of bytes of stack space reserved
1048 for arguments which are passed in registers.
1049
1050 OLD_STACK_LEVEL is a pointer to an rtx which olds the old stack level
1051 and may be modified by this routine.
1052
f2d33f13 1053 OLD_PENDING_ADJ, MUST_PREALLOCATE and FLAGS are pointers to integer
d7cdf113
JL
1054 flags which may may be modified by this routine. */
1055
1056static void
1057initialize_argument_information (num_actuals, args, args_size, n_named_args,
1058 actparms, fndecl, args_so_far,
1059 reg_parm_stack_space, old_stack_level,
f2d33f13 1060 old_pending_adj, must_preallocate,
7d167afd 1061 ecf_flags)
91813b28 1062 int num_actuals ATTRIBUTE_UNUSED;
d7cdf113
JL
1063 struct arg_data *args;
1064 struct args_size *args_size;
91813b28 1065 int n_named_args ATTRIBUTE_UNUSED;
d7cdf113
JL
1066 tree actparms;
1067 tree fndecl;
959f3a06 1068 CUMULATIVE_ARGS *args_so_far;
d7cdf113
JL
1069 int reg_parm_stack_space;
1070 rtx *old_stack_level;
1071 int *old_pending_adj;
1072 int *must_preallocate;
f2d33f13 1073 int *ecf_flags;
d7cdf113
JL
1074{
1075 /* 1 if scanning parms front to back, -1 if scanning back to front. */
1076 int inc;
1077
1078 /* Count arg position in order args appear. */
1079 int argpos;
1080
4fc026cd 1081 struct args_size alignment_pad;
d7cdf113
JL
1082 int i;
1083 tree p;
1084
1085 args_size->constant = 0;
1086 args_size->var = 0;
1087
1088 /* In this loop, we consider args in the order they are written.
1089 We fill up ARGS from the front or from the back if necessary
1090 so that in any case the first arg to be pushed ends up at the front. */
1091
f73ad30e
JH
1092 if (PUSH_ARGS_REVERSED)
1093 {
1094 i = num_actuals - 1, inc = -1;
1095 /* In this case, must reverse order of args
1096 so that we compute and push the last arg first. */
1097 }
1098 else
1099 {
1100 i = 0, inc = 1;
1101 }
d7cdf113
JL
1102
1103 /* I counts args in order (to be) pushed; ARGPOS counts in order written. */
1104 for (p = actparms, argpos = 0; p; p = TREE_CHAIN (p), i += inc, argpos++)
1105 {
1106 tree type = TREE_TYPE (TREE_VALUE (p));
1107 int unsignedp;
1108 enum machine_mode mode;
1109
1110 args[i].tree_value = TREE_VALUE (p);
1111
1112 /* Replace erroneous argument with constant zero. */
d0f062fb 1113 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
d7cdf113
JL
1114 args[i].tree_value = integer_zero_node, type = integer_type_node;
1115
1116 /* If TYPE is a transparent union, pass things the way we would
1117 pass the first field of the union. We have already verified that
1118 the modes are the same. */
1119 if (TYPE_TRANSPARENT_UNION (type))
1120 type = TREE_TYPE (TYPE_FIELDS (type));
1121
1122 /* Decide where to pass this arg.
1123
1124 args[i].reg is nonzero if all or part is passed in registers.
1125
1126 args[i].partial is nonzero if part but not all is passed in registers,
1127 and the exact value says how many words are passed in registers.
1128
1129 args[i].pass_on_stack is nonzero if the argument must at least be
1130 computed on the stack. It may then be loaded back into registers
1131 if args[i].reg is nonzero.
1132
1133 These decisions are driven by the FUNCTION_... macros and must agree
1134 with those made by function.c. */
1135
1136 /* See if this argument should be passed by invisible reference. */
1137 if ((TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1138 && contains_placeholder_p (TYPE_SIZE (type)))
1139 || TREE_ADDRESSABLE (type)
1140#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
959f3a06 1141 || FUNCTION_ARG_PASS_BY_REFERENCE (*args_so_far, TYPE_MODE (type),
d7cdf113
JL
1142 type, argpos < n_named_args)
1143#endif
1144 )
1145 {
1146 /* If we're compiling a thunk, pass through invisible
1147 references instead of making a copy. */
1148 if (current_function_is_thunk
1149#ifdef FUNCTION_ARG_CALLEE_COPIES
959f3a06 1150 || (FUNCTION_ARG_CALLEE_COPIES (*args_so_far, TYPE_MODE (type),
d7cdf113
JL
1151 type, argpos < n_named_args)
1152 /* If it's in a register, we must make a copy of it too. */
1153 /* ??? Is this a sufficient test? Is there a better one? */
1154 && !(TREE_CODE (args[i].tree_value) == VAR_DECL
1155 && REG_P (DECL_RTL (args[i].tree_value)))
1156 && ! TREE_ADDRESSABLE (type))
1157#endif
1158 )
1159 {
1160 /* C++ uses a TARGET_EXPR to indicate that we want to make a
1161 new object from the argument. If we are passing by
1162 invisible reference, the callee will do that for us, so we
1163 can strip off the TARGET_EXPR. This is not always safe,
1164 but it is safe in the only case where this is a useful
1165 optimization; namely, when the argument is a plain object.
1166 In that case, the frontend is just asking the backend to
1167 make a bitwise copy of the argument. */
1168
1169 if (TREE_CODE (args[i].tree_value) == TARGET_EXPR
2f939d94 1170 && (DECL_P (TREE_OPERAND (args[i].tree_value, 1)))
d7cdf113
JL
1171 && ! REG_P (DECL_RTL (TREE_OPERAND (args[i].tree_value, 1))))
1172 args[i].tree_value = TREE_OPERAND (args[i].tree_value, 1);
1173
1174 args[i].tree_value = build1 (ADDR_EXPR,
1175 build_pointer_type (type),
1176 args[i].tree_value);
1177 type = build_pointer_type (type);
1178 }
1179 else
1180 {
1181 /* We make a copy of the object and pass the address to the
1182 function being called. */
1183 rtx copy;
1184
d0f062fb 1185 if (!COMPLETE_TYPE_P (type)
d7cdf113
JL
1186 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
1187 || (flag_stack_check && ! STACK_CHECK_BUILTIN
05bccae2
RK
1188 && (0 < compare_tree_int (TYPE_SIZE_UNIT (type),
1189 STACK_CHECK_MAX_VAR_SIZE))))
d7cdf113
JL
1190 {
1191 /* This is a variable-sized object. Make space on the stack
1192 for it. */
1193 rtx size_rtx = expr_size (TREE_VALUE (p));
1194
1195 if (*old_stack_level == 0)
1196 {
1197 emit_stack_save (SAVE_BLOCK, old_stack_level, NULL_RTX);
1198 *old_pending_adj = pending_stack_adjust;
1199 pending_stack_adjust = 0;
1200 }
1201
1202 copy = gen_rtx_MEM (BLKmode,
1203 allocate_dynamic_stack_space (size_rtx,
1204 NULL_RTX,
1205 TYPE_ALIGN (type)));
1206 }
1207 else
1208 {
1209 int size = int_size_in_bytes (type);
1210 copy = assign_stack_temp (TYPE_MODE (type), size, 0);
1211 }
1212
1213 MEM_SET_IN_STRUCT_P (copy, AGGREGATE_TYPE_P (type));
1214
1215 store_expr (args[i].tree_value, copy, 0);
2a8f6b90 1216 *ecf_flags &= ~(ECF_CONST | ECF_PURE);
d7cdf113
JL
1217
1218 args[i].tree_value = build1 (ADDR_EXPR,
1219 build_pointer_type (type),
1220 make_tree (type, copy));
1221 type = build_pointer_type (type);
1222 }
1223 }
1224
1225 mode = TYPE_MODE (type);
1226 unsignedp = TREE_UNSIGNED (type);
1227
1228#ifdef PROMOTE_FUNCTION_ARGS
1229 mode = promote_mode (type, mode, &unsignedp, 1);
1230#endif
1231
1232 args[i].unsignedp = unsignedp;
1233 args[i].mode = mode;
7d167afd
JJ
1234
1235#ifdef FUNCTION_INCOMING_ARG
1236 /* If this is a sibling call and the machine has register windows, the
1237 register window has to be unwinded before calling the routine, so
1238 arguments have to go into the incoming registers. */
3ccb603d 1239 if (*ecf_flags & ECF_SIBCALL)
7d167afd
JJ
1240 args[i].reg = FUNCTION_INCOMING_ARG (*args_so_far, mode, type,
1241 argpos < n_named_args);
1242 else
1243#endif
1244 args[i].reg = FUNCTION_ARG (*args_so_far, mode, type,
1245 argpos < n_named_args);
1246
d7cdf113
JL
1247#ifdef FUNCTION_ARG_PARTIAL_NREGS
1248 if (args[i].reg)
1249 args[i].partial
959f3a06 1250 = FUNCTION_ARG_PARTIAL_NREGS (*args_so_far, mode, type,
d7cdf113
JL
1251 argpos < n_named_args);
1252#endif
1253
1254 args[i].pass_on_stack = MUST_PASS_IN_STACK (mode, type);
1255
1256 /* If FUNCTION_ARG returned a (parallel [(expr_list (nil) ...) ...]),
1257 it means that we are to pass this arg in the register(s) designated
1258 by the PARALLEL, but also to pass it in the stack. */
1259 if (args[i].reg && GET_CODE (args[i].reg) == PARALLEL
1260 && XEXP (XVECEXP (args[i].reg, 0, 0), 0) == 0)
1261 args[i].pass_on_stack = 1;
1262
1263 /* If this is an addressable type, we must preallocate the stack
1264 since we must evaluate the object into its final location.
1265
1266 If this is to be passed in both registers and the stack, it is simpler
1267 to preallocate. */
1268 if (TREE_ADDRESSABLE (type)
1269 || (args[i].pass_on_stack && args[i].reg != 0))
1270 *must_preallocate = 1;
1271
1272 /* If this is an addressable type, we cannot pre-evaluate it. Thus,
1273 we cannot consider this function call constant. */
1274 if (TREE_ADDRESSABLE (type))
2a8f6b90 1275 *ecf_flags &= ~(ECF_CONST | ECF_PURE);
d7cdf113
JL
1276
1277 /* Compute the stack-size of this argument. */
1278 if (args[i].reg == 0 || args[i].partial != 0
1279 || reg_parm_stack_space > 0
1280 || args[i].pass_on_stack)
1281 locate_and_pad_parm (mode, type,
1282#ifdef STACK_PARMS_IN_REG_PARM_AREA
1283 1,
1284#else
1285 args[i].reg != 0,
1286#endif
1287 fndecl, args_size, &args[i].offset,
4fc026cd 1288 &args[i].size, &alignment_pad);
d7cdf113
JL
1289
1290#ifndef ARGS_GROW_DOWNWARD
1291 args[i].slot_offset = *args_size;
1292#endif
1293
4fc026cd
CM
1294 args[i].alignment_pad = alignment_pad;
1295
d7cdf113
JL
1296 /* If a part of the arg was put into registers,
1297 don't include that part in the amount pushed. */
1298 if (reg_parm_stack_space == 0 && ! args[i].pass_on_stack)
1299 args[i].size.constant -= ((args[i].partial * UNITS_PER_WORD)
1300 / (PARM_BOUNDARY / BITS_PER_UNIT)
1301 * (PARM_BOUNDARY / BITS_PER_UNIT));
1302
1303 /* Update ARGS_SIZE, the total stack space for args so far. */
1304
1305 args_size->constant += args[i].size.constant;
1306 if (args[i].size.var)
1307 {
1308 ADD_PARM_SIZE (*args_size, args[i].size.var);
1309 }
1310
1311 /* Since the slot offset points to the bottom of the slot,
1312 we must record it after incrementing if the args grow down. */
1313#ifdef ARGS_GROW_DOWNWARD
1314 args[i].slot_offset = *args_size;
1315
1316 args[i].slot_offset.constant = -args_size->constant;
1317 if (args_size->var)
fed3cef0 1318 SUB_PARM_SIZE (args[i].slot_offset, args_size->var);
d7cdf113
JL
1319#endif
1320
1321 /* Increment ARGS_SO_FAR, which has info about which arg-registers
1322 have been used, etc. */
1323
959f3a06 1324 FUNCTION_ARG_ADVANCE (*args_so_far, TYPE_MODE (type), type,
d7cdf113
JL
1325 argpos < n_named_args);
1326 }
1327}
1328
599f37b6
JL
1329/* Update ARGS_SIZE to contain the total size for the argument block.
1330 Return the original constant component of the argument block's size.
1331
1332 REG_PARM_STACK_SPACE holds the number of bytes of stack space reserved
1333 for arguments passed in registers. */
1334
1335static int
c2f8b491
JH
1336compute_argument_block_size (reg_parm_stack_space, args_size,
1337 preferred_stack_boundary)
599f37b6
JL
1338 int reg_parm_stack_space;
1339 struct args_size *args_size;
c2f8b491 1340 int preferred_stack_boundary ATTRIBUTE_UNUSED;
599f37b6
JL
1341{
1342 int unadjusted_args_size = args_size->constant;
1343
f73ad30e
JH
1344 /* For accumulate outgoing args mode we don't need to align, since the frame
1345 will be already aligned. Align to STACK_BOUNDARY in order to prevent
1346 backends from generating missaligned frame sizes. */
1347#ifdef STACK_BOUNDARY
1348 if (ACCUMULATE_OUTGOING_ARGS && preferred_stack_boundary > STACK_BOUNDARY)
1349 preferred_stack_boundary = STACK_BOUNDARY;
1350#endif
1351
599f37b6
JL
1352 /* Compute the actual size of the argument block required. The variable
1353 and constant sizes must be combined, the size may have to be rounded,
1354 and there may be a minimum required size. */
1355
1356 if (args_size->var)
1357 {
1358 args_size->var = ARGS_SIZE_TREE (*args_size);
1359 args_size->constant = 0;
1360
1361#ifdef PREFERRED_STACK_BOUNDARY
c2f8b491
JH
1362 preferred_stack_boundary /= BITS_PER_UNIT;
1363 if (preferred_stack_boundary > 1)
1503a7ec
JH
1364 {
1365 /* We don't handle this case yet. To handle it correctly we have
1366 to add the delta, round and substract the delta.
1367 Currently no machine description requires this support. */
1368 if (stack_pointer_delta & (preferred_stack_boundary - 1))
1369 abort();
1370 args_size->var = round_up (args_size->var, preferred_stack_boundary);
1371 }
599f37b6
JL
1372#endif
1373
1374 if (reg_parm_stack_space > 0)
1375 {
1376 args_size->var
1377 = size_binop (MAX_EXPR, args_size->var,
fed3cef0 1378 ssize_int (reg_parm_stack_space));
599f37b6
JL
1379
1380#ifndef OUTGOING_REG_PARM_STACK_SPACE
1381 /* The area corresponding to register parameters is not to count in
1382 the size of the block we need. So make the adjustment. */
1383 args_size->var
1384 = size_binop (MINUS_EXPR, args_size->var,
fed3cef0 1385 ssize_int (reg_parm_stack_space));
599f37b6
JL
1386#endif
1387 }
1388 }
1389 else
1390 {
1391#ifdef PREFERRED_STACK_BOUNDARY
c2f8b491 1392 preferred_stack_boundary /= BITS_PER_UNIT;
0a1c58a2
JL
1393 if (preferred_stack_boundary < 1)
1394 preferred_stack_boundary = 1;
fb5eebb9 1395 args_size->constant = (((args_size->constant
1503a7ec 1396 + stack_pointer_delta
c2f8b491
JH
1397 + preferred_stack_boundary - 1)
1398 / preferred_stack_boundary
1399 * preferred_stack_boundary)
1503a7ec 1400 - stack_pointer_delta);
599f37b6
JL
1401#endif
1402
1403 args_size->constant = MAX (args_size->constant,
1404 reg_parm_stack_space);
1405
1406#ifdef MAYBE_REG_PARM_STACK_SPACE
1407 if (reg_parm_stack_space == 0)
1408 args_size->constant = 0;
1409#endif
1410
1411#ifndef OUTGOING_REG_PARM_STACK_SPACE
1412 args_size->constant -= reg_parm_stack_space;
1413#endif
1414 }
1415 return unadjusted_args_size;
1416}
1417
19832c77 1418/* Precompute parameters as needed for a function call.
cc0b1adc 1419
f2d33f13 1420 FLAGS is mask of ECF_* constants.
cc0b1adc 1421
cc0b1adc
JL
1422 NUM_ACTUALS is the number of arguments.
1423
1424 ARGS is an array containing information for each argument; this routine
40d6e956
JH
1425 fills in the INITIAL_VALUE and VALUE fields for each precomputed argument.
1426 */
cc0b1adc
JL
1427
1428static void
40d6e956 1429precompute_arguments (flags, num_actuals, args)
f2d33f13 1430 int flags;
cc0b1adc
JL
1431 int num_actuals;
1432 struct arg_data *args;
cc0b1adc
JL
1433{
1434 int i;
1435
1436 /* If this function call is cse'able, precompute all the parameters.
1437 Note that if the parameter is constructed into a temporary, this will
1438 cause an additional copy because the parameter will be constructed
1439 into a temporary location and then copied into the outgoing arguments.
1440 If a parameter contains a call to alloca and this function uses the
1441 stack, precompute the parameter. */
1442
1443 /* If we preallocated the stack space, and some arguments must be passed
1444 on the stack, then we must precompute any parameter which contains a
1445 function call which will store arguments on the stack.
1446 Otherwise, evaluating the parameter may clobber previous parameters
40d6e956
JH
1447 which have already been stored into the stack. (we have code to avoid
1448 such case by saving the ougoing stack arguments, but it results in
1449 worse code) */
cc0b1adc
JL
1450
1451 for (i = 0; i < num_actuals; i++)
2a8f6b90 1452 if ((flags & (ECF_CONST | ECF_PURE))
40d6e956 1453 || calls_function (args[i].tree_value, !ACCUMULATE_OUTGOING_ARGS))
cc0b1adc
JL
1454 {
1455 /* If this is an addressable type, we cannot pre-evaluate it. */
1456 if (TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value)))
1457 abort ();
1458
1459 push_temp_slots ();
1460
47841d1b 1461 args[i].value
cc0b1adc
JL
1462 = expand_expr (args[i].tree_value, NULL_RTX, VOIDmode, 0);
1463
1464 preserve_temp_slots (args[i].value);
1465 pop_temp_slots ();
1466
1467 /* ANSI doesn't require a sequence point here,
1468 but PCC has one, so this will avoid some problems. */
1469 emit_queue ();
1470
1471 args[i].initial_value = args[i].value
47841d1b 1472 = protect_from_queue (args[i].value, 0);
cc0b1adc
JL
1473
1474 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) != args[i].mode)
47841d1b
JJ
1475 {
1476 args[i].value
1477 = convert_modes (args[i].mode,
1478 TYPE_MODE (TREE_TYPE (args[i].tree_value)),
1479 args[i].value, args[i].unsignedp);
1480#ifdef PROMOTE_FOR_CALL_ONLY
1481 /* CSE will replace this only if it contains args[i].value
1482 pseudo, so convert it down to the declared mode using
1483 a SUBREG. */
1484 if (GET_CODE (args[i].value) == REG
1485 && GET_MODE_CLASS (args[i].mode) == MODE_INT)
1486 {
1487 args[i].initial_value
1488 = gen_rtx_SUBREG (TYPE_MODE (TREE_TYPE (args[i].tree_value)),
1489 args[i].value, 0);
1490 SUBREG_PROMOTED_VAR_P (args[i].initial_value) = 1;
1491 SUBREG_PROMOTED_UNSIGNED_P (args[i].initial_value)
1492 = args[i].unsignedp;
1493 }
1494#endif
1495 }
cc0b1adc
JL
1496 }
1497}
1498
0f9b3ea6
JL
1499/* Given the current state of MUST_PREALLOCATE and information about
1500 arguments to a function call in NUM_ACTUALS, ARGS and ARGS_SIZE,
1501 compute and return the final value for MUST_PREALLOCATE. */
1502
1503static int
1504finalize_must_preallocate (must_preallocate, num_actuals, args, args_size)
1505 int must_preallocate;
1506 int num_actuals;
1507 struct arg_data *args;
1508 struct args_size *args_size;
1509{
1510 /* See if we have or want to preallocate stack space.
1511
1512 If we would have to push a partially-in-regs parm
1513 before other stack parms, preallocate stack space instead.
1514
1515 If the size of some parm is not a multiple of the required stack
1516 alignment, we must preallocate.
1517
1518 If the total size of arguments that would otherwise create a copy in
1519 a temporary (such as a CALL) is more than half the total argument list
1520 size, preallocation is faster.
1521
1522 Another reason to preallocate is if we have a machine (like the m88k)
1523 where stack alignment is required to be maintained between every
1524 pair of insns, not just when the call is made. However, we assume here
1525 that such machines either do not have push insns (and hence preallocation
1526 would occur anyway) or the problem is taken care of with
1527 PUSH_ROUNDING. */
1528
1529 if (! must_preallocate)
1530 {
1531 int partial_seen = 0;
1532 int copy_to_evaluate_size = 0;
1533 int i;
1534
1535 for (i = 0; i < num_actuals && ! must_preallocate; i++)
1536 {
1537 if (args[i].partial > 0 && ! args[i].pass_on_stack)
1538 partial_seen = 1;
1539 else if (partial_seen && args[i].reg == 0)
1540 must_preallocate = 1;
1541
1542 if (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1543 && (TREE_CODE (args[i].tree_value) == CALL_EXPR
1544 || TREE_CODE (args[i].tree_value) == TARGET_EXPR
1545 || TREE_CODE (args[i].tree_value) == COND_EXPR
1546 || TREE_ADDRESSABLE (TREE_TYPE (args[i].tree_value))))
1547 copy_to_evaluate_size
1548 += int_size_in_bytes (TREE_TYPE (args[i].tree_value));
1549 }
1550
1551 if (copy_to_evaluate_size * 2 >= args_size->constant
1552 && args_size->constant > 0)
1553 must_preallocate = 1;
1554 }
1555 return must_preallocate;
1556}
599f37b6 1557
a45bdd02
JL
1558/* If we preallocated stack space, compute the address of each argument
1559 and store it into the ARGS array.
1560
1561 We need not ensure it is a valid memory address here; it will be
1562 validized when it is used.
1563
1564 ARGBLOCK is an rtx for the address of the outgoing arguments. */
1565
1566static void
1567compute_argument_addresses (args, argblock, num_actuals)
1568 struct arg_data *args;
1569 rtx argblock;
1570 int num_actuals;
1571{
1572 if (argblock)
1573 {
1574 rtx arg_reg = argblock;
1575 int i, arg_offset = 0;
1576
1577 if (GET_CODE (argblock) == PLUS)
1578 arg_reg = XEXP (argblock, 0), arg_offset = INTVAL (XEXP (argblock, 1));
1579
1580 for (i = 0; i < num_actuals; i++)
1581 {
1582 rtx offset = ARGS_SIZE_RTX (args[i].offset);
1583 rtx slot_offset = ARGS_SIZE_RTX (args[i].slot_offset);
1584 rtx addr;
1585
1586 /* Skip this parm if it will not be passed on the stack. */
1587 if (! args[i].pass_on_stack && args[i].reg != 0)
1588 continue;
1589
1590 if (GET_CODE (offset) == CONST_INT)
1591 addr = plus_constant (arg_reg, INTVAL (offset));
1592 else
1593 addr = gen_rtx_PLUS (Pmode, arg_reg, offset);
1594
1595 addr = plus_constant (addr, arg_offset);
1596 args[i].stack = gen_rtx_MEM (args[i].mode, addr);
1597 MEM_SET_IN_STRUCT_P
1598 (args[i].stack,
1599 AGGREGATE_TYPE_P (TREE_TYPE (args[i].tree_value)));
1600
1601 if (GET_CODE (slot_offset) == CONST_INT)
1602 addr = plus_constant (arg_reg, INTVAL (slot_offset));
1603 else
1604 addr = gen_rtx_PLUS (Pmode, arg_reg, slot_offset);
1605
1606 addr = plus_constant (addr, arg_offset);
1607 args[i].stack_slot = gen_rtx_MEM (args[i].mode, addr);
1608 }
1609 }
1610}
1611
1612/* Given a FNDECL and EXP, return an rtx suitable for use as a target address
1613 in a call instruction.
1614
1615 FNDECL is the tree node for the target function. For an indirect call
1616 FNDECL will be NULL_TREE.
1617
1618 EXP is the CALL_EXPR for this call. */
1619
1620static rtx
1621rtx_for_function_call (fndecl, exp)
1622 tree fndecl;
1623 tree exp;
1624{
1625 rtx funexp;
1626
1627 /* Get the function to call, in the form of RTL. */
1628 if (fndecl)
1629 {
1630 /* If this is the first use of the function, see if we need to
1631 make an external definition for it. */
1632 if (! TREE_USED (fndecl))
1633 {
1634 assemble_external (fndecl);
1635 TREE_USED (fndecl) = 1;
1636 }
1637
1638 /* Get a SYMBOL_REF rtx for the function address. */
1639 funexp = XEXP (DECL_RTL (fndecl), 0);
1640 }
1641 else
1642 /* Generate an rtx (probably a pseudo-register) for the address. */
1643 {
91ab1046 1644 rtx funaddr;
a45bdd02 1645 push_temp_slots ();
91ab1046
DT
1646 funaddr = funexp =
1647 expand_expr (TREE_OPERAND (exp, 0), NULL_RTX, VOIDmode, 0);
a45bdd02
JL
1648 pop_temp_slots (); /* FUNEXP can't be BLKmode */
1649
1650 /* Check the function is executable. */
1651 if (current_function_check_memory_usage)
91ab1046
DT
1652 {
1653#ifdef POINTERS_EXTEND_UNSIGNED
1654 /* It might be OK to convert funexp in place, but there's
1655 a lot going on between here and when it happens naturally
1656 that this seems safer. */
1657 funaddr = convert_memory_address (Pmode, funexp);
1658#endif
1659 emit_library_call (chkr_check_exec_libfunc, 1,
1660 VOIDmode, 1,
1661 funaddr, Pmode);
1662 }
a45bdd02
JL
1663 emit_queue ();
1664 }
1665 return funexp;
1666}
1667
21a3b983
JL
1668/* Do the register loads required for any wholly-register parms or any
1669 parms which are passed both on the stack and in a register. Their
1670 expressions were already evaluated.
1671
1672 Mark all register-parms as living through the call, putting these USE
1673 insns in the CALL_INSN_FUNCTION_USAGE field. */
1674
1675static void
1676load_register_parameters (args, num_actuals, call_fusage)
1677 struct arg_data *args;
1678 int num_actuals;
1679 rtx *call_fusage;
1680{
1681 int i, j;
1682
1683#ifdef LOAD_ARGS_REVERSED
1684 for (i = num_actuals - 1; i >= 0; i--)
1685#else
1686 for (i = 0; i < num_actuals; i++)
1687#endif
1688 {
1689 rtx reg = args[i].reg;
1690 int partial = args[i].partial;
1691 int nregs;
1692
1693 if (reg)
1694 {
1695 /* Set to non-negative if must move a word at a time, even if just
1696 one word (e.g, partial == 1 && mode == DFmode). Set to -1 if
1697 we just use a normal move insn. This value can be zero if the
1698 argument is a zero size structure with no fields. */
1699 nregs = (partial ? partial
1700 : (TYPE_MODE (TREE_TYPE (args[i].tree_value)) == BLKmode
1701 ? ((int_size_in_bytes (TREE_TYPE (args[i].tree_value))
1702 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
1703 : -1));
1704
1705 /* Handle calls that pass values in multiple non-contiguous
1706 locations. The Irix 6 ABI has examples of this. */
1707
1708 if (GET_CODE (reg) == PARALLEL)
19caa751
RK
1709 emit_group_load (reg, args[i].value,
1710 int_size_in_bytes (TREE_TYPE (args[i].tree_value)),
1711 TYPE_ALIGN (TREE_TYPE (args[i].tree_value)));
21a3b983
JL
1712
1713 /* If simple case, just do move. If normal partial, store_one_arg
1714 has already loaded the register for us. In all other cases,
1715 load the register(s) from memory. */
1716
1717 else if (nregs == -1)
1718 emit_move_insn (reg, args[i].value);
1719
1720 /* If we have pre-computed the values to put in the registers in
1721 the case of non-aligned structures, copy them in now. */
1722
1723 else if (args[i].n_aligned_regs != 0)
1724 for (j = 0; j < args[i].n_aligned_regs; j++)
1725 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg) + j),
1726 args[i].aligned_regs[j]);
1727
1728 else if (partial == 0 || args[i].pass_on_stack)
1729 move_block_to_reg (REGNO (reg),
1730 validize_mem (args[i].value), nregs,
1731 args[i].mode);
1732
1733 /* Handle calls that pass values in multiple non-contiguous
1734 locations. The Irix 6 ABI has examples of this. */
1735 if (GET_CODE (reg) == PARALLEL)
1736 use_group_regs (call_fusage, reg);
1737 else if (nregs == -1)
1738 use_reg (call_fusage, reg);
1739 else
1740 use_regs (call_fusage, REGNO (reg), nregs == 0 ? 1 : nregs);
1741 }
1742 }
1743}
1744
f2d33f13
JH
1745/* Try to integreate function. See expand_inline_function for documentation
1746 about the parameters. */
1747
1748static rtx
1749try_to_integrate (fndecl, actparms, target, ignore, type, structure_value_addr)
1750 tree fndecl;
1751 tree actparms;
1752 rtx target;
1753 int ignore;
1754 tree type;
1755 rtx structure_value_addr;
1756{
1757 rtx temp;
1758 rtx before_call;
1759 int i;
1760 rtx old_stack_level = 0;
7657ad0a 1761 int reg_parm_stack_space = 0;
f2d33f13
JH
1762
1763#ifdef REG_PARM_STACK_SPACE
1764#ifdef MAYBE_REG_PARM_STACK_SPACE
1765 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
1766#else
1767 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
1768#endif
1769#endif
1770
1771 before_call = get_last_insn ();
1772
1773 temp = expand_inline_function (fndecl, actparms, target,
1774 ignore, type,
1775 structure_value_addr);
1776
1777 /* If inlining succeeded, return. */
1778 if (temp != (rtx) (HOST_WIDE_INT) - 1)
1779 {
1780 if (ACCUMULATE_OUTGOING_ARGS)
1781 {
1782 /* If the outgoing argument list must be preserved, push
1783 the stack before executing the inlined function if it
1784 makes any calls. */
1785
1786 for (i = reg_parm_stack_space - 1; i >= 0; i--)
1787 if (i < highest_outgoing_arg_in_use && stack_usage_map[i] != 0)
1788 break;
1789
1790 if (stack_arg_under_construction || i >= 0)
1791 {
1792 rtx first_insn
1793 = before_call ? NEXT_INSN (before_call) : get_insns ();
1794 rtx insn = NULL_RTX, seq;
1795
1796 /* Look for a call in the inline function code.
1797 If DECL_SAVED_INSNS (fndecl)->outgoing_args_size is
1798 nonzero then there is a call and it is not necessary
1799 to scan the insns. */
1800
1801 if (DECL_SAVED_INSNS (fndecl)->outgoing_args_size == 0)
1802 for (insn = first_insn; insn; insn = NEXT_INSN (insn))
1803 if (GET_CODE (insn) == CALL_INSN)
1804 break;
1805
1806 if (insn)
1807 {
1808 /* Reserve enough stack space so that the largest
1809 argument list of any function call in the inline
1810 function does not overlap the argument list being
1811 evaluated. This is usually an overestimate because
1812 allocate_dynamic_stack_space reserves space for an
1813 outgoing argument list in addition to the requested
1814 space, but there is no way to ask for stack space such
1815 that an argument list of a certain length can be
1816 safely constructed.
1817
1818 Add the stack space reserved for register arguments, if
1819 any, in the inline function. What is really needed is the
1820 largest value of reg_parm_stack_space in the inline
1821 function, but that is not available. Using the current
1822 value of reg_parm_stack_space is wrong, but gives
1823 correct results on all supported machines. */
1824
1825 int adjust = (DECL_SAVED_INSNS (fndecl)->outgoing_args_size
1826 + reg_parm_stack_space);
1827
1828 start_sequence ();
1829 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
1830 allocate_dynamic_stack_space (GEN_INT (adjust),
1831 NULL_RTX, BITS_PER_UNIT);
1832 seq = get_insns ();
1833 end_sequence ();
1834 emit_insns_before (seq, first_insn);
1835 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1836 }
1837 }
1838 }
1839
1840 /* If the result is equivalent to TARGET, return TARGET to simplify
1841 checks in store_expr. They can be equivalent but not equal in the
1842 case of a function that returns BLKmode. */
1843 if (temp != target && rtx_equal_p (temp, target))
1844 return target;
1845 return temp;
1846 }
1847
1848 /* If inlining failed, mark FNDECL as needing to be compiled
1849 separately after all. If function was declared inline,
1850 give a warning. */
1851 if (DECL_INLINE (fndecl) && warn_inline && !flag_no_inline
1852 && optimize > 0 && !TREE_ADDRESSABLE (fndecl))
1853 {
1854 warning_with_decl (fndecl, "inlining failed in call to `%s'");
1855 warning ("called from here");
1856 }
1857 mark_addressable (fndecl);
1858 return (rtx) (HOST_WIDE_INT) - 1;
1859}
1860
739fb049
MM
1861/* We need to pop PENDING_STACK_ADJUST bytes. But, if the arguments
1862 wouldn't fill up an even multiple of PREFERRED_UNIT_STACK_BOUNDARY
1863 bytes, then we would need to push some additional bytes to pad the
ce48579b
RH
1864 arguments. So, we compute an adjust to the stack pointer for an
1865 amount that will leave the stack under-aligned by UNADJUSTED_ARGS_SIZE
1866 bytes. Then, when the arguments are pushed the stack will be perfectly
1867 aligned. ARGS_SIZE->CONSTANT is set to the number of bytes that should
1868 be popped after the call. Returns the adjustment. */
739fb049 1869
ce48579b 1870static int
739fb049
MM
1871combine_pending_stack_adjustment_and_call (unadjusted_args_size,
1872 args_size,
1873 preferred_unit_stack_boundary)
1874 int unadjusted_args_size;
1875 struct args_size *args_size;
1876 int preferred_unit_stack_boundary;
1877{
1878 /* The number of bytes to pop so that the stack will be
1879 under-aligned by UNADJUSTED_ARGS_SIZE bytes. */
1880 HOST_WIDE_INT adjustment;
1881 /* The alignment of the stack after the arguments are pushed, if we
1882 just pushed the arguments without adjust the stack here. */
1883 HOST_WIDE_INT unadjusted_alignment;
1884
1885 unadjusted_alignment
1886 = ((stack_pointer_delta + unadjusted_args_size)
1887 % preferred_unit_stack_boundary);
1888
1889 /* We want to get rid of as many of the PENDING_STACK_ADJUST bytes
1890 as possible -- leaving just enough left to cancel out the
1891 UNADJUSTED_ALIGNMENT. In other words, we want to ensure that the
1892 PENDING_STACK_ADJUST is non-negative, and congruent to
1893 -UNADJUSTED_ALIGNMENT modulo the PREFERRED_UNIT_STACK_BOUNDARY. */
1894
1895 /* Begin by trying to pop all the bytes. */
1896 unadjusted_alignment
1897 = (unadjusted_alignment
1898 - (pending_stack_adjust % preferred_unit_stack_boundary));
1899 adjustment = pending_stack_adjust;
1900 /* Push enough additional bytes that the stack will be aligned
1901 after the arguments are pushed. */
1902 if (unadjusted_alignment >= 0)
1903 adjustment -= preferred_unit_stack_boundary - unadjusted_alignment;
1904 else
1905 adjustment += unadjusted_alignment;
1906
1907 /* Now, sets ARGS_SIZE->CONSTANT so that we pop the right number of
1908 bytes after the call. The right number is the entire
1909 PENDING_STACK_ADJUST less our ADJUSTMENT plus the amount required
1910 by the arguments in the first place. */
1911 args_size->constant
1912 = pending_stack_adjust - adjustment + unadjusted_args_size;
1913
ce48579b 1914 return adjustment;
739fb049
MM
1915}
1916
51bbfa0c
RS
1917/* Generate all the code for a function call
1918 and return an rtx for its value.
1919 Store the value in TARGET (specified as an rtx) if convenient.
1920 If the value is stored in TARGET then TARGET is returned.
1921 If IGNORE is nonzero, then we ignore the value of the function call. */
1922
1923rtx
8129842c 1924expand_call (exp, target, ignore)
51bbfa0c
RS
1925 tree exp;
1926 rtx target;
1927 int ignore;
51bbfa0c 1928{
0a1c58a2
JL
1929 /* Nonzero if we are currently expanding a call. */
1930 static int currently_expanding_call = 0;
1931
51bbfa0c
RS
1932 /* List of actual parameters. */
1933 tree actparms = TREE_OPERAND (exp, 1);
1934 /* RTX for the function to be called. */
1935 rtx funexp;
0a1c58a2
JL
1936 /* Sequence of insns to perform a tail recursive "call". */
1937 rtx tail_recursion_insns = NULL_RTX;
1938 /* Sequence of insns to perform a normal "call". */
1939 rtx normal_call_insns = NULL_RTX;
1940 /* Sequence of insns to perform a tail recursive "call". */
1941 rtx tail_call_insns = NULL_RTX;
51bbfa0c
RS
1942 /* Data type of the function. */
1943 tree funtype;
1944 /* Declaration of the function being called,
1945 or 0 if the function is computed (not known by name). */
1946 tree fndecl = 0;
1947 char *name = 0;
0a1c58a2 1948 rtx insn;
4d393a0b
JH
1949 int try_tail_call = 1;
1950 int try_tail_recursion = 1;
0a1c58a2 1951 int pass;
51bbfa0c
RS
1952
1953 /* Register in which non-BLKmode value will be returned,
1954 or 0 if no value or if value is BLKmode. */
1955 rtx valreg;
1956 /* Address where we should return a BLKmode value;
1957 0 if value not BLKmode. */
1958 rtx structure_value_addr = 0;
1959 /* Nonzero if that address is being passed by treating it as
1960 an extra, implicit first parameter. Otherwise,
1961 it is passed by being copied directly into struct_value_rtx. */
1962 int structure_value_addr_parm = 0;
1963 /* Size of aggregate value wanted, or zero if none wanted
1964 or if we are using the non-reentrant PCC calling convention
1965 or expecting the value in registers. */
e5e809f4 1966 HOST_WIDE_INT struct_value_size = 0;
51bbfa0c
RS
1967 /* Nonzero if called function returns an aggregate in memory PCC style,
1968 by returning the address of where to find it. */
1969 int pcc_struct_value = 0;
1970
1971 /* Number of actual parameters in this call, including struct value addr. */
1972 int num_actuals;
1973 /* Number of named args. Args after this are anonymous ones
1974 and they must all go on the stack. */
1975 int n_named_args;
51bbfa0c
RS
1976
1977 /* Vector of information about each argument.
1978 Arguments are numbered in the order they will be pushed,
1979 not the order they are written. */
1980 struct arg_data *args;
1981
1982 /* Total size in bytes of all the stack-parms scanned so far. */
1983 struct args_size args_size;
1984 /* Size of arguments before any adjustments (such as rounding). */
599f37b6 1985 int unadjusted_args_size;
51bbfa0c
RS
1986 /* Data on reg parms scanned so far. */
1987 CUMULATIVE_ARGS args_so_far;
1988 /* Nonzero if a reg parm has been scanned. */
1989 int reg_parm_seen;
efd65a8b 1990 /* Nonzero if this is an indirect function call. */
51bbfa0c
RS
1991
1992 /* Nonzero if we must avoid push-insns in the args for this call.
1993 If stack space is allocated for register parameters, but not by the
1994 caller, then it is preallocated in the fixed part of the stack frame.
1995 So the entire argument block must then be preallocated (i.e., we
1996 ignore PUSH_ROUNDING in that case). */
1997
f73ad30e 1998 int must_preallocate = !PUSH_ARGS;
51bbfa0c 1999
f72aed24 2000 /* Size of the stack reserved for parameter registers. */
6f90e075
JW
2001 int reg_parm_stack_space = 0;
2002
51bbfa0c
RS
2003 /* Address of space preallocated for stack parms
2004 (on machines that lack push insns), or 0 if space not preallocated. */
2005 rtx argblock = 0;
2006
f2d33f13
JH
2007 /* Mask of ECF_ flags. */
2008 int flags = 0;
51bbfa0c
RS
2009 /* Nonzero if this is a call to an inline function. */
2010 int is_integrable = 0;
f73ad30e 2011#ifdef REG_PARM_STACK_SPACE
51bbfa0c
RS
2012 /* Define the boundary of the register parm stack space that needs to be
2013 save, if any. */
2014 int low_to_save = -1, high_to_save;
2015 rtx save_area = 0; /* Place that it is saved */
2016#endif
2017
51bbfa0c
RS
2018 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
2019 char *initial_stack_usage_map = stack_usage_map;
a544cfd2 2020 int old_stack_arg_under_construction = 0;
51bbfa0c
RS
2021
2022 rtx old_stack_level = 0;
79be3418 2023 int old_pending_adj = 0;
51bbfa0c 2024 int old_inhibit_defer_pop = inhibit_defer_pop;
1503a7ec 2025 int old_stack_allocated;
0a1c58a2 2026 rtx call_fusage;
51bbfa0c 2027 register tree p;
21a3b983 2028 register int i;
739fb049
MM
2029 /* The alignment of the stack, in bits. */
2030 HOST_WIDE_INT preferred_stack_boundary;
2031 /* The alignment of the stack, in bytes. */
2032 HOST_WIDE_INT preferred_unit_stack_boundary;
51bbfa0c 2033
7815214e
RK
2034 /* The value of the function call can be put in a hard register. But
2035 if -fcheck-memory-usage, code which invokes functions (and thus
2036 damages some hard registers) can be inserted before using the value.
2037 So, target is always a pseudo-register in that case. */
7d384cc0 2038 if (current_function_check_memory_usage)
7815214e
RK
2039 target = 0;
2040
f2d33f13
JH
2041 /* See if this is "nothrow" function call. */
2042 if (TREE_NOTHROW (exp))
2043 flags |= ECF_NOTHROW;
2044
51bbfa0c
RS
2045 /* See if we can find a DECL-node for the actual function.
2046 As a result, decide whether this is a call to an integrable function. */
2047
39b0dce7
JM
2048 fndecl = get_callee_fndecl (exp);
2049 if (fndecl)
51bbfa0c 2050 {
39b0dce7
JM
2051 if (!flag_no_inline
2052 && fndecl != current_function_decl
2053 && DECL_INLINE (fndecl)
2054 && DECL_SAVED_INSNS (fndecl)
2055 && DECL_SAVED_INSNS (fndecl)->inlinable)
2056 is_integrable = 1;
2057 else if (! TREE_ADDRESSABLE (fndecl))
51bbfa0c 2058 {
39b0dce7
JM
2059 /* In case this function later becomes inlinable,
2060 record that there was already a non-inline call to it.
51bbfa0c 2061
39b0dce7
JM
2062 Use abstraction instead of setting TREE_ADDRESSABLE
2063 directly. */
2064 if (DECL_INLINE (fndecl) && warn_inline && !flag_no_inline
2065 && optimize > 0)
2066 {
2067 warning_with_decl (fndecl, "can't inline call to `%s'");
2068 warning ("called from here");
51bbfa0c 2069 }
39b0dce7 2070 mark_addressable (fndecl);
51bbfa0c 2071 }
39b0dce7
JM
2072
2073 flags |= flags_from_decl_or_type (fndecl);
51bbfa0c
RS
2074 }
2075
fdff8c6d 2076 /* If we don't have specific function to call, see if we have a
f2d33f13 2077 attributes set in the type. */
39b0dce7
JM
2078 else
2079 {
2080 p = TREE_OPERAND (exp, 0);
2081 flags |= flags_from_decl_or_type (TREE_TYPE (TREE_TYPE (p)));
2082 }
fdff8c6d 2083
6f90e075
JW
2084#ifdef REG_PARM_STACK_SPACE
2085#ifdef MAYBE_REG_PARM_STACK_SPACE
2086 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
2087#else
2088 reg_parm_stack_space = REG_PARM_STACK_SPACE (fndecl);
2089#endif
2090#endif
2091
f73ad30e
JH
2092#ifndef OUTGOING_REG_PARM_STACK_SPACE
2093 if (reg_parm_stack_space > 0 && PUSH_ARGS)
e5e809f4
JL
2094 must_preallocate = 1;
2095#endif
2096
51bbfa0c
RS
2097 /* Warn if this value is an aggregate type,
2098 regardless of which calling convention we are using for it. */
05e3bdb9 2099 if (warn_aggregate_return && AGGREGATE_TYPE_P (TREE_TYPE (exp)))
51bbfa0c
RS
2100 warning ("function call has aggregate value");
2101
2102 /* Set up a place to return a structure. */
2103
2104 /* Cater to broken compilers. */
2105 if (aggregate_value_p (exp))
2106 {
2107 /* This call returns a big structure. */
2a8f6b90 2108 flags &= ~(ECF_CONST | ECF_PURE);
51bbfa0c
RS
2109
2110#ifdef PCC_STATIC_STRUCT_RETURN
9e7b1d0a
RS
2111 {
2112 pcc_struct_value = 1;
0dd532dc
JW
2113 /* Easier than making that case work right. */
2114 if (is_integrable)
2115 {
2116 /* In case this is a static function, note that it has been
2117 used. */
2118 if (! TREE_ADDRESSABLE (fndecl))
2119 mark_addressable (fndecl);
2120 is_integrable = 0;
2121 }
9e7b1d0a
RS
2122 }
2123#else /* not PCC_STATIC_STRUCT_RETURN */
2124 {
2125 struct_value_size = int_size_in_bytes (TREE_TYPE (exp));
51bbfa0c 2126
9e7b1d0a
RS
2127 if (target && GET_CODE (target) == MEM)
2128 structure_value_addr = XEXP (target, 0);
2129 else
2130 {
e9a25f70
JL
2131 /* Assign a temporary to hold the value. */
2132 tree d;
51bbfa0c 2133
9e7b1d0a
RS
2134 /* For variable-sized objects, we must be called with a target
2135 specified. If we were to allocate space on the stack here,
2136 we would have no way of knowing when to free it. */
51bbfa0c 2137
002bdd6c
RK
2138 if (struct_value_size < 0)
2139 abort ();
2140
e9a25f70
JL
2141 /* This DECL is just something to feed to mark_addressable;
2142 it doesn't get pushed. */
2143 d = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (exp));
2144 DECL_RTL (d) = assign_temp (TREE_TYPE (exp), 1, 0, 1);
2145 mark_addressable (d);
14a774a9 2146 mark_temp_addr_taken (DECL_RTL (d));
e9a25f70 2147 structure_value_addr = XEXP (DECL_RTL (d), 0);
e5e809f4 2148 TREE_USED (d) = 1;
9e7b1d0a
RS
2149 target = 0;
2150 }
2151 }
2152#endif /* not PCC_STATIC_STRUCT_RETURN */
51bbfa0c
RS
2153 }
2154
2155 /* If called function is inline, try to integrate it. */
2156
2157 if (is_integrable)
2158 {
f2d33f13
JH
2159 rtx temp = try_to_integrate (fndecl, actparms, target,
2160 ignore, TREE_TYPE (exp),
2161 structure_value_addr);
2162 if (temp != (rtx) (HOST_WIDE_INT) - 1)
2163 return temp;
51bbfa0c
RS
2164 }
2165
4d393a0b
JH
2166 if (fndecl && DECL_NAME (fndecl))
2167 name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
2168
2169 /* Figure out the amount to which the stack should be aligned. */
2170#ifdef PREFERRED_STACK_BOUNDARY
2171 preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
2172#else
2173 preferred_stack_boundary = STACK_BOUNDARY;
2174#endif
2175
2176 /* Operand 0 is a pointer-to-function; get the type of the function. */
2177 funtype = TREE_TYPE (TREE_OPERAND (exp, 0));
2178 if (! POINTER_TYPE_P (funtype))
2179 abort ();
2180 funtype = TREE_TYPE (funtype);
2181
2182 /* See if this is a call to a function that can return more than once
2183 or a call to longjmp or malloc. */
2184 flags |= special_function_p (fndecl, flags);
2185
2186 if (flags & ECF_MAY_BE_ALLOCA)
2187 current_function_calls_alloca = 1;
2188
2189 /* If struct_value_rtx is 0, it means pass the address
2190 as if it were an extra parameter. */
2191 if (structure_value_addr && struct_value_rtx == 0)
2192 {
2193 /* If structure_value_addr is a REG other than
2194 virtual_outgoing_args_rtx, we can use always use it. If it
2195 is not a REG, we must always copy it into a register.
2196 If it is virtual_outgoing_args_rtx, we must copy it to another
2197 register in some cases. */
2198 rtx temp = (GET_CODE (structure_value_addr) != REG
2199 || (ACCUMULATE_OUTGOING_ARGS
2200 && stack_arg_under_construction
2201 && structure_value_addr == virtual_outgoing_args_rtx)
2202 ? copy_addr_to_reg (structure_value_addr)
2203 : structure_value_addr);
2204
2205 actparms
2206 = tree_cons (error_mark_node,
2207 make_tree (build_pointer_type (TREE_TYPE (funtype)),
2208 temp),
2209 actparms);
2210 structure_value_addr_parm = 1;
2211 }
2212
2213 /* Count the arguments and set NUM_ACTUALS. */
2214 for (p = actparms, num_actuals = 0; p; p = TREE_CHAIN (p))
2215 num_actuals++;
2216
2217 /* Compute number of named args.
2218 Normally, don't include the last named arg if anonymous args follow.
2219 We do include the last named arg if STRICT_ARGUMENT_NAMING is nonzero.
2220 (If no anonymous args follow, the result of list_length is actually
2221 one too large. This is harmless.)
2222
2223 If PRETEND_OUTGOING_VARARGS_NAMED is set and STRICT_ARGUMENT_NAMING is
2224 zero, this machine will be able to place unnamed args that were
2225 passed in registers into the stack. So treat all args as named.
2226 This allows the insns emitting for a specific argument list to be
2227 independent of the function declaration.
2228
2229 If PRETEND_OUTGOING_VARARGS_NAMED is not set, we do not have any
2230 reliable way to pass unnamed args in registers, so we must force
2231 them into memory. */
2232
2233 if ((STRICT_ARGUMENT_NAMING
2234 || ! PRETEND_OUTGOING_VARARGS_NAMED)
2235 && TYPE_ARG_TYPES (funtype) != 0)
2236 n_named_args
2237 = (list_length (TYPE_ARG_TYPES (funtype))
2238 /* Don't include the last named arg. */
2239 - (STRICT_ARGUMENT_NAMING ? 0 : 1)
2240 /* Count the struct value address, if it is passed as a parm. */
2241 + structure_value_addr_parm);
2242 else
2243 /* If we know nothing, treat all args as named. */
2244 n_named_args = num_actuals;
2245
2246 /* Start updating where the next arg would go.
2247
2248 On some machines (such as the PA) indirect calls have a different
2249 calling convention than normal calls. The last argument in
2250 INIT_CUMULATIVE_ARGS tells the backend if this is an indirect call
2251 or not. */
2252 INIT_CUMULATIVE_ARGS (args_so_far, funtype, NULL_RTX, (fndecl == 0));
2253
2254
2255 /* Make a vector to hold all the information about each arg. */
2256 args = (struct arg_data *) alloca (num_actuals
2257 * sizeof (struct arg_data));
2258 bzero ((char *) args, num_actuals * sizeof (struct arg_data));
2259
2260 /* Build up entries inthe ARGS array, compute the size of the arguments
2261 into ARGS_SIZE, etc. */
2262 initialize_argument_information (num_actuals, args, &args_size,
2263 n_named_args, actparms, fndecl,
2264 &args_so_far, reg_parm_stack_space,
2265 &old_stack_level, &old_pending_adj,
2266 &must_preallocate, &flags);
2267
2268 if (args_size.var)
2269 {
2270 /* If this function requires a variable-sized argument list, don't
2271 try to make a cse'able block for this call. We may be able to
2272 do this eventually, but it is too complicated to keep track of
2273 what insns go in the cse'able block and which don't. */
2274
2275 flags &= ~(ECF_CONST | ECF_PURE);
2276 must_preallocate = 1;
2277 }
2278
2279 /* Now make final decision about preallocating stack space. */
2280 must_preallocate = finalize_must_preallocate (must_preallocate,
2281 num_actuals, args,
2282 &args_size);
2283
2284 /* If the structure value address will reference the stack pointer, we
2285 must stabilize it. We don't need to do this if we know that we are
2286 not going to adjust the stack pointer in processing this call. */
2287
2288 if (structure_value_addr
2289 && (reg_mentioned_p (virtual_stack_dynamic_rtx, structure_value_addr)
2290 || reg_mentioned_p (virtual_outgoing_args_rtx,
2291 structure_value_addr))
2292 && (args_size.var
2293 || (!ACCUMULATE_OUTGOING_ARGS && args_size.constant)))
2294 structure_value_addr = copy_to_reg (structure_value_addr);
0a1c58a2 2295
194c7c45
RH
2296 /* Tail calls can make things harder to debug, and we're traditionally
2297 pushed these optimizations into -O2. Don't try if we're already
2298 expanding a call, as that means we're an argument. Similarly, if
2299 there's pending loops or cleanups we know there's code to follow
e2ee9912
RH
2300 the call.
2301
2302 If rtx_equal_function_value_matters is false, that means we've
2303 finished with regular parsing. Which means that some of the
2304 machinery we use to generate tail-calls is no longer in place.
2305 This is most often true of sjlj-exceptions, which we couldn't
2306 tail-call to anyway. */
0a1c58a2 2307
840e7b51
RH
2308 if (currently_expanding_call++ != 0
2309 || !flag_optimize_sibling_calls
4d393a0b 2310 || !rtx_equal_function_value_matters
4d393a0b
JH
2311 || !stmt_loop_nest_empty ()
2312 || any_pending_cleanups (1)
2313 || args_size.var)
2314 try_tail_call = try_tail_recursion = 0;
2315
2316 /* Tail recursion fails, when we are not dealing with recursive calls. */
2317 if (!try_tail_recursion
2318 || TREE_CODE (TREE_OPERAND (exp, 0)) != ADDR_EXPR
2319 || TREE_OPERAND (TREE_OPERAND (exp, 0), 0) != current_function_decl)
2320 try_tail_recursion = 0;
2321
2322 /* Rest of purposes for tail call optimizations to fail. */
2323 if (
2324#ifdef HAVE_sibcall_epilogue
2325 !HAVE_sibcall_epilogue
2326#else
2327 1
2328#endif
2329 || !try_tail_call
2330 /* Doing sibling call optimization needs some work, since
2331 structure_value_addr can be allocated on the stack.
2332 It does not seem worth the effort since few optimizable
2333 sibling calls will return a structure. */
2334 || structure_value_addr != NULL_RTX
2335 /* If the register holding the address is a callee saved
2336 register, then we lose. We have no way to prevent that,
2337 so we only allow calls to named functions. */
2338 /* ??? This could be done by having the insn constraints
2339 use a register class that is all call-clobbered. Any
2340 reload insns generated to fix things up would appear
2341 before the sibcall_epilogue. */
2342 || fndecl == NULL_TREE
2343 || (flags & (ECF_RETURNS_TWICE | ECF_LONGJMP))
2344 || !FUNCTION_OK_FOR_SIBCALL (fndecl)
2345 /* If this function requires more stack slots than the current
2346 function, we cannot change it into a sibling call. */
2347 || args_size.constant > current_function_args_size
2348 /* If the callee pops its own arguments, then it must pop exactly
2349 the same number of arguments as the current function. */
2350 || RETURN_POPS_ARGS (fndecl, funtype, args_size.constant)
2351 != RETURN_POPS_ARGS (current_function_decl,
2352 TREE_TYPE (current_function_decl),
2353 current_function_args_size))
2354 try_tail_call = 0;
194c7c45 2355
4d393a0b
JH
2356 if (try_tail_call || try_tail_recursion)
2357 {
2358 int end, inc;
2359 actparms = NULL_TREE;
194c7c45
RH
2360 /* Ok, we're going to give the tail call the old college try.
2361 This means we're going to evaluate the function arguments
2362 up to three times. There are two degrees of badness we can
2363 encounter, those that can be unsaved and those that can't.
2364 (See unsafe_for_reeval commentary for details.)
2365
2366 Generate a new argument list. Pass safe arguments through
2367 unchanged. For the easy badness wrap them in UNSAVE_EXPRs.
2368 For hard badness, evaluate them now and put their resulting
4d393a0b 2369 rtx in a temporary VAR_DECL.
194c7c45 2370
4d393a0b
JH
2371 initialize_argument_information has ordered the array for the
2372 order to be pushed, and we must remember this when reconstructing
2373 the original argument orde. */
2374
2375 if (PUSH_ARGS_REVERSED)
2376 {
2377 inc = 1;
2378 i = 0;
2379 end = num_actuals;
2380 }
2381 else
2382 {
2383 inc = -1;
2384 i = num_actuals - 1;
2385 end = -1;
2386 }
2387
2388 for (; i != end; i += inc)
2389 {
2390 switch (unsafe_for_reeval (args[i].tree_value))
0a1c58a2 2391 {
4d393a0b
JH
2392 case 0: /* Safe. */
2393 break;
0a1c58a2 2394
4d393a0b
JH
2395 case 1: /* Mildly unsafe. */
2396 args[i].tree_value = unsave_expr (args[i].tree_value);
2397 break;
194c7c45 2398
4d393a0b
JH
2399 case 2: /* Wildly unsafe. */
2400 {
2401 tree var = build_decl (VAR_DECL, NULL_TREE,
2402 TREE_TYPE (args[i].tree_value));
2403 DECL_RTL (var) = expand_expr (args[i].tree_value, NULL_RTX,
2404 VOIDmode, EXPAND_NORMAL);
2405 args[i].tree_value = var;
2406 }
2407 break;
194c7c45 2408
4d393a0b
JH
2409 default:
2410 abort ();
2411 }
2412 /* We need to build actparms for optimize_tail_recursion. We can
2413 safely trash away TREE_PURPOSE, since it is unused by this
2414 function. */
2415 if (try_tail_recursion)
2416 actparms = tree_cons (NULL_TREE, args[i].tree_value, actparms);
2417 }
194c7c45
RH
2418 /* Expanding one of those dangerous arguments could have added
2419 cleanups, but otherwise give it a whirl. */
4d393a0b
JH
2420 if (any_pending_cleanups (1))
2421 try_tail_call = try_tail_recursion = 0;
0a1c58a2
JL
2422 }
2423
2424 /* Generate a tail recursion sequence when calling ourselves. */
2425
4d393a0b 2426 if (try_tail_recursion)
0a1c58a2
JL
2427 {
2428 /* We want to emit any pending stack adjustments before the tail
2429 recursion "call". That way we know any adjustment after the tail
2430 recursion call can be ignored if we indeed use the tail recursion
2431 call expansion. */
2432 int save_pending_stack_adjust = pending_stack_adjust;
1503a7ec 2433 int save_stack_pointer_delta = stack_pointer_delta;
0a1c58a2
JL
2434
2435 /* Use a new sequence to hold any RTL we generate. We do not even
2436 know if we will use this RTL yet. The final decision can not be
2437 made until after RTL generation for the entire function is
2438 complete. */
b06775f9 2439 start_sequence ();
0a1c58a2 2440
b06775f9
RH
2441 if (optimize_tail_recursion (actparms, get_last_insn ()))
2442 tail_recursion_insns = get_insns ();
0a1c58a2
JL
2443 end_sequence ();
2444
0a1c58a2
JL
2445 /* Restore the original pending stack adjustment for the sibling and
2446 normal call cases below. */
2447 pending_stack_adjust = save_pending_stack_adjust;
1503a7ec 2448 stack_pointer_delta = save_stack_pointer_delta;
0a1c58a2
JL
2449 }
2450
4d393a0b
JH
2451 if (profile_arc_flag && (flags & ECF_FORK_OR_EXEC))
2452 {
2453 /* A fork duplicates the profile information, and an exec discards
2454 it. We can't rely on fork/exec to be paired. So write out the
2455 profile information we have gathered so far, and clear it. */
2456 /* ??? When Linux's __clone is called with CLONE_VM set, profiling
2457 is subject to race conditions, just as with multithreaded
2458 programs. */
2459
2460 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__bb_fork_func"), 0,
2461 VOIDmode, 0);
2462 }
0a1c58a2 2463
c2f8b491
JH
2464 /* Ensure current function's preferred stack boundary is at least
2465 what we need. We don't have to increase alignment for recursive
2466 functions. */
2467 if (cfun->preferred_stack_boundary < preferred_stack_boundary
2468 && fndecl != current_function_decl)
2469 cfun->preferred_stack_boundary = preferred_stack_boundary;
2470
4d393a0b 2471 preferred_unit_stack_boundary = preferred_stack_boundary / BITS_PER_UNIT;
51bbfa0c 2472
4d393a0b 2473 function_call_count++;
39842893 2474
0a1c58a2
JL
2475 /* We want to make two insn chains; one for a sibling call, the other
2476 for a normal call. We will select one of the two chains after
2477 initial RTL generation is complete. */
2478 for (pass = 0; pass < 2; pass++)
2479 {
2480 int sibcall_failure = 0;
2481 /* We want to emit ay pending stack adjustments before the tail
2482 recursion "call". That way we know any adjustment after the tail
2483 recursion call can be ignored if we indeed use the tail recursion
2484 call expansion. */
2485 int save_pending_stack_adjust;
1503a7ec 2486 int save_stack_pointer_delta;
0a1c58a2 2487 rtx insns;
7d167afd 2488 rtx before_call, next_arg_reg;
39842893 2489
0a1c58a2
JL
2490 if (pass == 0)
2491 {
4d393a0b 2492 if (! try_tail_call)
0a1c58a2 2493 continue;
51bbfa0c 2494
1c81f9fe
JM
2495 /* Emit any queued insns now; otherwise they would end up in
2496 only one of the alternates. */
2497 emit_queue ();
2498
e245d3af
RH
2499 /* We know at this point that there are not currently any
2500 pending cleanups. If, however, in the process of evaluating
2501 the arguments we were to create some, we'll need to be
2502 able to get rid of them. */
2503 expand_start_target_temps ();
2504
0a1c58a2
JL
2505 /* State variables we need to save and restore between
2506 iterations. */
2507 save_pending_stack_adjust = pending_stack_adjust;
1503a7ec 2508 save_stack_pointer_delta = stack_pointer_delta;
0a1c58a2 2509 }
f2d33f13
JH
2510 if (pass)
2511 flags &= ~ECF_SIBCALL;
2512 else
2513 flags |= ECF_SIBCALL;
51bbfa0c 2514
0a1c58a2 2515 /* Other state variables that we must reinitialize each time
f2d33f13 2516 through the loop (that are not initialized by the loop itself). */
0a1c58a2
JL
2517 argblock = 0;
2518 call_fusage = 0;
fa76d9e0 2519
0a1c58a2 2520 /* Start a new sequence for the normal call case.
51bbfa0c 2521
0a1c58a2
JL
2522 From this point on, if the sibling call fails, we want to set
2523 sibcall_failure instead of continuing the loop. */
2524 start_sequence ();
eecb6f50 2525
0a1c58a2
JL
2526 /* When calling a const function, we must pop the stack args right away,
2527 so that the pop is deleted or moved with the call. */
2a8f6b90 2528 if (flags & (ECF_CONST | ECF_PURE))
0a1c58a2 2529 NO_DEFER_POP;
51bbfa0c 2530
0a1c58a2
JL
2531 /* Don't let pending stack adjusts add up to too much.
2532 Also, do all pending adjustments now if there is any chance
2533 this might be a call to alloca or if we are expanding a sibling
2534 call sequence. */
2535 if (pending_stack_adjust >= 32
f2d33f13 2536 || (pending_stack_adjust > 0 && (flags & ECF_MAY_BE_ALLOCA))
0a1c58a2
JL
2537 || pass == 0)
2538 do_pending_stack_adjust ();
51bbfa0c 2539
0a1c58a2
JL
2540 /* Push the temporary stack slot level so that we can free any
2541 temporaries we make. */
2542 push_temp_slots ();
51bbfa0c 2543
51bbfa0c 2544
6f90e075 2545#ifdef FINAL_REG_PARM_STACK_SPACE
0a1c58a2
JL
2546 reg_parm_stack_space = FINAL_REG_PARM_STACK_SPACE (args_size.constant,
2547 args_size.var);
6f90e075 2548#endif
0a1c58a2 2549 /* Precompute any arguments as needed. */
f8a097cd
JH
2550 if (pass)
2551 precompute_arguments (flags, num_actuals, args);
51bbfa0c 2552
0a1c58a2
JL
2553 /* Now we are about to start emitting insns that can be deleted
2554 if a libcall is deleted. */
2a8f6b90 2555 if (flags & (ECF_CONST | ECF_PURE | ECF_MALLOC))
0a1c58a2 2556 start_sequence ();
51bbfa0c 2557
ce48579b
RH
2558 /* Compute the actual size of the argument block required. The variable
2559 and constant sizes must be combined, the size may have to be rounded,
2560 and there may be a minimum required size. When generating a sibcall
2561 pattern, do not round up, since we'll be re-using whatever space our
2562 caller provided. */
2563 unadjusted_args_size
2564 = compute_argument_block_size (reg_parm_stack_space, &args_size,
2565 (pass == 0 ? 0
2566 : preferred_stack_boundary));
2567
1503a7ec 2568 old_stack_allocated = stack_pointer_delta - pending_stack_adjust;
ce48579b 2569
f8a097cd
JH
2570 /* The argument block when performing a sibling call is the
2571 incoming argument block. */
2572 if (pass == 0)
2573 argblock = virtual_incoming_args_rtx;
ce48579b 2574
0a1c58a2
JL
2575 /* If we have no actual push instructions, or shouldn't use them,
2576 make space for all args right now. */
f8a097cd 2577 else if (args_size.var != 0)
51bbfa0c 2578 {
0a1c58a2
JL
2579 if (old_stack_level == 0)
2580 {
2581 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
2582 old_pending_adj = pending_stack_adjust;
2583 pending_stack_adjust = 0;
0a1c58a2
JL
2584 /* stack_arg_under_construction says whether a stack arg is
2585 being constructed at the old stack level. Pushing the stack
2586 gets a clean outgoing argument block. */
2587 old_stack_arg_under_construction = stack_arg_under_construction;
2588 stack_arg_under_construction = 0;
0a1c58a2
JL
2589 }
2590 argblock = push_block (ARGS_SIZE_RTX (args_size), 0, 0);
51bbfa0c 2591 }
0a1c58a2
JL
2592 else
2593 {
2594 /* Note that we must go through the motions of allocating an argument
2595 block even if the size is zero because we may be storing args
2596 in the area reserved for register arguments, which may be part of
2597 the stack frame. */
26a258fe 2598
0a1c58a2 2599 int needed = args_size.constant;
51bbfa0c 2600
0a1c58a2
JL
2601 /* Store the maximum argument space used. It will be pushed by
2602 the prologue (if ACCUMULATE_OUTGOING_ARGS, or stack overflow
2603 checking). */
51bbfa0c 2604
0a1c58a2
JL
2605 if (needed > current_function_outgoing_args_size)
2606 current_function_outgoing_args_size = needed;
51bbfa0c 2607
0a1c58a2
JL
2608 if (must_preallocate)
2609 {
f73ad30e
JH
2610 if (ACCUMULATE_OUTGOING_ARGS)
2611 {
f8a097cd
JH
2612 /* Since the stack pointer will never be pushed, it is
2613 possible for the evaluation of a parm to clobber
2614 something we have already written to the stack.
2615 Since most function calls on RISC machines do not use
2616 the stack, this is uncommon, but must work correctly.
26a258fe 2617
f73ad30e 2618 Therefore, we save any area of the stack that was already
f8a097cd
JH
2619 written and that we are using. Here we set up to do this
2620 by making a new stack usage map from the old one. The
2621 actual save will be done by store_one_arg.
26a258fe 2622
f73ad30e
JH
2623 Another approach might be to try to reorder the argument
2624 evaluations to avoid this conflicting stack usage. */
26a258fe 2625
e5e809f4 2626#ifndef OUTGOING_REG_PARM_STACK_SPACE
f8a097cd
JH
2627 /* Since we will be writing into the entire argument area,
2628 the map must be allocated for its entire size, not just
2629 the part that is the responsibility of the caller. */
f73ad30e 2630 needed += reg_parm_stack_space;
51bbfa0c
RS
2631#endif
2632
2633#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
2634 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2635 needed + 1);
51bbfa0c 2636#else
f73ad30e
JH
2637 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
2638 needed);
51bbfa0c 2639#endif
f8a097cd
JH
2640 stack_usage_map
2641 = (char *) alloca (highest_outgoing_arg_in_use);
51bbfa0c 2642
f73ad30e
JH
2643 if (initial_highest_arg_in_use)
2644 bcopy (initial_stack_usage_map, stack_usage_map,
2645 initial_highest_arg_in_use);
2f4aa534 2646
f73ad30e
JH
2647 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
2648 bzero (&stack_usage_map[initial_highest_arg_in_use],
2649 (highest_outgoing_arg_in_use
2650 - initial_highest_arg_in_use));
2651 needed = 0;
2f4aa534 2652
f8a097cd
JH
2653 /* The address of the outgoing argument list must not be
2654 copied to a register here, because argblock would be left
2655 pointing to the wrong place after the call to
f73ad30e 2656 allocate_dynamic_stack_space below. */
2f4aa534 2657
f73ad30e
JH
2658 argblock = virtual_outgoing_args_rtx;
2659 }
2660 else
26a258fe 2661 {
f73ad30e 2662 if (inhibit_defer_pop == 0)
0a1c58a2 2663 {
f73ad30e 2664 /* Try to reuse some or all of the pending_stack_adjust
ce48579b
RH
2665 to get this space. */
2666 needed
2667 = (combine_pending_stack_adjustment_and_call
2668 (unadjusted_args_size,
2669 &args_size,
2670 preferred_unit_stack_boundary));
2671
2672 /* combine_pending_stack_adjustment_and_call computes
2673 an adjustment before the arguments are allocated.
2674 Account for them and see whether or not the stack
2675 needs to go up or down. */
2676 needed = unadjusted_args_size - needed;
2677
2678 if (needed < 0)
f73ad30e 2679 {
ce48579b
RH
2680 /* We're releasing stack space. */
2681 /* ??? We can avoid any adjustment at all if we're
2682 already aligned. FIXME. */
2683 pending_stack_adjust = -needed;
2684 do_pending_stack_adjust ();
f73ad30e
JH
2685 needed = 0;
2686 }
ce48579b
RH
2687 else
2688 /* We need to allocate space. We'll do that in
2689 push_block below. */
2690 pending_stack_adjust = 0;
0a1c58a2 2691 }
ce48579b
RH
2692
2693 /* Special case this because overhead of `push_block' in
2694 this case is non-trivial. */
f73ad30e
JH
2695 if (needed == 0)
2696 argblock = virtual_outgoing_args_rtx;
0a1c58a2 2697 else
f73ad30e
JH
2698 argblock = push_block (GEN_INT (needed), 0, 0);
2699
f8a097cd
JH
2700 /* We only really need to call `copy_to_reg' in the case
2701 where push insns are going to be used to pass ARGBLOCK
2702 to a function call in ARGS. In that case, the stack
2703 pointer changes value from the allocation point to the
2704 call point, and hence the value of
2705 VIRTUAL_OUTGOING_ARGS_RTX changes as well. But might
2706 as well always do it. */
f73ad30e 2707 argblock = copy_to_reg (argblock);
0a1c58a2 2708
f8a097cd 2709 /* The save/restore code in store_one_arg handles all
ce48579b
RH
2710 cases except one: a constructor call (including a C
2711 function returning a BLKmode struct) to initialize
2712 an argument. */
f8a097cd
JH
2713 if (stack_arg_under_construction)
2714 {
e5e809f4 2715#ifndef OUTGOING_REG_PARM_STACK_SPACE
ce48579b
RH
2716 rtx push_size = GEN_INT (reg_parm_stack_space
2717 + args_size.constant);
bfbf933a 2718#else
f8a097cd 2719 rtx push_size = GEN_INT (args_size.constant);
bfbf933a 2720#endif
f8a097cd
JH
2721 if (old_stack_level == 0)
2722 {
ce48579b
RH
2723 emit_stack_save (SAVE_BLOCK, &old_stack_level,
2724 NULL_RTX);
f8a097cd
JH
2725 old_pending_adj = pending_stack_adjust;
2726 pending_stack_adjust = 0;
ce48579b
RH
2727 /* stack_arg_under_construction says whether a stack
2728 arg is being constructed at the old stack level.
2729 Pushing the stack gets a clean outgoing argument
2730 block. */
2731 old_stack_arg_under_construction
2732 = stack_arg_under_construction;
f8a097cd
JH
2733 stack_arg_under_construction = 0;
2734 /* Make a new map for the new argument list. */
ce48579b
RH
2735 stack_usage_map = (char *)
2736 alloca (highest_outgoing_arg_in_use);
f8a097cd
JH
2737 bzero (stack_usage_map, highest_outgoing_arg_in_use);
2738 highest_outgoing_arg_in_use = 0;
2739 }
ce48579b
RH
2740 allocate_dynamic_stack_space (push_size, NULL_RTX,
2741 BITS_PER_UNIT);
f8a097cd 2742 }
ce48579b
RH
2743 /* If argument evaluation might modify the stack pointer,
2744 copy the address of the argument list to a register. */
f8a097cd
JH
2745 for (i = 0; i < num_actuals; i++)
2746 if (args[i].pass_on_stack)
2747 {
2748 argblock = copy_addr_to_reg (argblock);
2749 break;
2750 }
f73ad30e 2751 }
0a1c58a2 2752 }
bfbf933a 2753 }
bfbf933a 2754
0a1c58a2 2755 compute_argument_addresses (args, argblock, num_actuals);
bfbf933a 2756
c795bca9 2757#ifdef PREFERRED_STACK_BOUNDARY
0a1c58a2
JL
2758 /* If we push args individually in reverse order, perform stack alignment
2759 before the first push (the last arg). */
f73ad30e
JH
2760 if (PUSH_ARGS_REVERSED && argblock == 0
2761 && args_size.constant != unadjusted_args_size)
4e217aed 2762 {
0a1c58a2
JL
2763 /* When the stack adjustment is pending, we get better code
2764 by combining the adjustments. */
739fb049
MM
2765 if (pending_stack_adjust
2766 && ! (flags & (ECF_CONST | ECF_PURE))
0a1c58a2 2767 && ! inhibit_defer_pop)
ce48579b
RH
2768 {
2769 pending_stack_adjust
2770 = (combine_pending_stack_adjustment_and_call
2771 (unadjusted_args_size,
2772 &args_size,
2773 preferred_unit_stack_boundary));
2774 do_pending_stack_adjust ();
2775 }
0a1c58a2
JL
2776 else if (argblock == 0)
2777 anti_adjust_stack (GEN_INT (args_size.constant
2778 - unadjusted_args_size));
0a1c58a2 2779 }
ebcd0b57
JH
2780 /* Now that the stack is properly aligned, pops can't safely
2781 be deferred during the evaluation of the arguments. */
2782 NO_DEFER_POP;
51bbfa0c
RS
2783#endif
2784
0a1c58a2
JL
2785 /* Don't try to defer pops if preallocating, not even from the first arg,
2786 since ARGBLOCK probably refers to the SP. */
2787 if (argblock)
2788 NO_DEFER_POP;
51bbfa0c 2789
0a1c58a2 2790 funexp = rtx_for_function_call (fndecl, exp);
51bbfa0c 2791
0a1c58a2
JL
2792 /* Figure out the register where the value, if any, will come back. */
2793 valreg = 0;
2794 if (TYPE_MODE (TREE_TYPE (exp)) != VOIDmode
2795 && ! structure_value_addr)
2796 {
2797 if (pcc_struct_value)
2798 valreg = hard_function_value (build_pointer_type (TREE_TYPE (exp)),
7d167afd 2799 fndecl, (pass == 0));
0a1c58a2 2800 else
7d167afd 2801 valreg = hard_function_value (TREE_TYPE (exp), fndecl, (pass == 0));
0a1c58a2 2802 }
51bbfa0c 2803
0a1c58a2
JL
2804 /* Precompute all register parameters. It isn't safe to compute anything
2805 once we have started filling any specific hard regs. */
2806 precompute_register_parameters (num_actuals, args, &reg_parm_seen);
51bbfa0c 2807
f73ad30e 2808#ifdef REG_PARM_STACK_SPACE
0a1c58a2
JL
2809 /* Save the fixed argument area if it's part of the caller's frame and
2810 is clobbered by argument setup for this call. */
f8a097cd 2811 if (ACCUMULATE_OUTGOING_ARGS && pass)
f73ad30e
JH
2812 save_area = save_fixed_argument_area (reg_parm_stack_space, argblock,
2813 &low_to_save, &high_to_save);
b94301c2 2814#endif
51bbfa0c 2815
0a1c58a2
JL
2816 /* Now store (and compute if necessary) all non-register parms.
2817 These come before register parms, since they can require block-moves,
2818 which could clobber the registers used for register parms.
2819 Parms which have partial registers are not stored here,
2820 but we do preallocate space here if they want that. */
51bbfa0c 2821
0a1c58a2
JL
2822 for (i = 0; i < num_actuals; i++)
2823 if (args[i].reg == 0 || args[i].pass_on_stack)
f8a097cd 2824 store_one_arg (&args[i], argblock, flags,
0a1c58a2
JL
2825 args_size.var != 0, reg_parm_stack_space);
2826
2827 /* If we have a parm that is passed in registers but not in memory
2828 and whose alignment does not permit a direct copy into registers,
2829 make a group of pseudos that correspond to each register that we
2830 will later fill. */
2831 if (STRICT_ALIGNMENT)
2832 store_unaligned_arguments_into_pseudos (args, num_actuals);
2833
2834 /* Now store any partially-in-registers parm.
2835 This is the last place a block-move can happen. */
2836 if (reg_parm_seen)
2837 for (i = 0; i < num_actuals; i++)
2838 if (args[i].partial != 0 && ! args[i].pass_on_stack)
f8a097cd 2839 store_one_arg (&args[i], argblock, flags,
0a1c58a2 2840 args_size.var != 0, reg_parm_stack_space);
51bbfa0c 2841
c795bca9 2842#ifdef PREFERRED_STACK_BOUNDARY
0a1c58a2
JL
2843 /* If we pushed args in forward order, perform stack alignment
2844 after pushing the last arg. */
f73ad30e 2845 if (!PUSH_ARGS_REVERSED && argblock == 0)
0a1c58a2
JL
2846 anti_adjust_stack (GEN_INT (args_size.constant
2847 - unadjusted_args_size));
51bbfa0c
RS
2848#endif
2849
0a1c58a2
JL
2850 /* If register arguments require space on the stack and stack space
2851 was not preallocated, allocate stack space here for arguments
2852 passed in registers. */
f73ad30e
JH
2853#ifdef OUTGOING_REG_PARM_STACK_SPACE
2854 if (!ACCUMULATE_OUTGOING_ARGS
1dfb49b9 2855 && must_preallocate == 0 && reg_parm_stack_space > 0)
0a1c58a2 2856 anti_adjust_stack (GEN_INT (reg_parm_stack_space));
756e0e12
RS
2857#endif
2858
0a1c58a2
JL
2859 /* Pass the function the address in which to return a
2860 structure value. */
2861 if (pass != 0 && structure_value_addr && ! structure_value_addr_parm)
2862 {
2863 emit_move_insn (struct_value_rtx,
2864 force_reg (Pmode,
2865 force_operand (structure_value_addr,
2866 NULL_RTX)));
2867
2868 /* Mark the memory for the aggregate as write-only. */
2869 if (current_function_check_memory_usage)
2870 emit_library_call (chkr_set_right_libfunc, 1,
2871 VOIDmode, 3,
2872 structure_value_addr, ptr_mode,
2873 GEN_INT (struct_value_size),
2874 TYPE_MODE (sizetype),
2875 GEN_INT (MEMORY_USE_WO),
2876 TYPE_MODE (integer_type_node));
2877
2878 if (GET_CODE (struct_value_rtx) == REG)
2879 use_reg (&call_fusage, struct_value_rtx);
2880 }
c2939b57 2881
0a1c58a2
JL
2882 funexp = prepare_call_address (funexp, fndecl, &call_fusage,
2883 reg_parm_seen);
51bbfa0c 2884
0a1c58a2
JL
2885 load_register_parameters (args, num_actuals, &call_fusage);
2886
2887 /* Perform postincrements before actually calling the function. */
2888 emit_queue ();
51bbfa0c 2889
0a1c58a2
JL
2890 /* Save a pointer to the last insn before the call, so that we can
2891 later safely search backwards to find the CALL_INSN. */
2892 before_call = get_last_insn ();
51bbfa0c 2893
7d167afd
JJ
2894 /* Set up next argument register. For sibling calls on machines
2895 with register windows this should be the incoming register. */
2896#ifdef FUNCTION_INCOMING_ARG
2897 if (pass == 0)
2898 next_arg_reg = FUNCTION_INCOMING_ARG (args_so_far, VOIDmode,
2899 void_type_node, 1);
2900 else
2901#endif
2902 next_arg_reg = FUNCTION_ARG (args_so_far, VOIDmode,
2903 void_type_node, 1);
2904
0a1c58a2
JL
2905 /* All arguments and registers used for the call must be set up by
2906 now! */
2907
ebcd0b57 2908#ifdef PREFERRED_STACK_BOUNDARY
ce48579b
RH
2909 /* Stack must be properly aligned now. */
2910 if (pass && stack_pointer_delta % preferred_unit_stack_boundary)
d9a7d592 2911 abort ();
ebcd0b57
JH
2912#endif
2913
0a1c58a2
JL
2914 /* Generate the actual call instruction. */
2915 emit_call_1 (funexp, fndecl, funtype, unadjusted_args_size,
2916 args_size.constant, struct_value_size,
7d167afd 2917 next_arg_reg, valreg, old_inhibit_defer_pop, call_fusage,
f2d33f13 2918 flags);
0a1c58a2 2919
1503a7ec
JH
2920 /* Verify that we've deallocated all the stack we used. */
2921 if (pass
2922 && old_stack_allocated != stack_pointer_delta - pending_stack_adjust)
2923 abort();
2924
0a1c58a2
JL
2925 /* If call is cse'able, make appropriate pair of reg-notes around it.
2926 Test valreg so we don't crash; may safely ignore `const'
2927 if return type is void. Disable for PARALLEL return values, because
2928 we have no way to move such values into a pseudo register. */
8be9eb00
RH
2929 if (pass
2930 && (flags & (ECF_CONST | ECF_PURE))
2a8f6b90 2931 && valreg != 0 && GET_CODE (valreg) != PARALLEL)
9ae8ffe7 2932 {
0a1c58a2
JL
2933 rtx note = 0;
2934 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2935 rtx insns;
9ae8ffe7 2936
0a1c58a2
JL
2937 /* Mark the return value as a pointer if needed. */
2938 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
bdb429a5 2939 mark_reg_pointer (temp, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp))));
0a1c58a2
JL
2940
2941 /* Construct an "equal form" for the value which mentions all the
2942 arguments in order as well as the function name. */
5591ee6f
JH
2943 for (i = 0; i < num_actuals; i++)
2944 note = gen_rtx_EXPR_LIST (VOIDmode, args[i].initial_value, note);
0a1c58a2 2945 note = gen_rtx_EXPR_LIST (VOIDmode, funexp, note);
9ae8ffe7 2946
0a1c58a2
JL
2947 insns = get_insns ();
2948 end_sequence ();
9ae8ffe7 2949
2a8f6b90
JH
2950 if (flags & ECF_PURE)
2951 note = gen_rtx_EXPR_LIST (VOIDmode,
2952 gen_rtx_USE (VOIDmode,
2953 gen_rtx_MEM (BLKmode,
2954 gen_rtx_SCRATCH (VOIDmode))), note);
2955
0a1c58a2
JL
2956 emit_libcall_block (insns, temp, valreg, note);
2957
2958 valreg = temp;
2959 }
2a8f6b90 2960 else if (flags & (ECF_CONST | ECF_PURE))
0a1c58a2
JL
2961 {
2962 /* Otherwise, just write out the sequence without a note. */
2963 rtx insns = get_insns ();
9ae8ffe7 2964
0a1c58a2
JL
2965 end_sequence ();
2966 emit_insns (insns);
2967 }
f2d33f13 2968 else if (flags & ECF_MALLOC)
0a1c58a2
JL
2969 {
2970 rtx temp = gen_reg_rtx (GET_MODE (valreg));
2971 rtx last, insns;
2972
2973 /* The return value from a malloc-like function is a pointer. */
2974 if (TREE_CODE (TREE_TYPE (exp)) == POINTER_TYPE)
bdb429a5 2975 mark_reg_pointer (temp, BIGGEST_ALIGNMENT);
0a1c58a2
JL
2976
2977 emit_move_insn (temp, valreg);
2978
2979 /* The return value from a malloc-like function can not alias
2980 anything else. */
2981 last = get_last_insn ();
2982 REG_NOTES (last) =
2983 gen_rtx_EXPR_LIST (REG_NOALIAS, temp, REG_NOTES (last));
2984
2985 /* Write out the sequence. */
2986 insns = get_insns ();
2987 end_sequence ();
2988 emit_insns (insns);
2989 valreg = temp;
2990 }
51bbfa0c 2991
0a1c58a2
JL
2992 /* For calls to `setjmp', etc., inform flow.c it should complain
2993 if nonvolatile values are live. For functions that cannot return,
2994 inform flow that control does not fall through. */
51bbfa0c 2995
f2d33f13 2996 if ((flags & (ECF_RETURNS_TWICE | ECF_NORETURN | ECF_LONGJMP)) || pass == 0)
c2939b57 2997 {
0a1c58a2
JL
2998 /* The barrier or NOTE_INSN_SETJMP note must be emitted
2999 immediately after the CALL_INSN. Some ports emit more
3000 than just a CALL_INSN above, so we must search for it here. */
51bbfa0c 3001
0a1c58a2
JL
3002 rtx last = get_last_insn ();
3003 while (GET_CODE (last) != CALL_INSN)
3004 {
3005 last = PREV_INSN (last);
3006 /* There was no CALL_INSN? */
3007 if (last == before_call)
3008 abort ();
3009 }
51bbfa0c 3010
f2d33f13 3011 if (flags & ECF_RETURNS_TWICE)
0a1c58a2
JL
3012 {
3013 emit_note_after (NOTE_INSN_SETJMP, last);
3014 current_function_calls_setjmp = 1;
0a1c58a2
JL
3015 }
3016 else
3017 emit_barrier_after (last);
3018 }
51bbfa0c 3019
f2d33f13 3020 if (flags & ECF_LONGJMP)
4d393a0b 3021 current_function_calls_longjmp = 1;
51bbfa0c 3022
25a1fcb4
RK
3023 /* If this function is returning into a memory location marked as
3024 readonly, it means it is initializing that location. But we normally
3025 treat functions as not clobbering such locations, so we need to
3026 specify that this one does. */
3027 if (target != 0 && GET_CODE (target) == MEM
3028 && structure_value_addr != 0 && RTX_UNCHANGING_P (target))
3029 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
3030
0a1c58a2 3031 /* If value type not void, return an rtx for the value. */
51bbfa0c 3032
0a1c58a2
JL
3033 /* If there are cleanups to be called, don't use a hard reg as target.
3034 We need to double check this and see if it matters anymore. */
194c7c45
RH
3035 if (any_pending_cleanups (1))
3036 {
3037 if (target && REG_P (target)
3038 && REGNO (target) < FIRST_PSEUDO_REGISTER)
3039 target = 0;
3040 sibcall_failure = 1;
3041 }
51bbfa0c 3042
0a1c58a2
JL
3043 if (TYPE_MODE (TREE_TYPE (exp)) == VOIDmode
3044 || ignore)
29008b51 3045 {
0a1c58a2 3046 target = const0_rtx;
29008b51 3047 }
0a1c58a2
JL
3048 else if (structure_value_addr)
3049 {
3050 if (target == 0 || GET_CODE (target) != MEM)
3051 {
3052 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3053 memory_address (TYPE_MODE (TREE_TYPE (exp)),
3054 structure_value_addr));
3055 MEM_SET_IN_STRUCT_P (target,
3056 AGGREGATE_TYPE_P (TREE_TYPE (exp)));
3057 }
3058 }
3059 else if (pcc_struct_value)
cacbd532 3060 {
0a1c58a2
JL
3061 /* This is the special C++ case where we need to
3062 know what the true target was. We take care to
3063 never use this value more than once in one expression. */
3064 target = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)),
3065 copy_to_reg (valreg));
c6df88cb 3066 MEM_SET_IN_STRUCT_P (target, AGGREGATE_TYPE_P (TREE_TYPE (exp)));
cacbd532 3067 }
0a1c58a2
JL
3068 /* Handle calls that return values in multiple non-contiguous locations.
3069 The Irix 6 ABI has examples of this. */
3070 else if (GET_CODE (valreg) == PARALLEL)
3071 {
3072 int bytes = int_size_in_bytes (TREE_TYPE (exp));
cacbd532 3073
0a1c58a2
JL
3074 if (target == 0)
3075 {
3076 target = assign_stack_temp (TYPE_MODE (TREE_TYPE (exp)),
3077 bytes, 0);
3078 MEM_SET_IN_STRUCT_P (target, AGGREGATE_TYPE_P (TREE_TYPE (exp)));
3079 preserve_temp_slots (target);
3080 }
3081
3082 if (! rtx_equal_p (target, valreg))
3083 emit_group_store (target, valreg, bytes,
19caa751
RK
3084 TYPE_ALIGN (TREE_TYPE (exp)));
3085
0a1c58a2
JL
3086 /* We can not support sibling calls for this case. */
3087 sibcall_failure = 1;
3088 }
3089 else if (target
3090 && GET_MODE (target) == TYPE_MODE (TREE_TYPE (exp))
3091 && GET_MODE (target) == GET_MODE (valreg))
3092 {
3093 /* TARGET and VALREG cannot be equal at this point because the
3094 latter would not have REG_FUNCTION_VALUE_P true, while the
3095 former would if it were referring to the same register.
3096
3097 If they refer to the same register, this move will be a no-op,
3098 except when function inlining is being done. */
3099 emit_move_insn (target, valreg);
3100 }
3101 else if (TYPE_MODE (TREE_TYPE (exp)) == BLKmode)
3102 target = copy_blkmode_from_reg (target, valreg, TREE_TYPE (exp));
3103 else
3104 target = copy_to_reg (valreg);
51bbfa0c 3105
84b55618 3106#ifdef PROMOTE_FUNCTION_RETURN
0a1c58a2
JL
3107 /* If we promoted this return value, make the proper SUBREG. TARGET
3108 might be const0_rtx here, so be careful. */
3109 if (GET_CODE (target) == REG
3110 && TYPE_MODE (TREE_TYPE (exp)) != BLKmode
3111 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
3112 {
3113 tree type = TREE_TYPE (exp);
3114 int unsignedp = TREE_UNSIGNED (type);
84b55618 3115
0a1c58a2
JL
3116 /* If we don't promote as expected, something is wrong. */
3117 if (GET_MODE (target)
3118 != promote_mode (type, TYPE_MODE (type), &unsignedp, 1))
3119 abort ();
5d2ac65e 3120
0a1c58a2
JL
3121 target = gen_rtx_SUBREG (TYPE_MODE (type), target, 0);
3122 SUBREG_PROMOTED_VAR_P (target) = 1;
3123 SUBREG_PROMOTED_UNSIGNED_P (target) = unsignedp;
3124 }
84b55618
RK
3125#endif
3126
0a1c58a2
JL
3127 /* If size of args is variable or this was a constructor call for a stack
3128 argument, restore saved stack-pointer value. */
51bbfa0c 3129
0a1c58a2
JL
3130 if (old_stack_level)
3131 {
3132 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
3133 pending_stack_adjust = old_pending_adj;
0a1c58a2
JL
3134 stack_arg_under_construction = old_stack_arg_under_construction;
3135 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3136 stack_usage_map = initial_stack_usage_map;
0a1c58a2
JL
3137 sibcall_failure = 1;
3138 }
f8a097cd 3139 else if (ACCUMULATE_OUTGOING_ARGS && pass)
0a1c58a2 3140 {
51bbfa0c 3141#ifdef REG_PARM_STACK_SPACE
0a1c58a2
JL
3142 if (save_area)
3143 {
3144 restore_fixed_argument_area (save_area, argblock,
3145 high_to_save, low_to_save);
0a1c58a2 3146 }
b94301c2 3147#endif
51bbfa0c 3148
0a1c58a2
JL
3149 /* If we saved any argument areas, restore them. */
3150 for (i = 0; i < num_actuals; i++)
3151 if (args[i].save_area)
3152 {
3153 enum machine_mode save_mode = GET_MODE (args[i].save_area);
3154 rtx stack_area
3155 = gen_rtx_MEM (save_mode,
3156 memory_address (save_mode,
3157 XEXP (args[i].stack_slot, 0)));
3158
3159 if (save_mode != BLKmode)
3160 emit_move_insn (stack_area, args[i].save_area);
3161 else
3162 emit_block_move (stack_area,
3163 validize_mem (args[i].save_area),
3164 GEN_INT (args[i].size.constant),
19caa751 3165 PARM_BOUNDARY);
0a1c58a2 3166 }
51bbfa0c 3167
0a1c58a2
JL
3168 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3169 stack_usage_map = initial_stack_usage_map;
3170 }
51bbfa0c 3171
0a1c58a2
JL
3172 /* If this was alloca, record the new stack level for nonlocal gotos.
3173 Check for the handler slots since we might not have a save area
3174 for non-local gotos. */
59257ff7 3175
f2d33f13 3176 if ((flags & ECF_MAY_BE_ALLOCA) && nonlocal_goto_handler_slots != 0)
0a1c58a2 3177 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
51bbfa0c 3178
0a1c58a2
JL
3179 pop_temp_slots ();
3180
3181 /* Free up storage we no longer need. */
3182 for (i = 0; i < num_actuals; ++i)
3183 if (args[i].aligned_regs)
3184 free (args[i].aligned_regs);
3185
e245d3af
RH
3186 if (pass == 0)
3187 {
3188 /* Undo the fake expand_start_target_temps we did earlier. If
3189 there had been any cleanups created, we've already set
3190 sibcall_failure. */
3191 expand_end_target_temps ();
3192 }
3193
0a1c58a2
JL
3194 insns = get_insns ();
3195 end_sequence ();
3196
3197 if (pass == 0)
3198 {
3199 tail_call_insns = insns;
3200
7d167afd
JJ
3201 /* If something prevents making this a sibling call,
3202 zero out the sequence. */
3203 if (sibcall_failure)
0a1c58a2 3204 tail_call_insns = NULL_RTX;
0a1c58a2
JL
3205 /* Restore the pending stack adjustment now that we have
3206 finished generating the sibling call sequence. */
1503a7ec 3207
0a1c58a2 3208 pending_stack_adjust = save_pending_stack_adjust;
1503a7ec 3209 stack_pointer_delta = save_stack_pointer_delta;
4d393a0b
JH
3210
3211 /* Prepare arg structure for next iteration. */
3212 for (i = 0 ; i < num_actuals ; i++)
3213 {
3214 args[i].value = 0;
3215 args[i].aligned_regs = 0;
3216 args[i].stack = 0;
3217 }
0a1c58a2
JL
3218 }
3219 else
3220 normal_call_insns = insns;
3221 }
3222
3223 /* The function optimize_sibling_and_tail_recursive_calls doesn't
3224 handle CALL_PLACEHOLDERs inside other CALL_PLACEHOLDERs. This
3225 can happen if the arguments to this function call an inline
3226 function who's expansion contains another CALL_PLACEHOLDER.
3227
3228 If there are any C_Ps in any of these sequences, replace them
3229 with their normal call. */
3230
3231 for (insn = normal_call_insns; insn; insn = NEXT_INSN (insn))
3232 if (GET_CODE (insn) == CALL_INSN
3233 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3234 replace_call_placeholder (insn, sibcall_use_normal);
3235
3236 for (insn = tail_call_insns; insn; insn = NEXT_INSN (insn))
3237 if (GET_CODE (insn) == CALL_INSN
3238 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3239 replace_call_placeholder (insn, sibcall_use_normal);
3240
3241 for (insn = tail_recursion_insns; insn; insn = NEXT_INSN (insn))
3242 if (GET_CODE (insn) == CALL_INSN
3243 && GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
3244 replace_call_placeholder (insn, sibcall_use_normal);
3245
3246 /* If this was a potential tail recursion site, then emit a
3247 CALL_PLACEHOLDER with the normal and the tail recursion streams.
3248 One of them will be selected later. */
3249 if (tail_recursion_insns || tail_call_insns)
3250 {
3251 /* The tail recursion label must be kept around. We could expose
3252 its use in the CALL_PLACEHOLDER, but that creates unwanted edges
3253 and makes determining true tail recursion sites difficult.
3254
3255 So we set LABEL_PRESERVE_P here, then clear it when we select
3256 one of the call sequences after rtl generation is complete. */
3257 if (tail_recursion_insns)
3258 LABEL_PRESERVE_P (tail_recursion_label) = 1;
3259 emit_call_insn (gen_rtx_CALL_PLACEHOLDER (VOIDmode, normal_call_insns,
3260 tail_call_insns,
3261 tail_recursion_insns,
3262 tail_recursion_label));
3263 }
3264 else
3265 emit_insns (normal_call_insns);
51bbfa0c 3266
0a1c58a2 3267 currently_expanding_call--;
8e6a59fe 3268
51bbfa0c
RS
3269 return target;
3270}
3271\f
12a22e76
JM
3272/* Returns nonzero if FUN is the symbol for a library function which can
3273 not throw. */
3274
3275static int
3276libfunc_nothrow (fun)
3277 rtx fun;
3278{
3279 if (fun == throw_libfunc
3280 || fun == rethrow_libfunc
3281 || fun == sjthrow_libfunc
3282 || fun == sjpopnthrow_libfunc)
3283 return 0;
3284
3285 return 1;
3286}
43bc5f13 3287\f
de76b467
JH
3288/* Output a library call to function FUN (a SYMBOL_REF rtx).
3289 The RETVAL parameter specifies whether return value needs to be saved, other
3290 parameters are documented in the emit_library_call function bellow. */
3291static rtx
2a8f6b90 3292emit_library_call_value_1 (retval, orgfun, value, fn_type, outmode, nargs, p)
de76b467
JH
3293 int retval;
3294 rtx orgfun;
3295 rtx value;
2a8f6b90 3296 int fn_type;
de76b467
JH
3297 enum machine_mode outmode;
3298 int nargs;
3299 va_list p;
43bc5f13 3300{
3c0fca12
RH
3301 /* Total size in bytes of all the stack-parms scanned so far. */
3302 struct args_size args_size;
3303 /* Size of arguments before any adjustments (such as rounding). */
3304 struct args_size original_args_size;
3305 register int argnum;
3306 rtx fun;
3307 int inc;
3308 int count;
3309 struct args_size alignment_pad;
3310 rtx argblock = 0;
3311 CUMULATIVE_ARGS args_so_far;
3312 struct arg { rtx value; enum machine_mode mode; rtx reg; int partial;
3313 struct args_size offset; struct args_size size; rtx save_area; };
3314 struct arg *argvec;
3315 int old_inhibit_defer_pop = inhibit_defer_pop;
3316 rtx call_fusage = 0;
3317 rtx mem_value = 0;
5591ee6f 3318 rtx valreg;
3c0fca12
RH
3319 int pcc_struct_value = 0;
3320 int struct_value_size = 0;
f2d33f13 3321 int flags = 0;
3c0fca12 3322 int reg_parm_stack_space = 0;
3c0fca12 3323 int needed;
3c0fca12 3324
f73ad30e 3325#ifdef REG_PARM_STACK_SPACE
3c0fca12
RH
3326 /* Define the boundary of the register parm stack space that needs to be
3327 save, if any. */
3328 int low_to_save = -1, high_to_save = 0;
3329 rtx save_area = 0; /* Place that it is saved */
3330#endif
3331
3c0fca12
RH
3332 /* Size of the stack reserved for parameter registers. */
3333 int initial_highest_arg_in_use = highest_outgoing_arg_in_use;
3334 char *initial_stack_usage_map = stack_usage_map;
3c0fca12
RH
3335
3336#ifdef REG_PARM_STACK_SPACE
3337#ifdef MAYBE_REG_PARM_STACK_SPACE
3338 reg_parm_stack_space = MAYBE_REG_PARM_STACK_SPACE;
3339#else
3340 reg_parm_stack_space = REG_PARM_STACK_SPACE ((tree) 0);
3341#endif
3342#endif
3343
2a8f6b90 3344 if (fn_type == 1)
f2d33f13 3345 flags |= ECF_CONST;
2a8f6b90
JH
3346 else if (fn_type == 2)
3347 flags |= ECF_PURE;
3c0fca12
RH
3348 fun = orgfun;
3349
f2d33f13
JH
3350 if (libfunc_nothrow (fun))
3351 flags |= ECF_NOTHROW;
3c0fca12
RH
3352
3353#ifdef PREFERRED_STACK_BOUNDARY
3354 /* Ensure current function's preferred stack boundary is at least
3355 what we need. */
3356 if (cfun->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
3357 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
3358#endif
3359
3360 /* If this kind of value comes back in memory,
3361 decide where in memory it should come back. */
de76b467 3362 if (outmode != VOIDmode && aggregate_value_p (type_for_mode (outmode, 0)))
3c0fca12
RH
3363 {
3364#ifdef PCC_STATIC_STRUCT_RETURN
3365 rtx pointer_reg
3366 = hard_function_value (build_pointer_type (type_for_mode (outmode, 0)),
3367 0, 0);
3368 mem_value = gen_rtx_MEM (outmode, pointer_reg);
3369 pcc_struct_value = 1;
3370 if (value == 0)
3371 value = gen_reg_rtx (outmode);
3372#else /* not PCC_STATIC_STRUCT_RETURN */
3373 struct_value_size = GET_MODE_SIZE (outmode);
3374 if (value != 0 && GET_CODE (value) == MEM)
3375 mem_value = value;
3376 else
3377 mem_value = assign_stack_temp (outmode, GET_MODE_SIZE (outmode), 0);
3378#endif
3379
3380 /* This call returns a big structure. */
2a8f6b90 3381 flags &= ~(ECF_CONST | ECF_PURE);
3c0fca12
RH
3382 }
3383
3384 /* ??? Unfinished: must pass the memory address as an argument. */
3385
3386 /* Copy all the libcall-arguments out of the varargs data
3387 and into a vector ARGVEC.
3388
3389 Compute how to pass each argument. We only support a very small subset
3390 of the full argument passing conventions to limit complexity here since
3391 library functions shouldn't have many args. */
3392
3393 argvec = (struct arg *) alloca ((nargs + 1) * sizeof (struct arg));
3394 bzero ((char *) argvec, (nargs + 1) * sizeof (struct arg));
3395
3396 INIT_CUMULATIVE_ARGS (args_so_far, NULL_TREE, fun, 0);
3397
3398 args_size.constant = 0;
3399 args_size.var = 0;
3400
3401 count = 0;
3402
5591ee6f
JH
3403 /* Now we are about to start emitting insns that can be deleted
3404 if a libcall is deleted. */
2a8f6b90 3405 if (flags & (ECF_CONST | ECF_PURE))
5591ee6f
JH
3406 start_sequence ();
3407
3c0fca12
RH
3408 push_temp_slots ();
3409
3410 /* If there's a structure value address to be passed,
3411 either pass it in the special place, or pass it as an extra argument. */
3412 if (mem_value && struct_value_rtx == 0 && ! pcc_struct_value)
3413 {
3414 rtx addr = XEXP (mem_value, 0);
3415 nargs++;
3416
3417 /* Make sure it is a reasonable operand for a move or push insn. */
3418 if (GET_CODE (addr) != REG && GET_CODE (addr) != MEM
3419 && ! (CONSTANT_P (addr) && LEGITIMATE_CONSTANT_P (addr)))
3420 addr = force_operand (addr, NULL_RTX);
3421
3422 argvec[count].value = addr;
3423 argvec[count].mode = Pmode;
3424 argvec[count].partial = 0;
3425
3426 argvec[count].reg = FUNCTION_ARG (args_so_far, Pmode, NULL_TREE, 1);
3427#ifdef FUNCTION_ARG_PARTIAL_NREGS
3428 if (FUNCTION_ARG_PARTIAL_NREGS (args_so_far, Pmode, NULL_TREE, 1))
3429 abort ();
3430#endif
3431
3432 locate_and_pad_parm (Pmode, NULL_TREE,
a4d5044f
CM
3433#ifdef STACK_PARMS_IN_REG_PARM_AREA
3434 1,
3435#else
3436 argvec[count].reg != 0,
3437#endif
3c0fca12
RH
3438 NULL_TREE, &args_size, &argvec[count].offset,
3439 &argvec[count].size, &alignment_pad);
3440
3441
3442 if (argvec[count].reg == 0 || argvec[count].partial != 0
3443 || reg_parm_stack_space > 0)
3444 args_size.constant += argvec[count].size.constant;
3445
3446 FUNCTION_ARG_ADVANCE (args_so_far, Pmode, (tree) 0, 1);
3447
3448 count++;
3449 }
3450
3451 for (; count < nargs; count++)
3452 {
3453 rtx val = va_arg (p, rtx);
3454 enum machine_mode mode = va_arg (p, enum machine_mode);
3455
3456 /* We cannot convert the arg value to the mode the library wants here;
3457 must do it earlier where we know the signedness of the arg. */
3458 if (mode == BLKmode
3459 || (GET_MODE (val) != mode && GET_MODE (val) != VOIDmode))
3460 abort ();
3461
3462 /* On some machines, there's no way to pass a float to a library fcn.
3463 Pass it as a double instead. */
3464#ifdef LIBGCC_NEEDS_DOUBLE
3465 if (LIBGCC_NEEDS_DOUBLE && mode == SFmode)
3466 val = convert_modes (DFmode, SFmode, val, 0), mode = DFmode;
3467#endif
3468
3469 /* There's no need to call protect_from_queue, because
3470 either emit_move_insn or emit_push_insn will do that. */
3471
3472 /* Make sure it is a reasonable operand for a move or push insn. */
3473 if (GET_CODE (val) != REG && GET_CODE (val) != MEM
3474 && ! (CONSTANT_P (val) && LEGITIMATE_CONSTANT_P (val)))
3475 val = force_operand (val, NULL_RTX);
3476
3477#ifdef FUNCTION_ARG_PASS_BY_REFERENCE
3478 if (FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far, mode, NULL_TREE, 1))
3479 {
3480 /* We do not support FUNCTION_ARG_CALLEE_COPIES here since it can
3481 be viewed as just an efficiency improvement. */
3482 rtx slot = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
3483 emit_move_insn (slot, val);
de76b467 3484 val = force_operand (XEXP (slot, 0), NULL_RTX);
3c0fca12
RH
3485 mode = Pmode;
3486 }
3487#endif
3488
3489 argvec[count].value = val;
3490 argvec[count].mode = mode;
3491
3492 argvec[count].reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1);
3493
3494#ifdef FUNCTION_ARG_PARTIAL_NREGS
3495 argvec[count].partial
3496 = FUNCTION_ARG_PARTIAL_NREGS (args_so_far, mode, NULL_TREE, 1);
3497#else
3498 argvec[count].partial = 0;
3499#endif
3500
3501 locate_and_pad_parm (mode, NULL_TREE,
a4d5044f
CM
3502#ifdef STACK_PARMS_IN_REG_PARM_AREA
3503 1,
3504#else
3505 argvec[count].reg != 0,
3506#endif
3c0fca12
RH
3507 NULL_TREE, &args_size, &argvec[count].offset,
3508 &argvec[count].size, &alignment_pad);
3509
3510 if (argvec[count].size.var)
3511 abort ();
3512
3513 if (reg_parm_stack_space == 0 && argvec[count].partial)
3514 argvec[count].size.constant -= argvec[count].partial * UNITS_PER_WORD;
3515
3516 if (argvec[count].reg == 0 || argvec[count].partial != 0
3517 || reg_parm_stack_space > 0)
3518 args_size.constant += argvec[count].size.constant;
3519
3520 FUNCTION_ARG_ADVANCE (args_so_far, mode, (tree) 0, 1);
3521 }
3c0fca12
RH
3522
3523#ifdef FINAL_REG_PARM_STACK_SPACE
3524 reg_parm_stack_space = FINAL_REG_PARM_STACK_SPACE (args_size.constant,
3525 args_size.var);
3526#endif
3527 /* If this machine requires an external definition for library
3528 functions, write one out. */
3529 assemble_external_libcall (fun);
3530
3531 original_args_size = args_size;
3532#ifdef PREFERRED_STACK_BOUNDARY
1503a7ec
JH
3533 args_size.constant = (((args_size.constant
3534 + stack_pointer_delta
3535 + STACK_BYTES - 1)
3536 / STACK_BYTES
3537 * STACK_BYTES)
3538 - stack_pointer_delta);
3c0fca12
RH
3539#endif
3540
3541 args_size.constant = MAX (args_size.constant,
3542 reg_parm_stack_space);
3543
3544#ifndef OUTGOING_REG_PARM_STACK_SPACE
3545 args_size.constant -= reg_parm_stack_space;
3546#endif
3547
3548 if (args_size.constant > current_function_outgoing_args_size)
3549 current_function_outgoing_args_size = args_size.constant;
3550
f73ad30e
JH
3551 if (ACCUMULATE_OUTGOING_ARGS)
3552 {
3553 /* Since the stack pointer will never be pushed, it is possible for
3554 the evaluation of a parm to clobber something we have already
3555 written to the stack. Since most function calls on RISC machines
3556 do not use the stack, this is uncommon, but must work correctly.
3c0fca12 3557
f73ad30e
JH
3558 Therefore, we save any area of the stack that was already written
3559 and that we are using. Here we set up to do this by making a new
3560 stack usage map from the old one.
3c0fca12 3561
f73ad30e
JH
3562 Another approach might be to try to reorder the argument
3563 evaluations to avoid this conflicting stack usage. */
3c0fca12 3564
f73ad30e 3565 needed = args_size.constant;
3c0fca12
RH
3566
3567#ifndef OUTGOING_REG_PARM_STACK_SPACE
f73ad30e
JH
3568 /* Since we will be writing into the entire argument area, the
3569 map must be allocated for its entire size, not just the part that
3570 is the responsibility of the caller. */
3571 needed += reg_parm_stack_space;
3c0fca12
RH
3572#endif
3573
3574#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3575 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3576 needed + 1);
3c0fca12 3577#else
f73ad30e
JH
3578 highest_outgoing_arg_in_use = MAX (initial_highest_arg_in_use,
3579 needed);
3c0fca12 3580#endif
f73ad30e 3581 stack_usage_map = (char *) alloca (highest_outgoing_arg_in_use);
3c0fca12 3582
f73ad30e
JH
3583 if (initial_highest_arg_in_use)
3584 bcopy (initial_stack_usage_map, stack_usage_map,
3585 initial_highest_arg_in_use);
3c0fca12 3586
f73ad30e
JH
3587 if (initial_highest_arg_in_use != highest_outgoing_arg_in_use)
3588 bzero (&stack_usage_map[initial_highest_arg_in_use],
3589 highest_outgoing_arg_in_use - initial_highest_arg_in_use);
3590 needed = 0;
3c0fca12 3591
f73ad30e
JH
3592 /* The address of the outgoing argument list must not be copied to a
3593 register here, because argblock would be left pointing to the
3594 wrong place after the call to allocate_dynamic_stack_space below.
3595 */
3c0fca12 3596
f73ad30e
JH
3597 argblock = virtual_outgoing_args_rtx;
3598 }
3599 else
3600 {
3601 if (!PUSH_ARGS)
3602 argblock = push_block (GEN_INT (args_size.constant), 0, 0);
3603 }
3c0fca12 3604
3c0fca12
RH
3605#ifdef PREFERRED_STACK_BOUNDARY
3606 /* If we push args individually in reverse order, perform stack alignment
3607 before the first push (the last arg). */
f73ad30e 3608 if (argblock == 0 && PUSH_ARGS_REVERSED)
3c0fca12
RH
3609 anti_adjust_stack (GEN_INT (args_size.constant
3610 - original_args_size.constant));
3611#endif
3c0fca12 3612
f73ad30e
JH
3613 if (PUSH_ARGS_REVERSED)
3614 {
3615 inc = -1;
3616 argnum = nargs - 1;
3617 }
3618 else
3619 {
3620 inc = 1;
3621 argnum = 0;
3622 }
3c0fca12 3623
f73ad30e
JH
3624#ifdef REG_PARM_STACK_SPACE
3625 if (ACCUMULATE_OUTGOING_ARGS)
3626 {
3627 /* The argument list is the property of the called routine and it
3628 may clobber it. If the fixed area has been used for previous
3629 parameters, we must save and restore it.
3c0fca12 3630
f73ad30e 3631 Here we compute the boundary of the that needs to be saved, if any. */
3c0fca12
RH
3632
3633#ifdef ARGS_GROW_DOWNWARD
f73ad30e 3634 for (count = 0; count < reg_parm_stack_space + 1; count++)
3c0fca12 3635#else
f73ad30e 3636 for (count = 0; count < reg_parm_stack_space; count++)
3c0fca12 3637#endif
f73ad30e
JH
3638 {
3639 if (count >= highest_outgoing_arg_in_use
3640 || stack_usage_map[count] == 0)
3641 continue;
3c0fca12 3642
f73ad30e
JH
3643 if (low_to_save == -1)
3644 low_to_save = count;
3c0fca12 3645
f73ad30e
JH
3646 high_to_save = count;
3647 }
3c0fca12 3648
f73ad30e
JH
3649 if (low_to_save >= 0)
3650 {
3651 int num_to_save = high_to_save - low_to_save + 1;
3652 enum machine_mode save_mode
3653 = mode_for_size (num_to_save * BITS_PER_UNIT, MODE_INT, 1);
3654 rtx stack_area;
3c0fca12 3655
f73ad30e
JH
3656 /* If we don't have the required alignment, must do this in BLKmode. */
3657 if ((low_to_save & (MIN (GET_MODE_SIZE (save_mode),
3658 BIGGEST_ALIGNMENT / UNITS_PER_WORD) - 1)))
3659 save_mode = BLKmode;
3c0fca12
RH
3660
3661#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3662 stack_area = gen_rtx_MEM (save_mode,
3663 memory_address (save_mode,
3664 plus_constant (argblock,
3665 - high_to_save)));
3c0fca12 3666#else
f73ad30e
JH
3667 stack_area = gen_rtx_MEM (save_mode,
3668 memory_address (save_mode,
3669 plus_constant (argblock,
3670 low_to_save)));
3c0fca12 3671#endif
f73ad30e
JH
3672 if (save_mode == BLKmode)
3673 {
3674 save_area = assign_stack_temp (BLKmode, num_to_save, 0);
3675 emit_block_move (validize_mem (save_area), stack_area,
19caa751 3676 GEN_INT (num_to_save), PARM_BOUNDARY);
f73ad30e
JH
3677 }
3678 else
3679 {
3680 save_area = gen_reg_rtx (save_mode);
3681 emit_move_insn (save_area, stack_area);
3682 }
3c0fca12
RH
3683 }
3684 }
3685#endif
3686
3687 /* Push the args that need to be pushed. */
3688
3689 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3690 are to be pushed. */
3691 for (count = 0; count < nargs; count++, argnum += inc)
3692 {
3693 register enum machine_mode mode = argvec[argnum].mode;
3694 register rtx val = argvec[argnum].value;
3695 rtx reg = argvec[argnum].reg;
3696 int partial = argvec[argnum].partial;
f73ad30e 3697 int lower_bound = 0, upper_bound = 0, i;
3c0fca12
RH
3698
3699 if (! (reg != 0 && partial == 0))
3700 {
f73ad30e
JH
3701 if (ACCUMULATE_OUTGOING_ARGS)
3702 {
f8a097cd
JH
3703 /* If this is being stored into a pre-allocated, fixed-size,
3704 stack area, save any previous data at that location. */
3c0fca12
RH
3705
3706#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3707 /* stack_slot is negative, but we want to index stack_usage_map
3708 with positive values. */
3709 upper_bound = -argvec[argnum].offset.constant + 1;
3710 lower_bound = upper_bound - argvec[argnum].size.constant;
3c0fca12 3711#else
f73ad30e
JH
3712 lower_bound = argvec[argnum].offset.constant;
3713 upper_bound = lower_bound + argvec[argnum].size.constant;
3c0fca12
RH
3714#endif
3715
f73ad30e
JH
3716 for (i = lower_bound; i < upper_bound; i++)
3717 if (stack_usage_map[i]
f8a097cd
JH
3718 /* Don't store things in the fixed argument area at this
3719 point; it has already been saved. */
f73ad30e
JH
3720 && i > reg_parm_stack_space)
3721 break;
3c0fca12 3722
f73ad30e
JH
3723 if (i != upper_bound)
3724 {
f8a097cd
JH
3725 /* We need to make a save area. See what mode we can make
3726 it. */
f73ad30e 3727 enum machine_mode save_mode
f8a097cd
JH
3728 = mode_for_size (argvec[argnum].size.constant
3729 * BITS_PER_UNIT,
f73ad30e
JH
3730 MODE_INT, 1);
3731 rtx stack_area
3732 = gen_rtx_MEM
3733 (save_mode,
3734 memory_address
3735 (save_mode,
3736 plus_constant (argblock,
3737 argvec[argnum].offset.constant)));
3738 argvec[argnum].save_area = gen_reg_rtx (save_mode);
3739
3740 emit_move_insn (argvec[argnum].save_area, stack_area);
3741 }
3c0fca12 3742 }
19caa751 3743
3c0fca12
RH
3744 emit_push_insn (val, mode, NULL_TREE, NULL_RTX, 0, partial, reg, 0,
3745 argblock, GEN_INT (argvec[argnum].offset.constant),
3746 reg_parm_stack_space, ARGS_SIZE_RTX (alignment_pad));
3747
3c0fca12 3748 /* Now mark the segment we just used. */
f73ad30e
JH
3749 if (ACCUMULATE_OUTGOING_ARGS)
3750 for (i = lower_bound; i < upper_bound; i++)
3751 stack_usage_map[i] = 1;
3c0fca12
RH
3752
3753 NO_DEFER_POP;
3754 }
3755 }
3756
3c0fca12
RH
3757#ifdef PREFERRED_STACK_BOUNDARY
3758 /* If we pushed args in forward order, perform stack alignment
3759 after pushing the last arg. */
f73ad30e 3760 if (argblock == 0 && !PUSH_ARGS_REVERSED)
3c0fca12
RH
3761 anti_adjust_stack (GEN_INT (args_size.constant
3762 - original_args_size.constant));
3763#endif
3c0fca12 3764
f73ad30e
JH
3765 if (PUSH_ARGS_REVERSED)
3766 argnum = nargs - 1;
3767 else
3768 argnum = 0;
3c0fca12
RH
3769
3770 fun = prepare_call_address (fun, NULL_TREE, &call_fusage, 0);
3771
3772 /* Now load any reg parms into their regs. */
3773
3774 /* ARGNUM indexes the ARGVEC array in the order in which the arguments
3775 are to be pushed. */
3776 for (count = 0; count < nargs; count++, argnum += inc)
3777 {
3778 register rtx val = argvec[argnum].value;
3779 rtx reg = argvec[argnum].reg;
3780 int partial = argvec[argnum].partial;
3781
3782 /* Handle calls that pass values in multiple non-contiguous
3783 locations. The PA64 has examples of this for library calls. */
3784 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3785 emit_group_load (reg, val,
3786 GET_MODE_SIZE (GET_MODE (val)),
3787 GET_MODE_ALIGNMENT (GET_MODE (val)));
3788 else if (reg != 0 && partial == 0)
3789 emit_move_insn (reg, val);
3790
3791 NO_DEFER_POP;
3792 }
3793
3c0fca12
RH
3794 /* Any regs containing parms remain in use through the call. */
3795 for (count = 0; count < nargs; count++)
3796 {
3797 rtx reg = argvec[count].reg;
3798 if (reg != 0 && GET_CODE (reg) == PARALLEL)
3799 use_group_regs (&call_fusage, reg);
3800 else if (reg != 0)
3801 use_reg (&call_fusage, reg);
3802 }
3803
3804 /* Pass the function the address in which to return a structure value. */
3805 if (mem_value != 0 && struct_value_rtx != 0 && ! pcc_struct_value)
3806 {
3807 emit_move_insn (struct_value_rtx,
3808 force_reg (Pmode,
3809 force_operand (XEXP (mem_value, 0),
3810 NULL_RTX)));
3811 if (GET_CODE (struct_value_rtx) == REG)
3812 use_reg (&call_fusage, struct_value_rtx);
3813 }
3814
3815 /* Don't allow popping to be deferred, since then
3816 cse'ing of library calls could delete a call and leave the pop. */
3817 NO_DEFER_POP;
5591ee6f
JH
3818 valreg = (mem_value == 0 && outmode != VOIDmode
3819 ? hard_libcall_value (outmode) : NULL_RTX);
3c0fca12 3820
ebcd0b57 3821#ifdef PREFERRED_STACK_BOUNDARY
ce48579b 3822 /* Stack must be properly aligned now. */
ebcd0b57
JH
3823 if (stack_pointer_delta & (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT - 1))
3824 abort();
3825#endif
3826
3c0fca12
RH
3827 /* We pass the old value of inhibit_defer_pop + 1 to emit_call_1, which
3828 will set inhibit_defer_pop to that value. */
de76b467
JH
3829 /* The return type is needed to decide how many bytes the function pops.
3830 Signedness plays no role in that, so for simplicity, we pretend it's
3831 always signed. We also assume that the list of arguments passed has
3832 no impact, so we pretend it is unknown. */
3c0fca12
RH
3833
3834 emit_call_1 (fun,
3835 get_identifier (XSTR (orgfun, 0)),
de76b467
JH
3836 build_function_type (outmode == VOIDmode ? void_type_node
3837 : type_for_mode (outmode, 0), NULL_TREE),
3c0fca12
RH
3838 original_args_size.constant, args_size.constant,
3839 struct_value_size,
3840 FUNCTION_ARG (args_so_far, VOIDmode, void_type_node, 1),
5591ee6f 3841 valreg,
f2d33f13 3842 old_inhibit_defer_pop + 1, call_fusage, flags);
3c0fca12
RH
3843
3844 /* Now restore inhibit_defer_pop to its actual original value. */
3845 OK_DEFER_POP;
3846
5591ee6f
JH
3847 /* If call is cse'able, make appropriate pair of reg-notes around it.
3848 Test valreg so we don't crash; may safely ignore `const'
3849 if return type is void. Disable for PARALLEL return values, because
3850 we have no way to move such values into a pseudo register. */
2a8f6b90 3851 if ((flags & (ECF_CONST | ECF_PURE))
5591ee6f
JH
3852 && valreg != 0 && GET_CODE (valreg) != PARALLEL)
3853 {
3854 rtx note = 0;
3855 rtx temp = gen_reg_rtx (GET_MODE (valreg));
3856 rtx insns;
3857 int i;
3858
3859 /* Construct an "equal form" for the value which mentions all the
3860 arguments in order as well as the function name. */
3861 for (i = 0; i < nargs; i++)
3862 note = gen_rtx_EXPR_LIST (VOIDmode, argvec[i].value, note);
3863 note = gen_rtx_EXPR_LIST (VOIDmode, fun, note);
3864
3865 insns = get_insns ();
3866 end_sequence ();
3867
2a8f6b90
JH
3868 if (flags & ECF_PURE)
3869 note = gen_rtx_EXPR_LIST (VOIDmode,
3870 gen_rtx_USE (VOIDmode,
3871 gen_rtx_MEM (BLKmode,
3872 gen_rtx_SCRATCH (VOIDmode))), note);
3873
5591ee6f
JH
3874 emit_libcall_block (insns, temp, valreg, note);
3875
3876 valreg = temp;
3877 }
2a8f6b90 3878 else if (flags & (ECF_CONST | ECF_PURE))
5591ee6f
JH
3879 {
3880 /* Otherwise, just write out the sequence without a note. */
3881 rtx insns = get_insns ();
3882
3883 end_sequence ();
3884 emit_insns (insns);
3885 }
3c0fca12
RH
3886 pop_temp_slots ();
3887
3888 /* Copy the value to the right place. */
de76b467 3889 if (outmode != VOIDmode && retval)
3c0fca12
RH
3890 {
3891 if (mem_value)
3892 {
3893 if (value == 0)
3894 value = mem_value;
3895 if (value != mem_value)
3896 emit_move_insn (value, mem_value);
3897 }
3898 else if (value != 0)
3899 emit_move_insn (value, hard_libcall_value (outmode));
3900 else
3901 value = hard_libcall_value (outmode);
3902 }
3903
f73ad30e 3904 if (ACCUMULATE_OUTGOING_ARGS)
3c0fca12 3905 {
f73ad30e
JH
3906#ifdef REG_PARM_STACK_SPACE
3907 if (save_area)
3908 {
3909 enum machine_mode save_mode = GET_MODE (save_area);
3c0fca12 3910#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
3911 rtx stack_area
3912 = gen_rtx_MEM (save_mode,
3913 memory_address (save_mode,
3914 plus_constant (argblock,
3915 - high_to_save)));
3c0fca12 3916#else
f73ad30e
JH
3917 rtx stack_area
3918 = gen_rtx_MEM (save_mode,
3919 memory_address (save_mode,
3920 plus_constant (argblock, low_to_save)));
3c0fca12 3921#endif
f73ad30e
JH
3922 if (save_mode != BLKmode)
3923 emit_move_insn (stack_area, save_area);
3924 else
3925 emit_block_move (stack_area, validize_mem (save_area),
3926 GEN_INT (high_to_save - low_to_save + 1),
19caa751 3927 PARM_BOUNDARY);
f73ad30e 3928 }
3c0fca12 3929#endif
f73ad30e
JH
3930
3931 /* If we saved any argument areas, restore them. */
3932 for (count = 0; count < nargs; count++)
3933 if (argvec[count].save_area)
3934 {
3935 enum machine_mode save_mode = GET_MODE (argvec[count].save_area);
3936 rtx stack_area
3937 = gen_rtx_MEM (save_mode,
3938 memory_address
3939 (save_mode,
3940 plus_constant (argblock,
3941 argvec[count].offset.constant)));
3942
3943 emit_move_insn (stack_area, argvec[count].save_area);
3944 }
3c0fca12 3945
f73ad30e
JH
3946 highest_outgoing_arg_in_use = initial_highest_arg_in_use;
3947 stack_usage_map = initial_stack_usage_map;
3948 }
43bc5f13 3949
de76b467
JH
3950 return value;
3951
3952}
3953\f
3954/* Output a library call to function FUN (a SYMBOL_REF rtx)
3955 (emitting the queue unless NO_QUEUE is nonzero),
3956 for a value of mode OUTMODE,
3957 with NARGS different arguments, passed as alternating rtx values
3958 and machine_modes to convert them to.
3959 The rtx values should have been passed through protect_from_queue already.
3960
2a8f6b90
JH
3961 FN_TYPE will is zero for `normal' calls, one for `const' calls, wich
3962 which will be enclosed in REG_LIBCALL/REG_RETVAL notes and two for `pure'
3963 calls, that are handled like `const' calls with extra
3964 (use (memory (scratch)). */
de76b467
JH
3965
3966void
2a8f6b90 3967emit_library_call VPARAMS((rtx orgfun, int fn_type, enum machine_mode outmode,
de76b467
JH
3968 int nargs, ...))
3969{
3970#ifndef ANSI_PROTOTYPES
3971 rtx orgfun;
2a8f6b90 3972 int fn_type;
de76b467
JH
3973 enum machine_mode outmode;
3974 int nargs;
3975#endif
3976 va_list p;
3977
3978 VA_START (p, nargs);
3979
3980#ifndef ANSI_PROTOTYPES
3981 orgfun = va_arg (p, rtx);
2a8f6b90 3982 fn_type = va_arg (p, int);
de76b467
JH
3983 outmode = va_arg (p, enum machine_mode);
3984 nargs = va_arg (p, int);
3985#endif
3986
2a8f6b90 3987 emit_library_call_value_1 (0, orgfun, NULL_RTX, fn_type, outmode, nargs, p);
de76b467
JH
3988
3989 va_end (p);
3990}
3991\f
3992/* Like emit_library_call except that an extra argument, VALUE,
3993 comes second and says where to store the result.
3994 (If VALUE is zero, this function chooses a convenient way
3995 to return the value.
3996
3997 This function returns an rtx for where the value is to be found.
3998 If VALUE is nonzero, VALUE is returned. */
3999
4000rtx
2a8f6b90 4001emit_library_call_value VPARAMS((rtx orgfun, rtx value, int fn_type,
de76b467
JH
4002 enum machine_mode outmode, int nargs, ...))
4003{
4004#ifndef ANSI_PROTOTYPES
4005 rtx orgfun;
4006 rtx value;
2a8f6b90 4007 int fn_type;
de76b467
JH
4008 enum machine_mode outmode;
4009 int nargs;
4010#endif
4011 va_list p;
4012
4013 VA_START (p, nargs);
4014
4015#ifndef ANSI_PROTOTYPES
4016 orgfun = va_arg (p, rtx);
4017 value = va_arg (p, rtx);
2a8f6b90 4018 fn_type = va_arg (p, int);
de76b467
JH
4019 outmode = va_arg (p, enum machine_mode);
4020 nargs = va_arg (p, int);
4021#endif
4022
2a8f6b90 4023 value = emit_library_call_value_1 (1, orgfun, value, fn_type, outmode, nargs, p);
de76b467
JH
4024
4025 va_end (p);
4026
fac0ad80 4027 return value;
322e3e34
RK
4028}
4029\f
51bbfa0c
RS
4030#if 0
4031/* Return an rtx which represents a suitable home on the stack
4032 given TYPE, the type of the argument looking for a home.
4033 This is called only for BLKmode arguments.
4034
4035 SIZE is the size needed for this target.
4036 ARGS_ADDR is the address of the bottom of the argument block for this call.
4037 OFFSET describes this parameter's offset into ARGS_ADDR. It is meaningless
4038 if this machine uses push insns. */
4039
4040static rtx
4041target_for_arg (type, size, args_addr, offset)
4042 tree type;
4043 rtx size;
4044 rtx args_addr;
4045 struct args_size offset;
4046{
4047 rtx target;
4048 rtx offset_rtx = ARGS_SIZE_RTX (offset);
4049
4050 /* We do not call memory_address if possible,
4051 because we want to address as close to the stack
4052 as possible. For non-variable sized arguments,
4053 this will be stack-pointer relative addressing. */
4054 if (GET_CODE (offset_rtx) == CONST_INT)
4055 target = plus_constant (args_addr, INTVAL (offset_rtx));
4056 else
4057 {
4058 /* I have no idea how to guarantee that this
4059 will work in the presence of register parameters. */
38a448ca 4060 target = gen_rtx_PLUS (Pmode, args_addr, offset_rtx);
51bbfa0c
RS
4061 target = memory_address (QImode, target);
4062 }
4063
38a448ca 4064 return gen_rtx_MEM (BLKmode, target);
51bbfa0c
RS
4065}
4066#endif
4067\f
4068/* Store a single argument for a function call
4069 into the register or memory area where it must be passed.
4070 *ARG describes the argument value and where to pass it.
4071
4072 ARGBLOCK is the address of the stack-block for all the arguments,
d45cf215 4073 or 0 on a machine where arguments are pushed individually.
51bbfa0c
RS
4074
4075 MAY_BE_ALLOCA nonzero says this could be a call to `alloca'
4076 so must be careful about how the stack is used.
4077
4078 VARIABLE_SIZE nonzero says that this was a variable-sized outgoing
4079 argument stack. This is used if ACCUMULATE_OUTGOING_ARGS to indicate
4080 that we need not worry about saving and restoring the stack.
4081
4082 FNDECL is the declaration of the function we are calling. */
4083
4084static void
f8a097cd 4085store_one_arg (arg, argblock, flags, variable_size,
6f90e075 4086 reg_parm_stack_space)
51bbfa0c
RS
4087 struct arg_data *arg;
4088 rtx argblock;
f8a097cd 4089 int flags;
0f9b3ea6 4090 int variable_size ATTRIBUTE_UNUSED;
6f90e075 4091 int reg_parm_stack_space;
51bbfa0c
RS
4092{
4093 register tree pval = arg->tree_value;
4094 rtx reg = 0;
4095 int partial = 0;
4096 int used = 0;
6a651371 4097 int i, lower_bound = 0, upper_bound = 0;
51bbfa0c
RS
4098
4099 if (TREE_CODE (pval) == ERROR_MARK)
4100 return;
4101
cc79451b
RK
4102 /* Push a new temporary level for any temporaries we make for
4103 this argument. */
4104 push_temp_slots ();
4105
f8a097cd 4106 if (ACCUMULATE_OUTGOING_ARGS && !(flags & ECF_SIBCALL))
51bbfa0c 4107 {
f73ad30e
JH
4108 /* If this is being stored into a pre-allocated, fixed-size, stack area,
4109 save any previous data at that location. */
4110 if (argblock && ! variable_size && arg->stack)
4111 {
51bbfa0c 4112#ifdef ARGS_GROW_DOWNWARD
f73ad30e
JH
4113 /* stack_slot is negative, but we want to index stack_usage_map
4114 with positive values. */
4115 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4116 upper_bound = -INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1)) + 1;
4117 else
4118 upper_bound = 0;
51bbfa0c 4119
f73ad30e 4120 lower_bound = upper_bound - arg->size.constant;
51bbfa0c 4121#else
f73ad30e
JH
4122 if (GET_CODE (XEXP (arg->stack_slot, 0)) == PLUS)
4123 lower_bound = INTVAL (XEXP (XEXP (arg->stack_slot, 0), 1));
4124 else
4125 lower_bound = 0;
51bbfa0c 4126
f73ad30e 4127 upper_bound = lower_bound + arg->size.constant;
51bbfa0c
RS
4128#endif
4129
f73ad30e
JH
4130 for (i = lower_bound; i < upper_bound; i++)
4131 if (stack_usage_map[i]
4132 /* Don't store things in the fixed argument area at this point;
4133 it has already been saved. */
4134 && i > reg_parm_stack_space)
4135 break;
51bbfa0c 4136
f73ad30e 4137 if (i != upper_bound)
51bbfa0c 4138 {
f73ad30e
JH
4139 /* We need to make a save area. See what mode we can make it. */
4140 enum machine_mode save_mode
4141 = mode_for_size (arg->size.constant * BITS_PER_UNIT, MODE_INT, 1);
4142 rtx stack_area
4143 = gen_rtx_MEM (save_mode,
4144 memory_address (save_mode,
4145 XEXP (arg->stack_slot, 0)));
4146
4147 if (save_mode == BLKmode)
4148 {
4149 arg->save_area = assign_stack_temp (BLKmode,
4150 arg->size.constant, 0);
4151 MEM_SET_IN_STRUCT_P (arg->save_area,
4152 AGGREGATE_TYPE_P (TREE_TYPE
4153 (arg->tree_value)));
4154 preserve_temp_slots (arg->save_area);
4155 emit_block_move (validize_mem (arg->save_area), stack_area,
4156 GEN_INT (arg->size.constant),
19caa751 4157 PARM_BOUNDARY);
f73ad30e
JH
4158 }
4159 else
4160 {
4161 arg->save_area = gen_reg_rtx (save_mode);
4162 emit_move_insn (arg->save_area, stack_area);
4163 }
51bbfa0c
RS
4164 }
4165 }
f73ad30e
JH
4166 /* Now that we have saved any slots that will be overwritten by this
4167 store, mark all slots this store will use. We must do this before
4168 we actually expand the argument since the expansion itself may
4169 trigger library calls which might need to use the same stack slot. */
4170 if (argblock && ! variable_size && arg->stack)
4171 for (i = lower_bound; i < upper_bound; i++)
4172 stack_usage_map[i] = 1;
51bbfa0c 4173 }
b564df06 4174
51bbfa0c
RS
4175 /* If this isn't going to be placed on both the stack and in registers,
4176 set up the register and number of words. */
4177 if (! arg->pass_on_stack)
4178 reg = arg->reg, partial = arg->partial;
4179
4180 if (reg != 0 && partial == 0)
4181 /* Being passed entirely in a register. We shouldn't be called in
4182 this case. */
4183 abort ();
4184
4ab56118
RK
4185 /* If this arg needs special alignment, don't load the registers
4186 here. */
4187 if (arg->n_aligned_regs != 0)
4188 reg = 0;
4ab56118 4189
4ab56118 4190 /* If this is being passed partially in a register, we can't evaluate
51bbfa0c
RS
4191 it directly into its stack slot. Otherwise, we can. */
4192 if (arg->value == 0)
d64f5a78 4193 {
d64f5a78
RS
4194 /* stack_arg_under_construction is nonzero if a function argument is
4195 being evaluated directly into the outgoing argument list and
4196 expand_call must take special action to preserve the argument list
4197 if it is called recursively.
4198
4199 For scalar function arguments stack_usage_map is sufficient to
4200 determine which stack slots must be saved and restored. Scalar
4201 arguments in general have pass_on_stack == 0.
4202
4203 If this argument is initialized by a function which takes the
4204 address of the argument (a C++ constructor or a C function
4205 returning a BLKmode structure), then stack_usage_map is
4206 insufficient and expand_call must push the stack around the
4207 function call. Such arguments have pass_on_stack == 1.
4208
4209 Note that it is always safe to set stack_arg_under_construction,
4210 but this generates suboptimal code if set when not needed. */
4211
4212 if (arg->pass_on_stack)
4213 stack_arg_under_construction++;
f73ad30e 4214
3a08477a
RK
4215 arg->value = expand_expr (pval,
4216 (partial
4217 || TYPE_MODE (TREE_TYPE (pval)) != arg->mode)
4218 ? NULL_RTX : arg->stack,
e5d70561 4219 VOIDmode, 0);
1efe6448
RK
4220
4221 /* If we are promoting object (or for any other reason) the mode
4222 doesn't agree, convert the mode. */
4223
7373d92d
RK
4224 if (arg->mode != TYPE_MODE (TREE_TYPE (pval)))
4225 arg->value = convert_modes (arg->mode, TYPE_MODE (TREE_TYPE (pval)),
4226 arg->value, arg->unsignedp);
1efe6448 4227
d64f5a78
RS
4228 if (arg->pass_on_stack)
4229 stack_arg_under_construction--;
d64f5a78 4230 }
51bbfa0c
RS
4231
4232 /* Don't allow anything left on stack from computation
4233 of argument to alloca. */
f8a097cd 4234 if (flags & ECF_MAY_BE_ALLOCA)
51bbfa0c
RS
4235 do_pending_stack_adjust ();
4236
4237 if (arg->value == arg->stack)
7815214e 4238 {
c5c76735 4239 /* If the value is already in the stack slot, we are done. */
7d384cc0 4240 if (current_function_check_memory_usage && GET_CODE (arg->stack) == MEM)
7815214e 4241 {
7815214e 4242 emit_library_call (chkr_set_right_libfunc, 1, VOIDmode, 3,
6a9c4aed 4243 XEXP (arg->stack, 0), Pmode,
7d384cc0 4244 ARGS_SIZE_RTX (arg->size),
7815214e 4245 TYPE_MODE (sizetype),
956d6950
JL
4246 GEN_INT (MEMORY_USE_RW),
4247 TYPE_MODE (integer_type_node));
7815214e
RK
4248 }
4249 }
1efe6448 4250 else if (arg->mode != BLKmode)
51bbfa0c
RS
4251 {
4252 register int size;
4253
4254 /* Argument is a scalar, not entirely passed in registers.
4255 (If part is passed in registers, arg->partial says how much
4256 and emit_push_insn will take care of putting it there.)
4257
4258 Push it, and if its size is less than the
4259 amount of space allocated to it,
4260 also bump stack pointer by the additional space.
4261 Note that in C the default argument promotions
4262 will prevent such mismatches. */
4263
1efe6448 4264 size = GET_MODE_SIZE (arg->mode);
51bbfa0c
RS
4265 /* Compute how much space the push instruction will push.
4266 On many machines, pushing a byte will advance the stack
4267 pointer by a halfword. */
4268#ifdef PUSH_ROUNDING
4269 size = PUSH_ROUNDING (size);
4270#endif
4271 used = size;
4272
4273 /* Compute how much space the argument should get:
4274 round up to a multiple of the alignment for arguments. */
1efe6448 4275 if (none != FUNCTION_ARG_PADDING (arg->mode, TREE_TYPE (pval)))
51bbfa0c
RS
4276 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
4277 / (PARM_BOUNDARY / BITS_PER_UNIT))
4278 * (PARM_BOUNDARY / BITS_PER_UNIT));
4279
4280 /* This isn't already where we want it on the stack, so put it there.
4281 This can either be done with push or copy insns. */
e5e809f4
JL
4282 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), NULL_RTX, 0,
4283 partial, reg, used - size, argblock,
4fc026cd
CM
4284 ARGS_SIZE_RTX (arg->offset), reg_parm_stack_space,
4285 ARGS_SIZE_RTX (arg->alignment_pad));
51bbfa0c
RS
4286 }
4287 else
4288 {
4289 /* BLKmode, at least partly to be pushed. */
4290
4291 register int excess;
4292 rtx size_rtx;
4293
4294 /* Pushing a nonscalar.
4295 If part is passed in registers, PARTIAL says how much
4296 and emit_push_insn will take care of putting it there. */
4297
4298 /* Round its size up to a multiple
4299 of the allocation unit for arguments. */
4300
4301 if (arg->size.var != 0)
4302 {
4303 excess = 0;
4304 size_rtx = ARGS_SIZE_RTX (arg->size);
4305 }
4306 else
4307 {
51bbfa0c
RS
4308 /* PUSH_ROUNDING has no effect on us, because
4309 emit_push_insn for BLKmode is careful to avoid it. */
0cf91217 4310 excess = (arg->size.constant - int_size_in_bytes (TREE_TYPE (pval))
51bbfa0c 4311 + partial * UNITS_PER_WORD);
e4f93898 4312 size_rtx = expr_size (pval);
51bbfa0c
RS
4313 }
4314
1efe6448 4315 emit_push_insn (arg->value, arg->mode, TREE_TYPE (pval), size_rtx,
19caa751
RK
4316 TYPE_ALIGN (TREE_TYPE (pval)), partial, reg, excess,
4317 argblock, ARGS_SIZE_RTX (arg->offset),
4fc026cd
CM
4318 reg_parm_stack_space,
4319 ARGS_SIZE_RTX (arg->alignment_pad));
51bbfa0c
RS
4320 }
4321
4322
4323 /* Unless this is a partially-in-register argument, the argument is now
4324 in the stack.
4325
4326 ??? Note that this can change arg->value from arg->stack to
4327 arg->stack_slot and it matters when they are not the same.
4328 It isn't totally clear that this is correct in all cases. */
4329 if (partial == 0)
3b917a55 4330 arg->value = arg->stack_slot;
51bbfa0c
RS
4331
4332 /* Once we have pushed something, pops can't safely
4333 be deferred during the rest of the arguments. */
4334 NO_DEFER_POP;
4335
4336 /* ANSI doesn't require a sequence point here,
4337 but PCC has one, so this will avoid some problems. */
4338 emit_queue ();
4339
db907e7b
RK
4340 /* Free any temporary slots made in processing this argument. Show
4341 that we might have taken the address of something and pushed that
4342 as an operand. */
4343 preserve_temp_slots (NULL_RTX);
51bbfa0c 4344 free_temp_slots ();
cc79451b 4345 pop_temp_slots ();
51bbfa0c 4346}