1 /* Expand builtin functions.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
30 #include "hard-reg-set.h"
33 #include "insn-flags.h"
34 #include "insn-codes.h"
35 #include "insn-config.h"
39 #include "typeclass.h"
44 #define CALLED_AS_BUILT_IN(NODE) \
45 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
47 /* Register mappings for target machines without register windows. */
48 #ifndef INCOMING_REGNO
49 #define INCOMING_REGNO(OUT) (OUT)
51 #ifndef OUTGOING_REGNO
52 #define OUTGOING_REGNO(IN) (IN)
55 #ifndef PAD_VARARGS_DOWN
56 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
59 /* Define the names of the builtin function types and codes. */
60 const char *const built_in_class_names
[4]
61 = {"NOT_BUILT_IN", "BUILT_IN_FRONTEND", "BUILT_IN_MD", "BUILT_IN_NORMAL"};
63 #define DEF_BUILTIN(x) STRINGIFY(x),
64 const char *const built_in_names
[(int) END_BUILTINS
] =
66 #include "builtins.def"
70 tree (*lang_type_promotes_to
) PARAMS ((tree
));
72 static int get_pointer_alignment
PARAMS ((tree
, unsigned));
73 static tree c_strlen
PARAMS ((tree
));
74 static rtx get_memory_rtx
PARAMS ((tree
));
75 static int apply_args_size
PARAMS ((void));
76 static int apply_result_size
PARAMS ((void));
77 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
78 static rtx result_vector
PARAMS ((int, rtx
));
80 static rtx expand_builtin_apply_args
PARAMS ((void));
81 static rtx expand_builtin_apply_args_1
PARAMS ((void));
82 static rtx expand_builtin_apply
PARAMS ((rtx
, rtx
, rtx
));
83 static void expand_builtin_return
PARAMS ((rtx
));
84 static rtx expand_builtin_classify_type
PARAMS ((tree
));
85 static rtx expand_builtin_mathfn
PARAMS ((tree
, rtx
, rtx
));
86 static rtx expand_builtin_constant_p
PARAMS ((tree
));
87 static rtx expand_builtin_args_info
PARAMS ((tree
));
88 static rtx expand_builtin_next_arg
PARAMS ((tree
));
89 static rtx expand_builtin_va_start
PARAMS ((int, tree
));
90 static rtx expand_builtin_va_end
PARAMS ((tree
));
91 static rtx expand_builtin_va_copy
PARAMS ((tree
));
93 static rtx expand_builtin_memcmp
PARAMS ((tree
, tree
, rtx
));
94 static rtx expand_builtin_strcmp
PARAMS ((tree
, rtx
));
96 static rtx expand_builtin_memcpy
PARAMS ((tree
));
97 static rtx expand_builtin_strcpy
PARAMS ((tree
));
98 static rtx expand_builtin_memset
PARAMS ((tree
));
99 static rtx expand_builtin_bzero
PARAMS ((tree
));
100 static rtx expand_builtin_strlen
PARAMS ((tree
, rtx
,
102 static rtx expand_builtin_alloca
PARAMS ((tree
, rtx
));
103 static rtx expand_builtin_ffs
PARAMS ((tree
, rtx
, rtx
));
104 static rtx expand_builtin_frame_address
PARAMS ((tree
));
105 static tree stabilize_va_list
PARAMS ((tree
, int));
106 static rtx expand_builtin_expect
PARAMS ((tree
, rtx
));
108 /* Return the alignment in bits of EXP, a pointer valued expression.
109 But don't return more than MAX_ALIGN no matter what.
110 The alignment returned is, by default, the alignment of the thing that
111 EXP points to (if it is not a POINTER_TYPE, 0 is returned).
113 Otherwise, look at the expression to see if we can do better, i.e., if the
114 expression is actually pointing at an object whose alignment is tighter. */
117 get_pointer_alignment (exp
, max_align
)
121 unsigned align
, inner
;
123 if (TREE_CODE (TREE_TYPE (exp
)) != POINTER_TYPE
)
126 align
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
127 align
= MIN (align
, max_align
);
131 switch (TREE_CODE (exp
))
135 case NON_LVALUE_EXPR
:
136 exp
= TREE_OPERAND (exp
, 0);
137 if (TREE_CODE (TREE_TYPE (exp
)) != POINTER_TYPE
)
140 inner
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp
)));
141 align
= MIN (inner
, max_align
);
145 /* If sum of pointer + int, restrict our maximum alignment to that
146 imposed by the integer. If not, we can't do any better than
148 if (! host_integerp (TREE_OPERAND (exp
, 1), 1))
151 while (((tree_low_cst (TREE_OPERAND (exp
, 1), 1) * BITS_PER_UNIT
)
156 exp
= TREE_OPERAND (exp
, 0);
160 /* See what we are pointing at and look at its alignment. */
161 exp
= TREE_OPERAND (exp
, 0);
162 if (TREE_CODE (exp
) == FUNCTION_DECL
)
163 align
= FUNCTION_BOUNDARY
;
164 else if (DECL_P (exp
))
165 align
= DECL_ALIGN (exp
);
166 #ifdef CONSTANT_ALIGNMENT
167 else if (TREE_CODE_CLASS (TREE_CODE (exp
)) == 'c')
168 align
= CONSTANT_ALIGNMENT (exp
, align
);
170 return MIN (align
, max_align
);
178 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
179 way, because it could contain a zero byte in the middle.
180 TREE_STRING_LENGTH is the size of the character array, not the string.
182 The value returned is of type `ssizetype'.
184 Unfortunately, string_constant can't access the values of const char
185 arrays with initializers, so neither can we do so here. */
195 src
= string_constant (src
, &offset_node
);
199 max
= TREE_STRING_LENGTH (src
);
200 ptr
= TREE_STRING_POINTER (src
);
202 if (offset_node
&& TREE_CODE (offset_node
) != INTEGER_CST
)
204 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
205 compute the offset to the following null if we don't know where to
206 start searching for it. */
209 for (i
= 0; i
< max
; i
++)
213 /* We don't know the starting offset, but we do know that the string
214 has no internal zero bytes. We can assume that the offset falls
215 within the bounds of the string; otherwise, the programmer deserves
216 what he gets. Subtract the offset from the length of the string,
217 and return that. This would perhaps not be valid if we were dealing
218 with named arrays in addition to literal string constants. */
220 return size_diffop (size_int (max
), offset_node
);
223 /* We have a known offset into the string. Start searching there for
225 if (offset_node
== 0)
229 /* Did we get a long long offset? If so, punt. */
230 if (TREE_INT_CST_HIGH (offset_node
) != 0)
232 offset
= TREE_INT_CST_LOW (offset_node
);
235 /* If the offset is known to be out of bounds, warn, and call strlen at
237 if (offset
< 0 || offset
> max
)
239 warning ("offset outside bounds of constant string");
243 /* Use strlen to search for the first zero byte. Since any strings
244 constructed with build_string will have nulls appended, we win even
245 if we get handed something like (char[4])"abcd".
247 Since OFFSET is our starting index into the string, no further
248 calculation is needed. */
249 return ssize_int (strlen (ptr
+ offset
));
252 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
253 times to get the address of either a higher stack frame, or a return
254 address located within it (depending on FNDECL_CODE). */
257 expand_builtin_return_addr (fndecl_code
, count
, tem
)
258 enum built_in_function fndecl_code
;
264 /* Some machines need special handling before we can access
265 arbitrary frames. For example, on the sparc, we must first flush
266 all register windows to the stack. */
267 #ifdef SETUP_FRAME_ADDRESSES
269 SETUP_FRAME_ADDRESSES ();
272 /* On the sparc, the return address is not in the frame, it is in a
273 register. There is no way to access it off of the current frame
274 pointer, but it can be accessed off the previous frame pointer by
275 reading the value from the register window save area. */
276 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
277 if (fndecl_code
== BUILT_IN_RETURN_ADDRESS
)
281 /* Scan back COUNT frames to the specified frame. */
282 for (i
= 0; i
< count
; i
++)
284 /* Assume the dynamic chain pointer is in the word that the
285 frame address points to, unless otherwise specified. */
286 #ifdef DYNAMIC_CHAIN_ADDRESS
287 tem
= DYNAMIC_CHAIN_ADDRESS (tem
);
289 tem
= memory_address (Pmode
, tem
);
290 tem
= copy_to_reg (gen_rtx_MEM (Pmode
, tem
));
293 /* For __builtin_frame_address, return what we've got. */
294 if (fndecl_code
== BUILT_IN_FRAME_ADDRESS
)
297 /* For __builtin_return_address, Get the return address from that
299 #ifdef RETURN_ADDR_RTX
300 tem
= RETURN_ADDR_RTX (count
, tem
);
302 tem
= memory_address (Pmode
,
303 plus_constant (tem
, GET_MODE_SIZE (Pmode
)));
304 tem
= gen_rtx_MEM (Pmode
, tem
);
309 /* __builtin_setjmp is passed a pointer to an array of five words (not
310 all will be used on all machines). It operates similarly to the C
311 library function of the same name, but is more efficient. Much of
312 the code below (and for longjmp) is copied from the handling of
315 NOTE: This is intended for use by GNAT and the exception handling
316 scheme in the compiler and will only work in the method used by
320 expand_builtin_setjmp (buf_addr
, target
, first_label
, next_label
)
323 rtx first_label
, next_label
;
325 rtx lab1
= gen_label_rtx ();
326 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
327 enum machine_mode value_mode
;
330 value_mode
= TYPE_MODE (integer_type_node
);
332 #ifdef POINTERS_EXTEND_UNSIGNED
333 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
336 buf_addr
= force_reg (Pmode
, force_operand (buf_addr
, NULL_RTX
));
338 if (target
== 0 || GET_CODE (target
) != REG
339 || REGNO (target
) < FIRST_PSEUDO_REGISTER
)
340 target
= gen_reg_rtx (value_mode
);
344 /* We store the frame pointer and the address of lab1 in the buffer
345 and use the rest of it for the stack save area, which is
346 machine-dependent. */
348 #ifndef BUILTIN_SETJMP_FRAME_VALUE
349 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
352 emit_move_insn (gen_rtx_MEM (Pmode
, buf_addr
),
353 BUILTIN_SETJMP_FRAME_VALUE
);
354 emit_move_insn (validize_mem
356 plus_constant (buf_addr
,
357 GET_MODE_SIZE (Pmode
)))),
358 force_reg (Pmode
, gen_rtx_LABEL_REF (Pmode
, lab1
)));
360 stack_save
= gen_rtx_MEM (sa_mode
,
361 plus_constant (buf_addr
,
362 2 * GET_MODE_SIZE (Pmode
)));
363 emit_stack_save (SAVE_NONLOCAL
, &stack_save
, NULL_RTX
);
365 /* If there is further processing to do, do it. */
366 #ifdef HAVE_builtin_setjmp_setup
367 if (HAVE_builtin_setjmp_setup
)
368 emit_insn (gen_builtin_setjmp_setup (buf_addr
));
371 /* Set TARGET to zero and branch to the first-time-through label. */
372 emit_move_insn (target
, const0_rtx
);
373 emit_jump_insn (gen_jump (first_label
));
377 /* Tell flow about the strange goings on. Putting `lab1' on
378 `nonlocal_goto_handler_labels' to indicates that function
379 calls may traverse the arc back to this label. */
381 current_function_has_nonlocal_label
= 1;
382 nonlocal_goto_handler_labels
383 = gen_rtx_EXPR_LIST (VOIDmode
, lab1
, nonlocal_goto_handler_labels
);
385 /* Clobber the FP when we get here, so we have to make sure it's
386 marked as used by this function. */
387 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
389 /* Mark the static chain as clobbered here so life information
390 doesn't get messed up for it. */
391 emit_insn (gen_rtx_CLOBBER (VOIDmode
, static_chain_rtx
));
393 /* Now put in the code to restore the frame pointer, and argument
394 pointer, if needed. The code below is from expand_end_bindings
395 in stmt.c; see detailed documentation there. */
396 #ifdef HAVE_nonlocal_goto
397 if (! HAVE_nonlocal_goto
)
399 emit_move_insn (virtual_stack_vars_rtx
, hard_frame_pointer_rtx
);
401 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
402 if (fixed_regs
[ARG_POINTER_REGNUM
])
404 #ifdef ELIMINABLE_REGS
406 static struct elims
{int from
, to
;} elim_regs
[] = ELIMINABLE_REGS
;
408 for (i
= 0; i
< sizeof elim_regs
/ sizeof elim_regs
[0]; i
++)
409 if (elim_regs
[i
].from
== ARG_POINTER_REGNUM
410 && elim_regs
[i
].to
== HARD_FRAME_POINTER_REGNUM
)
413 if (i
== sizeof elim_regs
/ sizeof elim_regs
[0])
416 /* Now restore our arg pointer from the address at which it
417 was saved in our stack frame.
418 If there hasn't be space allocated for it yet, make
420 if (arg_pointer_save_area
== 0)
421 arg_pointer_save_area
422 = assign_stack_local (Pmode
, GET_MODE_SIZE (Pmode
), 0);
423 emit_move_insn (virtual_incoming_args_rtx
,
424 copy_to_reg (arg_pointer_save_area
));
429 #ifdef HAVE_builtin_setjmp_receiver
430 if (HAVE_builtin_setjmp_receiver
)
431 emit_insn (gen_builtin_setjmp_receiver (lab1
));
434 #ifdef HAVE_nonlocal_goto_receiver
435 if (HAVE_nonlocal_goto_receiver
)
436 emit_insn (gen_nonlocal_goto_receiver ());
443 /* Set TARGET, and branch to the next-time-through label. */
444 emit_move_insn (target
, const1_rtx
);
445 emit_jump_insn (gen_jump (next_label
));
451 /* __builtin_longjmp is passed a pointer to an array of five words (not
452 all will be used on all machines). It operates similarly to the C
453 library function of the same name, but is more efficient. Much of
454 the code below is copied from the handling of non-local gotos.
456 NOTE: This is intended for use by GNAT and the exception handling
457 scheme in the compiler and will only work in the method used by
461 expand_builtin_longjmp (buf_addr
, value
)
465 enum machine_mode sa_mode
= STACK_SAVEAREA_MODE (SAVE_NONLOCAL
);
467 #ifdef POINTERS_EXTEND_UNSIGNED
468 buf_addr
= convert_memory_address (Pmode
, buf_addr
);
470 buf_addr
= force_reg (Pmode
, buf_addr
);
472 /* We used to store value in static_chain_rtx, but that fails if pointers
473 are smaller than integers. We instead require that the user must pass
474 a second argument of 1, because that is what builtin_setjmp will
475 return. This also makes EH slightly more efficient, since we are no
476 longer copying around a value that we don't care about. */
477 if (value
!= const1_rtx
)
480 #ifdef HAVE_builtin_longjmp
481 if (HAVE_builtin_longjmp
)
482 emit_insn (gen_builtin_longjmp (buf_addr
));
486 fp
= gen_rtx_MEM (Pmode
, buf_addr
);
487 lab
= gen_rtx_MEM (Pmode
, plus_constant (buf_addr
,
488 GET_MODE_SIZE (Pmode
)));
490 stack
= gen_rtx_MEM (sa_mode
, plus_constant (buf_addr
,
491 2 * GET_MODE_SIZE (Pmode
)));
493 /* Pick up FP, label, and SP from the block and jump. This code is
494 from expand_goto in stmt.c; see there for detailed comments. */
495 #if HAVE_nonlocal_goto
496 if (HAVE_nonlocal_goto
)
497 /* We have to pass a value to the nonlocal_goto pattern that will
498 get copied into the static_chain pointer, but it does not matter
499 what that value is, because builtin_setjmp does not use it. */
500 emit_insn (gen_nonlocal_goto (value
, fp
, stack
, lab
));
504 lab
= copy_to_reg (lab
);
506 emit_move_insn (hard_frame_pointer_rtx
, fp
);
507 emit_stack_restore (SAVE_NONLOCAL
, stack
, NULL_RTX
);
509 emit_insn (gen_rtx_USE (VOIDmode
, hard_frame_pointer_rtx
));
510 emit_insn (gen_rtx_USE (VOIDmode
, stack_pointer_rtx
));
511 emit_indirect_jump (lab
);
516 /* Get a MEM rtx for expression EXP which can be used in a string instruction
517 (cmpstrsi, movstrsi, ..). */
525 mem
= gen_rtx_MEM (BLKmode
,
526 memory_address (BLKmode
,
527 expand_expr (exp
, NULL_RTX
,
528 ptr_mode
, EXPAND_SUM
)));
530 RTX_UNCHANGING_P (mem
) = TREE_READONLY (exp
);
532 /* Figure out the type of the object pointed to. Set MEM_IN_STRUCT_P
533 if the value is the address of a structure or if the expression is
534 cast to a pointer to structure type. */
537 while (TREE_CODE (exp
) == NOP_EXPR
)
539 tree cast_type
= TREE_TYPE (exp
);
540 if (TREE_CODE (cast_type
) == POINTER_TYPE
541 && AGGREGATE_TYPE_P (TREE_TYPE (cast_type
)))
546 exp
= TREE_OPERAND (exp
, 0);
549 if (is_aggregate
== 0)
553 if (TREE_CODE (exp
) == ADDR_EXPR
)
554 /* If this is the address of an object, check whether the
555 object is an array. */
556 type
= TREE_TYPE (TREE_OPERAND (exp
, 0));
558 type
= TREE_TYPE (TREE_TYPE (exp
));
559 is_aggregate
= AGGREGATE_TYPE_P (type
);
562 MEM_SET_IN_STRUCT_P (mem
, is_aggregate
);
566 /* Built-in functions to perform an untyped call and return. */
568 /* For each register that may be used for calling a function, this
569 gives a mode used to copy the register's value. VOIDmode indicates
570 the register is not used for calling a function. If the machine
571 has register windows, this gives only the outbound registers.
572 INCOMING_REGNO gives the corresponding inbound register. */
573 static enum machine_mode apply_args_mode
[FIRST_PSEUDO_REGISTER
];
575 /* For each register that may be used for returning values, this gives
576 a mode used to copy the register's value. VOIDmode indicates the
577 register is not used for returning values. If the machine has
578 register windows, this gives only the outbound registers.
579 INCOMING_REGNO gives the corresponding inbound register. */
580 static enum machine_mode apply_result_mode
[FIRST_PSEUDO_REGISTER
];
582 /* For each register that may be used for calling a function, this
583 gives the offset of that register into the block returned by
584 __builtin_apply_args. 0 indicates that the register is not
585 used for calling a function. */
586 static int apply_args_reg_offset
[FIRST_PSEUDO_REGISTER
];
588 /* Return the offset of register REGNO into the block returned by
589 __builtin_apply_args. This is not declared static, since it is
590 needed in objc-act.c. */
593 apply_args_register_offset (regno
)
598 /* Arguments are always put in outgoing registers (in the argument
599 block) if such make sense. */
600 #ifdef OUTGOING_REGNO
601 regno
= OUTGOING_REGNO(regno
);
603 return apply_args_reg_offset
[regno
];
606 /* Return the size required for the block returned by __builtin_apply_args,
607 and initialize apply_args_mode. */
612 static int size
= -1;
614 enum machine_mode mode
;
616 /* The values computed by this function never change. */
619 /* The first value is the incoming arg-pointer. */
620 size
= GET_MODE_SIZE (Pmode
);
622 /* The second value is the structure value address unless this is
623 passed as an "invisible" first argument. */
624 if (struct_value_rtx
)
625 size
+= GET_MODE_SIZE (Pmode
);
627 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
628 if (FUNCTION_ARG_REGNO_P (regno
))
630 /* Search for the proper mode for copying this register's
631 value. I'm not sure this is right, but it works so far. */
632 enum machine_mode best_mode
= VOIDmode
;
634 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
636 mode
= GET_MODE_WIDER_MODE (mode
))
637 if (HARD_REGNO_MODE_OK (regno
, mode
)
638 && HARD_REGNO_NREGS (regno
, mode
) == 1)
641 if (best_mode
== VOIDmode
)
642 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
);
644 mode
= GET_MODE_WIDER_MODE (mode
))
645 if (HARD_REGNO_MODE_OK (regno
, mode
)
646 && (mov_optab
->handlers
[(int) mode
].insn_code
647 != CODE_FOR_nothing
))
651 if (mode
== VOIDmode
)
654 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
655 if (size
% align
!= 0)
656 size
= CEIL (size
, align
) * align
;
657 apply_args_reg_offset
[regno
] = size
;
658 size
+= GET_MODE_SIZE (mode
);
659 apply_args_mode
[regno
] = mode
;
663 apply_args_mode
[regno
] = VOIDmode
;
664 apply_args_reg_offset
[regno
] = 0;
670 /* Return the size required for the block returned by __builtin_apply,
671 and initialize apply_result_mode. */
676 static int size
= -1;
678 enum machine_mode mode
;
680 /* The values computed by this function never change. */
685 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
686 if (FUNCTION_VALUE_REGNO_P (regno
))
688 /* Search for the proper mode for copying this register's
689 value. I'm not sure this is right, but it works so far. */
690 enum machine_mode best_mode
= VOIDmode
;
692 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
694 mode
= GET_MODE_WIDER_MODE (mode
))
695 if (HARD_REGNO_MODE_OK (regno
, mode
))
698 if (best_mode
== VOIDmode
)
699 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_FLOAT
);
701 mode
= GET_MODE_WIDER_MODE (mode
))
702 if (HARD_REGNO_MODE_OK (regno
, mode
)
703 && (mov_optab
->handlers
[(int) mode
].insn_code
704 != CODE_FOR_nothing
))
708 if (mode
== VOIDmode
)
711 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
712 if (size
% align
!= 0)
713 size
= CEIL (size
, align
) * align
;
714 size
+= GET_MODE_SIZE (mode
);
715 apply_result_mode
[regno
] = mode
;
718 apply_result_mode
[regno
] = VOIDmode
;
720 /* Allow targets that use untyped_call and untyped_return to override
721 the size so that machine-specific information can be stored here. */
722 #ifdef APPLY_RESULT_SIZE
723 size
= APPLY_RESULT_SIZE
;
729 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
730 /* Create a vector describing the result block RESULT. If SAVEP is true,
731 the result block is used to save the values; otherwise it is used to
732 restore the values. */
735 result_vector (savep
, result
)
739 int regno
, size
, align
, nelts
;
740 enum machine_mode mode
;
742 rtx
*savevec
= (rtx
*) alloca (FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
745 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
746 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
748 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
749 if (size
% align
!= 0)
750 size
= CEIL (size
, align
) * align
;
751 reg
= gen_rtx_REG (mode
, savep
? regno
: INCOMING_REGNO (regno
));
752 mem
= change_address (result
, mode
,
753 plus_constant (XEXP (result
, 0), size
));
754 savevec
[nelts
++] = (savep
755 ? gen_rtx_SET (VOIDmode
, mem
, reg
)
756 : gen_rtx_SET (VOIDmode
, reg
, mem
));
757 size
+= GET_MODE_SIZE (mode
);
759 return gen_rtx_PARALLEL (VOIDmode
, gen_rtvec_v (nelts
, savevec
));
761 #endif /* HAVE_untyped_call or HAVE_untyped_return */
763 /* Save the state required to perform an untyped call with the same
764 arguments as were passed to the current function. */
767 expand_builtin_apply_args_1 ()
770 int size
, align
, regno
;
771 enum machine_mode mode
;
773 /* Create a block where the arg-pointer, structure value address,
774 and argument registers can be saved. */
775 registers
= assign_stack_local (BLKmode
, apply_args_size (), -1);
777 /* Walk past the arg-pointer and structure value address. */
778 size
= GET_MODE_SIZE (Pmode
);
779 if (struct_value_rtx
)
780 size
+= GET_MODE_SIZE (Pmode
);
782 /* Save each register used in calling a function to the block. */
783 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
784 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
788 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
789 if (size
% align
!= 0)
790 size
= CEIL (size
, align
) * align
;
792 tem
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
794 emit_move_insn (change_address (registers
, mode
,
795 plus_constant (XEXP (registers
, 0),
798 size
+= GET_MODE_SIZE (mode
);
801 /* Save the arg pointer to the block. */
802 emit_move_insn (change_address (registers
, Pmode
, XEXP (registers
, 0)),
803 copy_to_reg (virtual_incoming_args_rtx
));
804 size
= GET_MODE_SIZE (Pmode
);
806 /* Save the structure value address unless this is passed as an
807 "invisible" first argument. */
808 if (struct_value_incoming_rtx
)
810 emit_move_insn (change_address (registers
, Pmode
,
811 plus_constant (XEXP (registers
, 0),
813 copy_to_reg (struct_value_incoming_rtx
));
814 size
+= GET_MODE_SIZE (Pmode
);
817 /* Return the address of the block. */
818 return copy_addr_to_reg (XEXP (registers
, 0));
821 /* __builtin_apply_args returns block of memory allocated on
822 the stack into which is stored the arg pointer, structure
823 value address, static chain, and all the registers that might
824 possibly be used in performing a function call. The code is
825 moved to the start of the function so the incoming values are
828 expand_builtin_apply_args ()
830 /* Don't do __builtin_apply_args more than once in a function.
831 Save the result of the first call and reuse it. */
832 if (apply_args_value
!= 0)
833 return apply_args_value
;
835 /* When this function is called, it means that registers must be
836 saved on entry to this function. So we migrate the
837 call to the first insn of this function. */
842 temp
= expand_builtin_apply_args_1 ();
846 apply_args_value
= temp
;
848 /* Put the sequence after the NOTE that starts the function.
849 If this is inside a SEQUENCE, make the outer-level insn
850 chain current, so the code is placed at the start of the
852 push_topmost_sequence ();
853 emit_insns_before (seq
, NEXT_INSN (get_insns ()));
854 pop_topmost_sequence ();
859 /* Perform an untyped call and save the state required to perform an
860 untyped return of whatever value was returned by the given function. */
863 expand_builtin_apply (function
, arguments
, argsize
)
864 rtx function
, arguments
, argsize
;
866 int size
, align
, regno
;
867 enum machine_mode mode
;
868 rtx incoming_args
, result
, reg
, dest
, call_insn
;
869 rtx old_stack_level
= 0;
872 /* Create a block where the return registers can be saved. */
873 result
= assign_stack_local (BLKmode
, apply_result_size (), -1);
875 /* Fetch the arg pointer from the ARGUMENTS block. */
876 incoming_args
= gen_reg_rtx (Pmode
);
877 emit_move_insn (incoming_args
,
878 gen_rtx_MEM (Pmode
, arguments
));
879 #ifndef STACK_GROWS_DOWNWARD
880 incoming_args
= expand_binop (Pmode
, sub_optab
, incoming_args
, argsize
,
881 incoming_args
, 0, OPTAB_LIB_WIDEN
);
884 /* Perform postincrements before actually calling the function. */
887 /* Push a new argument block and copy the arguments. */
888 do_pending_stack_adjust ();
890 /* Save the stack with nonlocal if available */
891 #ifdef HAVE_save_stack_nonlocal
892 if (HAVE_save_stack_nonlocal
)
893 emit_stack_save (SAVE_NONLOCAL
, &old_stack_level
, NULL_RTX
);
896 emit_stack_save (SAVE_BLOCK
, &old_stack_level
, NULL_RTX
);
898 /* Push a block of memory onto the stack to store the memory arguments.
899 Save the address in a register, and copy the memory arguments. ??? I
900 haven't figured out how the calling convention macros effect this,
901 but it's likely that the source and/or destination addresses in
902 the block copy will need updating in machine specific ways. */
903 dest
= allocate_dynamic_stack_space (argsize
, 0, BITS_PER_UNIT
);
904 emit_block_move (gen_rtx_MEM (BLKmode
, dest
),
905 gen_rtx_MEM (BLKmode
, incoming_args
),
906 argsize
, PARM_BOUNDARY
);
908 /* Refer to the argument block. */
910 arguments
= gen_rtx_MEM (BLKmode
, arguments
);
912 /* Walk past the arg-pointer and structure value address. */
913 size
= GET_MODE_SIZE (Pmode
);
914 if (struct_value_rtx
)
915 size
+= GET_MODE_SIZE (Pmode
);
917 /* Restore each of the registers previously saved. Make USE insns
918 for each of these registers for use in making the call. */
919 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
920 if ((mode
= apply_args_mode
[regno
]) != VOIDmode
)
922 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
923 if (size
% align
!= 0)
924 size
= CEIL (size
, align
) * align
;
925 reg
= gen_rtx_REG (mode
, regno
);
927 change_address (arguments
, mode
,
928 plus_constant (XEXP (arguments
, 0),
931 use_reg (&call_fusage
, reg
);
932 size
+= GET_MODE_SIZE (mode
);
935 /* Restore the structure value address unless this is passed as an
936 "invisible" first argument. */
937 size
= GET_MODE_SIZE (Pmode
);
938 if (struct_value_rtx
)
940 rtx value
= gen_reg_rtx (Pmode
);
941 emit_move_insn (value
,
942 change_address (arguments
, Pmode
,
943 plus_constant (XEXP (arguments
, 0),
945 emit_move_insn (struct_value_rtx
, value
);
946 if (GET_CODE (struct_value_rtx
) == REG
)
947 use_reg (&call_fusage
, struct_value_rtx
);
948 size
+= GET_MODE_SIZE (Pmode
);
951 /* All arguments and registers used for the call are set up by now! */
952 function
= prepare_call_address (function
, NULL_TREE
, &call_fusage
, 0);
954 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
955 and we don't want to load it into a register as an optimization,
956 because prepare_call_address already did it if it should be done. */
957 if (GET_CODE (function
) != SYMBOL_REF
)
958 function
= memory_address (FUNCTION_MODE
, function
);
960 /* Generate the actual call instruction and save the return value. */
961 #ifdef HAVE_untyped_call
962 if (HAVE_untyped_call
)
963 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE
, function
),
964 result
, result_vector (1, result
)));
967 #ifdef HAVE_call_value
972 /* Locate the unique return register. It is not possible to
973 express a call that sets more than one return register using
974 call_value; use untyped_call for that. In fact, untyped_call
975 only needs to save the return registers in the given block. */
976 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
977 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
980 abort (); /* HAVE_untyped_call required. */
981 valreg
= gen_rtx_REG (mode
, regno
);
984 emit_call_insn (gen_call_value (valreg
,
985 gen_rtx_MEM (FUNCTION_MODE
, function
),
986 const0_rtx
, NULL_RTX
, const0_rtx
));
988 emit_move_insn (change_address (result
, GET_MODE (valreg
),
996 /* Find the CALL insn we just emitted. */
997 for (call_insn
= get_last_insn ();
998 call_insn
&& GET_CODE (call_insn
) != CALL_INSN
;
999 call_insn
= PREV_INSN (call_insn
))
1005 /* Put the register usage information on the CALL. If there is already
1006 some usage information, put ours at the end. */
1007 if (CALL_INSN_FUNCTION_USAGE (call_insn
))
1011 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
); XEXP (link
, 1) != 0;
1012 link
= XEXP (link
, 1))
1015 XEXP (link
, 1) = call_fusage
;
1018 CALL_INSN_FUNCTION_USAGE (call_insn
) = call_fusage
;
1020 /* Restore the stack. */
1021 #ifdef HAVE_save_stack_nonlocal
1022 if (HAVE_save_stack_nonlocal
)
1023 emit_stack_restore (SAVE_NONLOCAL
, old_stack_level
, NULL_RTX
);
1026 emit_stack_restore (SAVE_BLOCK
, old_stack_level
, NULL_RTX
);
1028 /* Return the address of the result block. */
1029 return copy_addr_to_reg (XEXP (result
, 0));
1032 /* Perform an untyped return. */
1035 expand_builtin_return (result
)
1038 int size
, align
, regno
;
1039 enum machine_mode mode
;
1041 rtx call_fusage
= 0;
1043 apply_result_size ();
1044 result
= gen_rtx_MEM (BLKmode
, result
);
1046 #ifdef HAVE_untyped_return
1047 if (HAVE_untyped_return
)
1049 emit_jump_insn (gen_untyped_return (result
, result_vector (0, result
)));
1055 /* Restore the return value and note that each value is used. */
1057 for (regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
1058 if ((mode
= apply_result_mode
[regno
]) != VOIDmode
)
1060 align
= GET_MODE_ALIGNMENT (mode
) / BITS_PER_UNIT
;
1061 if (size
% align
!= 0)
1062 size
= CEIL (size
, align
) * align
;
1063 reg
= gen_rtx_REG (mode
, INCOMING_REGNO (regno
));
1064 emit_move_insn (reg
,
1065 change_address (result
, mode
,
1066 plus_constant (XEXP (result
, 0),
1069 push_to_sequence (call_fusage
);
1070 emit_insn (gen_rtx_USE (VOIDmode
, reg
));
1071 call_fusage
= get_insns ();
1073 size
+= GET_MODE_SIZE (mode
);
1076 /* Put the USE insns before the return. */
1077 emit_insns (call_fusage
);
1079 /* Return whatever values was restored by jumping directly to the end
1081 expand_null_return ();
1084 /* Expand a call to __builtin_classify_type with arguments found in
1087 expand_builtin_classify_type (arglist
)
1092 tree type
= TREE_TYPE (TREE_VALUE (arglist
));
1093 enum tree_code code
= TREE_CODE (type
);
1094 if (code
== VOID_TYPE
)
1095 return GEN_INT (void_type_class
);
1096 if (code
== INTEGER_TYPE
)
1097 return GEN_INT (integer_type_class
);
1098 if (code
== CHAR_TYPE
)
1099 return GEN_INT (char_type_class
);
1100 if (code
== ENUMERAL_TYPE
)
1101 return GEN_INT (enumeral_type_class
);
1102 if (code
== BOOLEAN_TYPE
)
1103 return GEN_INT (boolean_type_class
);
1104 if (code
== POINTER_TYPE
)
1105 return GEN_INT (pointer_type_class
);
1106 if (code
== REFERENCE_TYPE
)
1107 return GEN_INT (reference_type_class
);
1108 if (code
== OFFSET_TYPE
)
1109 return GEN_INT (offset_type_class
);
1110 if (code
== REAL_TYPE
)
1111 return GEN_INT (real_type_class
);
1112 if (code
== COMPLEX_TYPE
)
1113 return GEN_INT (complex_type_class
);
1114 if (code
== FUNCTION_TYPE
)
1115 return GEN_INT (function_type_class
);
1116 if (code
== METHOD_TYPE
)
1117 return GEN_INT (method_type_class
);
1118 if (code
== RECORD_TYPE
)
1119 return GEN_INT (record_type_class
);
1120 if (code
== UNION_TYPE
|| code
== QUAL_UNION_TYPE
)
1121 return GEN_INT (union_type_class
);
1122 if (code
== ARRAY_TYPE
)
1124 if (TYPE_STRING_FLAG (type
))
1125 return GEN_INT (string_type_class
);
1127 return GEN_INT (array_type_class
);
1129 if (code
== SET_TYPE
)
1130 return GEN_INT (set_type_class
);
1131 if (code
== FILE_TYPE
)
1132 return GEN_INT (file_type_class
);
1133 if (code
== LANG_TYPE
)
1134 return GEN_INT (lang_type_class
);
1136 return GEN_INT (no_type_class
);
1139 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1141 expand_builtin_constant_p (exp
)
1144 tree arglist
= TREE_OPERAND (exp
, 1);
1145 enum machine_mode value_mode
= TYPE_MODE (TREE_TYPE (exp
));
1151 tree arg
= TREE_VALUE (arglist
);
1154 /* We return 1 for a numeric type that's known to be a constant
1155 value at compile-time or for an aggregate type that's a
1156 literal constant. */
1159 /* If we know this is a constant, emit the constant of one. */
1160 if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'c'
1161 || (TREE_CODE (arg
) == CONSTRUCTOR
1162 && TREE_CONSTANT (arg
))
1163 || (TREE_CODE (arg
) == ADDR_EXPR
1164 && TREE_CODE (TREE_OPERAND (arg
, 0)) == STRING_CST
))
1167 /* If we aren't going to be running CSE or this expression
1168 has side effects, show we don't know it to be a constant.
1169 Likewise if it's a pointer or aggregate type since in those
1170 case we only want literals, since those are only optimized
1171 when generating RTL, not later. */
1172 if (TREE_SIDE_EFFECTS (arg
) || cse_not_expected
1173 || AGGREGATE_TYPE_P (TREE_TYPE (arg
))
1174 || POINTER_TYPE_P (TREE_TYPE (arg
)))
1177 /* Otherwise, emit (constant_p_rtx (ARG)) and let CSE get a
1178 chance to see if it can deduce whether ARG is constant. */
1180 tmp
= expand_expr (arg
, NULL_RTX
, VOIDmode
, 0);
1181 tmp
= gen_rtx_CONSTANT_P_RTX (value_mode
, tmp
);
1186 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1187 Return 0 if a normal call should be emitted rather than expanding the
1188 function in-line. EXP is the expression that is a call to the builtin
1189 function; if convenient, the result should be placed in TARGET.
1190 SUBTARGET may be used as the target for computing one of EXP's operands. */
1192 expand_builtin_mathfn (exp
, target
, subtarget
)
1194 rtx target
, subtarget
;
1196 optab builtin_optab
;
1198 tree fndecl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
1199 tree arglist
= TREE_OPERAND (exp
, 1);
1202 /* Arg could be wrong type if user redeclared this fcn wrong. */
1203 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != REAL_TYPE
)
1206 /* Stabilize and compute the argument. */
1207 if (TREE_CODE (TREE_VALUE (arglist
)) != VAR_DECL
1208 && TREE_CODE (TREE_VALUE (arglist
)) != PARM_DECL
)
1210 exp
= copy_node (exp
);
1211 TREE_OPERAND (exp
, 1) = arglist
;
1212 /* Wrap the computation of the argument in a SAVE_EXPR. That
1213 way, if we need to expand the argument again (as in the
1214 flag_errno_math case below where we cannot directly set
1215 errno), we will not perform side-effects more than once.
1216 Note that here we're mutating the original EXP as well as the
1217 copy; that's the right thing to do in case the original EXP
1218 is expanded later. */
1219 TREE_VALUE (arglist
) = save_expr (TREE_VALUE (arglist
));
1220 arglist
= copy_node (arglist
);
1222 op0
= expand_expr (TREE_VALUE (arglist
), subtarget
, VOIDmode
, 0);
1224 /* Make a suitable register to place result in. */
1225 target
= gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp
)));
1230 switch (DECL_FUNCTION_CODE (fndecl
))
1233 builtin_optab
= sin_optab
; break;
1235 builtin_optab
= cos_optab
; break;
1236 case BUILT_IN_FSQRT
:
1237 builtin_optab
= sqrt_optab
; break;
1242 /* Compute into TARGET.
1243 Set TARGET to wherever the result comes back. */
1244 target
= expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist
))),
1245 builtin_optab
, op0
, target
, 0);
1247 /* If we were unable to expand via the builtin, stop the
1248 sequence (without outputting the insns) and return 0, causing
1249 a call to the library function. */
1256 /* Check the results by default. But if flag_fast_math is turned on,
1257 then assume sqrt will always be called with valid arguments. */
1259 if (flag_errno_math
&& ! flag_fast_math
)
1263 /* Don't define the builtin FP instructions
1264 if your machine is not IEEE. */
1265 if (TARGET_FLOAT_FORMAT
!= IEEE_FLOAT_FORMAT
)
1268 lab1
= gen_label_rtx ();
1270 /* Test the result; if it is NaN, set errno=EDOM because
1271 the argument was not in the domain. */
1272 emit_cmp_and_jump_insns (target
, target
, EQ
, 0, GET_MODE (target
),
1277 #ifdef GEN_ERRNO_RTX
1278 rtx errno_rtx
= GEN_ERRNO_RTX
;
1281 = gen_rtx_MEM (word_mode
, gen_rtx_SYMBOL_REF (Pmode
, "errno"));
1284 emit_move_insn (errno_rtx
, GEN_INT (TARGET_EDOM
));
1287 /* We can't set errno=EDOM directly; let the library call do it.
1288 Pop the arguments right away in case the call gets deleted. */
1290 expand_call (exp
, target
, 0);
1297 /* Output the entire sequence. */
1298 insns
= get_insns ();
1305 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1306 if we failed the caller should emit a normal call, otherwise
1307 try to get the result in TARGET, if convenient (and in mode MODE if that's
1310 expand_builtin_strlen (exp
, target
, mode
)
1313 enum machine_mode mode
;
1315 tree arglist
= TREE_OPERAND (exp
, 1);
1316 enum machine_mode value_mode
= TYPE_MODE (TREE_TYPE (exp
));
1319 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1320 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
)
1325 tree src
= TREE_VALUE (arglist
);
1326 tree len
= c_strlen (src
);
1329 = get_pointer_alignment (src
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
1331 rtx result
, src_reg
, char_rtx
, before_strlen
;
1332 enum machine_mode insn_mode
= value_mode
, char_mode
;
1333 enum insn_code icode
= CODE_FOR_nothing
;
1335 /* If the length is known, just return it. */
1337 return expand_expr (len
, target
, mode
, EXPAND_MEMORY_USE_BAD
);
1339 /* If SRC is not a pointer type, don't do this operation inline. */
1343 /* Bail out if we can't compute strlen in the right mode. */
1344 while (insn_mode
!= VOIDmode
)
1346 icode
= strlen_optab
->handlers
[(int) insn_mode
].insn_code
;
1347 if (icode
!= CODE_FOR_nothing
)
1350 insn_mode
= GET_MODE_WIDER_MODE (insn_mode
);
1352 if (insn_mode
== VOIDmode
)
1355 /* Make a place to write the result of the instruction. */
1358 && GET_CODE (result
) == REG
1359 && GET_MODE (result
) == insn_mode
1360 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
1361 result
= gen_reg_rtx (insn_mode
);
1363 /* Make a place to hold the source address. We will not expand
1364 the actual source until we are sure that the expansion will
1365 not fail -- there are trees that cannot be expanded twice. */
1366 src_reg
= gen_reg_rtx (Pmode
);
1368 /* Mark the beginning of the strlen sequence so we can emit the
1369 source operand later. */
1370 before_strlen
= get_last_insn();
1372 /* Check the string is readable and has an end. */
1373 if (current_function_check_memory_usage
)
1374 emit_library_call (chkr_check_str_libfunc
, 1, VOIDmode
, 2,
1376 GEN_INT (MEMORY_USE_RO
),
1377 TYPE_MODE (integer_type_node
));
1379 char_rtx
= const0_rtx
;
1380 char_mode
= insn_data
[(int)icode
].operand
[2].mode
;
1381 if (! (*insn_data
[(int)icode
].operand
[2].predicate
) (char_rtx
, char_mode
))
1382 char_rtx
= copy_to_mode_reg (char_mode
, char_rtx
);
1384 pat
= GEN_FCN (icode
) (result
, gen_rtx_MEM (BLKmode
, src_reg
),
1385 char_rtx
, GEN_INT (align
));
1390 /* Now that we are assured of success, expand the source. */
1392 pat
= memory_address (BLKmode
,
1393 expand_expr (src
, src_reg
, ptr_mode
, EXPAND_SUM
));
1395 emit_move_insn (src_reg
, pat
);
1396 pat
= gen_sequence ();
1400 emit_insn_after (pat
, before_strlen
);
1402 emit_insn_before (pat
, get_insns ());
1404 /* Return the value in the proper mode for this function. */
1405 if (GET_MODE (result
) == value_mode
)
1407 else if (target
!= 0)
1408 convert_move (target
, result
, 0);
1410 target
= convert_to_mode (value_mode
, result
, 0);
1416 /* Expand a call to the memcpy builtin, with arguments in ARGLIST. */
1418 expand_builtin_memcpy (arglist
)
1422 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1423 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
1424 || TREE_CHAIN (arglist
) == 0
1425 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
))))
1427 || TREE_CHAIN (TREE_CHAIN (arglist
)) == 0
1428 || (TREE_CODE (TREE_TYPE (TREE_VALUE
1429 (TREE_CHAIN (TREE_CHAIN (arglist
)))))
1434 tree dest
= TREE_VALUE (arglist
);
1435 tree src
= TREE_VALUE (TREE_CHAIN (arglist
));
1436 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
1438 int src_align
= get_pointer_alignment (src
, BIGGEST_ALIGNMENT
);
1439 int dest_align
= get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
1440 rtx dest_mem
, src_mem
, dest_addr
, len_rtx
;
1442 /* If either SRC or DEST is not a pointer type, don't do
1443 this operation in-line. */
1444 if (src_align
== 0 || dest_align
== 0)
1447 dest_mem
= get_memory_rtx (dest
);
1448 src_mem
= get_memory_rtx (src
);
1449 len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
1451 /* Just copy the rights of SRC to the rights of DEST. */
1452 if (current_function_check_memory_usage
)
1453 emit_library_call (chkr_copy_bitmap_libfunc
, 1, VOIDmode
, 3,
1454 XEXP (dest_mem
, 0), Pmode
,
1455 XEXP (src_mem
, 0), Pmode
,
1456 len_rtx
, TYPE_MODE (sizetype
));
1458 /* Copy word part most expediently. */
1460 = emit_block_move (dest_mem
, src_mem
, len_rtx
,
1461 MIN (src_align
, dest_align
));
1464 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
1470 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
1471 if we failed the caller should emit a normal call. */
1474 expand_builtin_strcpy (exp
)
1477 tree arglist
= TREE_OPERAND (exp
, 1);
1481 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1482 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
1483 || TREE_CHAIN (arglist
) == 0
1484 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
))))
1489 tree len
= c_strlen (TREE_VALUE (TREE_CHAIN (arglist
)));
1494 len
= size_binop (PLUS_EXPR
, len
, ssize_int (1));
1495 chainon (arglist
, build_tree_list (NULL_TREE
, len
));
1498 result
= expand_builtin_memcpy (arglist
);
1501 TREE_CHAIN (TREE_CHAIN (arglist
)) = 0;
1505 /* Expand expression EXP, which is a call to the memset builtin. Return 0
1506 if we failed the caller should emit a normal call. */
1509 expand_builtin_memset (exp
)
1512 tree arglist
= TREE_OPERAND (exp
, 1);
1515 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1516 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
1517 || TREE_CHAIN (arglist
) == 0
1518 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
))))
1520 || TREE_CHAIN (TREE_CHAIN (arglist
)) == 0
1522 != (TREE_CODE (TREE_TYPE
1524 (TREE_CHAIN (TREE_CHAIN (arglist
))))))))
1528 tree dest
= TREE_VALUE (arglist
);
1529 tree val
= TREE_VALUE (TREE_CHAIN (arglist
));
1530 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
1532 int dest_align
= get_pointer_alignment (dest
, BIGGEST_ALIGNMENT
);
1533 rtx dest_mem
, dest_addr
, len_rtx
;
1535 /* If DEST is not a pointer type, don't do this
1536 operation in-line. */
1537 if (dest_align
== 0)
1540 /* If the arguments have side-effects, then we can only evaluate
1541 them at most once. The following code evaluates them twice if
1542 they are not constants because we break out to expand_call
1543 in that case. They can't be constants if they have side-effects
1544 so we can check for that first. Alternatively, we could call
1545 save_expr to make multiple evaluation safe. */
1546 if (TREE_SIDE_EFFECTS (val
) || TREE_SIDE_EFFECTS (len
))
1549 /* If VAL is not 0, don't do this operation in-line. */
1550 if (expand_expr (val
, NULL_RTX
, VOIDmode
, 0) != const0_rtx
)
1553 len_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
1555 dest_mem
= get_memory_rtx (dest
);
1557 /* Just check DST is writable and mark it as readable. */
1558 if (current_function_check_memory_usage
)
1559 emit_library_call (chkr_check_addr_libfunc
, 1, VOIDmode
, 3,
1560 XEXP (dest_mem
, 0), Pmode
,
1561 len_rtx
, TYPE_MODE (sizetype
),
1562 GEN_INT (MEMORY_USE_WO
),
1563 TYPE_MODE (integer_type_node
));
1566 dest_addr
= clear_storage (dest_mem
, len_rtx
, dest_align
);
1569 dest_addr
= force_operand (XEXP (dest_mem
, 0), NULL_RTX
);
1575 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
1576 if we failed the caller should emit a normal call. */
1578 expand_builtin_bzero (exp
)
1581 tree arglist
= TREE_OPERAND (exp
, 1);
1582 tree dest
, size
, newarglist
;
1586 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1587 || TREE_CODE (TREE_TYPE (dest
= TREE_VALUE (arglist
))) != POINTER_TYPE
1588 || TREE_CHAIN (arglist
) == 0
1589 || (TREE_CODE (TREE_TYPE (size
= TREE_VALUE (TREE_CHAIN (arglist
))))
1593 /* New argument list transforming bzero(ptr x, int y) to
1594 memset(ptr x, int 0, size_t y). */
1596 newarglist
= build_tree_list (NULL_TREE
, convert (sizetype
, size
));
1597 newarglist
= tree_cons (NULL_TREE
, integer_zero_node
, newarglist
);
1598 newarglist
= tree_cons (NULL_TREE
, dest
, newarglist
);
1600 TREE_OPERAND (exp
, 1) = newarglist
;
1601 result
= expand_builtin_memset(exp
);
1603 /* Always restore the original arguments. */
1604 TREE_OPERAND (exp
, 1) = arglist
;
1609 #ifdef HAVE_cmpstrsi
1610 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
1611 ARGLIST is the argument list for this call. Return 0 if we failed and the
1612 caller should emit a normal call, otherwise try to get the result in
1613 TARGET, if convenient. */
1615 expand_builtin_memcmp (exp
, arglist
, target
)
1620 /* If we need to check memory accesses, call the library function. */
1621 if (current_function_check_memory_usage
)
1625 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1626 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
1627 || TREE_CHAIN (arglist
) == 0
1628 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
)))) != POINTER_TYPE
1629 || TREE_CHAIN (TREE_CHAIN (arglist
)) == 0
1630 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
))))) != INTEGER_TYPE
)
1634 enum machine_mode mode
;
1635 tree arg1
= TREE_VALUE (arglist
);
1636 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
1637 tree len
= TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
)));
1638 rtx arg1_rtx
, arg2_rtx
, arg3_rtx
;
1643 = get_pointer_alignment (arg1
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
1645 = get_pointer_alignment (arg2
, BIGGEST_ALIGNMENT
) / BITS_PER_UNIT
;
1646 enum machine_mode insn_mode
1647 = insn_data
[(int) CODE_FOR_cmpstrsi
].operand
[0].mode
;
1649 /* If we don't have POINTER_TYPE, call the function. */
1650 if (arg1_align
== 0 || arg2_align
== 0)
1653 /* Make a place to write the result of the instruction. */
1656 && GET_CODE (result
) == REG
&& GET_MODE (result
) == insn_mode
1657 && REGNO (result
) >= FIRST_PSEUDO_REGISTER
))
1658 result
= gen_reg_rtx (insn_mode
);
1660 arg1_rtx
= get_memory_rtx (arg1
);
1661 arg2_rtx
= get_memory_rtx (arg2
);
1662 arg3_rtx
= expand_expr (len
, NULL_RTX
, VOIDmode
, 0);
1666 insn
= gen_cmpstrsi (result
, arg1_rtx
, arg2_rtx
, arg3_rtx
,
1667 GEN_INT (MIN (arg1_align
, arg2_align
)));
1672 emit_library_call_value (memcmp_libfunc
, result
, 2,
1673 TYPE_MODE (integer_type_node
), 3,
1674 XEXP (arg1_rtx
, 0), Pmode
,
1675 XEXP (arg2_rtx
, 0), Pmode
,
1676 convert_to_mode (TYPE_MODE (sizetype
), arg3_rtx
,
1677 TREE_UNSIGNED (sizetype
)),
1678 TYPE_MODE (sizetype
));
1680 /* Return the value in the proper mode for this function. */
1681 mode
= TYPE_MODE (TREE_TYPE (exp
));
1682 if (GET_MODE (result
) == mode
)
1684 else if (target
!= 0)
1686 convert_move (target
, result
, 0);
1690 return convert_to_mode (mode
, result
, 0);
1694 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
1695 if we failed the caller should emit a normal call, otherwise try to get
1696 the result in TARGET, if convenient. */
1699 expand_builtin_strcmp (exp
, target
)
1703 tree arglist
= TREE_OPERAND (exp
, 1);
1705 /* If we need to check memory accesses, call the library function. */
1706 if (current_function_check_memory_usage
)
1710 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1711 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
1712 || TREE_CHAIN (arglist
) == 0
1713 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
))))
1717 else if (! HAVE_cmpstrsi
)
1720 tree arg1
= TREE_VALUE (arglist
);
1721 tree arg2
= TREE_VALUE (TREE_CHAIN (arglist
));
1722 tree len
= c_strlen (arg1
);
1723 tree len2
= c_strlen (arg2
);
1727 len
= size_binop (PLUS_EXPR
, ssize_int (1), len
);
1730 len2
= size_binop (PLUS_EXPR
, ssize_int (1), len2
);
1732 /* If we don't have a constant length for the first, use the length
1733 of the second, if we know it. We don't require a constant for
1734 this case; some cost analysis could be done if both are available
1735 but neither is constant. For now, assume they're equally cheap.
1737 If both strings have constant lengths, use the smaller. This
1738 could arise if optimization results in strcpy being called with
1739 two fixed strings, or if the code was machine-generated. We should
1740 add some code to the `memcmp' handler below to deal with such
1741 situations, someday. */
1743 if (!len
|| TREE_CODE (len
) != INTEGER_CST
)
1750 else if (len2
&& TREE_CODE (len2
) == INTEGER_CST
1751 && tree_int_cst_lt (len2
, len
))
1754 chainon (arglist
, build_tree_list (NULL_TREE
, len
));
1755 result
= expand_builtin_memcmp (exp
, arglist
, target
);
1757 TREE_CHAIN (TREE_CHAIN (arglist
)) = 0;
1764 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
1765 if that's convenient. */
1768 expand_builtin_saveregs ()
1772 /* Don't do __builtin_saveregs more than once in a function.
1773 Save the result of the first call and reuse it. */
1774 if (saveregs_value
!= 0)
1775 return saveregs_value
;
1777 /* When this function is called, it means that registers must be
1778 saved on entry to this function. So we migrate the call to the
1779 first insn of this function. */
1783 #ifdef EXPAND_BUILTIN_SAVEREGS
1784 /* Do whatever the machine needs done in this case. */
1785 val
= EXPAND_BUILTIN_SAVEREGS ();
1787 /* ??? We used to try and build up a call to the out of line function,
1788 guessing about what registers needed saving etc. This became much
1789 harder with __builtin_va_start, since we don't have a tree for a
1790 call to __builtin_saveregs to fall back on. There was exactly one
1791 port (i860) that used this code, and I'm unconvinced it could actually
1792 handle the general case. So we no longer try to handle anything
1793 weird and make the backend absorb the evil. */
1795 error ("__builtin_saveregs not supported by this target");
1802 saveregs_value
= val
;
1804 /* Put the sequence after the NOTE that starts the function. If this
1805 is inside a SEQUENCE, make the outer-level insn chain current, so
1806 the code is placed at the start of the function. */
1807 push_topmost_sequence ();
1808 emit_insns_after (seq
, get_insns ());
1809 pop_topmost_sequence ();
1814 /* __builtin_args_info (N) returns word N of the arg space info
1815 for the current function. The number and meanings of words
1816 is controlled by the definition of CUMULATIVE_ARGS. */
1818 expand_builtin_args_info (exp
)
1821 tree arglist
= TREE_OPERAND (exp
, 1);
1822 int nwords
= sizeof (CUMULATIVE_ARGS
) / sizeof (int);
1823 int *word_ptr
= (int *) ¤t_function_args_info
;
1825 /* These are used by the code below that is if 0'ed away */
1827 tree type
, elts
, result
;
1830 if (sizeof (CUMULATIVE_ARGS
) % sizeof (int) != 0)
1835 tree arg
= TREE_VALUE (arglist
);
1836 if (TREE_CODE (arg
) != INTEGER_CST
)
1837 error ("argument of `__builtin_args_info' must be constant");
1840 int wordnum
= TREE_INT_CST_LOW (arg
);
1842 if (wordnum
< 0 || wordnum
>= nwords
|| TREE_INT_CST_HIGH (arg
))
1843 error ("argument of `__builtin_args_info' out of range");
1845 return GEN_INT (word_ptr
[wordnum
]);
1849 error ("missing argument in `__builtin_args_info'");
1854 for (i
= 0; i
< nwords
; i
++)
1855 elts
= tree_cons (NULL_TREE
, build_int_2 (word_ptr
[i
], 0));
1857 type
= build_array_type (integer_type_node
,
1858 build_index_type (build_int_2 (nwords
, 0)));
1859 result
= build (CONSTRUCTOR
, type
, NULL_TREE
, nreverse (elts
));
1860 TREE_CONSTANT (result
) = 1;
1861 TREE_STATIC (result
) = 1;
1862 result
= build1 (INDIRECT_REF
, build_pointer_type (type
), result
);
1863 TREE_CONSTANT (result
) = 1;
1864 return expand_expr (result
, NULL_RTX
, VOIDmode
, EXPAND_MEMORY_USE_BAD
);
1868 /* Expand ARGLIST, from a call to __builtin_next_arg. */
1870 expand_builtin_next_arg (arglist
)
1873 tree fntype
= TREE_TYPE (current_function_decl
);
1875 if ((TYPE_ARG_TYPES (fntype
) == 0
1876 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype
)))
1878 && ! current_function_varargs
)
1880 error ("`va_start' used in function with fixed args");
1886 tree last_parm
= tree_last (DECL_ARGUMENTS (current_function_decl
));
1887 tree arg
= TREE_VALUE (arglist
);
1889 /* Strip off all nops for the sake of the comparison. This
1890 is not quite the same as STRIP_NOPS. It does more.
1891 We must also strip off INDIRECT_EXPR for C++ reference
1893 while (TREE_CODE (arg
) == NOP_EXPR
1894 || TREE_CODE (arg
) == CONVERT_EXPR
1895 || TREE_CODE (arg
) == NON_LVALUE_EXPR
1896 || TREE_CODE (arg
) == INDIRECT_REF
)
1897 arg
= TREE_OPERAND (arg
, 0);
1898 if (arg
!= last_parm
)
1899 warning ("second parameter of `va_start' not last named argument");
1901 else if (! current_function_varargs
)
1902 /* Evidently an out of date version of <stdarg.h>; can't validate
1903 va_start's second argument, but can still work as intended. */
1904 warning ("`__builtin_next_arg' called without an argument");
1906 return expand_binop (Pmode
, add_optab
,
1907 current_function_internal_arg_pointer
,
1908 current_function_arg_offset_rtx
,
1909 NULL_RTX
, 0, OPTAB_LIB_WIDEN
);
1912 /* Make it easier for the backends by protecting the valist argument
1913 from multiple evaluations. */
1916 stabilize_va_list (valist
, needs_lvalue
)
1920 if (TREE_CODE (va_list_type_node
) == ARRAY_TYPE
)
1922 if (TREE_SIDE_EFFECTS (valist
))
1923 valist
= save_expr (valist
);
1925 /* For this case, the backends will be expecting a pointer to
1926 TREE_TYPE (va_list_type_node), but it's possible we've
1927 actually been given an array (an actual va_list_type_node).
1929 if (TREE_CODE (TREE_TYPE (valist
)) == ARRAY_TYPE
)
1931 tree p1
= build_pointer_type (TREE_TYPE (va_list_type_node
));
1932 tree p2
= build_pointer_type (va_list_type_node
);
1934 valist
= build1 (ADDR_EXPR
, p2
, valist
);
1935 valist
= fold (build1 (NOP_EXPR
, p1
, valist
));
1944 if (! TREE_SIDE_EFFECTS (valist
))
1947 pt
= build_pointer_type (va_list_type_node
);
1948 valist
= fold (build1 (ADDR_EXPR
, pt
, valist
));
1949 TREE_SIDE_EFFECTS (valist
) = 1;
1952 if (TREE_SIDE_EFFECTS (valist
))
1953 valist
= save_expr (valist
);
1954 valist
= fold (build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (valist
)),
1961 /* The "standard" implementation of va_start: just assign `nextarg' to
1964 std_expand_builtin_va_start (stdarg_p
, valist
, nextarg
)
1965 int stdarg_p ATTRIBUTE_UNUSED
;
1972 nextarg
= plus_constant (nextarg
, -UNITS_PER_WORD
);
1974 t
= build (MODIFY_EXPR
, TREE_TYPE (valist
), valist
,
1975 make_tree (ptr_type_node
, nextarg
));
1976 TREE_SIDE_EFFECTS (t
) = 1;
1978 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
1981 /* Expand ARGLIST, which from a call to __builtin_stdarg_va_start or
1982 __builtin_varargs_va_start, depending on STDARG_P. */
1984 expand_builtin_va_start (stdarg_p
, arglist
)
1989 tree chain
= arglist
, valist
;
1992 nextarg
= expand_builtin_next_arg (chain
= TREE_CHAIN (arglist
));
1994 nextarg
= expand_builtin_next_arg (NULL_TREE
);
1996 if (TREE_CHAIN (chain
))
1997 error ("too many arguments to function `va_start'");
1999 valist
= stabilize_va_list (TREE_VALUE (arglist
), 1);
2001 #ifdef EXPAND_BUILTIN_VA_START
2002 EXPAND_BUILTIN_VA_START (stdarg_p
, valist
, nextarg
);
2004 std_expand_builtin_va_start (stdarg_p
, valist
, nextarg
);
2010 /* Allocate an alias set for use in storing and reading from the varargs
2013 get_varargs_alias_set ()
2015 static int set
= -1;
2017 set
= new_alias_set ();
2021 /* The "standard" implementation of va_arg: read the value from the
2022 current (padded) address and increment by the (padded) size. */
2024 std_expand_builtin_va_arg (valist
, type
)
2028 HOST_WIDE_INT align
;
2029 HOST_WIDE_INT rounded_size
;
2032 /* Compute the rounded size of the type. */
2033 align
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
2034 rounded_size
= (((int_size_in_bytes (type
) + align
- 1) / align
) * align
);
2038 if (PAD_VARARGS_DOWN
)
2040 /* Small args are padded downward. */
2043 adj
= TREE_INT_CST_LOW (TYPE_SIZE (type
)) / BITS_PER_UNIT
;
2044 if (rounded_size
> align
)
2047 addr_tree
= build (PLUS_EXPR
, TREE_TYPE (addr_tree
), addr_tree
,
2048 build_int_2 (rounded_size
- adj
, 0));
2051 addr
= expand_expr (addr_tree
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
2052 addr
= copy_to_reg (addr
);
2054 /* Compute new value for AP. */
2055 t
= build (MODIFY_EXPR
, TREE_TYPE (valist
), valist
,
2056 build (PLUS_EXPR
, TREE_TYPE (valist
), valist
,
2057 build_int_2 (rounded_size
, 0)));
2058 TREE_SIDE_EFFECTS (t
) = 1;
2059 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2064 /* Expand __builtin_va_arg, which is not really a builtin function, but
2065 a very special sort of operator. */
2067 expand_builtin_va_arg (valist
, type
)
2071 tree promoted_type
, want_va_type
, have_va_type
;
2073 /* Verify that valist is of the proper type. */
2075 want_va_type
= va_list_type_node
;
2076 have_va_type
= TREE_TYPE (valist
);
2077 if (TREE_CODE (want_va_type
) == ARRAY_TYPE
)
2079 /* If va_list is an array type, the argument may have decayed
2080 to a pointer type, e.g. by being passed to another function.
2081 In that case, unwrap both types so that we can compare the
2082 underlying records. */
2083 if (TREE_CODE (have_va_type
) == ARRAY_TYPE
2084 || TREE_CODE (have_va_type
) == POINTER_TYPE
)
2086 want_va_type
= TREE_TYPE (want_va_type
);
2087 have_va_type
= TREE_TYPE (have_va_type
);
2090 if (TYPE_MAIN_VARIANT (want_va_type
) != TYPE_MAIN_VARIANT (have_va_type
))
2092 error ("first argument to `va_arg' not of type `va_list'");
2096 /* Generate a diagnostic for requesting data of a type that cannot
2097 be passed through `...' due to type promotion at the call site. */
2098 else if ((promoted_type
= (*lang_type_promotes_to
) (type
)) != NULL_TREE
)
2100 const char *name
= "<anonymous type>", *pname
= 0;
2101 static int gave_help
;
2103 if (TYPE_NAME (type
))
2105 if (TREE_CODE (TYPE_NAME (type
)) == IDENTIFIER_NODE
)
2106 name
= IDENTIFIER_POINTER (TYPE_NAME (type
));
2107 else if (TREE_CODE (TYPE_NAME (type
)) == TYPE_DECL
2108 && DECL_NAME (TYPE_NAME (type
)))
2109 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type
)));
2111 if (TYPE_NAME (promoted_type
))
2113 if (TREE_CODE (TYPE_NAME (promoted_type
)) == IDENTIFIER_NODE
)
2114 pname
= IDENTIFIER_POINTER (TYPE_NAME (promoted_type
));
2115 else if (TREE_CODE (TYPE_NAME (promoted_type
)) == TYPE_DECL
2116 && DECL_NAME (TYPE_NAME (promoted_type
)))
2117 pname
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type
)));
2120 error ("`%s' is promoted to `%s' when passed through `...'", name
, pname
);
2124 error ("(so you should pass `%s' not `%s' to `va_arg')", pname
, name
);
2131 /* Make it easier for the backends by protecting the valist argument
2132 from multiple evaluations. */
2133 valist
= stabilize_va_list (valist
, 0);
2135 #ifdef EXPAND_BUILTIN_VA_ARG
2136 addr
= EXPAND_BUILTIN_VA_ARG (valist
, type
);
2138 addr
= std_expand_builtin_va_arg (valist
, type
);
2142 result
= gen_rtx_MEM (TYPE_MODE (type
), addr
);
2143 MEM_ALIAS_SET (result
) = get_varargs_alias_set ();
2148 /* Expand ARGLIST, from a call to __builtin_va_end. */
2150 expand_builtin_va_end (arglist
)
2153 tree valist
= TREE_VALUE (arglist
);
2155 #ifdef EXPAND_BUILTIN_VA_END
2156 valist
= stabilize_va_list (valist
, 0);
2157 EXPAND_BUILTIN_VA_END(arglist
);
2159 /* Evaluate for side effects, if needed. I hate macros that don't
2161 if (TREE_SIDE_EFFECTS (valist
))
2162 expand_expr (valist
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2168 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
2169 builtin rather than just as an assignment in stdarg.h because of the
2170 nastiness of array-type va_list types. */
2172 expand_builtin_va_copy (arglist
)
2177 dst
= TREE_VALUE (arglist
);
2178 src
= TREE_VALUE (TREE_CHAIN (arglist
));
2180 dst
= stabilize_va_list (dst
, 1);
2181 src
= stabilize_va_list (src
, 0);
2183 if (TREE_CODE (va_list_type_node
) != ARRAY_TYPE
)
2185 t
= build (MODIFY_EXPR
, va_list_type_node
, dst
, src
);
2186 TREE_SIDE_EFFECTS (t
) = 1;
2187 expand_expr (t
, const0_rtx
, VOIDmode
, EXPAND_NORMAL
);
2191 rtx dstb
, srcb
, size
;
2193 /* Evaluate to pointers. */
2194 dstb
= expand_expr (dst
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
2195 srcb
= expand_expr (src
, NULL_RTX
, Pmode
, EXPAND_NORMAL
);
2196 size
= expand_expr (TYPE_SIZE_UNIT (va_list_type_node
), NULL_RTX
,
2197 VOIDmode
, EXPAND_NORMAL
);
2199 /* "Dereference" to BLKmode memories. */
2200 dstb
= gen_rtx_MEM (BLKmode
, dstb
);
2201 MEM_ALIAS_SET (dstb
) = get_alias_set (TREE_TYPE (TREE_TYPE (dst
)));
2202 srcb
= gen_rtx_MEM (BLKmode
, srcb
);
2203 MEM_ALIAS_SET (srcb
) = get_alias_set (TREE_TYPE (TREE_TYPE (src
)));
2206 emit_block_move (dstb
, srcb
, size
, TYPE_ALIGN (va_list_type_node
));
2212 /* Expand a call to one of the builtin functions __builtin_frame_address or
2213 __builtin_return_address. */
2215 expand_builtin_frame_address (exp
)
2218 tree fndecl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
2219 tree arglist
= TREE_OPERAND (exp
, 1);
2221 /* The argument must be a nonnegative integer constant.
2222 It counts the number of frames to scan up the stack.
2223 The value is the return address saved in that frame. */
2225 /* Warning about missing arg was already issued. */
2227 else if (TREE_CODE (TREE_VALUE (arglist
)) != INTEGER_CST
2228 || tree_int_cst_sgn (TREE_VALUE (arglist
)) < 0)
2230 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
2231 error ("invalid arg to `__builtin_frame_address'");
2233 error ("invalid arg to `__builtin_return_address'");
2238 rtx tem
= expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl
),
2239 TREE_INT_CST_LOW (TREE_VALUE (arglist
)),
2240 hard_frame_pointer_rtx
);
2242 /* Some ports cannot access arbitrary stack frames. */
2245 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
2246 warning ("unsupported arg to `__builtin_frame_address'");
2248 warning ("unsupported arg to `__builtin_return_address'");
2252 /* For __builtin_frame_address, return what we've got. */
2253 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_FRAME_ADDRESS
)
2256 if (GET_CODE (tem
) != REG
2257 && ! CONSTANT_P (tem
))
2258 tem
= copy_to_mode_reg (Pmode
, tem
);
2263 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
2264 we failed and the caller should emit a normal call, otherwise try to get
2265 the result in TARGET, if convenient. */
2267 expand_builtin_alloca (arglist
, target
)
2274 /* Arg could be non-integer if user redeclared this fcn wrong. */
2275 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
)
2278 /* Compute the argument. */
2279 op0
= expand_expr (TREE_VALUE (arglist
), NULL_RTX
, VOIDmode
, 0);
2281 /* Allocate the desired space. */
2282 return allocate_dynamic_stack_space (op0
, target
, BITS_PER_UNIT
);
2285 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
2286 Return 0 if a normal call should be emitted rather than expanding the
2287 function in-line. If convenient, the result should be placed in TARGET.
2288 SUBTARGET may be used as the target for computing one of EXP's operands. */
2290 expand_builtin_ffs (arglist
, target
, subtarget
)
2292 rtx target
, subtarget
;
2296 /* Arg could be non-integer if user redeclared this fcn wrong. */
2297 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != INTEGER_TYPE
)
2300 /* Compute the argument. */
2301 op0
= expand_expr (TREE_VALUE (arglist
), subtarget
, VOIDmode
, 0);
2302 /* Compute ffs, into TARGET if possible.
2303 Set TARGET to wherever the result comes back. */
2304 target
= expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist
))),
2305 ffs_optab
, op0
, target
, 1);
2311 /* Expand a call to __builtin_expect. We return our argument and
2312 emit a NOTE_INSN_EXPECTED_VALUE note. */
2315 expand_builtin_expect (arglist
, target
)
2322 if (arglist
== NULL_TREE
2323 || TREE_CHAIN (arglist
) == NULL_TREE
)
2325 exp
= TREE_VALUE (arglist
);
2326 c
= TREE_VALUE (TREE_CHAIN (arglist
));
2328 if (TREE_CODE (c
) != INTEGER_CST
)
2330 error ("second arg to `__builtin_expect' must be a constant");
2331 c
= integer_zero_node
;
2334 target
= expand_expr (exp
, target
, VOIDmode
, EXPAND_NORMAL
);
2336 /* Don't bother with expected value notes for integral constants. */
2337 if (GET_CODE (target
) != CONST_INT
)
2339 /* We do need to force this into a register so that we can be
2340 moderately sure to be able to correctly interpret the branch
2342 target
= force_reg (GET_MODE (target
), target
);
2344 rtx_c
= expand_expr (c
, NULL_RTX
, GET_MODE (target
), EXPAND_NORMAL
);
2346 note
= emit_note (NULL
, NOTE_INSN_EXPECTED_VALUE
);
2347 NOTE_EXPECTED_VALUE (note
) = gen_rtx_EQ (VOIDmode
, target
, rtx_c
);
2353 /* Expand an expression EXP that calls a built-in function,
2354 with result going to TARGET if that's convenient
2355 (and in mode MODE if that's convenient).
2356 SUBTARGET may be used as the target for computing one of EXP's operands.
2357 IGNORE is nonzero if the value is to be ignored. */
2360 expand_builtin (exp
, target
, subtarget
, mode
, ignore
)
2364 enum machine_mode mode
;
2367 tree fndecl
= TREE_OPERAND (TREE_OPERAND (exp
, 0), 0);
2368 tree arglist
= TREE_OPERAND (exp
, 1);
2369 enum built_in_function fcode
= DECL_FUNCTION_CODE (fndecl
);
2371 #ifdef MD_EXPAND_BUILTIN
2372 if (DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_MD
)
2373 return MD_EXPAND_BUILTIN (exp
, target
, subtarget
, mode
, ignore
);
2376 /* When not optimizing, generate calls to library functions for a certain
2378 if (! optimize
&& ! CALLED_AS_BUILT_IN (fndecl
)
2379 && (fcode
== BUILT_IN_SIN
|| fcode
== BUILT_IN_COS
2380 || fcode
== BUILT_IN_FSQRT
|| fcode
== BUILT_IN_MEMSET
2381 || fcode
== BUILT_IN_MEMCPY
|| fcode
== BUILT_IN_MEMCMP
2382 || fcode
== BUILT_IN_BCMP
|| fcode
== BUILT_IN_BZERO
2383 || fcode
== BUILT_IN_STRLEN
|| fcode
== BUILT_IN_STRCPY
2384 || fcode
== BUILT_IN_STRCMP
|| fcode
== BUILT_IN_FFS
))
2385 return expand_call (exp
, target
, ignore
);
2392 /* build_function_call changes these into ABS_EXPR. */
2397 /* Treat these like sqrt, but only if the user asks for them. */
2398 if (! flag_fast_math
)
2400 case BUILT_IN_FSQRT
:
2401 target
= expand_builtin_mathfn (exp
, target
, subtarget
);
2409 case BUILT_IN_APPLY_ARGS
:
2410 return expand_builtin_apply_args ();
2412 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
2413 FUNCTION with a copy of the parameters described by
2414 ARGUMENTS, and ARGSIZE. It returns a block of memory
2415 allocated on the stack into which is stored all the registers
2416 that might possibly be used for returning the result of a
2417 function. ARGUMENTS is the value returned by
2418 __builtin_apply_args. ARGSIZE is the number of bytes of
2419 arguments that must be copied. ??? How should this value be
2420 computed? We'll also need a safe worst case value for varargs
2422 case BUILT_IN_APPLY
:
2424 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2425 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist
)))
2426 || TREE_CHAIN (arglist
) == 0
2427 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist
)))) != POINTER_TYPE
2428 || TREE_CHAIN (TREE_CHAIN (arglist
)) == 0
2429 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
))))) != INTEGER_TYPE
)
2437 for (t
= arglist
, i
= 0; t
; t
= TREE_CHAIN (t
), i
++)
2438 ops
[i
] = expand_expr (TREE_VALUE (t
), NULL_RTX
, VOIDmode
, 0);
2440 return expand_builtin_apply (ops
[0], ops
[1], ops
[2]);
2443 /* __builtin_return (RESULT) causes the function to return the
2444 value described by RESULT. RESULT is address of the block of
2445 memory returned by __builtin_apply. */
2446 case BUILT_IN_RETURN
:
2448 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2449 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) == POINTER_TYPE
)
2450 expand_builtin_return (expand_expr (TREE_VALUE (arglist
),
2451 NULL_RTX
, VOIDmode
, 0));
2454 case BUILT_IN_SAVEREGS
:
2455 return expand_builtin_saveregs ();
2457 case BUILT_IN_ARGS_INFO
:
2458 return expand_builtin_args_info (exp
);
2460 /* Return the address of the first anonymous stack arg. */
2461 case BUILT_IN_NEXT_ARG
:
2462 return expand_builtin_next_arg (arglist
);
2464 case BUILT_IN_CLASSIFY_TYPE
:
2465 return expand_builtin_classify_type (arglist
);
2467 case BUILT_IN_CONSTANT_P
:
2468 return expand_builtin_constant_p (exp
);
2470 case BUILT_IN_FRAME_ADDRESS
:
2471 case BUILT_IN_RETURN_ADDRESS
:
2472 return expand_builtin_frame_address (exp
);
2474 /* Returns the address of the area where the structure is returned.
2476 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS
:
2478 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl
)))
2479 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl
))) != MEM
)
2482 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl
)), 0);
2484 case BUILT_IN_ALLOCA
:
2485 target
= expand_builtin_alloca (arglist
, target
);
2491 target
= expand_builtin_ffs (arglist
, target
, subtarget
);
2496 case BUILT_IN_STRLEN
:
2497 target
= expand_builtin_strlen (exp
, target
, mode
);
2502 case BUILT_IN_STRCPY
:
2503 target
= expand_builtin_strcpy (exp
);
2508 case BUILT_IN_MEMCPY
:
2509 target
= expand_builtin_memcpy (arglist
);
2514 case BUILT_IN_MEMSET
:
2515 target
= expand_builtin_memset (exp
);
2520 case BUILT_IN_BZERO
:
2521 target
= expand_builtin_bzero (exp
);
2526 /* These comparison functions need an instruction that returns an actual
2527 index. An ordinary compare that just sets the condition codes
2529 #ifdef HAVE_cmpstrsi
2530 case BUILT_IN_STRCMP
:
2531 target
= expand_builtin_strcmp (exp
, target
);
2537 case BUILT_IN_MEMCMP
:
2538 target
= expand_builtin_memcmp (exp
, arglist
, target
);
2543 case BUILT_IN_STRCMP
:
2545 case BUILT_IN_MEMCMP
:
2549 case BUILT_IN_SETJMP
:
2551 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
)
2555 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
2557 rtx lab
= gen_label_rtx ();
2558 rtx ret
= expand_builtin_setjmp (buf_addr
, target
, lab
, lab
);
2563 /* __builtin_longjmp is passed a pointer to an array of five words.
2564 It's similar to the C library longjmp function but works with
2565 __builtin_setjmp above. */
2566 case BUILT_IN_LONGJMP
:
2567 if (arglist
== 0 || TREE_CHAIN (arglist
) == 0
2568 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist
))) != POINTER_TYPE
)
2572 rtx buf_addr
= expand_expr (TREE_VALUE (arglist
), subtarget
,
2574 rtx value
= expand_expr (TREE_VALUE (TREE_CHAIN (arglist
)),
2575 NULL_RTX
, VOIDmode
, 0);
2577 if (value
!= const1_rtx
)
2579 error ("__builtin_longjmp second argument must be 1");
2583 expand_builtin_longjmp (buf_addr
, value
);
2590 emit_insn (gen_trap ());
2593 error ("__builtin_trap not supported by this target");
2597 /* Various hooks for the DWARF 2 __throw routine. */
2598 case BUILT_IN_UNWIND_INIT
:
2599 expand_builtin_unwind_init ();
2601 case BUILT_IN_DWARF_CFA
:
2602 return virtual_cfa_rtx
;
2603 #ifdef DWARF2_UNWIND_INFO
2604 case BUILT_IN_DWARF_FP_REGNUM
:
2605 return expand_builtin_dwarf_fp_regnum ();
2606 case BUILT_IN_INIT_DWARF_REG_SIZES
:
2607 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist
));
2610 case BUILT_IN_FROB_RETURN_ADDR
:
2611 return expand_builtin_frob_return_addr (TREE_VALUE (arglist
));
2612 case BUILT_IN_EXTRACT_RETURN_ADDR
:
2613 return expand_builtin_extract_return_addr (TREE_VALUE (arglist
));
2614 case BUILT_IN_EH_RETURN
:
2615 expand_builtin_eh_return (TREE_VALUE (arglist
),
2616 TREE_VALUE (TREE_CHAIN (arglist
)),
2617 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist
))));
2619 case BUILT_IN_VARARGS_START
:
2620 return expand_builtin_va_start (0, arglist
);
2621 case BUILT_IN_STDARG_START
:
2622 return expand_builtin_va_start (1, arglist
);
2623 case BUILT_IN_VA_END
:
2624 return expand_builtin_va_end (arglist
);
2625 case BUILT_IN_VA_COPY
:
2626 return expand_builtin_va_copy (arglist
);
2627 case BUILT_IN_EXPECT
:
2628 return expand_builtin_expect (arglist
, target
);
2630 default: /* just do library call, if unknown builtin */
2631 error ("built-in function `%s' not currently supported",
2632 IDENTIFIER_POINTER (DECL_NAME (fndecl
)));
2635 /* The switch statement above can drop through to cause the function
2636 to be called normally. */
2637 return expand_call (exp
, target
, ignore
);