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 /* Setup an array of _DECL trees, make sure each element is
71 initialized to NULL_TREE. */
72 #define DEF_BUILTIN(x) NULL_TREE,
73 tree built_in_decls[(int) END_BUILTINS] =
75 #include "builtins.def"
79 tree (*lang_type_promotes_to) PARAMS ((tree));
81 static int get_pointer_alignment PARAMS ((tree, unsigned));
82 static tree c_strlen PARAMS ((tree));
83 static rtx get_memory_rtx PARAMS ((tree));
84 static int apply_args_size PARAMS ((void));
85 static int apply_result_size PARAMS ((void));
86 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
87 static rtx result_vector PARAMS ((int, rtx));
89 static rtx expand_builtin_apply_args PARAMS ((void));
90 static rtx expand_builtin_apply_args_1 PARAMS ((void));
91 static rtx expand_builtin_apply PARAMS ((rtx, rtx, rtx));
92 static void expand_builtin_return PARAMS ((rtx));
93 static rtx expand_builtin_classify_type PARAMS ((tree));
94 static rtx expand_builtin_mathfn PARAMS ((tree, rtx, rtx));
95 static rtx expand_builtin_constant_p PARAMS ((tree));
96 static rtx expand_builtin_args_info PARAMS ((tree));
97 static rtx expand_builtin_next_arg PARAMS ((tree));
98 static rtx expand_builtin_va_start PARAMS ((int, tree));
99 static rtx expand_builtin_va_end PARAMS ((tree));
100 static rtx expand_builtin_va_copy PARAMS ((tree));
102 static rtx expand_builtin_memcmp PARAMS ((tree, tree, rtx));
103 static rtx expand_builtin_strcmp PARAMS ((tree, rtx));
105 static rtx expand_builtin_memcpy PARAMS ((tree));
106 static rtx expand_builtin_strcpy PARAMS ((tree));
107 static rtx expand_builtin_memset PARAMS ((tree));
108 static rtx expand_builtin_bzero PARAMS ((tree));
109 static rtx expand_builtin_strlen PARAMS ((tree, rtx,
111 static rtx expand_builtin_alloca PARAMS ((tree, rtx));
112 static rtx expand_builtin_ffs PARAMS ((tree, rtx, rtx));
113 static rtx expand_builtin_frame_address PARAMS ((tree));
114 static rtx expand_builtin_fputs PARAMS ((tree, int));
115 static tree stabilize_va_list PARAMS ((tree, int));
116 static rtx expand_builtin_expect PARAMS ((tree, rtx));
118 /* Return the alignment in bits of EXP, a pointer valued expression.
119 But don't return more than MAX_ALIGN no matter what.
120 The alignment returned is, by default, the alignment of the thing that
121 EXP points to (if it is not a POINTER_TYPE, 0 is returned).
123 Otherwise, look at the expression to see if we can do better, i.e., if the
124 expression is actually pointing at an object whose alignment is tighter. */
127 get_pointer_alignment (exp, max_align)
131 unsigned align, inner;
133 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
136 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
137 align = MIN (align, max_align);
141 switch (TREE_CODE (exp))
145 case NON_LVALUE_EXPR:
146 exp = TREE_OPERAND (exp, 0);
147 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
150 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
151 align = MIN (inner, max_align);
155 /* If sum of pointer + int, restrict our maximum alignment to that
156 imposed by the integer. If not, we can't do any better than
158 if (! host_integerp (TREE_OPERAND (exp, 1), 1))
161 while (((tree_low_cst (TREE_OPERAND (exp, 1), 1) * BITS_PER_UNIT)
166 exp = TREE_OPERAND (exp, 0);
170 /* See what we are pointing at and look at its alignment. */
171 exp = TREE_OPERAND (exp, 0);
172 if (TREE_CODE (exp) == FUNCTION_DECL)
173 align = FUNCTION_BOUNDARY;
174 else if (DECL_P (exp))
175 align = DECL_ALIGN (exp);
176 #ifdef CONSTANT_ALIGNMENT
177 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
178 align = CONSTANT_ALIGNMENT (exp, align);
180 return MIN (align, max_align);
188 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
189 way, because it could contain a zero byte in the middle.
190 TREE_STRING_LENGTH is the size of the character array, not the string.
192 The value returned is of type `ssizetype'.
194 Unfortunately, string_constant can't access the values of const char
195 arrays with initializers, so neither can we do so here. */
205 src = string_constant (src, &offset_node);
209 max = TREE_STRING_LENGTH (src);
210 ptr = TREE_STRING_POINTER (src);
212 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
214 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
215 compute the offset to the following null if we don't know where to
216 start searching for it. */
219 for (i = 0; i < max; i++)
223 /* We don't know the starting offset, but we do know that the string
224 has no internal zero bytes. We can assume that the offset falls
225 within the bounds of the string; otherwise, the programmer deserves
226 what he gets. Subtract the offset from the length of the string,
227 and return that. This would perhaps not be valid if we were dealing
228 with named arrays in addition to literal string constants. */
230 return size_diffop (size_int (max), offset_node);
233 /* We have a known offset into the string. Start searching there for
235 if (offset_node == 0)
239 /* Did we get a long long offset? If so, punt. */
240 if (TREE_INT_CST_HIGH (offset_node) != 0)
242 offset = TREE_INT_CST_LOW (offset_node);
245 /* If the offset is known to be out of bounds, warn, and call strlen at
247 if (offset < 0 || offset > max)
249 warning ("offset outside bounds of constant string");
253 /* Use strlen to search for the first zero byte. Since any strings
254 constructed with build_string will have nulls appended, we win even
255 if we get handed something like (char[4])"abcd".
257 Since OFFSET is our starting index into the string, no further
258 calculation is needed. */
259 return ssize_int (strlen (ptr + offset));
262 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
263 times to get the address of either a higher stack frame, or a return
264 address located within it (depending on FNDECL_CODE). */
267 expand_builtin_return_addr (fndecl_code, count, tem)
268 enum built_in_function fndecl_code;
274 /* Some machines need special handling before we can access
275 arbitrary frames. For example, on the sparc, we must first flush
276 all register windows to the stack. */
277 #ifdef SETUP_FRAME_ADDRESSES
279 SETUP_FRAME_ADDRESSES ();
282 /* On the sparc, the return address is not in the frame, it is in a
283 register. There is no way to access it off of the current frame
284 pointer, but it can be accessed off the previous frame pointer by
285 reading the value from the register window save area. */
286 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
287 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
291 /* Scan back COUNT frames to the specified frame. */
292 for (i = 0; i < count; i++)
294 /* Assume the dynamic chain pointer is in the word that the
295 frame address points to, unless otherwise specified. */
296 #ifdef DYNAMIC_CHAIN_ADDRESS
297 tem = DYNAMIC_CHAIN_ADDRESS (tem);
299 tem = memory_address (Pmode, tem);
300 tem = copy_to_reg (gen_rtx_MEM (Pmode, tem));
301 MEM_ALIAS_SET (tem) = get_frame_alias_set ();
304 /* For __builtin_frame_address, return what we've got. */
305 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
308 /* For __builtin_return_address, Get the return address from that
310 #ifdef RETURN_ADDR_RTX
311 tem = RETURN_ADDR_RTX (count, tem);
313 tem = memory_address (Pmode,
314 plus_constant (tem, GET_MODE_SIZE (Pmode)));
315 tem = gen_rtx_MEM (Pmode, tem);
316 MEM_ALIAS_SET (tem) = get_frame_alias_set ();
321 /* Alias set used for setjmp buffer. */
322 static HOST_WIDE_INT setjmp_alias_set = -1;
324 /* __builtin_setjmp is passed a pointer to an array of five words (not
325 all will be used on all machines). It operates similarly to the C
326 library function of the same name, but is more efficient. Much of
327 the code below (and for longjmp) is copied from the handling of
330 NOTE: This is intended for use by GNAT and the exception handling
331 scheme in the compiler and will only work in the method used by
335 expand_builtin_setjmp (buf_addr, target, first_label, next_label)
338 rtx first_label, next_label;
340 rtx lab1 = gen_label_rtx ();
341 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
342 enum machine_mode value_mode;
346 value_mode = TYPE_MODE (integer_type_node);
348 if (setjmp_alias_set == -1)
349 setjmp_alias_set = new_alias_set ();
351 #ifdef POINTERS_EXTEND_UNSIGNED
352 buf_addr = convert_memory_address (Pmode, buf_addr);
355 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
357 if (target == 0 || GET_CODE (target) != REG
358 || REGNO (target) < FIRST_PSEUDO_REGISTER)
359 target = gen_reg_rtx (value_mode);
363 /* We store the frame pointer and the address of lab1 in the buffer
364 and use the rest of it for the stack save area, which is
365 machine-dependent. */
367 #ifndef BUILTIN_SETJMP_FRAME_VALUE
368 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
371 mem = gen_rtx_MEM (Pmode, buf_addr);
372 MEM_ALIAS_SET (mem) = setjmp_alias_set;
373 emit_move_insn (mem, BUILTIN_SETJMP_FRAME_VALUE);
375 mem = gen_rtx_MEM (Pmode, plus_constant (buf_addr, GET_MODE_SIZE (Pmode))),
376 MEM_ALIAS_SET (mem) = setjmp_alias_set;
378 emit_move_insn (validize_mem (mem),
379 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, lab1)));
381 stack_save = gen_rtx_MEM (sa_mode,
382 plus_constant (buf_addr,
383 2 * GET_MODE_SIZE (Pmode)));
384 MEM_ALIAS_SET (stack_save) = setjmp_alias_set;
385 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
387 /* If there is further processing to do, do it. */
388 #ifdef HAVE_builtin_setjmp_setup
389 if (HAVE_builtin_setjmp_setup)
390 emit_insn (gen_builtin_setjmp_setup (buf_addr));
393 /* Set TARGET to zero and branch to the first-time-through label. */
394 emit_move_insn (target, const0_rtx);
395 emit_jump_insn (gen_jump (first_label));
399 /* Tell flow about the strange goings on. Putting `lab1' on
400 `nonlocal_goto_handler_labels' to indicates that function
401 calls may traverse the arc back to this label. */
403 current_function_has_nonlocal_label = 1;
404 nonlocal_goto_handler_labels
405 = gen_rtx_EXPR_LIST (VOIDmode, lab1, nonlocal_goto_handler_labels);
407 /* Clobber the FP when we get here, so we have to make sure it's
408 marked as used by this function. */
409 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
411 /* Mark the static chain as clobbered here so life information
412 doesn't get messed up for it. */
413 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
415 /* Now put in the code to restore the frame pointer, and argument
416 pointer, if needed. The code below is from expand_end_bindings
417 in stmt.c; see detailed documentation there. */
418 #ifdef HAVE_nonlocal_goto
419 if (! HAVE_nonlocal_goto)
421 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
423 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
424 if (fixed_regs[ARG_POINTER_REGNUM])
426 #ifdef ELIMINABLE_REGS
428 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
430 for (i = 0; i < ARRAY_SIZE (elim_regs); i++)
431 if (elim_regs[i].from == ARG_POINTER_REGNUM
432 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
435 if (i == ARRAY_SIZE (elim_regs))
438 /* Now restore our arg pointer from the address at which it
439 was saved in our stack frame.
440 If there hasn't be space allocated for it yet, make
442 if (arg_pointer_save_area == 0)
443 arg_pointer_save_area
444 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
445 emit_move_insn (virtual_incoming_args_rtx,
446 copy_to_reg (arg_pointer_save_area));
451 #ifdef HAVE_builtin_setjmp_receiver
452 if (HAVE_builtin_setjmp_receiver)
453 emit_insn (gen_builtin_setjmp_receiver (lab1));
456 #ifdef HAVE_nonlocal_goto_receiver
457 if (HAVE_nonlocal_goto_receiver)
458 emit_insn (gen_nonlocal_goto_receiver ());
465 /* Set TARGET, and branch to the next-time-through label. */
466 emit_move_insn (target, const1_rtx);
467 emit_jump_insn (gen_jump (next_label));
473 /* __builtin_longjmp is passed a pointer to an array of five words (not
474 all will be used on all machines). It operates similarly to the C
475 library function of the same name, but is more efficient. Much of
476 the code below is copied from the handling of non-local gotos.
478 NOTE: This is intended for use by GNAT and the exception handling
479 scheme in the compiler and will only work in the method used by
483 expand_builtin_longjmp (buf_addr, value)
487 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
489 if (setjmp_alias_set == -1)
490 setjmp_alias_set = new_alias_set ();
492 #ifdef POINTERS_EXTEND_UNSIGNED
493 buf_addr = convert_memory_address (Pmode, buf_addr);
495 buf_addr = force_reg (Pmode, buf_addr);
497 /* We used to store value in static_chain_rtx, but that fails if pointers
498 are smaller than integers. We instead require that the user must pass
499 a second argument of 1, because that is what builtin_setjmp will
500 return. This also makes EH slightly more efficient, since we are no
501 longer copying around a value that we don't care about. */
502 if (value != const1_rtx)
505 #ifdef HAVE_builtin_longjmp
506 if (HAVE_builtin_longjmp)
507 emit_insn (gen_builtin_longjmp (buf_addr));
511 fp = gen_rtx_MEM (Pmode, buf_addr);
512 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
513 GET_MODE_SIZE (Pmode)));
515 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
516 2 * GET_MODE_SIZE (Pmode)));
517 MEM_ALIAS_SET (fp) = MEM_ALIAS_SET (lab) = MEM_ALIAS_SET (stack)
520 /* Pick up FP, label, and SP from the block and jump. This code is
521 from expand_goto in stmt.c; see there for detailed comments. */
522 #if HAVE_nonlocal_goto
523 if (HAVE_nonlocal_goto)
524 /* We have to pass a value to the nonlocal_goto pattern that will
525 get copied into the static_chain pointer, but it does not matter
526 what that value is, because builtin_setjmp does not use it. */
527 emit_insn (gen_nonlocal_goto (value, fp, stack, lab));
531 lab = copy_to_reg (lab);
533 emit_move_insn (hard_frame_pointer_rtx, fp);
534 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
536 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
537 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
538 emit_indirect_jump (lab);
543 /* Get a MEM rtx for expression EXP which is the address of an operand
544 to be used to be used in a string instruction (cmpstrsi, movstrsi, ..). */
550 rtx mem = gen_rtx_MEM (BLKmode,
551 memory_address (BLKmode,
552 expand_expr (exp, NULL_RTX,
553 ptr_mode, EXPAND_SUM)));
555 /* Get an expression we can use to find the attributes to assign to MEM.
556 If it is an ADDR_EXPR, use the operand. Otherwise, dereference it if
557 we can. First remove any nops. */
558 while ((TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
559 || TREE_CODE (exp) == NON_LVALUE_EXPR)
560 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (exp, 0))))
561 exp = TREE_OPERAND (exp, 0);
563 if (TREE_CODE (exp) == ADDR_EXPR)
564 exp = TREE_OPERAND (exp, 0);
565 else if (POINTER_TYPE_P (TREE_TYPE (exp)))
566 exp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
570 set_mem_attributes (mem, exp, 0);
572 /* memcpy, memset and other builtin stringops can alias with anything. */
573 MEM_ALIAS_SET (mem) = 0;
577 /* Built-in functions to perform an untyped call and return. */
579 /* For each register that may be used for calling a function, this
580 gives a mode used to copy the register's value. VOIDmode indicates
581 the register is not used for calling a function. If the machine
582 has register windows, this gives only the outbound registers.
583 INCOMING_REGNO gives the corresponding inbound register. */
584 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
586 /* For each register that may be used for returning values, this gives
587 a mode used to copy the register's value. VOIDmode indicates the
588 register is not used for returning values. If the machine has
589 register windows, this gives only the outbound registers.
590 INCOMING_REGNO gives the corresponding inbound register. */
591 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
593 /* For each register that may be used for calling a function, this
594 gives the offset of that register into the block returned by
595 __builtin_apply_args. 0 indicates that the register is not
596 used for calling a function. */
597 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
599 /* Return the offset of register REGNO into the block returned by
600 __builtin_apply_args. This is not declared static, since it is
601 needed in objc-act.c. */
604 apply_args_register_offset (regno)
609 /* Arguments are always put in outgoing registers (in the argument
610 block) if such make sense. */
611 #ifdef OUTGOING_REGNO
612 regno = OUTGOING_REGNO(regno);
614 return apply_args_reg_offset[regno];
617 /* Return the size required for the block returned by __builtin_apply_args,
618 and initialize apply_args_mode. */
623 static int size = -1;
625 enum machine_mode mode;
627 /* The values computed by this function never change. */
630 /* The first value is the incoming arg-pointer. */
631 size = GET_MODE_SIZE (Pmode);
633 /* The second value is the structure value address unless this is
634 passed as an "invisible" first argument. */
635 if (struct_value_rtx)
636 size += GET_MODE_SIZE (Pmode);
638 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
639 if (FUNCTION_ARG_REGNO_P (regno))
641 /* Search for the proper mode for copying this register's
642 value. I'm not sure this is right, but it works so far. */
643 enum machine_mode best_mode = VOIDmode;
645 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
647 mode = GET_MODE_WIDER_MODE (mode))
648 if (HARD_REGNO_MODE_OK (regno, mode)
649 && HARD_REGNO_NREGS (regno, mode) == 1)
652 if (best_mode == VOIDmode)
653 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
655 mode = GET_MODE_WIDER_MODE (mode))
656 if (HARD_REGNO_MODE_OK (regno, mode)
657 && (mov_optab->handlers[(int) mode].insn_code
658 != CODE_FOR_nothing))
662 if (mode == VOIDmode)
665 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
666 if (size % align != 0)
667 size = CEIL (size, align) * align;
668 apply_args_reg_offset[regno] = size;
669 size += GET_MODE_SIZE (mode);
670 apply_args_mode[regno] = mode;
674 apply_args_mode[regno] = VOIDmode;
675 apply_args_reg_offset[regno] = 0;
681 /* Return the size required for the block returned by __builtin_apply,
682 and initialize apply_result_mode. */
687 static int size = -1;
689 enum machine_mode mode;
691 /* The values computed by this function never change. */
696 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
697 if (FUNCTION_VALUE_REGNO_P (regno))
699 /* Search for the proper mode for copying this register's
700 value. I'm not sure this is right, but it works so far. */
701 enum machine_mode best_mode = VOIDmode;
703 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
705 mode = GET_MODE_WIDER_MODE (mode))
706 if (HARD_REGNO_MODE_OK (regno, mode))
709 if (best_mode == VOIDmode)
710 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
712 mode = GET_MODE_WIDER_MODE (mode))
713 if (HARD_REGNO_MODE_OK (regno, mode)
714 && (mov_optab->handlers[(int) mode].insn_code
715 != CODE_FOR_nothing))
719 if (mode == VOIDmode)
722 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
723 if (size % align != 0)
724 size = CEIL (size, align) * align;
725 size += GET_MODE_SIZE (mode);
726 apply_result_mode[regno] = mode;
729 apply_result_mode[regno] = VOIDmode;
731 /* Allow targets that use untyped_call and untyped_return to override
732 the size so that machine-specific information can be stored here. */
733 #ifdef APPLY_RESULT_SIZE
734 size = APPLY_RESULT_SIZE;
740 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
741 /* Create a vector describing the result block RESULT. If SAVEP is true,
742 the result block is used to save the values; otherwise it is used to
743 restore the values. */
746 result_vector (savep, result)
750 int regno, size, align, nelts;
751 enum machine_mode mode;
753 rtx *savevec = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
756 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
757 if ((mode = apply_result_mode[regno]) != VOIDmode)
759 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
760 if (size % align != 0)
761 size = CEIL (size, align) * align;
762 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
763 mem = change_address (result, mode,
764 plus_constant (XEXP (result, 0), size));
765 savevec[nelts++] = (savep
766 ? gen_rtx_SET (VOIDmode, mem, reg)
767 : gen_rtx_SET (VOIDmode, reg, mem));
768 size += GET_MODE_SIZE (mode);
770 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
772 #endif /* HAVE_untyped_call or HAVE_untyped_return */
774 /* Save the state required to perform an untyped call with the same
775 arguments as were passed to the current function. */
778 expand_builtin_apply_args_1 ()
781 int size, align, regno;
782 enum machine_mode mode;
784 /* Create a block where the arg-pointer, structure value address,
785 and argument registers can be saved. */
786 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
788 /* Walk past the arg-pointer and structure value address. */
789 size = GET_MODE_SIZE (Pmode);
790 if (struct_value_rtx)
791 size += GET_MODE_SIZE (Pmode);
793 /* Save each register used in calling a function to the block. */
794 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
795 if ((mode = apply_args_mode[regno]) != VOIDmode)
799 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
800 if (size % align != 0)
801 size = CEIL (size, align) * align;
803 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
805 emit_move_insn (change_address (registers, mode,
806 plus_constant (XEXP (registers, 0),
809 size += GET_MODE_SIZE (mode);
812 /* Save the arg pointer to the block. */
813 emit_move_insn (change_address (registers, Pmode, XEXP (registers, 0)),
814 copy_to_reg (virtual_incoming_args_rtx));
815 size = GET_MODE_SIZE (Pmode);
817 /* Save the structure value address unless this is passed as an
818 "invisible" first argument. */
819 if (struct_value_incoming_rtx)
821 emit_move_insn (change_address (registers, Pmode,
822 plus_constant (XEXP (registers, 0),
824 copy_to_reg (struct_value_incoming_rtx));
825 size += GET_MODE_SIZE (Pmode);
828 /* Return the address of the block. */
829 return copy_addr_to_reg (XEXP (registers, 0));
832 /* __builtin_apply_args returns block of memory allocated on
833 the stack into which is stored the arg pointer, structure
834 value address, static chain, and all the registers that might
835 possibly be used in performing a function call. The code is
836 moved to the start of the function so the incoming values are
839 expand_builtin_apply_args ()
841 /* Don't do __builtin_apply_args more than once in a function.
842 Save the result of the first call and reuse it. */
843 if (apply_args_value != 0)
844 return apply_args_value;
846 /* When this function is called, it means that registers must be
847 saved on entry to this function. So we migrate the
848 call to the first insn of this function. */
853 temp = expand_builtin_apply_args_1 ();
857 apply_args_value = temp;
859 /* Put the sequence after the NOTE that starts the function.
860 If this is inside a SEQUENCE, make the outer-level insn
861 chain current, so the code is placed at the start of the
863 push_topmost_sequence ();
864 emit_insns_before (seq, NEXT_INSN (get_insns ()));
865 pop_topmost_sequence ();
870 /* Perform an untyped call and save the state required to perform an
871 untyped return of whatever value was returned by the given function. */
874 expand_builtin_apply (function, arguments, argsize)
875 rtx function, arguments, argsize;
877 int size, align, regno;
878 enum machine_mode mode;
879 rtx incoming_args, result, reg, dest, call_insn;
880 rtx old_stack_level = 0;
883 /* Create a block where the return registers can be saved. */
884 result = assign_stack_local (BLKmode, apply_result_size (), -1);
886 /* Fetch the arg pointer from the ARGUMENTS block. */
887 incoming_args = gen_reg_rtx (Pmode);
888 emit_move_insn (incoming_args,
889 gen_rtx_MEM (Pmode, arguments));
890 #ifndef STACK_GROWS_DOWNWARD
891 incoming_args = expand_binop (Pmode, sub_optab, incoming_args, argsize,
892 incoming_args, 0, OPTAB_LIB_WIDEN);
895 /* Perform postincrements before actually calling the function. */
898 /* Push a new argument block and copy the arguments. Do not allow
899 the (potential) memcpy call below to interfere with our stack
901 do_pending_stack_adjust ();
904 /* Save the stack with nonlocal if available */
905 #ifdef HAVE_save_stack_nonlocal
906 if (HAVE_save_stack_nonlocal)
907 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
910 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
912 /* Push a block of memory onto the stack to store the memory arguments.
913 Save the address in a register, and copy the memory arguments. ??? I
914 haven't figured out how the calling convention macros effect this,
915 but it's likely that the source and/or destination addresses in
916 the block copy will need updating in machine specific ways. */
917 dest = allocate_dynamic_stack_space (argsize, 0, BITS_PER_UNIT);
918 emit_block_move (gen_rtx_MEM (BLKmode, dest),
919 gen_rtx_MEM (BLKmode, incoming_args),
920 argsize, PARM_BOUNDARY);
922 /* Refer to the argument block. */
924 arguments = gen_rtx_MEM (BLKmode, arguments);
926 /* Walk past the arg-pointer and structure value address. */
927 size = GET_MODE_SIZE (Pmode);
928 if (struct_value_rtx)
929 size += GET_MODE_SIZE (Pmode);
931 /* Restore each of the registers previously saved. Make USE insns
932 for each of these registers for use in making the call. */
933 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
934 if ((mode = apply_args_mode[regno]) != VOIDmode)
936 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
937 if (size % align != 0)
938 size = CEIL (size, align) * align;
939 reg = gen_rtx_REG (mode, regno);
941 change_address (arguments, mode,
942 plus_constant (XEXP (arguments, 0),
945 use_reg (&call_fusage, reg);
946 size += GET_MODE_SIZE (mode);
949 /* Restore the structure value address unless this is passed as an
950 "invisible" first argument. */
951 size = GET_MODE_SIZE (Pmode);
952 if (struct_value_rtx)
954 rtx value = gen_reg_rtx (Pmode);
955 emit_move_insn (value,
956 change_address (arguments, Pmode,
957 plus_constant (XEXP (arguments, 0),
959 emit_move_insn (struct_value_rtx, value);
960 if (GET_CODE (struct_value_rtx) == REG)
961 use_reg (&call_fusage, struct_value_rtx);
962 size += GET_MODE_SIZE (Pmode);
965 /* All arguments and registers used for the call are set up by now! */
966 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0);
968 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
969 and we don't want to load it into a register as an optimization,
970 because prepare_call_address already did it if it should be done. */
971 if (GET_CODE (function) != SYMBOL_REF)
972 function = memory_address (FUNCTION_MODE, function);
974 /* Generate the actual call instruction and save the return value. */
975 #ifdef HAVE_untyped_call
976 if (HAVE_untyped_call)
977 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
978 result, result_vector (1, result)));
981 #ifdef HAVE_call_value
986 /* Locate the unique return register. It is not possible to
987 express a call that sets more than one return register using
988 call_value; use untyped_call for that. In fact, untyped_call
989 only needs to save the return registers in the given block. */
990 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
991 if ((mode = apply_result_mode[regno]) != VOIDmode)
994 abort (); /* HAVE_untyped_call required. */
995 valreg = gen_rtx_REG (mode, regno);
998 emit_call_insn (GEN_CALL_VALUE (valreg,
999 gen_rtx_MEM (FUNCTION_MODE, function),
1000 const0_rtx, NULL_RTX, const0_rtx));
1002 emit_move_insn (change_address (result, GET_MODE (valreg),
1010 /* Find the CALL insn we just emitted. */
1011 for (call_insn = get_last_insn ();
1012 call_insn && GET_CODE (call_insn) != CALL_INSN;
1013 call_insn = PREV_INSN (call_insn))
1019 /* Put the register usage information on the CALL. If there is already
1020 some usage information, put ours at the end. */
1021 if (CALL_INSN_FUNCTION_USAGE (call_insn))
1025 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
1026 link = XEXP (link, 1))
1029 XEXP (link, 1) = call_fusage;
1032 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
1034 /* Restore the stack. */
1035 #ifdef HAVE_save_stack_nonlocal
1036 if (HAVE_save_stack_nonlocal)
1037 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
1040 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1044 /* Return the address of the result block. */
1045 return copy_addr_to_reg (XEXP (result, 0));
1048 /* Perform an untyped return. */
1051 expand_builtin_return (result)
1054 int size, align, regno;
1055 enum machine_mode mode;
1057 rtx call_fusage = 0;
1059 apply_result_size ();
1060 result = gen_rtx_MEM (BLKmode, result);
1062 #ifdef HAVE_untyped_return
1063 if (HAVE_untyped_return)
1065 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1071 /* Restore the return value and note that each value is used. */
1073 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1074 if ((mode = apply_result_mode[regno]) != VOIDmode)
1076 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1077 if (size % align != 0)
1078 size = CEIL (size, align) * align;
1079 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1080 emit_move_insn (reg,
1081 change_address (result, mode,
1082 plus_constant (XEXP (result, 0),
1085 push_to_sequence (call_fusage);
1086 emit_insn (gen_rtx_USE (VOIDmode, reg));
1087 call_fusage = get_insns ();
1089 size += GET_MODE_SIZE (mode);
1092 /* Put the USE insns before the return. */
1093 emit_insns (call_fusage);
1095 /* Return whatever values was restored by jumping directly to the end
1097 expand_null_return ();
1100 /* Expand a call to __builtin_classify_type with arguments found in
1103 expand_builtin_classify_type (arglist)
1108 tree type = TREE_TYPE (TREE_VALUE (arglist));
1109 enum tree_code code = TREE_CODE (type);
1110 if (code == VOID_TYPE)
1111 return GEN_INT (void_type_class);
1112 if (code == INTEGER_TYPE)
1113 return GEN_INT (integer_type_class);
1114 if (code == CHAR_TYPE)
1115 return GEN_INT (char_type_class);
1116 if (code == ENUMERAL_TYPE)
1117 return GEN_INT (enumeral_type_class);
1118 if (code == BOOLEAN_TYPE)
1119 return GEN_INT (boolean_type_class);
1120 if (code == POINTER_TYPE)
1121 return GEN_INT (pointer_type_class);
1122 if (code == REFERENCE_TYPE)
1123 return GEN_INT (reference_type_class);
1124 if (code == OFFSET_TYPE)
1125 return GEN_INT (offset_type_class);
1126 if (code == REAL_TYPE)
1127 return GEN_INT (real_type_class);
1128 if (code == COMPLEX_TYPE)
1129 return GEN_INT (complex_type_class);
1130 if (code == FUNCTION_TYPE)
1131 return GEN_INT (function_type_class);
1132 if (code == METHOD_TYPE)
1133 return GEN_INT (method_type_class);
1134 if (code == RECORD_TYPE)
1135 return GEN_INT (record_type_class);
1136 if (code == UNION_TYPE || code == QUAL_UNION_TYPE)
1137 return GEN_INT (union_type_class);
1138 if (code == ARRAY_TYPE)
1140 if (TYPE_STRING_FLAG (type))
1141 return GEN_INT (string_type_class);
1143 return GEN_INT (array_type_class);
1145 if (code == SET_TYPE)
1146 return GEN_INT (set_type_class);
1147 if (code == FILE_TYPE)
1148 return GEN_INT (file_type_class);
1149 if (code == LANG_TYPE)
1150 return GEN_INT (lang_type_class);
1152 return GEN_INT (no_type_class);
1155 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1157 expand_builtin_constant_p (exp)
1160 tree arglist = TREE_OPERAND (exp, 1);
1161 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1167 tree arg = TREE_VALUE (arglist);
1170 /* We return 1 for a numeric type that's known to be a constant
1171 value at compile-time or for an aggregate type that's a
1172 literal constant. */
1175 /* If we know this is a constant, emit the constant of one. */
1176 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'c'
1177 || (TREE_CODE (arg) == CONSTRUCTOR
1178 && TREE_CONSTANT (arg))
1179 || (TREE_CODE (arg) == ADDR_EXPR
1180 && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST))
1183 /* If we aren't going to be running CSE or this expression
1184 has side effects, show we don't know it to be a constant.
1185 Likewise if it's a pointer or aggregate type since in those
1186 case we only want literals, since those are only optimized
1187 when generating RTL, not later. */
1188 if (TREE_SIDE_EFFECTS (arg) || cse_not_expected
1189 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
1190 || POINTER_TYPE_P (TREE_TYPE (arg)))
1193 /* Otherwise, emit (constant_p_rtx (ARG)) and let CSE get a
1194 chance to see if it can deduce whether ARG is constant. */
1196 tmp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
1197 tmp = gen_rtx_CONSTANT_P_RTX (value_mode, tmp);
1202 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1203 Return 0 if a normal call should be emitted rather than expanding the
1204 function in-line. EXP is the expression that is a call to the builtin
1205 function; if convenient, the result should be placed in TARGET.
1206 SUBTARGET may be used as the target for computing one of EXP's operands. */
1208 expand_builtin_mathfn (exp, target, subtarget)
1210 rtx target, subtarget;
1212 optab builtin_optab;
1214 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1215 tree arglist = TREE_OPERAND (exp, 1);
1218 /* Arg could be wrong type if user redeclared this fcn wrong. */
1219 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE)
1222 /* Stabilize and compute the argument. */
1223 if (TREE_CODE (TREE_VALUE (arglist)) != VAR_DECL
1224 && TREE_CODE (TREE_VALUE (arglist)) != PARM_DECL)
1226 exp = copy_node (exp);
1227 TREE_OPERAND (exp, 1) = arglist;
1228 /* Wrap the computation of the argument in a SAVE_EXPR. That
1229 way, if we need to expand the argument again (as in the
1230 flag_errno_math case below where we cannot directly set
1231 errno), we will not perform side-effects more than once.
1232 Note that here we're mutating the original EXP as well as the
1233 copy; that's the right thing to do in case the original EXP
1234 is expanded later. */
1235 TREE_VALUE (arglist) = save_expr (TREE_VALUE (arglist));
1236 arglist = copy_node (arglist);
1238 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
1240 /* Make a suitable register to place result in. */
1241 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
1246 switch (DECL_FUNCTION_CODE (fndecl))
1249 builtin_optab = sin_optab; break;
1251 builtin_optab = cos_optab; break;
1252 case BUILT_IN_FSQRT:
1253 builtin_optab = sqrt_optab; break;
1258 /* Compute into TARGET.
1259 Set TARGET to wherever the result comes back. */
1260 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
1261 builtin_optab, op0, target, 0);
1263 /* If we were unable to expand via the builtin, stop the
1264 sequence (without outputting the insns) and return 0, causing
1265 a call to the library function. */
1272 /* Check the results by default. But if flag_fast_math is turned on,
1273 then assume sqrt will always be called with valid arguments. */
1275 if (flag_errno_math && ! flag_fast_math)
1279 /* Don't define the builtin FP instructions
1280 if your machine is not IEEE. */
1281 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1284 lab1 = gen_label_rtx ();
1286 /* Test the result; if it is NaN, set errno=EDOM because
1287 the argument was not in the domain. */
1288 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1293 #ifdef GEN_ERRNO_RTX
1294 rtx errno_rtx = GEN_ERRNO_RTX;
1297 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1300 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1303 /* We can't set errno=EDOM directly; let the library call do it.
1304 Pop the arguments right away in case the call gets deleted. */
1306 expand_call (exp, target, 0);
1313 /* Output the entire sequence. */
1314 insns = get_insns ();
1321 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1322 if we failed the caller should emit a normal call, otherwise
1323 try to get the result in TARGET, if convenient (and in mode MODE if that's
1327 expand_builtin_strlen (exp, target, mode)
1330 enum machine_mode mode;
1332 tree arglist = TREE_OPERAND (exp, 1);
1333 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1336 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1337 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
1342 tree src = TREE_VALUE (arglist);
1343 tree len = c_strlen (src);
1346 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1348 rtx result, src_reg, char_rtx, before_strlen;
1349 enum machine_mode insn_mode = value_mode, char_mode;
1350 enum insn_code icode = CODE_FOR_nothing;
1352 /* If the length is known, just return it. */
1354 return expand_expr (len, target, mode, EXPAND_MEMORY_USE_BAD);
1356 /* If SRC is not a pointer type, don't do this operation inline. */
1360 /* Bail out if we can't compute strlen in the right mode. */
1361 while (insn_mode != VOIDmode)
1363 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1364 if (icode != CODE_FOR_nothing)
1367 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1369 if (insn_mode == VOIDmode)
1372 /* Make a place to write the result of the instruction. */
1375 && GET_CODE (result) == REG
1376 && GET_MODE (result) == insn_mode
1377 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1378 result = gen_reg_rtx (insn_mode);
1380 /* Make a place to hold the source address. We will not expand
1381 the actual source until we are sure that the expansion will
1382 not fail -- there are trees that cannot be expanded twice. */
1383 src_reg = gen_reg_rtx (Pmode);
1385 /* Mark the beginning of the strlen sequence so we can emit the
1386 source operand later. */
1387 before_strlen = get_last_insn();
1389 /* Check the string is readable and has an end. */
1390 if (current_function_check_memory_usage)
1391 emit_library_call (chkr_check_str_libfunc, 1, VOIDmode, 2,
1393 GEN_INT (MEMORY_USE_RO),
1394 TYPE_MODE (integer_type_node));
1396 char_rtx = const0_rtx;
1397 char_mode = insn_data[(int) icode].operand[2].mode;
1398 if (! (*insn_data[(int) icode].operand[2].predicate) (char_rtx,
1400 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1402 pat = GEN_FCN (icode) (result, gen_rtx_MEM (BLKmode, src_reg),
1403 char_rtx, GEN_INT (align));
1408 /* Now that we are assured of success, expand the source. */
1410 pat = memory_address (BLKmode,
1411 expand_expr (src, src_reg, ptr_mode, EXPAND_SUM));
1413 emit_move_insn (src_reg, pat);
1414 pat = gen_sequence ();
1418 emit_insn_after (pat, before_strlen);
1420 emit_insn_before (pat, get_insns ());
1422 /* Return the value in the proper mode for this function. */
1423 if (GET_MODE (result) == value_mode)
1425 else if (target != 0)
1426 convert_move (target, result, 0);
1428 target = convert_to_mode (value_mode, result, 0);
1434 /* Expand a call to the memcpy builtin, with arguments in ARGLIST. */
1436 expand_builtin_memcpy (arglist)
1440 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1441 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1442 || TREE_CHAIN (arglist) == 0
1443 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1445 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1446 || (TREE_CODE (TREE_TYPE (TREE_VALUE
1447 (TREE_CHAIN (TREE_CHAIN (arglist)))))
1452 tree dest = TREE_VALUE (arglist);
1453 tree src = TREE_VALUE (TREE_CHAIN (arglist));
1454 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1456 int src_align = get_pointer_alignment (src, BIGGEST_ALIGNMENT);
1457 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
1458 rtx dest_mem, src_mem, dest_addr, len_rtx;
1460 /* If either SRC or DEST is not a pointer type, don't do
1461 this operation in-line. */
1462 if (src_align == 0 || dest_align == 0)
1465 dest_mem = get_memory_rtx (dest);
1466 src_mem = get_memory_rtx (src);
1467 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1469 /* Just copy the rights of SRC to the rights of DEST. */
1470 if (current_function_check_memory_usage)
1471 emit_library_call (chkr_copy_bitmap_libfunc, 1, VOIDmode, 3,
1472 XEXP (dest_mem, 0), Pmode,
1473 XEXP (src_mem, 0), Pmode,
1474 len_rtx, TYPE_MODE (sizetype));
1476 /* Copy word part most expediently. */
1478 = emit_block_move (dest_mem, src_mem, len_rtx,
1479 MIN (src_align, dest_align));
1482 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1488 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
1489 if we failed the caller should emit a normal call. */
1492 expand_builtin_strcpy (exp)
1495 tree arglist = TREE_OPERAND (exp, 1);
1499 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1500 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1501 || TREE_CHAIN (arglist) == 0
1502 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1507 tree len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
1512 len = size_binop (PLUS_EXPR, len, ssize_int (1));
1513 chainon (arglist, build_tree_list (NULL_TREE, len));
1516 result = expand_builtin_memcpy (arglist);
1519 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1523 /* Expand expression EXP, which is a call to the memset builtin. Return 0
1524 if we failed the caller should emit a normal call. */
1527 expand_builtin_memset (exp)
1530 tree arglist = TREE_OPERAND (exp, 1);
1533 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1534 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1535 || TREE_CHAIN (arglist) == 0
1536 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1538 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1540 != (TREE_CODE (TREE_TYPE
1542 (TREE_CHAIN (TREE_CHAIN (arglist))))))))
1546 tree dest = TREE_VALUE (arglist);
1547 tree val = TREE_VALUE (TREE_CHAIN (arglist));
1548 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1550 int dest_align = get_pointer_alignment (dest, BIGGEST_ALIGNMENT);
1551 rtx dest_mem, dest_addr, len_rtx;
1553 /* If DEST is not a pointer type, don't do this
1554 operation in-line. */
1555 if (dest_align == 0)
1558 /* If the arguments have side-effects, then we can only evaluate
1559 them at most once. The following code evaluates them twice if
1560 they are not constants because we break out to expand_call
1561 in that case. They can't be constants if they have side-effects
1562 so we can check for that first. Alternatively, we could call
1563 save_expr to make multiple evaluation safe. */
1564 if (TREE_SIDE_EFFECTS (val) || TREE_SIDE_EFFECTS (len))
1567 /* If VAL is not 0, don't do this operation in-line. */
1568 if (expand_expr (val, NULL_RTX, VOIDmode, 0) != const0_rtx)
1571 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1573 dest_mem = get_memory_rtx (dest);
1575 /* Just check DST is writable and mark it as readable. */
1576 if (current_function_check_memory_usage)
1577 emit_library_call (chkr_check_addr_libfunc, 1, VOIDmode, 3,
1578 XEXP (dest_mem, 0), Pmode,
1579 len_rtx, TYPE_MODE (sizetype),
1580 GEN_INT (MEMORY_USE_WO),
1581 TYPE_MODE (integer_type_node));
1584 dest_addr = clear_storage (dest_mem, len_rtx, dest_align);
1587 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1593 /* Expand expression EXP, which is a call to the bzero builtin. Return 0
1594 if we failed the caller should emit a normal call. */
1596 expand_builtin_bzero (exp)
1599 tree arglist = TREE_OPERAND (exp, 1);
1600 tree dest, size, newarglist;
1604 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1605 || TREE_CODE (TREE_TYPE (dest = TREE_VALUE (arglist))) != POINTER_TYPE
1606 || TREE_CHAIN (arglist) == 0
1607 || (TREE_CODE (TREE_TYPE (size = TREE_VALUE (TREE_CHAIN (arglist))))
1611 /* New argument list transforming bzero(ptr x, int y) to
1612 memset(ptr x, int 0, size_t y). */
1614 newarglist = build_tree_list (NULL_TREE, convert (sizetype, size));
1615 newarglist = tree_cons (NULL_TREE, integer_zero_node, newarglist);
1616 newarglist = tree_cons (NULL_TREE, dest, newarglist);
1618 TREE_OPERAND (exp, 1) = newarglist;
1619 result = expand_builtin_memset(exp);
1621 /* Always restore the original arguments. */
1622 TREE_OPERAND (exp, 1) = arglist;
1627 #ifdef HAVE_cmpstrsi
1628 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
1629 ARGLIST is the argument list for this call. Return 0 if we failed and the
1630 caller should emit a normal call, otherwise try to get the result in
1631 TARGET, if convenient. */
1633 expand_builtin_memcmp (exp, arglist, target)
1638 /* If we need to check memory accesses, call the library function. */
1639 if (current_function_check_memory_usage)
1643 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1644 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1645 || TREE_CHAIN (arglist) == 0
1646 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
1647 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1648 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
1652 enum machine_mode mode;
1653 tree arg1 = TREE_VALUE (arglist);
1654 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1655 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1656 rtx arg1_rtx, arg2_rtx, arg3_rtx;
1661 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1663 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1664 enum machine_mode insn_mode
1665 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
1667 /* If we don't have POINTER_TYPE, call the function. */
1668 if (arg1_align == 0 || arg2_align == 0)
1671 /* Make a place to write the result of the instruction. */
1674 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
1675 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1676 result = gen_reg_rtx (insn_mode);
1678 arg1_rtx = get_memory_rtx (arg1);
1679 arg2_rtx = get_memory_rtx (arg2);
1680 arg3_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1684 insn = gen_cmpstrsi (result, arg1_rtx, arg2_rtx, arg3_rtx,
1685 GEN_INT (MIN (arg1_align, arg2_align)));
1690 emit_library_call_value (memcmp_libfunc, result, 2,
1691 TYPE_MODE (integer_type_node), 3,
1692 XEXP (arg1_rtx, 0), Pmode,
1693 XEXP (arg2_rtx, 0), Pmode,
1694 convert_to_mode (TYPE_MODE (sizetype), arg3_rtx,
1695 TREE_UNSIGNED (sizetype)),
1696 TYPE_MODE (sizetype));
1698 /* Return the value in the proper mode for this function. */
1699 mode = TYPE_MODE (TREE_TYPE (exp));
1700 if (GET_MODE (result) == mode)
1702 else if (target != 0)
1704 convert_move (target, result, 0);
1708 return convert_to_mode (mode, result, 0);
1712 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
1713 if we failed the caller should emit a normal call, otherwise try to get
1714 the result in TARGET, if convenient. */
1717 expand_builtin_strcmp (exp, target)
1721 tree arglist = TREE_OPERAND (exp, 1);
1723 /* If we need to check memory accesses, call the library function. */
1724 if (current_function_check_memory_usage)
1728 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1729 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1730 || TREE_CHAIN (arglist) == 0
1731 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1735 else if (! HAVE_cmpstrsi)
1738 tree arg1 = TREE_VALUE (arglist);
1739 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1740 tree len = c_strlen (arg1);
1741 tree len2 = c_strlen (arg2);
1745 len = size_binop (PLUS_EXPR, ssize_int (1), len);
1748 len2 = size_binop (PLUS_EXPR, ssize_int (1), len2);
1750 /* If we don't have a constant length for the first, use the length
1751 of the second, if we know it. We don't require a constant for
1752 this case; some cost analysis could be done if both are available
1753 but neither is constant. For now, assume they're equally cheap.
1755 If both strings have constant lengths, use the smaller. This
1756 could arise if optimization results in strcpy being called with
1757 two fixed strings, or if the code was machine-generated. We should
1758 add some code to the `memcmp' handler below to deal with such
1759 situations, someday. */
1761 if (!len || TREE_CODE (len) != INTEGER_CST)
1768 else if (len2 && TREE_CODE (len2) == INTEGER_CST
1769 && tree_int_cst_lt (len2, len))
1772 chainon (arglist, build_tree_list (NULL_TREE, len));
1773 result = expand_builtin_memcmp (exp, arglist, target);
1775 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1782 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
1783 if that's convenient. */
1786 expand_builtin_saveregs ()
1790 /* Don't do __builtin_saveregs more than once in a function.
1791 Save the result of the first call and reuse it. */
1792 if (saveregs_value != 0)
1793 return saveregs_value;
1795 /* When this function is called, it means that registers must be
1796 saved on entry to this function. So we migrate the call to the
1797 first insn of this function. */
1801 #ifdef EXPAND_BUILTIN_SAVEREGS
1802 /* Do whatever the machine needs done in this case. */
1803 val = EXPAND_BUILTIN_SAVEREGS ();
1805 /* ??? We used to try and build up a call to the out of line function,
1806 guessing about what registers needed saving etc. This became much
1807 harder with __builtin_va_start, since we don't have a tree for a
1808 call to __builtin_saveregs to fall back on. There was exactly one
1809 port (i860) that used this code, and I'm unconvinced it could actually
1810 handle the general case. So we no longer try to handle anything
1811 weird and make the backend absorb the evil. */
1813 error ("__builtin_saveregs not supported by this target");
1820 saveregs_value = val;
1822 /* Put the sequence after the NOTE that starts the function. If this
1823 is inside a SEQUENCE, make the outer-level insn chain current, so
1824 the code is placed at the start of the function. */
1825 push_topmost_sequence ();
1826 emit_insns_after (seq, get_insns ());
1827 pop_topmost_sequence ();
1832 /* __builtin_args_info (N) returns word N of the arg space info
1833 for the current function. The number and meanings of words
1834 is controlled by the definition of CUMULATIVE_ARGS. */
1837 expand_builtin_args_info (exp)
1840 tree arglist = TREE_OPERAND (exp, 1);
1841 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
1842 int *word_ptr = (int *) ¤t_function_args_info;
1844 /* These are used by the code below that is if 0'ed away */
1846 tree type, elts, result;
1849 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
1854 tree arg = TREE_VALUE (arglist);
1855 if (TREE_CODE (arg) != INTEGER_CST)
1856 error ("argument of `__builtin_args_info' must be constant");
1859 int wordnum = TREE_INT_CST_LOW (arg);
1861 if (wordnum < 0 || wordnum >= nwords || TREE_INT_CST_HIGH (arg))
1862 error ("argument of `__builtin_args_info' out of range");
1864 return GEN_INT (word_ptr[wordnum]);
1868 error ("missing argument in `__builtin_args_info'");
1873 for (i = 0; i < nwords; i++)
1874 elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
1876 type = build_array_type (integer_type_node,
1877 build_index_type (build_int_2 (nwords, 0)));
1878 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
1879 TREE_CONSTANT (result) = 1;
1880 TREE_STATIC (result) = 1;
1881 result = build1 (INDIRECT_REF, build_pointer_type (type), result);
1882 TREE_CONSTANT (result) = 1;
1883 return expand_expr (result, NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD);
1887 /* Expand ARGLIST, from a call to __builtin_next_arg. */
1889 expand_builtin_next_arg (arglist)
1892 tree fntype = TREE_TYPE (current_function_decl);
1894 if ((TYPE_ARG_TYPES (fntype) == 0
1895 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1897 && ! current_function_varargs)
1899 error ("`va_start' used in function with fixed args");
1905 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
1906 tree arg = TREE_VALUE (arglist);
1908 /* Strip off all nops for the sake of the comparison. This
1909 is not quite the same as STRIP_NOPS. It does more.
1910 We must also strip off INDIRECT_EXPR for C++ reference
1912 while (TREE_CODE (arg) == NOP_EXPR
1913 || TREE_CODE (arg) == CONVERT_EXPR
1914 || TREE_CODE (arg) == NON_LVALUE_EXPR
1915 || TREE_CODE (arg) == INDIRECT_REF)
1916 arg = TREE_OPERAND (arg, 0);
1917 if (arg != last_parm)
1918 warning ("second parameter of `va_start' not last named argument");
1920 else if (! current_function_varargs)
1921 /* Evidently an out of date version of <stdarg.h>; can't validate
1922 va_start's second argument, but can still work as intended. */
1923 warning ("`__builtin_next_arg' called without an argument");
1925 return expand_binop (Pmode, add_optab,
1926 current_function_internal_arg_pointer,
1927 current_function_arg_offset_rtx,
1928 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1931 /* Make it easier for the backends by protecting the valist argument
1932 from multiple evaluations. */
1935 stabilize_va_list (valist, needs_lvalue)
1939 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1941 if (TREE_SIDE_EFFECTS (valist))
1942 valist = save_expr (valist);
1944 /* For this case, the backends will be expecting a pointer to
1945 TREE_TYPE (va_list_type_node), but it's possible we've
1946 actually been given an array (an actual va_list_type_node).
1948 if (TREE_CODE (TREE_TYPE (valist)) == ARRAY_TYPE)
1950 tree p1 = build_pointer_type (TREE_TYPE (va_list_type_node));
1951 tree p2 = build_pointer_type (va_list_type_node);
1953 valist = build1 (ADDR_EXPR, p2, valist);
1954 valist = fold (build1 (NOP_EXPR, p1, valist));
1963 if (! TREE_SIDE_EFFECTS (valist))
1966 pt = build_pointer_type (va_list_type_node);
1967 valist = fold (build1 (ADDR_EXPR, pt, valist));
1968 TREE_SIDE_EFFECTS (valist) = 1;
1971 if (TREE_SIDE_EFFECTS (valist))
1972 valist = save_expr (valist);
1973 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
1980 /* The "standard" implementation of va_start: just assign `nextarg' to
1983 std_expand_builtin_va_start (stdarg_p, valist, nextarg)
1984 int stdarg_p ATTRIBUTE_UNUSED;
1991 nextarg = plus_constant (nextarg, -UNITS_PER_WORD);
1993 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
1994 make_tree (ptr_type_node, nextarg));
1995 TREE_SIDE_EFFECTS (t) = 1;
1997 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2000 /* Expand ARGLIST, which from a call to __builtin_stdarg_va_start or
2001 __builtin_varargs_va_start, depending on STDARG_P. */
2003 expand_builtin_va_start (stdarg_p, arglist)
2008 tree chain = arglist, valist;
2011 nextarg = expand_builtin_next_arg (chain = TREE_CHAIN (arglist));
2013 nextarg = expand_builtin_next_arg (NULL_TREE);
2015 if (TREE_CHAIN (chain))
2016 error ("too many arguments to function `va_start'");
2018 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
2020 #ifdef EXPAND_BUILTIN_VA_START
2021 EXPAND_BUILTIN_VA_START (stdarg_p, valist, nextarg);
2023 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
2029 /* The "standard" implementation of va_arg: read the value from the
2030 current (padded) address and increment by the (padded) size. */
2033 std_expand_builtin_va_arg (valist, type)
2037 HOST_WIDE_INT align;
2038 HOST_WIDE_INT rounded_size;
2041 /* Compute the rounded size of the type. */
2042 align = PARM_BOUNDARY / BITS_PER_UNIT;
2043 rounded_size = (((int_size_in_bytes (type) + align - 1) / align) * align);
2047 if (PAD_VARARGS_DOWN)
2049 /* Small args are padded downward. */
2052 adj = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_UNIT;
2053 if (rounded_size > align)
2056 addr_tree = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
2057 build_int_2 (rounded_size - adj, 0));
2060 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
2061 addr = copy_to_reg (addr);
2063 /* Compute new value for AP. */
2064 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
2065 build (PLUS_EXPR, TREE_TYPE (valist), valist,
2066 build_int_2 (rounded_size, 0)));
2067 TREE_SIDE_EFFECTS (t) = 1;
2068 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2073 /* Expand __builtin_va_arg, which is not really a builtin function, but
2074 a very special sort of operator. */
2077 expand_builtin_va_arg (valist, type)
2081 tree promoted_type, want_va_type, have_va_type;
2083 /* Verify that valist is of the proper type. */
2085 want_va_type = va_list_type_node;
2086 have_va_type = TREE_TYPE (valist);
2087 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
2089 /* If va_list is an array type, the argument may have decayed
2090 to a pointer type, e.g. by being passed to another function.
2091 In that case, unwrap both types so that we can compare the
2092 underlying records. */
2093 if (TREE_CODE (have_va_type) == ARRAY_TYPE
2094 || TREE_CODE (have_va_type) == POINTER_TYPE)
2096 want_va_type = TREE_TYPE (want_va_type);
2097 have_va_type = TREE_TYPE (have_va_type);
2100 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
2102 error ("first argument to `va_arg' not of type `va_list'");
2106 /* Generate a diagnostic for requesting data of a type that cannot
2107 be passed through `...' due to type promotion at the call site. */
2108 else if ((promoted_type = (*lang_type_promotes_to) (type)) != NULL_TREE)
2110 const char *name = "<anonymous type>", *pname = 0;
2111 static int gave_help;
2113 if (TYPE_NAME (type))
2115 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2116 name = IDENTIFIER_POINTER (TYPE_NAME (type));
2117 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2118 && DECL_NAME (TYPE_NAME (type)))
2119 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
2121 if (TYPE_NAME (promoted_type))
2123 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
2124 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
2125 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
2126 && DECL_NAME (TYPE_NAME (promoted_type)))
2127 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
2130 error ("`%s' is promoted to `%s' when passed through `...'", name, pname);
2134 error ("(so you should pass `%s' not `%s' to `va_arg')", pname, name);
2141 /* Make it easier for the backends by protecting the valist argument
2142 from multiple evaluations. */
2143 valist = stabilize_va_list (valist, 0);
2145 #ifdef EXPAND_BUILTIN_VA_ARG
2146 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
2148 addr = std_expand_builtin_va_arg (valist, type);
2152 result = gen_rtx_MEM (TYPE_MODE (type), addr);
2153 MEM_ALIAS_SET (result) = get_varargs_alias_set ();
2158 /* Expand ARGLIST, from a call to __builtin_va_end. */
2161 expand_builtin_va_end (arglist)
2164 tree valist = TREE_VALUE (arglist);
2166 #ifdef EXPAND_BUILTIN_VA_END
2167 valist = stabilize_va_list (valist, 0);
2168 EXPAND_BUILTIN_VA_END(arglist);
2170 /* Evaluate for side effects, if needed. I hate macros that don't
2172 if (TREE_SIDE_EFFECTS (valist))
2173 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
2179 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
2180 builtin rather than just as an assignment in stdarg.h because of the
2181 nastiness of array-type va_list types. */
2184 expand_builtin_va_copy (arglist)
2189 dst = TREE_VALUE (arglist);
2190 src = TREE_VALUE (TREE_CHAIN (arglist));
2192 dst = stabilize_va_list (dst, 1);
2193 src = stabilize_va_list (src, 0);
2195 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
2197 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
2198 TREE_SIDE_EFFECTS (t) = 1;
2199 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2203 rtx dstb, srcb, size;
2205 /* Evaluate to pointers. */
2206 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
2207 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
2208 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
2209 VOIDmode, EXPAND_NORMAL);
2211 /* "Dereference" to BLKmode memories. */
2212 dstb = gen_rtx_MEM (BLKmode, dstb);
2213 MEM_ALIAS_SET (dstb) = get_alias_set (TREE_TYPE (TREE_TYPE (dst)));
2214 srcb = gen_rtx_MEM (BLKmode, srcb);
2215 MEM_ALIAS_SET (srcb) = get_alias_set (TREE_TYPE (TREE_TYPE (src)));
2218 emit_block_move (dstb, srcb, size, TYPE_ALIGN (va_list_type_node));
2224 /* Expand a call to one of the builtin functions __builtin_frame_address or
2225 __builtin_return_address. */
2227 expand_builtin_frame_address (exp)
2230 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2231 tree arglist = TREE_OPERAND (exp, 1);
2233 /* The argument must be a nonnegative integer constant.
2234 It counts the number of frames to scan up the stack.
2235 The value is the return address saved in that frame. */
2237 /* Warning about missing arg was already issued. */
2239 else if (TREE_CODE (TREE_VALUE (arglist)) != INTEGER_CST
2240 || tree_int_cst_sgn (TREE_VALUE (arglist)) < 0)
2242 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2243 error ("invalid arg to `__builtin_frame_address'");
2245 error ("invalid arg to `__builtin_return_address'");
2250 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
2251 TREE_INT_CST_LOW (TREE_VALUE (arglist)),
2252 hard_frame_pointer_rtx);
2254 /* Some ports cannot access arbitrary stack frames. */
2257 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2258 warning ("unsupported arg to `__builtin_frame_address'");
2260 warning ("unsupported arg to `__builtin_return_address'");
2264 /* For __builtin_frame_address, return what we've got. */
2265 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2268 if (GET_CODE (tem) != REG
2269 && ! CONSTANT_P (tem))
2270 tem = copy_to_mode_reg (Pmode, tem);
2275 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
2276 we failed and the caller should emit a normal call, otherwise try to get
2277 the result in TARGET, if convenient. */
2279 expand_builtin_alloca (arglist, target)
2286 /* Arg could be non-integer if user redeclared this fcn wrong. */
2287 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2290 /* Compute the argument. */
2291 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
2293 /* Allocate the desired space. */
2294 return allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
2297 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
2298 Return 0 if a normal call should be emitted rather than expanding the
2299 function in-line. If convenient, the result should be placed in TARGET.
2300 SUBTARGET may be used as the target for computing one of EXP's operands. */
2302 expand_builtin_ffs (arglist, target, subtarget)
2304 rtx target, subtarget;
2308 /* Arg could be non-integer if user redeclared this fcn wrong. */
2309 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2312 /* Compute the argument. */
2313 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
2314 /* Compute ffs, into TARGET if possible.
2315 Set TARGET to wherever the result comes back. */
2316 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
2317 ffs_optab, op0, target, 1);
2323 /* If the string passed to fputs is a constant and is one character
2324 long, we attempt to transform this call into __builtin_fputc(). */
2326 expand_builtin_fputs (arglist, ignore)
2330 tree call_expr, len, stripped_string, newarglist;
2331 tree fn = built_in_decls[BUILT_IN_FPUTC];
2333 /* If the return value is used, or the replacement _DECL isn't
2334 initialized, don't do the transformation. */
2338 /* Verify the arguments in the original call. */
2340 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2341 || TREE_CHAIN (arglist) == 0
2342 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
2346 /* Get the length of the string passed to fputs. */
2347 len = c_strlen (TREE_VALUE (arglist));
2349 /* If the length != 1, punt. */
2350 if (len == 0 || compare_tree_int (len, 1))
2353 stripped_string = TREE_VALUE (arglist);
2354 STRIP_NOPS (stripped_string);
2355 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
2356 stripped_string = TREE_OPERAND (stripped_string, 0);
2358 /* New argument list transforming fputs(string, stream) to
2359 fputc(string[0], stream). */
2360 newarglist = build_tree_list (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)));
2362 tree_cons (NULL_TREE,
2363 build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0),
2366 #ifdef TEST_STDIO_OPTS
2367 warning ("Converted fputs(one-char-string, FILE*) -> fputc(char, FILE*)");
2370 call_expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (fn)), fn);
2371 call_expr = build (CALL_EXPR, TREE_TYPE (TREE_TYPE (fn)),
2372 call_expr, newarglist, NULL_TREE);
2373 TREE_SIDE_EFFECTS (call_expr) = 1;
2374 return expand_expr (call_expr, NULL_RTX, VOIDmode, 0);
2377 /* Expand a call to __builtin_expect. We return our argument and
2378 emit a NOTE_INSN_EXPECTED_VALUE note. */
2381 expand_builtin_expect (arglist, target)
2388 if (arglist == NULL_TREE
2389 || TREE_CHAIN (arglist) == NULL_TREE)
2391 exp = TREE_VALUE (arglist);
2392 c = TREE_VALUE (TREE_CHAIN (arglist));
2394 if (TREE_CODE (c) != INTEGER_CST)
2396 error ("second arg to `__builtin_expect' must be a constant");
2397 c = integer_zero_node;
2400 target = expand_expr (exp, target, VOIDmode, EXPAND_NORMAL);
2402 /* Don't bother with expected value notes for integral constants. */
2403 if (GET_CODE (target) != CONST_INT)
2405 /* We do need to force this into a register so that we can be
2406 moderately sure to be able to correctly interpret the branch
2408 target = force_reg (GET_MODE (target), target);
2410 rtx_c = expand_expr (c, NULL_RTX, GET_MODE (target), EXPAND_NORMAL);
2412 note = emit_note (NULL, NOTE_INSN_EXPECTED_VALUE);
2413 NOTE_EXPECTED_VALUE (note) = gen_rtx_EQ (VOIDmode, target, rtx_c);
2419 /* Expand an expression EXP that calls a built-in function,
2420 with result going to TARGET if that's convenient
2421 (and in mode MODE if that's convenient).
2422 SUBTARGET may be used as the target for computing one of EXP's operands.
2423 IGNORE is nonzero if the value is to be ignored. */
2426 expand_builtin (exp, target, subtarget, mode, ignore)
2430 enum machine_mode mode;
2433 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2434 tree arglist = TREE_OPERAND (exp, 1);
2435 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
2437 #ifdef MD_EXPAND_BUILTIN
2438 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
2439 return MD_EXPAND_BUILTIN (exp, target, subtarget, mode, ignore);
2442 /* When not optimizing, generate calls to library functions for a certain
2444 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl)
2445 && (fcode == BUILT_IN_SIN || fcode == BUILT_IN_COS
2446 || fcode == BUILT_IN_FSQRT || fcode == BUILT_IN_MEMSET
2447 || fcode == BUILT_IN_MEMCPY || fcode == BUILT_IN_MEMCMP
2448 || fcode == BUILT_IN_BCMP || fcode == BUILT_IN_BZERO
2449 || fcode == BUILT_IN_STRLEN || fcode == BUILT_IN_STRCPY
2450 || fcode == BUILT_IN_STRCMP || fcode == BUILT_IN_FFS
2451 || fcode == BUILT_IN_FPUTC || fcode == BUILT_IN_FPUTS))
2452 return expand_call (exp, target, ignore);
2459 /* build_function_call changes these into ABS_EXPR. */
2464 /* Treat these like sqrt, but only if the user asks for them. */
2465 if (! flag_fast_math)
2467 case BUILT_IN_FSQRT:
2468 target = expand_builtin_mathfn (exp, target, subtarget);
2476 case BUILT_IN_APPLY_ARGS:
2477 return expand_builtin_apply_args ();
2479 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
2480 FUNCTION with a copy of the parameters described by
2481 ARGUMENTS, and ARGSIZE. It returns a block of memory
2482 allocated on the stack into which is stored all the registers
2483 that might possibly be used for returning the result of a
2484 function. ARGUMENTS is the value returned by
2485 __builtin_apply_args. ARGSIZE is the number of bytes of
2486 arguments that must be copied. ??? How should this value be
2487 computed? We'll also need a safe worst case value for varargs
2489 case BUILT_IN_APPLY:
2491 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2492 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
2493 || TREE_CHAIN (arglist) == 0
2494 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
2495 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
2496 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
2504 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
2505 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
2507 return expand_builtin_apply (ops[0], ops[1], ops[2]);
2510 /* __builtin_return (RESULT) causes the function to return the
2511 value described by RESULT. RESULT is address of the block of
2512 memory returned by __builtin_apply. */
2513 case BUILT_IN_RETURN:
2515 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2516 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE)
2517 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
2518 NULL_RTX, VOIDmode, 0));
2521 case BUILT_IN_SAVEREGS:
2522 return expand_builtin_saveregs ();
2524 case BUILT_IN_ARGS_INFO:
2525 return expand_builtin_args_info (exp);
2527 /* Return the address of the first anonymous stack arg. */
2528 case BUILT_IN_NEXT_ARG:
2529 return expand_builtin_next_arg (arglist);
2531 case BUILT_IN_CLASSIFY_TYPE:
2532 return expand_builtin_classify_type (arglist);
2534 case BUILT_IN_CONSTANT_P:
2535 return expand_builtin_constant_p (exp);
2537 case BUILT_IN_FRAME_ADDRESS:
2538 case BUILT_IN_RETURN_ADDRESS:
2539 return expand_builtin_frame_address (exp);
2541 /* Returns the address of the area where the structure is returned.
2543 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
2545 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
2546 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
2549 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
2551 case BUILT_IN_ALLOCA:
2552 target = expand_builtin_alloca (arglist, target);
2558 target = expand_builtin_ffs (arglist, target, subtarget);
2563 case BUILT_IN_STRLEN:
2564 target = expand_builtin_strlen (exp, target, mode);
2569 case BUILT_IN_STRCPY:
2570 target = expand_builtin_strcpy (exp);
2575 case BUILT_IN_MEMCPY:
2576 target = expand_builtin_memcpy (arglist);
2581 case BUILT_IN_MEMSET:
2582 target = expand_builtin_memset (exp);
2587 case BUILT_IN_BZERO:
2588 target = expand_builtin_bzero (exp);
2593 /* These comparison functions need an instruction that returns an actual
2594 index. An ordinary compare that just sets the condition codes
2596 #ifdef HAVE_cmpstrsi
2597 case BUILT_IN_STRCMP:
2598 target = expand_builtin_strcmp (exp, target);
2604 case BUILT_IN_MEMCMP:
2605 target = expand_builtin_memcmp (exp, arglist, target);
2610 case BUILT_IN_STRCMP:
2612 case BUILT_IN_MEMCMP:
2616 case BUILT_IN_SETJMP:
2618 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2622 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2624 rtx lab = gen_label_rtx ();
2625 rtx ret = expand_builtin_setjmp (buf_addr, target, lab, lab);
2630 /* __builtin_longjmp is passed a pointer to an array of five words.
2631 It's similar to the C library longjmp function but works with
2632 __builtin_setjmp above. */
2633 case BUILT_IN_LONGJMP:
2634 if (arglist == 0 || TREE_CHAIN (arglist) == 0
2635 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2639 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2641 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
2642 NULL_RTX, VOIDmode, 0);
2644 if (value != const1_rtx)
2646 error ("__builtin_longjmp second argument must be 1");
2650 expand_builtin_longjmp (buf_addr, value);
2657 emit_insn (gen_trap ());
2660 error ("__builtin_trap not supported by this target");
2664 case BUILT_IN_FPUTC:
2667 case BUILT_IN_FPUTS:
2668 target = expand_builtin_fputs (arglist, ignore);
2673 /* Various hooks for the DWARF 2 __throw routine. */
2674 case BUILT_IN_UNWIND_INIT:
2675 expand_builtin_unwind_init ();
2677 case BUILT_IN_DWARF_CFA:
2678 return virtual_cfa_rtx;
2679 #ifdef DWARF2_UNWIND_INFO
2680 case BUILT_IN_DWARF_FP_REGNUM:
2681 return expand_builtin_dwarf_fp_regnum ();
2682 case BUILT_IN_INIT_DWARF_REG_SIZES:
2683 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
2686 case BUILT_IN_FROB_RETURN_ADDR:
2687 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
2688 case BUILT_IN_EXTRACT_RETURN_ADDR:
2689 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
2690 case BUILT_IN_EH_RETURN:
2691 expand_builtin_eh_return (TREE_VALUE (arglist),
2692 TREE_VALUE (TREE_CHAIN (arglist)),
2693 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))));
2695 case BUILT_IN_VARARGS_START:
2696 return expand_builtin_va_start (0, arglist);
2697 case BUILT_IN_STDARG_START:
2698 return expand_builtin_va_start (1, arglist);
2699 case BUILT_IN_VA_END:
2700 return expand_builtin_va_end (arglist);
2701 case BUILT_IN_VA_COPY:
2702 return expand_builtin_va_copy (arglist);
2703 case BUILT_IN_EXPECT:
2704 return expand_builtin_expect (arglist, target);
2706 default: /* just do library call, if unknown builtin */
2707 error ("built-in function `%s' not currently supported",
2708 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
2711 /* The switch statement above can drop through to cause the function
2712 to be called normally. */
2713 return expand_call (exp, target, ignore);