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 tree (*lang_type_promotes_to) PARAMS ((tree));
61 static int get_pointer_alignment PARAMS ((tree, unsigned));
62 static tree c_strlen PARAMS ((tree));
63 static rtx get_memory_rtx PARAMS ((tree));
64 static int apply_args_size PARAMS ((void));
65 static int apply_result_size PARAMS ((void));
66 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
67 static rtx result_vector PARAMS ((int, rtx));
69 static rtx expand_builtin_apply_args PARAMS ((void));
70 static rtx expand_builtin_apply_args_1 PARAMS ((void));
71 static rtx expand_builtin_apply PARAMS ((rtx, rtx, rtx));
72 static void expand_builtin_return PARAMS ((rtx));
73 static rtx expand_builtin_classify_type PARAMS ((tree));
74 static rtx expand_builtin_mathfn PARAMS ((tree, rtx, rtx));
75 static rtx expand_builtin_constant_p PARAMS ((tree));
76 static rtx expand_builtin_args_info PARAMS ((tree));
77 static rtx expand_builtin_next_arg PARAMS ((tree));
78 static rtx expand_builtin_va_start PARAMS ((int, tree));
79 static rtx expand_builtin_va_end PARAMS ((tree));
80 static rtx expand_builtin_va_copy PARAMS ((tree));
82 static rtx expand_builtin_memcmp PARAMS ((tree, tree, rtx));
83 static rtx expand_builtin_strcmp PARAMS ((tree, rtx));
85 static rtx expand_builtin_memcpy PARAMS ((tree));
86 static rtx expand_builtin_strcpy PARAMS ((tree));
87 static rtx expand_builtin_memset PARAMS ((tree));
88 static rtx expand_builtin_strlen PARAMS ((tree, rtx, enum machine_mode));
89 static rtx expand_builtin_alloca PARAMS ((tree, rtx));
90 static rtx expand_builtin_ffs PARAMS ((tree, rtx, rtx));
91 static rtx expand_builtin_frame_address PARAMS ((tree));
92 static tree stabilize_va_list PARAMS ((tree, int));
94 /* Return the alignment in bits of EXP, a pointer valued expression.
95 But don't return more than MAX_ALIGN no matter what.
96 The alignment returned is, by default, the alignment of the thing that
97 EXP points to (if it is not a POINTER_TYPE, 0 is returned).
99 Otherwise, look at the expression to see if we can do better, i.e., if the
100 expression is actually pointing at an object whose alignment is tighter. */
103 get_pointer_alignment (exp, max_align)
107 unsigned align, inner;
109 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
112 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
113 align = MIN (align, max_align);
117 switch (TREE_CODE (exp))
121 case NON_LVALUE_EXPR:
122 exp = TREE_OPERAND (exp, 0);
123 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
125 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
126 align = MIN (inner, max_align);
130 /* If sum of pointer + int, restrict our maximum alignment to that
131 imposed by the integer. If not, we can't do any better than
133 if (TREE_CODE (TREE_OPERAND (exp, 1)) != INTEGER_CST)
136 while (((TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT)
141 exp = TREE_OPERAND (exp, 0);
145 /* See what we are pointing at and look at its alignment. */
146 exp = TREE_OPERAND (exp, 0);
147 if (TREE_CODE (exp) == FUNCTION_DECL)
148 align = FUNCTION_BOUNDARY;
149 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
150 align = DECL_ALIGN (exp);
151 #ifdef CONSTANT_ALIGNMENT
152 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
153 align = CONSTANT_ALIGNMENT (exp, align);
155 return MIN (align, max_align);
163 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
164 way, because it could contain a zero byte in the middle.
165 TREE_STRING_LENGTH is the size of the character array, not the string.
167 The value returned is of type `ssizetype'.
169 Unfortunately, string_constant can't access the values of const char
170 arrays with initializers, so neither can we do so here. */
180 src = string_constant (src, &offset_node);
184 max = TREE_STRING_LENGTH (src);
185 ptr = TREE_STRING_POINTER (src);
187 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
189 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
190 compute the offset to the following null if we don't know where to
191 start searching for it. */
194 for (i = 0; i < max; i++)
198 /* We don't know the starting offset, but we do know that the string
199 has no internal zero bytes. We can assume that the offset falls
200 within the bounds of the string; otherwise, the programmer deserves
201 what he gets. Subtract the offset from the length of the string,
202 and return that. This would perhaps not be valid if we were dealing
203 with named arrays in addition to literal string constants. */
205 return size_diffop (size_int (max), offset_node);
208 /* We have a known offset into the string. Start searching there for
210 if (offset_node == 0)
214 /* Did we get a long long offset? If so, punt. */
215 if (TREE_INT_CST_HIGH (offset_node) != 0)
217 offset = TREE_INT_CST_LOW (offset_node);
220 /* If the offset is known to be out of bounds, warn, and call strlen at
222 if (offset < 0 || offset > max)
224 warning ("offset outside bounds of constant string");
228 /* Use strlen to search for the first zero byte. Since any strings
229 constructed with build_string will have nulls appended, we win even
230 if we get handed something like (char[4])"abcd".
232 Since OFFSET is our starting index into the string, no further
233 calculation is needed. */
234 return ssize_int (strlen (ptr + offset));
237 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
238 times to get the address of either a higher stack frame, or a return
239 address located within it (depending on FNDECL_CODE). */
242 expand_builtin_return_addr (fndecl_code, count, tem)
243 enum built_in_function fndecl_code;
249 /* Some machines need special handling before we can access
250 arbitrary frames. For example, on the sparc, we must first flush
251 all register windows to the stack. */
252 #ifdef SETUP_FRAME_ADDRESSES
254 SETUP_FRAME_ADDRESSES ();
257 /* On the sparc, the return address is not in the frame, it is in a
258 register. There is no way to access it off of the current frame
259 pointer, but it can be accessed off the previous frame pointer by
260 reading the value from the register window save area. */
261 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
262 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
266 /* Scan back COUNT frames to the specified frame. */
267 for (i = 0; i < count; i++)
269 /* Assume the dynamic chain pointer is in the word that the
270 frame address points to, unless otherwise specified. */
271 #ifdef DYNAMIC_CHAIN_ADDRESS
272 tem = DYNAMIC_CHAIN_ADDRESS (tem);
274 tem = memory_address (Pmode, tem);
275 tem = copy_to_reg (gen_rtx_MEM (Pmode, tem));
278 /* For __builtin_frame_address, return what we've got. */
279 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
282 /* For __builtin_return_address, Get the return address from that
284 #ifdef RETURN_ADDR_RTX
285 tem = RETURN_ADDR_RTX (count, tem);
287 tem = memory_address (Pmode,
288 plus_constant (tem, GET_MODE_SIZE (Pmode)));
289 tem = gen_rtx_MEM (Pmode, tem);
294 /* __builtin_setjmp is passed a pointer to an array of five words (not
295 all will be used on all machines). It operates similarly to the C
296 library function of the same name, but is more efficient. Much of
297 the code below (and for longjmp) is copied from the handling of
300 NOTE: This is intended for use by GNAT and the exception handling
301 scheme in the compiler and will only work in the method used by
305 expand_builtin_setjmp (buf_addr, target, first_label, next_label)
308 rtx first_label, next_label;
310 rtx lab1 = gen_label_rtx ();
311 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
312 enum machine_mode value_mode;
315 value_mode = TYPE_MODE (integer_type_node);
317 #ifdef POINTERS_EXTEND_UNSIGNED
318 buf_addr = convert_memory_address (Pmode, buf_addr);
321 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
323 if (target == 0 || GET_CODE (target) != REG
324 || REGNO (target) < FIRST_PSEUDO_REGISTER)
325 target = gen_reg_rtx (value_mode);
329 /* We store the frame pointer and the address of lab1 in the buffer
330 and use the rest of it for the stack save area, which is
331 machine-dependent. */
333 #ifndef BUILTIN_SETJMP_FRAME_VALUE
334 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
337 emit_move_insn (gen_rtx_MEM (Pmode, buf_addr),
338 BUILTIN_SETJMP_FRAME_VALUE);
339 emit_move_insn (validize_mem
341 plus_constant (buf_addr,
342 GET_MODE_SIZE (Pmode)))),
343 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, lab1)));
345 stack_save = gen_rtx_MEM (sa_mode,
346 plus_constant (buf_addr,
347 2 * GET_MODE_SIZE (Pmode)));
348 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
350 /* If there is further processing to do, do it. */
351 #ifdef HAVE_builtin_setjmp_setup
352 if (HAVE_builtin_setjmp_setup)
353 emit_insn (gen_builtin_setjmp_setup (buf_addr));
356 /* Set TARGET to zero and branch to the first-time-through label. */
357 emit_move_insn (target, const0_rtx);
358 emit_jump_insn (gen_jump (first_label));
362 /* Tell flow about the strange goings on. Putting `lab1' on
363 `nonlocal_goto_handler_labels' to indicates that function
364 calls may traverse the arc back to this label. */
366 current_function_has_nonlocal_label = 1;
367 nonlocal_goto_handler_labels =
368 gen_rtx_EXPR_LIST (VOIDmode, lab1, nonlocal_goto_handler_labels);
370 /* Clobber the FP when we get here, so we have to make sure it's
371 marked as used by this function. */
372 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
374 /* Mark the static chain as clobbered here so life information
375 doesn't get messed up for it. */
376 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
378 /* Now put in the code to restore the frame pointer, and argument
379 pointer, if needed. The code below is from expand_end_bindings
380 in stmt.c; see detailed documentation there. */
381 #ifdef HAVE_nonlocal_goto
382 if (! HAVE_nonlocal_goto)
384 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
386 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
387 if (fixed_regs[ARG_POINTER_REGNUM])
389 #ifdef ELIMINABLE_REGS
391 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
393 for (i = 0; i < sizeof elim_regs / sizeof elim_regs[0]; i++)
394 if (elim_regs[i].from == ARG_POINTER_REGNUM
395 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
398 if (i == sizeof elim_regs / sizeof elim_regs [0])
401 /* Now restore our arg pointer from the address at which it
402 was saved in our stack frame.
403 If there hasn't be space allocated for it yet, make
405 if (arg_pointer_save_area == 0)
406 arg_pointer_save_area
407 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
408 emit_move_insn (virtual_incoming_args_rtx,
409 copy_to_reg (arg_pointer_save_area));
414 #ifdef HAVE_builtin_setjmp_receiver
415 if (HAVE_builtin_setjmp_receiver)
416 emit_insn (gen_builtin_setjmp_receiver (lab1));
419 #ifdef HAVE_nonlocal_goto_receiver
420 if (HAVE_nonlocal_goto_receiver)
421 emit_insn (gen_nonlocal_goto_receiver ());
428 /* Set TARGET, and branch to the next-time-through label. */
429 emit_move_insn (target, const1_rtx);
430 emit_jump_insn (gen_jump (next_label));
436 /* __builtin_longjmp is passed a pointer to an array of five words (not
437 all will be used on all machines). It operates similarly to the C
438 library function of the same name, but is more efficient. Much of
439 the code below is copied from the handling of non-local gotos.
441 NOTE: This is intended for use by GNAT and the exception handling
442 scheme in the compiler and will only work in the method used by
446 expand_builtin_longjmp (buf_addr, value)
450 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
452 #ifdef POINTERS_EXTEND_UNSIGNED
453 buf_addr = convert_memory_address (Pmode, buf_addr);
455 buf_addr = force_reg (Pmode, buf_addr);
457 /* We used to store value in static_chain_rtx, but that fails if pointers
458 are smaller than integers. We instead require that the user must pass
459 a second argument of 1, because that is what builtin_setjmp will
460 return. This also makes EH slightly more efficient, since we are no
461 longer copying around a value that we don't care about. */
462 if (value != const1_rtx)
465 #ifdef HAVE_builtin_longjmp
466 if (HAVE_builtin_longjmp)
467 emit_insn (gen_builtin_longjmp (buf_addr));
471 fp = gen_rtx_MEM (Pmode, buf_addr);
472 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
473 GET_MODE_SIZE (Pmode)));
475 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
476 2 * GET_MODE_SIZE (Pmode)));
478 /* Pick up FP, label, and SP from the block and jump. This code is
479 from expand_goto in stmt.c; see there for detailed comments. */
480 #if HAVE_nonlocal_goto
481 if (HAVE_nonlocal_goto)
482 /* We have to pass a value to the nonlocal_goto pattern that will
483 get copied into the static_chain pointer, but it does not matter
484 what that value is, because builtin_setjmp does not use it. */
485 emit_insn (gen_nonlocal_goto (value, fp, stack, lab));
489 lab = copy_to_reg (lab);
491 emit_move_insn (hard_frame_pointer_rtx, fp);
492 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
494 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
495 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
496 emit_indirect_jump (lab);
501 /* Get a MEM rtx for expression EXP which can be used in a string instruction
502 (cmpstrsi, movstrsi, ..). */
510 mem = gen_rtx_MEM (BLKmode,
511 memory_address (BLKmode,
512 expand_expr (exp, NULL_RTX,
513 ptr_mode, EXPAND_SUM)));
515 RTX_UNCHANGING_P (mem) = TREE_READONLY (exp);
517 /* Figure out the type of the object pointed to. Set MEM_IN_STRUCT_P
518 if the value is the address of a structure or if the expression is
519 cast to a pointer to structure type. */
522 while (TREE_CODE (exp) == NOP_EXPR)
524 tree cast_type = TREE_TYPE (exp);
525 if (TREE_CODE (cast_type) == POINTER_TYPE
526 && AGGREGATE_TYPE_P (TREE_TYPE (cast_type)))
531 exp = TREE_OPERAND (exp, 0);
534 if (is_aggregate == 0)
538 if (TREE_CODE (exp) == ADDR_EXPR)
539 /* If this is the address of an object, check whether the
540 object is an array. */
541 type = TREE_TYPE (TREE_OPERAND (exp, 0));
543 type = TREE_TYPE (TREE_TYPE (exp));
544 is_aggregate = AGGREGATE_TYPE_P (type);
547 MEM_SET_IN_STRUCT_P (mem, is_aggregate);
551 /* Built-in functions to perform an untyped call and return. */
553 /* For each register that may be used for calling a function, this
554 gives a mode used to copy the register's value. VOIDmode indicates
555 the register is not used for calling a function. If the machine
556 has register windows, this gives only the outbound registers.
557 INCOMING_REGNO gives the corresponding inbound register. */
558 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
560 /* For each register that may be used for returning values, this gives
561 a mode used to copy the register's value. VOIDmode indicates the
562 register is not used for returning values. If the machine has
563 register windows, this gives only the outbound registers.
564 INCOMING_REGNO gives the corresponding inbound register. */
565 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
567 /* For each register that may be used for calling a function, this
568 gives the offset of that register into the block returned by
569 __builtin_apply_args. 0 indicates that the register is not
570 used for calling a function. */
571 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
573 /* Return the offset of register REGNO into the block returned by
574 __builtin_apply_args. This is not declared static, since it is
575 needed in objc-act.c. */
578 apply_args_register_offset (regno)
583 /* Arguments are always put in outgoing registers (in the argument
584 block) if such make sense. */
585 #ifdef OUTGOING_REGNO
586 regno = OUTGOING_REGNO(regno);
588 return apply_args_reg_offset[regno];
591 /* Return the size required for the block returned by __builtin_apply_args,
592 and initialize apply_args_mode. */
597 static int size = -1;
599 enum machine_mode mode;
601 /* The values computed by this function never change. */
604 /* The first value is the incoming arg-pointer. */
605 size = GET_MODE_SIZE (Pmode);
607 /* The second value is the structure value address unless this is
608 passed as an "invisible" first argument. */
609 if (struct_value_rtx)
610 size += GET_MODE_SIZE (Pmode);
612 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
613 if (FUNCTION_ARG_REGNO_P (regno))
615 /* Search for the proper mode for copying this register's
616 value. I'm not sure this is right, but it works so far. */
617 enum machine_mode best_mode = VOIDmode;
619 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
621 mode = GET_MODE_WIDER_MODE (mode))
622 if (HARD_REGNO_MODE_OK (regno, mode)
623 && HARD_REGNO_NREGS (regno, mode) == 1)
626 if (best_mode == VOIDmode)
627 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
629 mode = GET_MODE_WIDER_MODE (mode))
630 if (HARD_REGNO_MODE_OK (regno, mode)
631 && (mov_optab->handlers[(int) mode].insn_code
632 != CODE_FOR_nothing))
636 if (mode == VOIDmode)
639 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
640 if (size % align != 0)
641 size = CEIL (size, align) * align;
642 apply_args_reg_offset[regno] = size;
643 size += GET_MODE_SIZE (mode);
644 apply_args_mode[regno] = mode;
648 apply_args_mode[regno] = VOIDmode;
649 apply_args_reg_offset[regno] = 0;
655 /* Return the size required for the block returned by __builtin_apply,
656 and initialize apply_result_mode. */
661 static int size = -1;
663 enum machine_mode mode;
665 /* The values computed by this function never change. */
670 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
671 if (FUNCTION_VALUE_REGNO_P (regno))
673 /* Search for the proper mode for copying this register's
674 value. I'm not sure this is right, but it works so far. */
675 enum machine_mode best_mode = VOIDmode;
677 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
679 mode = GET_MODE_WIDER_MODE (mode))
680 if (HARD_REGNO_MODE_OK (regno, mode))
683 if (best_mode == VOIDmode)
684 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
686 mode = GET_MODE_WIDER_MODE (mode))
687 if (HARD_REGNO_MODE_OK (regno, mode)
688 && (mov_optab->handlers[(int) mode].insn_code
689 != CODE_FOR_nothing))
693 if (mode == VOIDmode)
696 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
697 if (size % align != 0)
698 size = CEIL (size, align) * align;
699 size += GET_MODE_SIZE (mode);
700 apply_result_mode[regno] = mode;
703 apply_result_mode[regno] = VOIDmode;
705 /* Allow targets that use untyped_call and untyped_return to override
706 the size so that machine-specific information can be stored here. */
707 #ifdef APPLY_RESULT_SIZE
708 size = APPLY_RESULT_SIZE;
714 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
715 /* Create a vector describing the result block RESULT. If SAVEP is true,
716 the result block is used to save the values; otherwise it is used to
717 restore the values. */
720 result_vector (savep, result)
724 int regno, size, align, nelts;
725 enum machine_mode mode;
727 rtx *savevec = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
730 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
731 if ((mode = apply_result_mode[regno]) != VOIDmode)
733 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
734 if (size % align != 0)
735 size = CEIL (size, align) * align;
736 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
737 mem = change_address (result, mode,
738 plus_constant (XEXP (result, 0), size));
739 savevec[nelts++] = (savep
740 ? gen_rtx_SET (VOIDmode, mem, reg)
741 : gen_rtx_SET (VOIDmode, reg, mem));
742 size += GET_MODE_SIZE (mode);
744 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
746 #endif /* HAVE_untyped_call or HAVE_untyped_return */
748 /* Save the state required to perform an untyped call with the same
749 arguments as were passed to the current function. */
752 expand_builtin_apply_args_1 ()
755 int size, align, regno;
756 enum machine_mode mode;
758 /* Create a block where the arg-pointer, structure value address,
759 and argument registers can be saved. */
760 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
762 /* Walk past the arg-pointer and structure value address. */
763 size = GET_MODE_SIZE (Pmode);
764 if (struct_value_rtx)
765 size += GET_MODE_SIZE (Pmode);
767 /* Save each register used in calling a function to the block. */
768 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
769 if ((mode = apply_args_mode[regno]) != VOIDmode)
773 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
774 if (size % align != 0)
775 size = CEIL (size, align) * align;
777 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
779 emit_move_insn (change_address (registers, mode,
780 plus_constant (XEXP (registers, 0),
783 size += GET_MODE_SIZE (mode);
786 /* Save the arg pointer to the block. */
787 emit_move_insn (change_address (registers, Pmode, XEXP (registers, 0)),
788 copy_to_reg (virtual_incoming_args_rtx));
789 size = GET_MODE_SIZE (Pmode);
791 /* Save the structure value address unless this is passed as an
792 "invisible" first argument. */
793 if (struct_value_incoming_rtx)
795 emit_move_insn (change_address (registers, Pmode,
796 plus_constant (XEXP (registers, 0),
798 copy_to_reg (struct_value_incoming_rtx));
799 size += GET_MODE_SIZE (Pmode);
802 /* Return the address of the block. */
803 return copy_addr_to_reg (XEXP (registers, 0));
806 /* __builtin_apply_args returns block of memory allocated on
807 the stack into which is stored the arg pointer, structure
808 value address, static chain, and all the registers that might
809 possibly be used in performing a function call. The code is
810 moved to the start of the function so the incoming values are
813 expand_builtin_apply_args ()
815 /* Don't do __builtin_apply_args more than once in a function.
816 Save the result of the first call and reuse it. */
817 if (apply_args_value != 0)
818 return apply_args_value;
820 /* When this function is called, it means that registers must be
821 saved on entry to this function. So we migrate the
822 call to the first insn of this function. */
827 temp = expand_builtin_apply_args_1 ();
831 apply_args_value = temp;
833 /* Put the sequence after the NOTE that starts the function.
834 If this is inside a SEQUENCE, make the outer-level insn
835 chain current, so the code is placed at the start of the
837 push_topmost_sequence ();
838 emit_insns_before (seq, NEXT_INSN (get_insns ()));
839 pop_topmost_sequence ();
844 /* Perform an untyped call and save the state required to perform an
845 untyped return of whatever value was returned by the given function. */
848 expand_builtin_apply (function, arguments, argsize)
849 rtx function, arguments, argsize;
851 int size, align, regno;
852 enum machine_mode mode;
853 rtx incoming_args, result, reg, dest, call_insn;
854 rtx old_stack_level = 0;
857 /* Create a block where the return registers can be saved. */
858 result = assign_stack_local (BLKmode, apply_result_size (), -1);
860 /* ??? The argsize value should be adjusted here. */
862 /* Fetch the arg pointer from the ARGUMENTS block. */
863 incoming_args = gen_reg_rtx (Pmode);
864 emit_move_insn (incoming_args,
865 gen_rtx_MEM (Pmode, arguments));
866 #ifndef STACK_GROWS_DOWNWARD
867 incoming_args = expand_binop (Pmode, sub_optab, incoming_args, argsize,
868 incoming_args, 0, OPTAB_LIB_WIDEN);
871 /* Perform postincrements before actually calling the function. */
874 /* Push a new argument block and copy the arguments. */
875 do_pending_stack_adjust ();
877 /* Save the stack with nonlocal if available */
878 #ifdef HAVE_save_stack_nonlocal
879 if (HAVE_save_stack_nonlocal)
880 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
883 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
885 /* Push a block of memory onto the stack to store the memory arguments.
886 Save the address in a register, and copy the memory arguments. ??? I
887 haven't figured out how the calling convention macros effect this,
888 but it's likely that the source and/or destination addresses in
889 the block copy will need updating in machine specific ways. */
890 dest = allocate_dynamic_stack_space (argsize, 0, 0);
891 emit_block_move (gen_rtx_MEM (BLKmode, dest),
892 gen_rtx_MEM (BLKmode, incoming_args),
894 PARM_BOUNDARY / BITS_PER_UNIT);
896 /* Refer to the argument block. */
898 arguments = gen_rtx_MEM (BLKmode, arguments);
900 /* Walk past the arg-pointer and structure value address. */
901 size = GET_MODE_SIZE (Pmode);
902 if (struct_value_rtx)
903 size += GET_MODE_SIZE (Pmode);
905 /* Restore each of the registers previously saved. Make USE insns
906 for each of these registers for use in making the call. */
907 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
908 if ((mode = apply_args_mode[regno]) != VOIDmode)
910 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
911 if (size % align != 0)
912 size = CEIL (size, align) * align;
913 reg = gen_rtx_REG (mode, regno);
915 change_address (arguments, mode,
916 plus_constant (XEXP (arguments, 0),
919 use_reg (&call_fusage, reg);
920 size += GET_MODE_SIZE (mode);
923 /* Restore the structure value address unless this is passed as an
924 "invisible" first argument. */
925 size = GET_MODE_SIZE (Pmode);
926 if (struct_value_rtx)
928 rtx value = gen_reg_rtx (Pmode);
929 emit_move_insn (value,
930 change_address (arguments, Pmode,
931 plus_constant (XEXP (arguments, 0),
933 emit_move_insn (struct_value_rtx, value);
934 if (GET_CODE (struct_value_rtx) == REG)
935 use_reg (&call_fusage, struct_value_rtx);
936 size += GET_MODE_SIZE (Pmode);
939 /* All arguments and registers used for the call are set up by now! */
940 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0);
942 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
943 and we don't want to load it into a register as an optimization,
944 because prepare_call_address already did it if it should be done. */
945 if (GET_CODE (function) != SYMBOL_REF)
946 function = memory_address (FUNCTION_MODE, function);
948 /* Generate the actual call instruction and save the return value. */
949 #ifdef HAVE_untyped_call
950 if (HAVE_untyped_call)
951 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
952 result, result_vector (1, result)));
955 #ifdef HAVE_call_value
960 /* Locate the unique return register. It is not possible to
961 express a call that sets more than one return register using
962 call_value; use untyped_call for that. In fact, untyped_call
963 only needs to save the return registers in the given block. */
964 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
965 if ((mode = apply_result_mode[regno]) != VOIDmode)
968 abort (); /* HAVE_untyped_call required. */
969 valreg = gen_rtx_REG (mode, regno);
972 emit_call_insn (gen_call_value (valreg,
973 gen_rtx_MEM (FUNCTION_MODE, function),
974 const0_rtx, NULL_RTX, const0_rtx));
976 emit_move_insn (change_address (result, GET_MODE (valreg),
984 /* Find the CALL insn we just emitted. */
985 for (call_insn = get_last_insn ();
986 call_insn && GET_CODE (call_insn) != CALL_INSN;
987 call_insn = PREV_INSN (call_insn))
993 /* Put the register usage information on the CALL. If there is already
994 some usage information, put ours at the end. */
995 if (CALL_INSN_FUNCTION_USAGE (call_insn))
999 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
1000 link = XEXP (link, 1))
1003 XEXP (link, 1) = call_fusage;
1006 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
1008 /* Restore the stack. */
1009 #ifdef HAVE_save_stack_nonlocal
1010 if (HAVE_save_stack_nonlocal)
1011 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1014 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1016 /* Return the address of the result block. */
1017 return copy_addr_to_reg (XEXP (result, 0));
1020 /* Perform an untyped return. */
1023 expand_builtin_return (result)
1026 int size, align, regno;
1027 enum machine_mode mode;
1029 rtx call_fusage = 0;
1031 apply_result_size ();
1032 result = gen_rtx_MEM (BLKmode, result);
1034 #ifdef HAVE_untyped_return
1035 if (HAVE_untyped_return)
1037 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1043 /* Restore the return value and note that each value is used. */
1045 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1046 if ((mode = apply_result_mode[regno]) != VOIDmode)
1048 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1049 if (size % align != 0)
1050 size = CEIL (size, align) * align;
1051 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1052 emit_move_insn (reg,
1053 change_address (result, mode,
1054 plus_constant (XEXP (result, 0),
1057 push_to_sequence (call_fusage);
1058 emit_insn (gen_rtx_USE (VOIDmode, reg));
1059 call_fusage = get_insns ();
1061 size += GET_MODE_SIZE (mode);
1064 /* Put the USE insns before the return. */
1065 emit_insns (call_fusage);
1067 /* Return whatever values was restored by jumping directly to the end
1069 expand_null_return ();
1072 /* Expand a call to __builtin_classify_type with arguments found in
1075 expand_builtin_classify_type (arglist)
1080 tree type = TREE_TYPE (TREE_VALUE (arglist));
1081 enum tree_code code = TREE_CODE (type);
1082 if (code == VOID_TYPE)
1083 return GEN_INT (void_type_class);
1084 if (code == INTEGER_TYPE)
1085 return GEN_INT (integer_type_class);
1086 if (code == CHAR_TYPE)
1087 return GEN_INT (char_type_class);
1088 if (code == ENUMERAL_TYPE)
1089 return GEN_INT (enumeral_type_class);
1090 if (code == BOOLEAN_TYPE)
1091 return GEN_INT (boolean_type_class);
1092 if (code == POINTER_TYPE)
1093 return GEN_INT (pointer_type_class);
1094 if (code == REFERENCE_TYPE)
1095 return GEN_INT (reference_type_class);
1096 if (code == OFFSET_TYPE)
1097 return GEN_INT (offset_type_class);
1098 if (code == REAL_TYPE)
1099 return GEN_INT (real_type_class);
1100 if (code == COMPLEX_TYPE)
1101 return GEN_INT (complex_type_class);
1102 if (code == FUNCTION_TYPE)
1103 return GEN_INT (function_type_class);
1104 if (code == METHOD_TYPE)
1105 return GEN_INT (method_type_class);
1106 if (code == RECORD_TYPE)
1107 return GEN_INT (record_type_class);
1108 if (code == UNION_TYPE || code == QUAL_UNION_TYPE)
1109 return GEN_INT (union_type_class);
1110 if (code == ARRAY_TYPE)
1112 if (TYPE_STRING_FLAG (type))
1113 return GEN_INT (string_type_class);
1115 return GEN_INT (array_type_class);
1117 if (code == SET_TYPE)
1118 return GEN_INT (set_type_class);
1119 if (code == FILE_TYPE)
1120 return GEN_INT (file_type_class);
1121 if (code == LANG_TYPE)
1122 return GEN_INT (lang_type_class);
1124 return GEN_INT (no_type_class);
1127 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1129 expand_builtin_constant_p (exp)
1132 tree arglist = TREE_OPERAND (exp, 1);
1133 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1139 tree arg = TREE_VALUE (arglist);
1142 /* We return 1 for a numeric type that's known to be a constant
1143 value at compile-time or for an aggregate type that's a
1144 literal constant. */
1147 /* If we know this is a constant, emit the constant of one. */
1148 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'c'
1149 || (TREE_CODE (arg) == CONSTRUCTOR
1150 && TREE_CONSTANT (arg))
1151 || (TREE_CODE (arg) == ADDR_EXPR
1152 && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST))
1155 /* If we aren't going to be running CSE or this expression
1156 has side effects, show we don't know it to be a constant.
1157 Likewise if it's a pointer or aggregate type since in those
1158 case we only want literals, since those are only optimized
1159 when generating RTL, not later. */
1160 if (TREE_SIDE_EFFECTS (arg) || cse_not_expected
1161 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
1162 || POINTER_TYPE_P (TREE_TYPE (arg)))
1165 /* Otherwise, emit (constant_p_rtx (ARG)) and let CSE get a
1166 chance to see if it can deduce whether ARG is constant. */
1168 tmp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
1169 tmp = gen_rtx_CONSTANT_P_RTX (value_mode, tmp);
1174 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1175 Return 0 if a normal call should be emitted rather than expanding the
1176 function in-line. EXP is the expression that is a call to the builtin
1177 function; if convenient, the result should be placed in TARGET.
1178 SUBTARGET may be used as the target for computing one of EXP's operands. */
1180 expand_builtin_mathfn (exp, target, subtarget)
1182 rtx target, subtarget;
1184 optab builtin_optab;
1186 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1187 tree arglist = TREE_OPERAND (exp, 1);
1190 /* Arg could be wrong type if user redeclared this fcn wrong. */
1191 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE)
1194 /* Stabilize and compute the argument. */
1195 if (TREE_CODE (TREE_VALUE (arglist)) != VAR_DECL
1196 && TREE_CODE (TREE_VALUE (arglist)) != PARM_DECL)
1198 exp = copy_node (exp);
1199 TREE_OPERAND (exp, 1) = arglist;
1200 /* Wrap the computation of the argument in a SAVE_EXPR. That
1201 way, if we need to expand the argument again (as in the
1202 flag_errno_math case below where we cannot directly set
1203 errno), we will not perform side-effects more than once.
1204 Note that here we're mutating the original EXP as well as the
1205 copy; that's the right thing to do in case the original EXP
1206 is expanded later. */
1207 TREE_VALUE (arglist) = save_expr (TREE_VALUE (arglist));
1208 arglist = copy_node (arglist);
1210 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
1212 /* Make a suitable register to place result in. */
1213 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
1218 switch (DECL_FUNCTION_CODE (fndecl))
1221 builtin_optab = sin_optab; break;
1223 builtin_optab = cos_optab; break;
1224 case BUILT_IN_FSQRT:
1225 builtin_optab = sqrt_optab; break;
1230 /* Compute into TARGET.
1231 Set TARGET to wherever the result comes back. */
1232 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
1233 builtin_optab, op0, target, 0);
1235 /* If we were unable to expand via the builtin, stop the
1236 sequence (without outputting the insns) and return 0, causing
1237 a call to the library function. */
1244 /* Check the results by default. But if flag_fast_math is turned on,
1245 then assume sqrt will always be called with valid arguments. */
1247 if (flag_errno_math && ! flag_fast_math)
1251 /* Don't define the builtin FP instructions
1252 if your machine is not IEEE. */
1253 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1256 lab1 = gen_label_rtx ();
1258 /* Test the result; if it is NaN, set errno=EDOM because
1259 the argument was not in the domain. */
1260 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1265 #ifdef GEN_ERRNO_RTX
1266 rtx errno_rtx = GEN_ERRNO_RTX;
1269 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1272 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1275 /* We can't set errno=EDOM directly; let the library call do it.
1276 Pop the arguments right away in case the call gets deleted. */
1278 expand_call (exp, target, 0);
1285 /* Output the entire sequence. */
1286 insns = get_insns ();
1293 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1294 if we failed the caller should emit a normal call, otherwise
1295 try to get the result in TARGET, if convenient (and in mode MODE if that's
1298 expand_builtin_strlen (exp, target, mode)
1301 enum machine_mode mode;
1303 tree arglist = TREE_OPERAND (exp, 1);
1304 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1307 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1308 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
1312 tree src = TREE_VALUE (arglist);
1313 tree len = c_strlen (src);
1316 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1318 rtx result, src_rtx, char_rtx;
1319 enum machine_mode insn_mode = value_mode, char_mode;
1320 enum insn_code icode = CODE_FOR_nothing;
1322 /* If the length is known, just return it. */
1324 return expand_expr (len, target, mode, EXPAND_MEMORY_USE_BAD);
1326 /* If SRC is not a pointer type, don't do this operation inline. */
1330 /* Call a function if we can't compute strlen in the right mode. */
1332 while (insn_mode != VOIDmode)
1334 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1335 if (icode != CODE_FOR_nothing)
1338 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1340 if (insn_mode == VOIDmode)
1343 /* Make a place to write the result of the instruction. */
1346 && GET_CODE (result) == REG
1347 && GET_MODE (result) == insn_mode
1348 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1349 result = gen_reg_rtx (insn_mode);
1351 /* Make sure the operands are acceptable to the predicates. */
1353 if (! (*insn_data[(int)icode].operand[0].predicate) (result, insn_mode))
1354 result = gen_reg_rtx (insn_mode);
1355 src_rtx = memory_address (BLKmode,
1356 expand_expr (src, NULL_RTX, ptr_mode,
1359 if (! (*insn_data[(int)icode].operand[1].predicate) (src_rtx, Pmode))
1360 src_rtx = copy_to_mode_reg (Pmode, src_rtx);
1362 /* Check the string is readable and has an end. */
1363 if (current_function_check_memory_usage)
1364 emit_library_call (chkr_check_str_libfunc, 1, VOIDmode, 2,
1366 GEN_INT (MEMORY_USE_RO),
1367 TYPE_MODE (integer_type_node));
1369 char_rtx = const0_rtx;
1370 char_mode = insn_data[(int)icode].operand[2].mode;
1371 if (! (*insn_data[(int)icode].operand[2].predicate) (char_rtx, char_mode))
1372 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1374 emit_insn (GEN_FCN (icode) (result,
1375 gen_rtx_MEM (BLKmode, src_rtx),
1376 char_rtx, GEN_INT (align)));
1378 /* Return the value in the proper mode for this function. */
1379 if (GET_MODE (result) == value_mode)
1381 else if (target != 0)
1383 convert_move (target, result, 0);
1387 return convert_to_mode (value_mode, result, 0);
1391 /* Expand a call to the memcpy builtin, with arguments in ARGLIST. */
1393 expand_builtin_memcpy (arglist)
1397 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1398 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1399 || TREE_CHAIN (arglist) == 0
1400 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1402 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1403 || (TREE_CODE (TREE_TYPE (TREE_VALUE
1404 (TREE_CHAIN (TREE_CHAIN (arglist)))))
1409 tree dest = TREE_VALUE (arglist);
1410 tree src = TREE_VALUE (TREE_CHAIN (arglist));
1411 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1414 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1416 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1417 rtx dest_mem, src_mem, dest_addr, len_rtx;
1419 /* If either SRC or DEST is not a pointer type, don't do
1420 this operation in-line. */
1421 if (src_align == 0 || dest_align == 0)
1424 dest_mem = get_memory_rtx (dest);
1425 src_mem = get_memory_rtx (src);
1426 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1428 /* Just copy the rights of SRC to the rights of DEST. */
1429 if (current_function_check_memory_usage)
1430 emit_library_call (chkr_copy_bitmap_libfunc, 1, VOIDmode, 3,
1431 XEXP (dest_mem, 0), Pmode,
1432 XEXP (src_mem, 0), Pmode,
1433 len_rtx, TYPE_MODE (sizetype));
1435 /* Copy word part most expediently. */
1437 = emit_block_move (dest_mem, src_mem, len_rtx,
1438 MIN (src_align, dest_align));
1441 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1447 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
1448 if we failed the caller should emit a normal call. */
1451 expand_builtin_strcpy (exp)
1454 tree arglist = TREE_OPERAND (exp, 1);
1458 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1459 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1460 || TREE_CHAIN (arglist) == 0
1461 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1466 tree len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
1471 len = size_binop (PLUS_EXPR, len, ssize_int (1));
1472 chainon (arglist, build_tree_list (NULL_TREE, len));
1475 result = expand_builtin_memcpy (arglist);
1478 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1482 /* Expand expression EXP, which is a call to the memset builtin. Return 0
1483 if we failed the caller should emit a normal call. */
1486 expand_builtin_memset (exp)
1489 tree arglist = TREE_OPERAND (exp, 1);
1492 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1493 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1494 || TREE_CHAIN (arglist) == 0
1495 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1497 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1499 != (TREE_CODE (TREE_TYPE
1501 (TREE_CHAIN (TREE_CHAIN (arglist))))))))
1505 tree dest = TREE_VALUE (arglist);
1506 tree val = TREE_VALUE (TREE_CHAIN (arglist));
1507 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1510 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1511 rtx dest_mem, dest_addr, len_rtx;
1513 /* If DEST is not a pointer type, don't do this
1514 operation in-line. */
1515 if (dest_align == 0)
1518 /* If the arguments have side-effects, then we can only evaluate
1519 them at most once. The following code evaluates them twice if
1520 they are not constants because we break out to expand_call
1521 in that case. They can't be constants if they have side-effects
1522 so we can check for that first. Alternatively, we could call
1523 save_expr to make multiple evaluation safe. */
1524 if (TREE_SIDE_EFFECTS (val) || TREE_SIDE_EFFECTS (len))
1527 /* If VAL is not 0, don't do this operation in-line. */
1528 if (expand_expr (val, NULL_RTX, VOIDmode, 0) != const0_rtx)
1531 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1533 dest_mem = get_memory_rtx (dest);
1535 /* Just check DST is writable and mark it as readable. */
1536 if (current_function_check_memory_usage)
1537 emit_library_call (chkr_check_addr_libfunc, 1, VOIDmode, 3,
1538 XEXP (dest_mem, 0), Pmode,
1539 len_rtx, TYPE_MODE (sizetype),
1540 GEN_INT (MEMORY_USE_WO),
1541 TYPE_MODE (integer_type_node));
1544 dest_addr = clear_storage (dest_mem, len_rtx, dest_align);
1547 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1553 #ifdef HAVE_cmpstrsi
1554 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
1555 ARGLIST is the argument list for this call. Return 0 if we failed and the
1556 caller should emit a normal call, otherwise try to get the result in
1557 TARGET, if convenient. */
1559 expand_builtin_memcmp (exp, arglist, target)
1564 /* If we need to check memory accesses, call the library function. */
1565 if (current_function_check_memory_usage)
1569 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1570 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1571 || TREE_CHAIN (arglist) == 0
1572 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
1573 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1574 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
1576 else if (!HAVE_cmpstrsi)
1580 enum machine_mode mode;
1581 tree arg1 = TREE_VALUE (arglist);
1582 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1583 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1587 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1589 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1590 enum machine_mode insn_mode
1591 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
1593 /* If we don't have POINTER_TYPE, call the function. */
1594 if (arg1_align == 0 || arg2_align == 0)
1597 /* Make a place to write the result of the instruction. */
1600 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
1601 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1602 result = gen_reg_rtx (insn_mode);
1604 emit_insn (gen_cmpstrsi (result, get_memory_rtx (arg1),
1605 get_memory_rtx (arg2),
1606 expand_expr (len, NULL_RTX, VOIDmode, 0),
1607 GEN_INT (MIN (arg1_align, arg2_align))));
1609 /* Return the value in the proper mode for this function. */
1610 mode = TYPE_MODE (TREE_TYPE (exp));
1611 if (GET_MODE (result) == mode)
1613 else if (target != 0)
1615 convert_move (target, result, 0);
1619 return convert_to_mode (mode, result, 0);
1623 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
1624 if we failed the caller should emit a normal call, otherwise try to get
1625 the result in TARGET, if convenient. */
1628 expand_builtin_strcmp (exp, target)
1632 tree arglist = TREE_OPERAND (exp, 1);
1634 /* If we need to check memory accesses, call the library function. */
1635 if (current_function_check_memory_usage)
1639 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1640 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1641 || TREE_CHAIN (arglist) == 0
1642 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1646 else if (! HAVE_cmpstrsi)
1649 tree arg1 = TREE_VALUE (arglist);
1650 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1651 tree len = c_strlen (arg1);
1652 tree len2 = c_strlen (arg2);
1656 len = size_binop (PLUS_EXPR, ssize_int (1), len);
1659 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
1661 /* If we don't have a constant length for the first, use the length
1662 of the second, if we know it. We don't require a constant for
1663 this case; some cost analysis could be done if both are available
1664 but neither is constant. For now, assume they're equally cheap.
1666 If both strings have constant lengths, use the smaller. This
1667 could arise if optimization results in strcpy being called with
1668 two fixed strings, or if the code was machine-generated. We should
1669 add some code to the `memcmp' handler below to deal with such
1670 situations, someday. */
1672 if (!len || TREE_CODE (len) != INTEGER_CST)
1679 else if (len2 && TREE_CODE (len2) == INTEGER_CST
1680 && tree_int_cst_lt (len2, len))
1683 chainon (arglist, build_tree_list (NULL_TREE, len));
1684 result = expand_builtin_memcmp (exp, arglist, target);
1686 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1693 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
1694 if that's convenient. */
1697 expand_builtin_saveregs ()
1701 /* Don't do __builtin_saveregs more than once in a function.
1702 Save the result of the first call and reuse it. */
1703 if (saveregs_value != 0)
1704 return saveregs_value;
1706 /* When this function is called, it means that registers must be
1707 saved on entry to this function. So we migrate the call to the
1708 first insn of this function. */
1712 #ifdef EXPAND_BUILTIN_SAVEREGS
1713 /* Do whatever the machine needs done in this case. */
1714 val = EXPAND_BUILTIN_SAVEREGS ();
1716 /* ??? We used to try and build up a call to the out of line function,
1717 guessing about what registers needed saving etc. This became much
1718 harder with __builtin_va_start, since we don't have a tree for a
1719 call to __builtin_saveregs to fall back on. There was exactly one
1720 port (i860) that used this code, and I'm unconvinced it could actually
1721 handle the general case. So we no longer try to handle anything
1722 weird and make the backend absorb the evil. */
1724 error ("__builtin_saveregs not supported by this target");
1731 saveregs_value = val;
1733 /* Put the sequence after the NOTE that starts the function. If this
1734 is inside a SEQUENCE, make the outer-level insn chain current, so
1735 the code is placed at the start of the function. */
1736 push_topmost_sequence ();
1737 emit_insns_after (seq, get_insns ());
1738 pop_topmost_sequence ();
1743 /* __builtin_args_info (N) returns word N of the arg space info
1744 for the current function. The number and meanings of words
1745 is controlled by the definition of CUMULATIVE_ARGS. */
1747 expand_builtin_args_info (exp)
1750 tree arglist = TREE_OPERAND (exp, 1);
1751 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
1752 int *word_ptr = (int *) ¤t_function_args_info;
1754 /* These are used by the code below that is if 0'ed away */
1756 tree type, elts, result;
1759 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
1764 tree arg = TREE_VALUE (arglist);
1765 if (TREE_CODE (arg) != INTEGER_CST)
1766 error ("argument of `__builtin_args_info' must be constant");
1769 int wordnum = TREE_INT_CST_LOW (arg);
1771 if (wordnum < 0 || wordnum >= nwords || TREE_INT_CST_HIGH (arg))
1772 error ("argument of `__builtin_args_info' out of range");
1774 return GEN_INT (word_ptr[wordnum]);
1778 error ("missing argument in `__builtin_args_info'");
1783 for (i = 0; i < nwords; i++)
1784 elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
1786 type = build_array_type (integer_type_node,
1787 build_index_type (build_int_2 (nwords, 0)));
1788 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
1789 TREE_CONSTANT (result) = 1;
1790 TREE_STATIC (result) = 1;
1791 result = build1 (INDIRECT_REF, build_pointer_type (type), result);
1792 TREE_CONSTANT (result) = 1;
1793 return expand_expr (result, NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD);
1797 /* Expand ARGLIST, from a call to __builtin_next_arg. */
1799 expand_builtin_next_arg (arglist)
1802 tree fntype = TREE_TYPE (current_function_decl);
1804 if ((TYPE_ARG_TYPES (fntype) == 0
1805 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1807 && ! current_function_varargs)
1809 error ("`va_start' used in function with fixed args");
1815 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
1816 tree arg = TREE_VALUE (arglist);
1818 /* Strip off all nops for the sake of the comparison. This
1819 is not quite the same as STRIP_NOPS. It does more.
1820 We must also strip off INDIRECT_EXPR for C++ reference
1822 while (TREE_CODE (arg) == NOP_EXPR
1823 || TREE_CODE (arg) == CONVERT_EXPR
1824 || TREE_CODE (arg) == NON_LVALUE_EXPR
1825 || TREE_CODE (arg) == INDIRECT_REF)
1826 arg = TREE_OPERAND (arg, 0);
1827 if (arg != last_parm)
1828 warning ("second parameter of `va_start' not last named argument");
1830 else if (! current_function_varargs)
1831 /* Evidently an out of date version of <stdarg.h>; can't validate
1832 va_start's second argument, but can still work as intended. */
1833 warning ("`__builtin_next_arg' called without an argument");
1835 return expand_binop (Pmode, add_optab,
1836 current_function_internal_arg_pointer,
1837 current_function_arg_offset_rtx,
1838 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1841 /* Make it easier for the backends by protecting the valist argument
1842 from multiple evaluations. */
1845 stabilize_va_list (valist, was_ptr)
1849 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1851 /* If stdarg.h took the address of an array-type valist that was passed
1852 as a parameter, we'll have taken the address of the parameter itself
1853 rather than the array as we'd intended. Undo this mistake. */
1857 STRIP_NOPS (valist);
1859 /* Two cases: either &array, which decomposed to
1860 <ptr <array <record> valist>>
1861 or &ptr, which turned into
1862 <ptr <ptr <record>>>
1863 In the first case we'll need to put the ADDR_EXPR back
1864 after frobbing the types as if &array[0]. */
1866 if (TREE_CODE (valist) != ADDR_EXPR)
1868 valist = TREE_OPERAND (valist, 0);
1871 if (TYPE_MAIN_VARIANT (TREE_TYPE (valist))
1872 == TYPE_MAIN_VARIANT (va_list_type_node))
1874 tree pt = build_pointer_type (TREE_TYPE (va_list_type_node));
1875 valist = build1 (ADDR_EXPR, pt, valist);
1876 TREE_SIDE_EFFECTS (valist)
1877 = TREE_SIDE_EFFECTS (TREE_OPERAND (valist, 0));
1881 if (! POINTER_TYPE_P (TREE_TYPE (valist))
1882 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (valist)))
1883 != TYPE_MAIN_VARIANT (TREE_TYPE (va_list_type_node))))
1887 if (TREE_SIDE_EFFECTS (valist))
1888 valist = save_expr (valist);
1896 if (! TREE_SIDE_EFFECTS (valist))
1899 pt = build_pointer_type (va_list_type_node);
1900 valist = fold (build1 (ADDR_EXPR, pt, valist));
1901 TREE_SIDE_EFFECTS (valist) = 1;
1903 if (TREE_SIDE_EFFECTS (valist))
1904 valist = save_expr (valist);
1905 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
1912 /* The "standard" implementation of va_start: just assign `nextarg' to
1915 std_expand_builtin_va_start (stdarg_p, valist, nextarg)
1916 int stdarg_p ATTRIBUTE_UNUSED;
1923 nextarg = plus_constant (nextarg, -UNITS_PER_WORD);
1925 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
1926 make_tree (ptr_type_node, nextarg));
1927 TREE_SIDE_EFFECTS (t) = 1;
1929 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1932 /* Expand ARGLIST, which from a call to __builtin_stdarg_va_start or
1933 __builtin_varargs_va_start, depending on STDARG_P. */
1935 expand_builtin_va_start (stdarg_p, arglist)
1940 tree chain = arglist, valist;
1943 nextarg = expand_builtin_next_arg (chain = TREE_CHAIN (arglist));
1945 nextarg = expand_builtin_next_arg (NULL_TREE);
1947 if (TREE_CHAIN (chain))
1948 error ("too many arguments to function `va_start'");
1950 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
1952 #ifdef EXPAND_BUILTIN_VA_START
1953 EXPAND_BUILTIN_VA_START (stdarg_p, valist, nextarg);
1955 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
1961 /* Allocate an alias set for use in storing and reading from the varargs
1964 get_varargs_alias_set ()
1966 static int set = -1;
1968 set = new_alias_set ();
1972 /* The "standard" implementation of va_arg: read the value from the
1973 current (padded) address and increment by the (padded) size. */
1975 std_expand_builtin_va_arg (valist, type)
1979 HOST_WIDE_INT align;
1980 HOST_WIDE_INT rounded_size;
1983 /* Compute the rounded size of the type. */
1984 align = PARM_BOUNDARY / BITS_PER_UNIT;
1985 rounded_size = (((int_size_in_bytes (type) + align - 1) / align) * align);
1989 if (PAD_VARARGS_DOWN)
1991 /* Small args are padded downward. */
1994 adj = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_UNIT;
1995 if (rounded_size > align)
1998 addr_tree = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
1999 build_int_2 (rounded_size - adj, 0));
2002 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2003 addr = copy_to_reg (addr);
2005 /* Compute new value for AP. */
2006 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
2007 build (PLUS_EXPR, TREE_TYPE (valist), valist,
2008 build_int_2 (rounded_size, 0)));
2009 TREE_SIDE_EFFECTS (t) = 1;
2010 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2015 /* Expand __builtin_va_arg, which is not really a builtin function, but
2016 a very special sort of operator. */
2018 expand_builtin_va_arg (valist, type)
2022 tree promoted_type, want_va_type, have_va_type;
2024 /* Verify that valist is of the proper type. */
2026 want_va_type = va_list_type_node;
2027 have_va_type = TREE_TYPE (valist);
2028 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
2030 /* If va_list is an array type, the argument may have decayed
2031 to a pointer type, e.g. by being passed to another function.
2032 In that case, unwrap both types so that we can compare the
2033 underlying records. */
2034 if (TREE_CODE (have_va_type) == ARRAY_TYPE
2035 || TREE_CODE (have_va_type) == POINTER_TYPE)
2037 want_va_type = TREE_TYPE (want_va_type);
2038 have_va_type = TREE_TYPE (have_va_type);
2041 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
2043 error ("first argument to `va_arg' not of type `va_list'");
2047 /* Generate a diagnostic for requesting data of a type that cannot
2048 be passed through `...' due to type promotion at the call site. */
2049 else if ((promoted_type = (*lang_type_promotes_to) (type)) != NULL_TREE)
2051 const char *name = "<anonymous type>", *pname = 0;
2052 static int gave_help;
2054 if (TYPE_NAME (type))
2056 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2057 name = IDENTIFIER_POINTER (TYPE_NAME (type));
2058 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2059 && DECL_NAME (TYPE_NAME (type)))
2060 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
2062 if (TYPE_NAME (promoted_type))
2064 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
2065 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
2066 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
2067 && DECL_NAME (TYPE_NAME (promoted_type)))
2068 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
2071 error ("`%s' is promoted to `%s' when passed through `...'", name, pname);
2075 error ("(so you should pass `%s' not `%s' to `va_arg')", pname, name);
2082 /* Make it easier for the backends by protecting the valist argument
2083 from multiple evaluations. */
2084 valist = stabilize_va_list (valist, 0);
2086 #ifdef EXPAND_BUILTIN_VA_ARG
2087 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
2089 addr = std_expand_builtin_va_arg (valist, type);
2093 result = gen_rtx_MEM (TYPE_MODE (type), addr);
2094 MEM_ALIAS_SET (result) = get_varargs_alias_set ();
2099 /* Expand ARGLIST, from a call to __builtin_va_end. */
2101 expand_builtin_va_end (arglist)
2104 tree valist = TREE_VALUE (arglist);
2106 #ifdef EXPAND_BUILTIN_VA_END
2107 valist = stabilize_va_list (valist, 0);
2108 EXPAND_BUILTIN_VA_END(arglist);
2110 /* Evaluate for side effects, if needed. I hate macros that don't
2112 if (TREE_SIDE_EFFECTS (valist))
2113 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
2119 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
2120 builtin rather than just as an assignment in stdarg.h because of the
2121 nastiness of array-type va_list types. */
2123 expand_builtin_va_copy (arglist)
2128 dst = TREE_VALUE (arglist);
2129 src = TREE_VALUE (TREE_CHAIN (arglist));
2131 dst = stabilize_va_list (dst, 1);
2132 src = stabilize_va_list (src, 0);
2134 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
2136 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
2137 TREE_SIDE_EFFECTS (t) = 1;
2138 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2142 rtx dstb, srcb, size;
2144 /* Evaluate to pointers. */
2145 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
2146 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
2147 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
2148 VOIDmode, EXPAND_NORMAL);
2150 /* "Dereference" to BLKmode memories. */
2151 dstb = gen_rtx_MEM (BLKmode, dstb);
2152 MEM_ALIAS_SET (dstb) = get_alias_set (TREE_TYPE (TREE_TYPE (dst)));
2153 srcb = gen_rtx_MEM (BLKmode, srcb);
2154 MEM_ALIAS_SET (srcb) = get_alias_set (TREE_TYPE (TREE_TYPE (src)));
2157 emit_block_move (dstb, srcb, size,
2158 TYPE_ALIGN (va_list_type_node) / BITS_PER_UNIT);
2164 /* Expand a call to one of the builtin functions __builtin_frame_address or
2165 __builtin_return_address. */
2167 expand_builtin_frame_address (exp)
2170 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2171 tree arglist = TREE_OPERAND (exp, 1);
2173 /* The argument must be a nonnegative integer constant.
2174 It counts the number of frames to scan up the stack.
2175 The value is the return address saved in that frame. */
2177 /* Warning about missing arg was already issued. */
2179 else if (TREE_CODE (TREE_VALUE (arglist)) != INTEGER_CST
2180 || tree_int_cst_sgn (TREE_VALUE (arglist)) < 0)
2182 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2183 error ("invalid arg to `__builtin_frame_address'");
2185 error ("invalid arg to `__builtin_return_address'");
2190 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
2191 TREE_INT_CST_LOW (TREE_VALUE (arglist)),
2192 hard_frame_pointer_rtx);
2194 /* Some ports cannot access arbitrary stack frames. */
2197 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2198 warning ("unsupported arg to `__builtin_frame_address'");
2200 warning ("unsupported arg to `__builtin_return_address'");
2204 /* For __builtin_frame_address, return what we've got. */
2205 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2208 if (GET_CODE (tem) != REG
2209 && ! CONSTANT_P (tem))
2210 tem = copy_to_mode_reg (Pmode, tem);
2215 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
2216 we failed and the caller should emit a normal call, otherwise try to get
2217 the result in TARGET, if convenient. */
2219 expand_builtin_alloca (arglist, target)
2226 /* Arg could be non-integer if user redeclared this fcn wrong. */
2227 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2230 /* Compute the argument. */
2231 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
2233 /* Allocate the desired space. */
2234 return allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
2237 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
2238 Return 0 if a normal call should be emitted rather than expanding the
2239 function in-line. If convenient, the result should be placed in TARGET.
2240 SUBTARGET may be used as the target for computing one of EXP's operands. */
2242 expand_builtin_ffs (arglist, target, subtarget)
2244 rtx target, subtarget;
2248 /* Arg could be non-integer if user redeclared this fcn wrong. */
2249 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2252 /* Compute the argument. */
2253 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
2254 /* Compute ffs, into TARGET if possible.
2255 Set TARGET to wherever the result comes back. */
2256 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
2257 ffs_optab, op0, target, 1);
2263 /* Expand an expression EXP that calls a built-in function,
2264 with result going to TARGET if that's convenient
2265 (and in mode MODE if that's convenient).
2266 SUBTARGET may be used as the target for computing one of EXP's operands.
2267 IGNORE is nonzero if the value is to be ignored. */
2270 expand_builtin (exp, target, subtarget, mode, ignore)
2274 enum machine_mode mode;
2277 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2278 tree arglist = TREE_OPERAND (exp, 1);
2279 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
2281 #ifdef MD_EXPAND_BUILTIN
2282 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
2283 return MD_EXPAND_BUILTIN (exp, target, subtarget, mode, ignore);
2286 /* When not optimizing, generate calls to library functions for a certain
2288 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl)
2289 && (fcode == BUILT_IN_SIN || fcode == BUILT_IN_COS
2290 || fcode == BUILT_IN_FSQRT || fcode == BUILT_IN_MEMSET
2291 || fcode == BUILT_IN_MEMCPY || fcode == BUILT_IN_MEMCMP
2292 || fcode == BUILT_IN_STRLEN || fcode == BUILT_IN_STRCPY
2293 || fcode == BUILT_IN_STRCMP || fcode == BUILT_IN_FFS))
2294 return expand_call (exp, target, ignore);
2301 /* build_function_call changes these into ABS_EXPR. */
2306 /* Treat these like sqrt, but only if the user asks for them. */
2307 if (! flag_fast_math)
2309 case BUILT_IN_FSQRT:
2310 target = expand_builtin_mathfn (exp, target, subtarget);
2318 case BUILT_IN_APPLY_ARGS:
2319 return expand_builtin_apply_args ();
2321 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
2322 FUNCTION with a copy of the parameters described by
2323 ARGUMENTS, and ARGSIZE. It returns a block of memory
2324 allocated on the stack into which is stored all the registers
2325 that might possibly be used for returning the result of a
2326 function. ARGUMENTS is the value returned by
2327 __builtin_apply_args. ARGSIZE is the number of bytes of
2328 arguments that must be copied. ??? How should this value be
2329 computed? We'll also need a safe worst case value for varargs
2331 case BUILT_IN_APPLY:
2333 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2334 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
2335 || TREE_CHAIN (arglist) == 0
2336 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
2337 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
2338 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
2346 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
2347 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
2349 return expand_builtin_apply (ops[0], ops[1], ops[2]);
2352 /* __builtin_return (RESULT) causes the function to return the
2353 value described by RESULT. RESULT is address of the block of
2354 memory returned by __builtin_apply. */
2355 case BUILT_IN_RETURN:
2357 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2358 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE)
2359 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
2360 NULL_RTX, VOIDmode, 0));
2363 case BUILT_IN_SAVEREGS:
2364 return expand_builtin_saveregs ();
2366 case BUILT_IN_ARGS_INFO:
2367 return expand_builtin_args_info (exp);
2369 /* Return the address of the first anonymous stack arg. */
2370 case BUILT_IN_NEXT_ARG:
2371 return expand_builtin_next_arg (arglist);
2373 case BUILT_IN_CLASSIFY_TYPE:
2374 return expand_builtin_classify_type (arglist);
2376 case BUILT_IN_CONSTANT_P:
2377 return expand_builtin_constant_p (exp);
2379 case BUILT_IN_FRAME_ADDRESS:
2380 case BUILT_IN_RETURN_ADDRESS:
2381 return expand_builtin_frame_address (exp);
2383 /* Returns the address of the area where the structure is returned.
2385 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
2387 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
2388 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
2391 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
2393 case BUILT_IN_ALLOCA:
2394 target = expand_builtin_alloca (arglist, target);
2400 target = expand_builtin_ffs (arglist, target, subtarget);
2405 case BUILT_IN_STRLEN:
2406 target = expand_builtin_strlen (exp, target, mode);
2411 case BUILT_IN_STRCPY:
2412 target = expand_builtin_strcpy (exp);
2417 case BUILT_IN_MEMCPY:
2418 target = expand_builtin_memcpy (arglist);
2423 case BUILT_IN_MEMSET:
2424 target = expand_builtin_memset (exp);
2429 /* These comparison functions need an instruction that returns an actual
2430 index. An ordinary compare that just sets the condition codes
2432 #ifdef HAVE_cmpstrsi
2433 case BUILT_IN_STRCMP:
2434 target = expand_builtin_strcmp (exp, target);
2439 case BUILT_IN_MEMCMP:
2440 target = expand_builtin_memcmp (exp, arglist, target);
2445 case BUILT_IN_STRCMP:
2446 case BUILT_IN_MEMCMP:
2450 case BUILT_IN_SETJMP:
2452 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2456 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2458 rtx lab = gen_label_rtx ();
2459 rtx ret = expand_builtin_setjmp (buf_addr, target, lab, lab);
2464 /* __builtin_longjmp is passed a pointer to an array of five words.
2465 It's similar to the C library longjmp function but works with
2466 __builtin_setjmp above. */
2467 case BUILT_IN_LONGJMP:
2468 if (arglist == 0 || TREE_CHAIN (arglist) == 0
2469 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2473 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2475 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
2476 NULL_RTX, VOIDmode, 0);
2478 if (value != const1_rtx)
2480 error ("__builtin_longjmp second argument must be 1");
2484 expand_builtin_longjmp (buf_addr, value);
2491 emit_insn (gen_trap ());
2494 error ("__builtin_trap not supported by this target");
2498 /* Various hooks for the DWARF 2 __throw routine. */
2499 case BUILT_IN_UNWIND_INIT:
2500 expand_builtin_unwind_init ();
2502 case BUILT_IN_DWARF_CFA:
2503 return virtual_cfa_rtx;
2504 #ifdef DWARF2_UNWIND_INFO
2505 case BUILT_IN_DWARF_FP_REGNUM:
2506 return expand_builtin_dwarf_fp_regnum ();
2507 case BUILT_IN_INIT_DWARF_REG_SIZES:
2508 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
2511 case BUILT_IN_FROB_RETURN_ADDR:
2512 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
2513 case BUILT_IN_EXTRACT_RETURN_ADDR:
2514 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
2515 case BUILT_IN_EH_RETURN:
2516 expand_builtin_eh_return (TREE_VALUE (arglist),
2517 TREE_VALUE (TREE_CHAIN (arglist)),
2518 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))));
2520 case BUILT_IN_VARARGS_START:
2521 return expand_builtin_va_start (0, arglist);
2522 case BUILT_IN_STDARG_START:
2523 return expand_builtin_va_start (1, arglist);
2524 case BUILT_IN_VA_END:
2525 return expand_builtin_va_end (arglist);
2526 case BUILT_IN_VA_COPY:
2527 return expand_builtin_va_copy (arglist);
2529 default: /* just do library call, if unknown builtin */
2530 error ("built-in function `%s' not currently supported",
2531 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
2534 /* The switch statement above can drop through to cause the function
2535 to be called normally. */
2536 return expand_call (exp, target, ignore);