* decl.c (pop_cp_function_context): Don't call free on a NULL
[official-gcc.git] / gcc / builtins.c
blobffc84fd9ccd120c17511bbd520e634ce538ed99f
1 /* Expand builtin functions.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "machmode.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "obstack.h"
27 #include "flags.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "except.h"
31 #include "function.h"
32 #include "insn-flags.h"
33 #include "insn-codes.h"
34 #include "insn-config.h"
35 #include "expr.h"
36 #include "recog.h"
37 #include "output.h"
38 #include "typeclass.h"
39 #include "defaults.h"
40 #include "toplev.h"
41 #include "tm_p.h"
43 #define CALLED_AS_BUILT_IN(NODE) \
44 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
46 #define CEIL(x,y) (((x) + (y) - 1) / (y))
48 /* Register mappings for target machines without register windows. */
49 #ifndef INCOMING_REGNO
50 #define INCOMING_REGNO(OUT) (OUT)
51 #endif
52 #ifndef OUTGOING_REGNO
53 #define OUTGOING_REGNO(IN) (IN)
54 #endif
56 tree (*lang_type_promotes_to) PROTO((tree));
58 static int get_pointer_alignment PROTO((tree, unsigned));
59 static tree c_strlen PROTO((tree));
60 static rtx get_memory_rtx PROTO((tree));
61 static int apply_args_size PROTO((void));
62 static int apply_result_size PROTO((void));
63 static rtx result_vector PROTO((int, rtx));
64 static rtx expand_builtin_apply_args PROTO((void));
65 static rtx expand_builtin_apply_args_1 PROTO((void));
66 static rtx expand_builtin_apply PROTO((rtx, rtx, rtx));
67 static void expand_builtin_return PROTO((rtx));
68 static rtx expand_builtin_classify_type PROTO((tree));
69 static rtx expand_builtin_mathfn PROTO((tree, rtx, rtx));
70 static rtx expand_builtin_constant_p PROTO((tree));
71 static rtx expand_builtin_args_info PROTO((tree));
72 static rtx expand_builtin_next_arg PROTO((tree));
73 static rtx expand_builtin_va_start PROTO((int, tree));
74 static rtx expand_builtin_va_end PROTO((tree));
75 static rtx expand_builtin_va_copy PROTO((tree));
76 #ifdef HAVE_cmpstrsi
77 static rtx expand_builtin_memcmp PROTO((tree, tree, rtx));
78 static rtx expand_builtin_strcmp PROTO((tree, rtx));
79 #endif
80 static rtx expand_builtin_memcpy PROTO((tree));
81 static rtx expand_builtin_strcpy PROTO((tree));
82 static rtx expand_builtin_memset PROTO((tree));
83 static rtx expand_builtin_strlen PROTO((tree, rtx, enum machine_mode));
84 static rtx expand_builtin_alloca PROTO((tree, rtx));
85 static rtx expand_builtin_ffs PROTO((tree, rtx, rtx));
86 static rtx expand_builtin_frame_address PROTO((tree));
87 static tree stabilize_va_list PROTO((tree, int));
89 /* Return the alignment in bits of EXP, a pointer valued expression.
90 But don't return more than MAX_ALIGN no matter what.
91 The alignment returned is, by default, the alignment of the thing that
92 EXP points to (if it is not a POINTER_TYPE, 0 is returned).
94 Otherwise, look at the expression to see if we can do better, i.e., if the
95 expression is actually pointing at an object whose alignment is tighter. */
97 static int
98 get_pointer_alignment (exp, max_align)
99 tree exp;
100 unsigned max_align;
102 unsigned align, inner;
104 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
105 return 0;
107 align = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
108 align = MIN (align, max_align);
110 while (1)
112 switch (TREE_CODE (exp))
114 case NOP_EXPR:
115 case CONVERT_EXPR:
116 case NON_LVALUE_EXPR:
117 exp = TREE_OPERAND (exp, 0);
118 if (TREE_CODE (TREE_TYPE (exp)) != POINTER_TYPE)
119 return align;
120 inner = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (exp)));
121 align = MIN (inner, max_align);
122 break;
124 case PLUS_EXPR:
125 /* If sum of pointer + int, restrict our maximum alignment to that
126 imposed by the integer. If not, we can't do any better than
127 ALIGN. */
128 if (TREE_CODE (TREE_OPERAND (exp, 1)) != INTEGER_CST)
129 return align;
131 while (((TREE_INT_CST_LOW (TREE_OPERAND (exp, 1)) * BITS_PER_UNIT)
132 & (max_align - 1))
133 != 0)
134 max_align >>= 1;
136 exp = TREE_OPERAND (exp, 0);
137 break;
139 case ADDR_EXPR:
140 /* See what we are pointing at and look at its alignment. */
141 exp = TREE_OPERAND (exp, 0);
142 if (TREE_CODE (exp) == FUNCTION_DECL)
143 align = FUNCTION_BOUNDARY;
144 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd')
145 align = DECL_ALIGN (exp);
146 #ifdef CONSTANT_ALIGNMENT
147 else if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c')
148 align = CONSTANT_ALIGNMENT (exp, align);
149 #endif
150 return MIN (align, max_align);
152 default:
153 return align;
158 /* Compute the length of a C string. TREE_STRING_LENGTH is not the right
159 way, because it could contain a zero byte in the middle.
160 TREE_STRING_LENGTH is the size of the character array, not the string.
162 Unfortunately, string_constant can't access the values of const char
163 arrays with initializers, so neither can we do so here. */
165 static tree
166 c_strlen (src)
167 tree src;
169 tree offset_node;
170 int offset, max;
171 char *ptr;
173 src = string_constant (src, &offset_node);
174 if (src == 0)
175 return 0;
176 max = TREE_STRING_LENGTH (src);
177 ptr = TREE_STRING_POINTER (src);
178 if (offset_node && TREE_CODE (offset_node) != INTEGER_CST)
180 /* If the string has an internal zero byte (e.g., "foo\0bar"), we can't
181 compute the offset to the following null if we don't know where to
182 start searching for it. */
183 int i;
184 for (i = 0; i < max; i++)
185 if (ptr[i] == 0)
186 return 0;
187 /* We don't know the starting offset, but we do know that the string
188 has no internal zero bytes. We can assume that the offset falls
189 within the bounds of the string; otherwise, the programmer deserves
190 what he gets. Subtract the offset from the length of the string,
191 and return that. */
192 /* This would perhaps not be valid if we were dealing with named
193 arrays in addition to literal string constants. */
194 return size_binop (MINUS_EXPR, size_int (max), offset_node);
197 /* We have a known offset into the string. Start searching there for
198 a null character. */
199 if (offset_node == 0)
200 offset = 0;
201 else
203 /* Did we get a long long offset? If so, punt. */
204 if (TREE_INT_CST_HIGH (offset_node) != 0)
205 return 0;
206 offset = TREE_INT_CST_LOW (offset_node);
208 /* If the offset is known to be out of bounds, warn, and call strlen at
209 runtime. */
210 if (offset < 0 || offset > max)
212 warning ("offset outside bounds of constant string");
213 return 0;
215 /* Use strlen to search for the first zero byte. Since any strings
216 constructed with build_string will have nulls appended, we win even
217 if we get handed something like (char[4])"abcd".
219 Since OFFSET is our starting index into the string, no further
220 calculation is needed. */
221 return size_int (strlen (ptr + offset));
224 /* Given TEM, a pointer to a stack frame, follow the dynamic chain COUNT
225 times to get the address of either a higher stack frame, or a return
226 address located within it (depending on FNDECL_CODE). */
228 expand_builtin_return_addr (fndecl_code, count, tem)
229 enum built_in_function fndecl_code;
230 int count;
231 rtx tem;
233 int i;
235 /* Some machines need special handling before we can access
236 arbitrary frames. For example, on the sparc, we must first flush
237 all register windows to the stack. */
238 #ifdef SETUP_FRAME_ADDRESSES
239 if (count > 0)
240 SETUP_FRAME_ADDRESSES ();
241 #endif
243 /* On the sparc, the return address is not in the frame, it is in a
244 register. There is no way to access it off of the current frame
245 pointer, but it can be accessed off the previous frame pointer by
246 reading the value from the register window save area. */
247 #ifdef RETURN_ADDR_IN_PREVIOUS_FRAME
248 if (fndecl_code == BUILT_IN_RETURN_ADDRESS)
249 count--;
250 #endif
252 /* Scan back COUNT frames to the specified frame. */
253 for (i = 0; i < count; i++)
255 /* Assume the dynamic chain pointer is in the word that the
256 frame address points to, unless otherwise specified. */
257 #ifdef DYNAMIC_CHAIN_ADDRESS
258 tem = DYNAMIC_CHAIN_ADDRESS (tem);
259 #endif
260 tem = memory_address (Pmode, tem);
261 tem = copy_to_reg (gen_rtx_MEM (Pmode, tem));
264 /* For __builtin_frame_address, return what we've got. */
265 if (fndecl_code == BUILT_IN_FRAME_ADDRESS)
266 return tem;
268 /* For __builtin_return_address, Get the return address from that
269 frame. */
270 #ifdef RETURN_ADDR_RTX
271 tem = RETURN_ADDR_RTX (count, tem);
272 #else
273 tem = memory_address (Pmode,
274 plus_constant (tem, GET_MODE_SIZE (Pmode)));
275 tem = gen_rtx_MEM (Pmode, tem);
276 #endif
277 return tem;
280 /* __builtin_setjmp is passed a pointer to an array of five words (not
281 all will be used on all machines). It operates similarly to the C
282 library function of the same name, but is more efficient. Much of
283 the code below (and for longjmp) is copied from the handling of
284 non-local gotos.
286 NOTE: This is intended for use by GNAT and the exception handling
287 scheme in the compiler and will only work in the method used by
288 them. */
291 expand_builtin_setjmp (buf_addr, target, first_label, next_label)
292 rtx buf_addr;
293 rtx target;
294 rtx first_label, next_label;
296 rtx lab1 = gen_label_rtx ();
297 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
298 enum machine_mode value_mode;
299 rtx stack_save;
301 value_mode = TYPE_MODE (integer_type_node);
303 #ifdef POINTERS_EXTEND_UNSIGNED
304 buf_addr = convert_memory_address (Pmode, buf_addr);
305 #endif
307 buf_addr = force_reg (Pmode, force_operand (buf_addr, NULL_RTX));
309 if (target == 0 || GET_CODE (target) != REG
310 || REGNO (target) < FIRST_PSEUDO_REGISTER)
311 target = gen_reg_rtx (value_mode);
313 emit_queue ();
315 /* We store the frame pointer and the address of lab1 in the buffer
316 and use the rest of it for the stack save area, which is
317 machine-dependent. */
319 #ifndef BUILTIN_SETJMP_FRAME_VALUE
320 #define BUILTIN_SETJMP_FRAME_VALUE virtual_stack_vars_rtx
321 #endif
323 emit_move_insn (gen_rtx_MEM (Pmode, buf_addr),
324 BUILTIN_SETJMP_FRAME_VALUE);
325 emit_move_insn (validize_mem
326 (gen_rtx_MEM (Pmode,
327 plus_constant (buf_addr,
328 GET_MODE_SIZE (Pmode)))),
329 force_reg (Pmode, gen_rtx_LABEL_REF (Pmode, lab1)));
331 stack_save = gen_rtx_MEM (sa_mode,
332 plus_constant (buf_addr,
333 2 * GET_MODE_SIZE (Pmode)));
334 emit_stack_save (SAVE_NONLOCAL, &stack_save, NULL_RTX);
336 /* If there is further processing to do, do it. */
337 #ifdef HAVE_builtin_setjmp_setup
338 if (HAVE_builtin_setjmp_setup)
339 emit_insn (gen_builtin_setjmp_setup (buf_addr));
340 #endif
342 /* Set TARGET to zero and branch to the first-time-through label. */
343 emit_move_insn (target, const0_rtx);
344 emit_jump_insn (gen_jump (first_label));
345 emit_barrier ();
346 emit_label (lab1);
348 /* Tell flow about the strange goings on. Putting `lab1' on
349 `nonlocal_goto_handler_labels' to indicates that function
350 calls may traverse the arc back to this label. */
352 current_function_has_nonlocal_label = 1;
353 nonlocal_goto_handler_labels =
354 gen_rtx_EXPR_LIST (VOIDmode, lab1, nonlocal_goto_handler_labels);
356 /* Clobber the FP when we get here, so we have to make sure it's
357 marked as used by this function. */
358 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
360 /* Mark the static chain as clobbered here so life information
361 doesn't get messed up for it. */
362 emit_insn (gen_rtx_CLOBBER (VOIDmode, static_chain_rtx));
364 /* Now put in the code to restore the frame pointer, and argument
365 pointer, if needed. The code below is from expand_end_bindings
366 in stmt.c; see detailed documentation there. */
367 #ifdef HAVE_nonlocal_goto
368 if (! HAVE_nonlocal_goto)
369 #endif
370 emit_move_insn (virtual_stack_vars_rtx, hard_frame_pointer_rtx);
372 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
373 if (fixed_regs[ARG_POINTER_REGNUM])
375 #ifdef ELIMINABLE_REGS
376 size_t i;
377 static struct elims {int from, to;} elim_regs[] = ELIMINABLE_REGS;
379 for (i = 0; i < sizeof elim_regs / sizeof elim_regs[0]; i++)
380 if (elim_regs[i].from == ARG_POINTER_REGNUM
381 && elim_regs[i].to == HARD_FRAME_POINTER_REGNUM)
382 break;
384 if (i == sizeof elim_regs / sizeof elim_regs [0])
385 #endif
387 /* Now restore our arg pointer from the address at which it
388 was saved in our stack frame.
389 If there hasn't be space allocated for it yet, make
390 some now. */
391 if (arg_pointer_save_area == 0)
392 arg_pointer_save_area
393 = assign_stack_local (Pmode, GET_MODE_SIZE (Pmode), 0);
394 emit_move_insn (virtual_incoming_args_rtx,
395 copy_to_reg (arg_pointer_save_area));
398 #endif
400 #ifdef HAVE_builtin_setjmp_receiver
401 if (HAVE_builtin_setjmp_receiver)
402 emit_insn (gen_builtin_setjmp_receiver (lab1));
403 else
404 #endif
405 #ifdef HAVE_nonlocal_goto_receiver
406 if (HAVE_nonlocal_goto_receiver)
407 emit_insn (gen_nonlocal_goto_receiver ());
408 else
409 #endif
411 ; /* Nothing */
414 /* Set TARGET, and branch to the next-time-through label. */
415 emit_move_insn (target, const1_rtx);
416 emit_jump_insn (gen_jump (next_label));
417 emit_barrier ();
419 return target;
422 /* __builtin_longjmp is passed a pointer to an array of five words (not
423 all will be used on all machines). It operates similarly to the C
424 library function of the same name, but is more efficient. Much of
425 the code below is copied from the handling of non-local gotos.
427 NOTE: This is intended for use by GNAT and the exception handling
428 scheme in the compiler and will only work in the method used by
429 them. */
431 void
432 expand_builtin_longjmp (buf_addr, value)
433 rtx buf_addr, value;
435 rtx fp, lab, stack;
436 enum machine_mode sa_mode = STACK_SAVEAREA_MODE (SAVE_NONLOCAL);
438 #ifdef POINTERS_EXTEND_UNSIGNED
439 buf_addr = convert_memory_address (Pmode, buf_addr);
440 #endif
441 buf_addr = force_reg (Pmode, buf_addr);
443 /* We used to store value in static_chain_rtx, but that fails if pointers
444 are smaller than integers. We instead require that the user must pass
445 a second argument of 1, because that is what builtin_setjmp will
446 return. This also makes EH slightly more efficient, since we are no
447 longer copying around a value that we don't care about. */
448 if (value != const1_rtx)
449 abort ();
451 #ifdef HAVE_builtin_longjmp
452 if (HAVE_builtin_longjmp)
453 emit_insn (gen_builtin_longjmp (buf_addr));
454 else
455 #endif
457 fp = gen_rtx_MEM (Pmode, buf_addr);
458 lab = gen_rtx_MEM (Pmode, plus_constant (buf_addr,
459 GET_MODE_SIZE (Pmode)));
461 stack = gen_rtx_MEM (sa_mode, plus_constant (buf_addr,
462 2 * GET_MODE_SIZE (Pmode)));
464 /* Pick up FP, label, and SP from the block and jump. This code is
465 from expand_goto in stmt.c; see there for detailed comments. */
466 #if HAVE_nonlocal_goto
467 if (HAVE_nonlocal_goto)
468 /* We have to pass a value to the nonlocal_goto pattern that will
469 get copied into the static_chain pointer, but it does not matter
470 what that value is, because builtin_setjmp does not use it. */
471 emit_insn (gen_nonlocal_goto (value, fp, stack, lab));
472 else
473 #endif
475 lab = copy_to_reg (lab);
477 emit_move_insn (hard_frame_pointer_rtx, fp);
478 emit_stack_restore (SAVE_NONLOCAL, stack, NULL_RTX);
480 emit_insn (gen_rtx_USE (VOIDmode, hard_frame_pointer_rtx));
481 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
482 emit_indirect_jump (lab);
487 /* Get a MEM rtx for expression EXP which can be used in a string instruction
488 (cmpstrsi, movstrsi, ..). */
489 static rtx
490 get_memory_rtx (exp)
491 tree exp;
493 rtx mem;
494 int is_aggregate;
496 mem = gen_rtx_MEM (BLKmode,
497 memory_address (BLKmode,
498 expand_expr (exp, NULL_RTX,
499 ptr_mode, EXPAND_SUM)));
501 RTX_UNCHANGING_P (mem) = TREE_READONLY (exp);
503 /* Figure out the type of the object pointed to. Set MEM_IN_STRUCT_P
504 if the value is the address of a structure or if the expression is
505 cast to a pointer to structure type. */
506 is_aggregate = 0;
508 while (TREE_CODE (exp) == NOP_EXPR)
510 tree cast_type = TREE_TYPE (exp);
511 if (TREE_CODE (cast_type) == POINTER_TYPE
512 && AGGREGATE_TYPE_P (TREE_TYPE (cast_type)))
514 is_aggregate = 1;
515 break;
517 exp = TREE_OPERAND (exp, 0);
520 if (is_aggregate == 0)
522 tree type;
524 if (TREE_CODE (exp) == ADDR_EXPR)
525 /* If this is the address of an object, check whether the
526 object is an array. */
527 type = TREE_TYPE (TREE_OPERAND (exp, 0));
528 else
529 type = TREE_TYPE (TREE_TYPE (exp));
530 is_aggregate = AGGREGATE_TYPE_P (type);
533 MEM_SET_IN_STRUCT_P (mem, is_aggregate);
534 return mem;
537 /* Built-in functions to perform an untyped call and return. */
539 /* For each register that may be used for calling a function, this
540 gives a mode used to copy the register's value. VOIDmode indicates
541 the register is not used for calling a function. If the machine
542 has register windows, this gives only the outbound registers.
543 INCOMING_REGNO gives the corresponding inbound register. */
544 static enum machine_mode apply_args_mode[FIRST_PSEUDO_REGISTER];
546 /* For each register that may be used for returning values, this gives
547 a mode used to copy the register's value. VOIDmode indicates the
548 register is not used for returning values. If the machine has
549 register windows, this gives only the outbound registers.
550 INCOMING_REGNO gives the corresponding inbound register. */
551 static enum machine_mode apply_result_mode[FIRST_PSEUDO_REGISTER];
553 /* For each register that may be used for calling a function, this
554 gives the offset of that register into the block returned by
555 __builtin_apply_args. 0 indicates that the register is not
556 used for calling a function. */
557 static int apply_args_reg_offset[FIRST_PSEUDO_REGISTER];
559 /* Return the offset of register REGNO into the block returned by
560 __builtin_apply_args. This is not declared static, since it is
561 needed in objc-act.c. */
563 int
564 apply_args_register_offset (regno)
565 int regno;
567 apply_args_size ();
569 /* Arguments are always put in outgoing registers (in the argument
570 block) if such make sense. */
571 #ifdef OUTGOING_REGNO
572 regno = OUTGOING_REGNO(regno);
573 #endif
574 return apply_args_reg_offset[regno];
577 /* Return the size required for the block returned by __builtin_apply_args,
578 and initialize apply_args_mode. */
580 static int
581 apply_args_size ()
583 static int size = -1;
584 int align, regno;
585 enum machine_mode mode;
587 /* The values computed by this function never change. */
588 if (size < 0)
590 /* The first value is the incoming arg-pointer. */
591 size = GET_MODE_SIZE (Pmode);
593 /* The second value is the structure value address unless this is
594 passed as an "invisible" first argument. */
595 if (struct_value_rtx)
596 size += GET_MODE_SIZE (Pmode);
598 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
599 if (FUNCTION_ARG_REGNO_P (regno))
601 /* Search for the proper mode for copying this register's
602 value. I'm not sure this is right, but it works so far. */
603 enum machine_mode best_mode = VOIDmode;
605 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
606 mode != VOIDmode;
607 mode = GET_MODE_WIDER_MODE (mode))
608 if (HARD_REGNO_MODE_OK (regno, mode)
609 && HARD_REGNO_NREGS (regno, mode) == 1)
610 best_mode = mode;
612 if (best_mode == VOIDmode)
613 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
614 mode != VOIDmode;
615 mode = GET_MODE_WIDER_MODE (mode))
616 if (HARD_REGNO_MODE_OK (regno, mode)
617 && (mov_optab->handlers[(int) mode].insn_code
618 != CODE_FOR_nothing))
619 best_mode = mode;
621 mode = best_mode;
622 if (mode == VOIDmode)
623 abort ();
625 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
626 if (size % align != 0)
627 size = CEIL (size, align) * align;
628 apply_args_reg_offset[regno] = size;
629 size += GET_MODE_SIZE (mode);
630 apply_args_mode[regno] = mode;
632 else
634 apply_args_mode[regno] = VOIDmode;
635 apply_args_reg_offset[regno] = 0;
638 return size;
641 /* Return the size required for the block returned by __builtin_apply,
642 and initialize apply_result_mode. */
644 static int
645 apply_result_size ()
647 static int size = -1;
648 int align, regno;
649 enum machine_mode mode;
651 /* The values computed by this function never change. */
652 if (size < 0)
654 size = 0;
656 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
657 if (FUNCTION_VALUE_REGNO_P (regno))
659 /* Search for the proper mode for copying this register's
660 value. I'm not sure this is right, but it works so far. */
661 enum machine_mode best_mode = VOIDmode;
663 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
664 mode != TImode;
665 mode = GET_MODE_WIDER_MODE (mode))
666 if (HARD_REGNO_MODE_OK (regno, mode))
667 best_mode = mode;
669 if (best_mode == VOIDmode)
670 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
671 mode != VOIDmode;
672 mode = GET_MODE_WIDER_MODE (mode))
673 if (HARD_REGNO_MODE_OK (regno, mode)
674 && (mov_optab->handlers[(int) mode].insn_code
675 != CODE_FOR_nothing))
676 best_mode = mode;
678 mode = best_mode;
679 if (mode == VOIDmode)
680 abort ();
682 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
683 if (size % align != 0)
684 size = CEIL (size, align) * align;
685 size += GET_MODE_SIZE (mode);
686 apply_result_mode[regno] = mode;
688 else
689 apply_result_mode[regno] = VOIDmode;
691 /* Allow targets that use untyped_call and untyped_return to override
692 the size so that machine-specific information can be stored here. */
693 #ifdef APPLY_RESULT_SIZE
694 size = APPLY_RESULT_SIZE;
695 #endif
697 return size;
700 #if defined (HAVE_untyped_call) || defined (HAVE_untyped_return)
701 /* Create a vector describing the result block RESULT. If SAVEP is true,
702 the result block is used to save the values; otherwise it is used to
703 restore the values. */
705 static rtx
706 result_vector (savep, result)
707 int savep;
708 rtx result;
710 int regno, size, align, nelts;
711 enum machine_mode mode;
712 rtx reg, mem;
713 rtx *savevec = (rtx *) alloca (FIRST_PSEUDO_REGISTER * sizeof (rtx));
715 size = nelts = 0;
716 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
717 if ((mode = apply_result_mode[regno]) != VOIDmode)
719 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
720 if (size % align != 0)
721 size = CEIL (size, align) * align;
722 reg = gen_rtx_REG (mode, savep ? regno : INCOMING_REGNO (regno));
723 mem = change_address (result, mode,
724 plus_constant (XEXP (result, 0), size));
725 savevec[nelts++] = (savep
726 ? gen_rtx_SET (VOIDmode, mem, reg)
727 : gen_rtx_SET (VOIDmode, reg, mem));
728 size += GET_MODE_SIZE (mode);
730 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelts, savevec));
732 #endif /* HAVE_untyped_call or HAVE_untyped_return */
734 /* Save the state required to perform an untyped call with the same
735 arguments as were passed to the current function. */
737 static rtx
738 expand_builtin_apply_args_1 ()
740 rtx registers;
741 int size, align, regno;
742 enum machine_mode mode;
744 /* Create a block where the arg-pointer, structure value address,
745 and argument registers can be saved. */
746 registers = assign_stack_local (BLKmode, apply_args_size (), -1);
748 /* Walk past the arg-pointer and structure value address. */
749 size = GET_MODE_SIZE (Pmode);
750 if (struct_value_rtx)
751 size += GET_MODE_SIZE (Pmode);
753 /* Save each register used in calling a function to the block. */
754 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
755 if ((mode = apply_args_mode[regno]) != VOIDmode)
757 rtx tem;
759 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
760 if (size % align != 0)
761 size = CEIL (size, align) * align;
763 tem = gen_rtx_REG (mode, INCOMING_REGNO (regno));
765 emit_move_insn (change_address (registers, mode,
766 plus_constant (XEXP (registers, 0),
767 size)),
768 tem);
769 size += GET_MODE_SIZE (mode);
772 /* Save the arg pointer to the block. */
773 emit_move_insn (change_address (registers, Pmode, XEXP (registers, 0)),
774 copy_to_reg (virtual_incoming_args_rtx));
775 size = GET_MODE_SIZE (Pmode);
777 /* Save the structure value address unless this is passed as an
778 "invisible" first argument. */
779 if (struct_value_incoming_rtx)
781 emit_move_insn (change_address (registers, Pmode,
782 plus_constant (XEXP (registers, 0),
783 size)),
784 copy_to_reg (struct_value_incoming_rtx));
785 size += GET_MODE_SIZE (Pmode);
788 /* Return the address of the block. */
789 return copy_addr_to_reg (XEXP (registers, 0));
792 /* __builtin_apply_args returns block of memory allocated on
793 the stack into which is stored the arg pointer, structure
794 value address, static chain, and all the registers that might
795 possibly be used in performing a function call. The code is
796 moved to the start of the function so the incoming values are
797 saved. */
798 static rtx
799 expand_builtin_apply_args ()
801 /* Don't do __builtin_apply_args more than once in a function.
802 Save the result of the first call and reuse it. */
803 if (apply_args_value != 0)
804 return apply_args_value;
806 /* When this function is called, it means that registers must be
807 saved on entry to this function. So we migrate the
808 call to the first insn of this function. */
809 rtx temp;
810 rtx seq;
812 start_sequence ();
813 temp = expand_builtin_apply_args_1 ();
814 seq = get_insns ();
815 end_sequence ();
817 apply_args_value = temp;
819 /* Put the sequence after the NOTE that starts the function.
820 If this is inside a SEQUENCE, make the outer-level insn
821 chain current, so the code is placed at the start of the
822 function. */
823 push_topmost_sequence ();
824 emit_insns_before (seq, NEXT_INSN (get_insns ()));
825 pop_topmost_sequence ();
826 return temp;
830 /* Perform an untyped call and save the state required to perform an
831 untyped return of whatever value was returned by the given function. */
833 static rtx
834 expand_builtin_apply (function, arguments, argsize)
835 rtx function, arguments, argsize;
837 int size, align, regno;
838 enum machine_mode mode;
839 rtx incoming_args, result, reg, dest, call_insn;
840 rtx old_stack_level = 0;
841 rtx call_fusage = 0;
843 /* Create a block where the return registers can be saved. */
844 result = assign_stack_local (BLKmode, apply_result_size (), -1);
846 /* ??? The argsize value should be adjusted here. */
848 /* Fetch the arg pointer from the ARGUMENTS block. */
849 incoming_args = gen_reg_rtx (Pmode);
850 emit_move_insn (incoming_args,
851 gen_rtx_MEM (Pmode, arguments));
852 #ifndef STACK_GROWS_DOWNWARD
853 incoming_args = expand_binop (Pmode, sub_optab, incoming_args, argsize,
854 incoming_args, 0, OPTAB_LIB_WIDEN);
855 #endif
857 /* Perform postincrements before actually calling the function. */
858 emit_queue ();
860 /* Push a new argument block and copy the arguments. */
861 do_pending_stack_adjust ();
863 /* Save the stack with nonlocal if available */
864 #ifdef HAVE_save_stack_nonlocal
865 if (HAVE_save_stack_nonlocal)
866 emit_stack_save (SAVE_NONLOCAL, &old_stack_level, NULL_RTX);
867 else
868 #endif
869 emit_stack_save (SAVE_BLOCK, &old_stack_level, NULL_RTX);
871 /* Push a block of memory onto the stack to store the memory arguments.
872 Save the address in a register, and copy the memory arguments. ??? I
873 haven't figured out how the calling convention macros effect this,
874 but it's likely that the source and/or destination addresses in
875 the block copy will need updating in machine specific ways. */
876 dest = allocate_dynamic_stack_space (argsize, 0, 0);
877 emit_block_move (gen_rtx_MEM (BLKmode, dest),
878 gen_rtx_MEM (BLKmode, incoming_args),
879 argsize,
880 PARM_BOUNDARY / BITS_PER_UNIT);
882 /* Refer to the argument block. */
883 apply_args_size ();
884 arguments = gen_rtx_MEM (BLKmode, arguments);
886 /* Walk past the arg-pointer and structure value address. */
887 size = GET_MODE_SIZE (Pmode);
888 if (struct_value_rtx)
889 size += GET_MODE_SIZE (Pmode);
891 /* Restore each of the registers previously saved. Make USE insns
892 for each of these registers for use in making the call. */
893 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
894 if ((mode = apply_args_mode[regno]) != VOIDmode)
896 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
897 if (size % align != 0)
898 size = CEIL (size, align) * align;
899 reg = gen_rtx_REG (mode, regno);
900 emit_move_insn (reg,
901 change_address (arguments, mode,
902 plus_constant (XEXP (arguments, 0),
903 size)));
905 use_reg (&call_fusage, reg);
906 size += GET_MODE_SIZE (mode);
909 /* Restore the structure value address unless this is passed as an
910 "invisible" first argument. */
911 size = GET_MODE_SIZE (Pmode);
912 if (struct_value_rtx)
914 rtx value = gen_reg_rtx (Pmode);
915 emit_move_insn (value,
916 change_address (arguments, Pmode,
917 plus_constant (XEXP (arguments, 0),
918 size)));
919 emit_move_insn (struct_value_rtx, value);
920 if (GET_CODE (struct_value_rtx) == REG)
921 use_reg (&call_fusage, struct_value_rtx);
922 size += GET_MODE_SIZE (Pmode);
925 /* All arguments and registers used for the call are set up by now! */
926 function = prepare_call_address (function, NULL_TREE, &call_fusage, 0);
928 /* Ensure address is valid. SYMBOL_REF is already valid, so no need,
929 and we don't want to load it into a register as an optimization,
930 because prepare_call_address already did it if it should be done. */
931 if (GET_CODE (function) != SYMBOL_REF)
932 function = memory_address (FUNCTION_MODE, function);
934 /* Generate the actual call instruction and save the return value. */
935 #ifdef HAVE_untyped_call
936 if (HAVE_untyped_call)
937 emit_call_insn (gen_untyped_call (gen_rtx_MEM (FUNCTION_MODE, function),
938 result, result_vector (1, result)));
939 else
940 #endif
941 #ifdef HAVE_call_value
942 if (HAVE_call_value)
944 rtx valreg = 0;
946 /* Locate the unique return register. It is not possible to
947 express a call that sets more than one return register using
948 call_value; use untyped_call for that. In fact, untyped_call
949 only needs to save the return registers in the given block. */
950 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
951 if ((mode = apply_result_mode[regno]) != VOIDmode)
953 if (valreg)
954 abort (); /* HAVE_untyped_call required. */
955 valreg = gen_rtx_REG (mode, regno);
958 emit_call_insn (gen_call_value (valreg,
959 gen_rtx_MEM (FUNCTION_MODE, function),
960 const0_rtx, NULL_RTX, const0_rtx));
962 emit_move_insn (change_address (result, GET_MODE (valreg),
963 XEXP (result, 0)),
964 valreg);
966 else
967 #endif
968 abort ();
970 /* Find the CALL insn we just emitted. */
971 for (call_insn = get_last_insn ();
972 call_insn && GET_CODE (call_insn) != CALL_INSN;
973 call_insn = PREV_INSN (call_insn))
976 if (! call_insn)
977 abort ();
979 /* Put the register usage information on the CALL. If there is already
980 some usage information, put ours at the end. */
981 if (CALL_INSN_FUNCTION_USAGE (call_insn))
983 rtx link;
985 for (link = CALL_INSN_FUNCTION_USAGE (call_insn); XEXP (link, 1) != 0;
986 link = XEXP (link, 1))
989 XEXP (link, 1) = call_fusage;
991 else
992 CALL_INSN_FUNCTION_USAGE (call_insn) = call_fusage;
994 /* Restore the stack. */
995 #ifdef HAVE_save_stack_nonlocal
996 if (HAVE_save_stack_nonlocal)
997 emit_stack_restore (SAVE_NONLOCAL, old_stack_level, NULL_RTX);
998 else
999 #endif
1000 emit_stack_restore (SAVE_BLOCK, old_stack_level, NULL_RTX);
1002 /* Return the address of the result block. */
1003 return copy_addr_to_reg (XEXP (result, 0));
1006 /* Perform an untyped return. */
1008 static void
1009 expand_builtin_return (result)
1010 rtx result;
1012 int size, align, regno;
1013 enum machine_mode mode;
1014 rtx reg;
1015 rtx call_fusage = 0;
1017 apply_result_size ();
1018 result = gen_rtx_MEM (BLKmode, result);
1020 #ifdef HAVE_untyped_return
1021 if (HAVE_untyped_return)
1023 emit_jump_insn (gen_untyped_return (result, result_vector (0, result)));
1024 emit_barrier ();
1025 return;
1027 #endif
1029 /* Restore the return value and note that each value is used. */
1030 size = 0;
1031 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1032 if ((mode = apply_result_mode[regno]) != VOIDmode)
1034 align = GET_MODE_ALIGNMENT (mode) / BITS_PER_UNIT;
1035 if (size % align != 0)
1036 size = CEIL (size, align) * align;
1037 reg = gen_rtx_REG (mode, INCOMING_REGNO (regno));
1038 emit_move_insn (reg,
1039 change_address (result, mode,
1040 plus_constant (XEXP (result, 0),
1041 size)));
1043 push_to_sequence (call_fusage);
1044 emit_insn (gen_rtx_USE (VOIDmode, reg));
1045 call_fusage = get_insns ();
1046 end_sequence ();
1047 size += GET_MODE_SIZE (mode);
1050 /* Put the USE insns before the return. */
1051 emit_insns (call_fusage);
1053 /* Return whatever values was restored by jumping directly to the end
1054 of the function. */
1055 expand_null_return ();
1058 /* Expand a call to __builtin_classify_type with arguments found in
1059 ARGLIST. */
1060 static rtx
1061 expand_builtin_classify_type (arglist)
1062 tree arglist;
1064 if (arglist != 0)
1066 tree type = TREE_TYPE (TREE_VALUE (arglist));
1067 enum tree_code code = TREE_CODE (type);
1068 if (code == VOID_TYPE)
1069 return GEN_INT (void_type_class);
1070 if (code == INTEGER_TYPE)
1071 return GEN_INT (integer_type_class);
1072 if (code == CHAR_TYPE)
1073 return GEN_INT (char_type_class);
1074 if (code == ENUMERAL_TYPE)
1075 return GEN_INT (enumeral_type_class);
1076 if (code == BOOLEAN_TYPE)
1077 return GEN_INT (boolean_type_class);
1078 if (code == POINTER_TYPE)
1079 return GEN_INT (pointer_type_class);
1080 if (code == REFERENCE_TYPE)
1081 return GEN_INT (reference_type_class);
1082 if (code == OFFSET_TYPE)
1083 return GEN_INT (offset_type_class);
1084 if (code == REAL_TYPE)
1085 return GEN_INT (real_type_class);
1086 if (code == COMPLEX_TYPE)
1087 return GEN_INT (complex_type_class);
1088 if (code == FUNCTION_TYPE)
1089 return GEN_INT (function_type_class);
1090 if (code == METHOD_TYPE)
1091 return GEN_INT (method_type_class);
1092 if (code == RECORD_TYPE)
1093 return GEN_INT (record_type_class);
1094 if (code == UNION_TYPE || code == QUAL_UNION_TYPE)
1095 return GEN_INT (union_type_class);
1096 if (code == ARRAY_TYPE)
1098 if (TYPE_STRING_FLAG (type))
1099 return GEN_INT (string_type_class);
1100 else
1101 return GEN_INT (array_type_class);
1103 if (code == SET_TYPE)
1104 return GEN_INT (set_type_class);
1105 if (code == FILE_TYPE)
1106 return GEN_INT (file_type_class);
1107 if (code == LANG_TYPE)
1108 return GEN_INT (lang_type_class);
1110 return GEN_INT (no_type_class);
1113 /* Expand expression EXP, which is a call to __builtin_constant_p. */
1114 static rtx
1115 expand_builtin_constant_p (exp)
1116 tree exp;
1118 tree arglist = TREE_OPERAND (exp, 1);
1119 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1121 if (arglist == 0)
1122 return const0_rtx;
1123 else
1125 tree arg = TREE_VALUE (arglist);
1126 rtx tmp;
1128 /* We return 1 for a numeric type that's known to be a constant
1129 value at compile-time or for an aggregate type that's a
1130 literal constant. */
1131 STRIP_NOPS (arg);
1133 /* If we know this is a constant, emit the constant of one. */
1134 if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'c'
1135 || (TREE_CODE (arg) == CONSTRUCTOR
1136 && TREE_CONSTANT (arg))
1137 || (TREE_CODE (arg) == ADDR_EXPR
1138 && TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST))
1139 return const1_rtx;
1141 /* If we aren't going to be running CSE or this expression
1142 has side effects, show we don't know it to be a constant.
1143 Likewise if it's a pointer or aggregate type since in those
1144 case we only want literals, since those are only optimized
1145 when generating RTL, not later. */
1146 if (TREE_SIDE_EFFECTS (arg) || cse_not_expected
1147 || AGGREGATE_TYPE_P (TREE_TYPE (arg))
1148 || POINTER_TYPE_P (TREE_TYPE (arg)))
1149 return const0_rtx;
1151 /* Otherwise, emit (constant_p_rtx (ARG)) and let CSE get a
1152 chance to see if it can deduce whether ARG is constant. */
1154 tmp = expand_expr (arg, NULL_RTX, VOIDmode, 0);
1155 tmp = gen_rtx_CONSTANT_P_RTX (value_mode, tmp);
1156 return tmp;
1160 /* Expand a call to one of the builtin math functions (sin, cos, or sqrt).
1161 Return 0 if a normal call should be emitted rather than expanding the
1162 function in-line. EXP is the expression that is a call to the builtin
1163 function; if convenient, the result should be placed in TARGET.
1164 SUBTARGET may be used as the target for computing one of EXP's operands. */
1165 static rtx
1166 expand_builtin_mathfn (exp, target, subtarget)
1167 tree exp;
1168 rtx target, subtarget;
1170 optab builtin_optab;
1171 rtx op0, insns;
1172 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1173 tree arglist = TREE_OPERAND (exp, 1);
1175 if (arglist == 0
1176 /* Arg could be wrong type if user redeclared this fcn wrong. */
1177 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != REAL_TYPE)
1178 return 0;
1180 /* Stabilize and compute the argument. */
1181 if (TREE_CODE (TREE_VALUE (arglist)) != VAR_DECL
1182 && TREE_CODE (TREE_VALUE (arglist)) != PARM_DECL)
1184 exp = copy_node (exp);
1185 arglist = copy_node (arglist);
1186 TREE_OPERAND (exp, 1) = arglist;
1187 TREE_VALUE (arglist) = save_expr (TREE_VALUE (arglist));
1189 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
1191 /* Make a suitable register to place result in. */
1192 target = gen_reg_rtx (TYPE_MODE (TREE_TYPE (exp)));
1194 emit_queue ();
1195 start_sequence ();
1197 switch (DECL_FUNCTION_CODE (fndecl))
1199 case BUILT_IN_SIN:
1200 builtin_optab = sin_optab; break;
1201 case BUILT_IN_COS:
1202 builtin_optab = cos_optab; break;
1203 case BUILT_IN_FSQRT:
1204 builtin_optab = sqrt_optab; break;
1205 default:
1206 abort ();
1209 /* Compute into TARGET.
1210 Set TARGET to wherever the result comes back. */
1211 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
1212 builtin_optab, op0, target, 0);
1214 /* If we were unable to expand via the builtin, stop the
1215 sequence (without outputting the insns) and return 0, causing
1216 a call to the library function. */
1217 if (target == 0)
1219 end_sequence ();
1220 return 0;
1223 /* Check the results by default. But if flag_fast_math is turned on,
1224 then assume sqrt will always be called with valid arguments. */
1226 if (flag_errno_math && ! flag_fast_math)
1228 rtx lab1;
1230 /* Don't define the builtin FP instructions
1231 if your machine is not IEEE. */
1232 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
1233 abort ();
1235 lab1 = gen_label_rtx ();
1237 /* Test the result; if it is NaN, set errno=EDOM because
1238 the argument was not in the domain. */
1239 emit_cmp_and_jump_insns (target, target, EQ, 0, GET_MODE (target),
1240 0, 0, lab1);
1242 #ifdef TARGET_EDOM
1244 #ifdef GEN_ERRNO_RTX
1245 rtx errno_rtx = GEN_ERRNO_RTX;
1246 #else
1247 rtx errno_rtx
1248 = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
1249 #endif
1251 emit_move_insn (errno_rtx, GEN_INT (TARGET_EDOM));
1253 #else
1254 /* We can't set errno=EDOM directly; let the library call do it.
1255 Pop the arguments right away in case the call gets deleted. */
1256 NO_DEFER_POP;
1257 expand_call (exp, target, 0);
1258 OK_DEFER_POP;
1259 #endif
1261 emit_label (lab1);
1264 /* Output the entire sequence. */
1265 insns = get_insns ();
1266 end_sequence ();
1267 emit_insns (insns);
1269 return target;
1272 /* Expand expression EXP which is a call to the strlen builtin. Return 0
1273 if we failed the caller should emit a normal call, otherwise
1274 try to get the result in TARGET, if convenient (and in mode MODE if that's
1275 convenient). */
1276 static rtx
1277 expand_builtin_strlen (exp, target, mode)
1278 tree exp;
1279 rtx target;
1280 enum machine_mode mode;
1282 tree arglist = TREE_OPERAND (exp, 1);
1283 enum machine_mode value_mode = TYPE_MODE (TREE_TYPE (exp));
1285 if (arglist == 0
1286 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1287 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
1288 return 0;
1289 else
1291 tree src = TREE_VALUE (arglist);
1292 tree len = c_strlen (src);
1294 int align
1295 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1297 rtx result, src_rtx, char_rtx;
1298 enum machine_mode insn_mode = value_mode, char_mode;
1299 enum insn_code icode;
1301 /* If the length is known, just return it. */
1302 if (len != 0)
1303 return expand_expr (len, target, mode, EXPAND_MEMORY_USE_BAD);
1305 /* If SRC is not a pointer type, don't do this operation inline. */
1306 if (align == 0)
1307 return 0;
1309 /* Call a function if we can't compute strlen in the right mode. */
1311 while (insn_mode != VOIDmode)
1313 icode = strlen_optab->handlers[(int) insn_mode].insn_code;
1314 if (icode != CODE_FOR_nothing)
1315 break;
1317 insn_mode = GET_MODE_WIDER_MODE (insn_mode);
1319 if (insn_mode == VOIDmode)
1320 return 0;
1322 /* Make a place to write the result of the instruction. */
1323 result = target;
1324 if (! (result != 0
1325 && GET_CODE (result) == REG
1326 && GET_MODE (result) == insn_mode
1327 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1328 result = gen_reg_rtx (insn_mode);
1330 /* Make sure the operands are acceptable to the predicates. */
1332 if (! (*insn_data[(int)icode].operand[0].predicate) (result, insn_mode))
1333 result = gen_reg_rtx (insn_mode);
1334 src_rtx = memory_address (BLKmode,
1335 expand_expr (src, NULL_RTX, ptr_mode,
1336 EXPAND_NORMAL));
1338 if (! (*insn_data[(int)icode].operand[1].predicate) (src_rtx, Pmode))
1339 src_rtx = copy_to_mode_reg (Pmode, src_rtx);
1341 /* Check the string is readable and has an end. */
1342 if (current_function_check_memory_usage)
1343 emit_library_call (chkr_check_str_libfunc, 1, VOIDmode, 2,
1344 src_rtx, Pmode,
1345 GEN_INT (MEMORY_USE_RO),
1346 TYPE_MODE (integer_type_node));
1348 char_rtx = const0_rtx;
1349 char_mode = insn_data[(int)icode].operand[2].mode;
1350 if (! (*insn_data[(int)icode].operand[2].predicate) (char_rtx, char_mode))
1351 char_rtx = copy_to_mode_reg (char_mode, char_rtx);
1353 emit_insn (GEN_FCN (icode) (result,
1354 gen_rtx_MEM (BLKmode, src_rtx),
1355 char_rtx, GEN_INT (align)));
1357 /* Return the value in the proper mode for this function. */
1358 if (GET_MODE (result) == value_mode)
1359 return result;
1360 else if (target != 0)
1362 convert_move (target, result, 0);
1363 return target;
1365 else
1366 return convert_to_mode (value_mode, result, 0);
1370 /* Expand a call to the memcpy builtin, with arguments in ARGLIST. */
1371 static rtx
1372 expand_builtin_memcpy (arglist)
1373 tree arglist;
1375 if (arglist == 0
1376 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1377 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1378 || TREE_CHAIN (arglist) == 0
1379 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1380 != POINTER_TYPE)
1381 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1382 || (TREE_CODE (TREE_TYPE (TREE_VALUE
1383 (TREE_CHAIN (TREE_CHAIN (arglist)))))
1384 != INTEGER_TYPE))
1385 return 0;
1386 else
1388 tree dest = TREE_VALUE (arglist);
1389 tree src = TREE_VALUE (TREE_CHAIN (arglist));
1390 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1392 int src_align
1393 = get_pointer_alignment (src, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1394 int dest_align
1395 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1396 rtx dest_mem, src_mem, dest_addr, len_rtx;
1398 /* If either SRC or DEST is not a pointer type, don't do
1399 this operation in-line. */
1400 if (src_align == 0 || dest_align == 0)
1401 return 0;
1403 dest_mem = get_memory_rtx (dest);
1404 src_mem = get_memory_rtx (src);
1405 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1407 /* Just copy the rights of SRC to the rights of DEST. */
1408 if (current_function_check_memory_usage)
1409 emit_library_call (chkr_copy_bitmap_libfunc, 1, VOIDmode, 3,
1410 XEXP (dest_mem, 0), Pmode,
1411 XEXP (src_mem, 0), Pmode,
1412 len_rtx, TYPE_MODE (sizetype));
1414 /* Copy word part most expediently. */
1415 dest_addr
1416 = emit_block_move (dest_mem, src_mem, len_rtx,
1417 MIN (src_align, dest_align));
1419 if (dest_addr == 0)
1420 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1422 return dest_addr;
1426 /* Expand expression EXP, which is a call to the strcpy builtin. Return 0
1427 if we failed the caller should emit a normal call. */
1428 static rtx
1429 expand_builtin_strcpy (exp)
1430 tree exp;
1432 tree arglist = TREE_OPERAND (exp, 1);
1433 rtx result;
1435 if (arglist == 0
1436 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1437 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1438 || TREE_CHAIN (arglist) == 0
1439 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE)
1440 return 0;
1441 else
1443 tree len = c_strlen (TREE_VALUE (TREE_CHAIN (arglist)));
1445 if (len == 0)
1446 return 0;
1448 len = size_binop (PLUS_EXPR, len, integer_one_node);
1450 chainon (arglist, build_tree_list (NULL_TREE, len));
1452 result = expand_builtin_memcpy (arglist);
1453 if (! result)
1454 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1455 return result;
1458 /* Expand expression EXP, which is a call to the memset builtin. Return 0
1459 if we failed the caller should emit a normal call. */
1460 static rtx
1461 expand_builtin_memset (exp)
1462 tree exp;
1464 tree arglist = TREE_OPERAND (exp, 1);
1466 if (arglist == 0
1467 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1468 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1469 || TREE_CHAIN (arglist) == 0
1470 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist))))
1471 != INTEGER_TYPE)
1472 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1473 || (INTEGER_TYPE
1474 != (TREE_CODE (TREE_TYPE
1475 (TREE_VALUE
1476 (TREE_CHAIN (TREE_CHAIN (arglist))))))))
1477 return 0;
1478 else
1480 tree dest = TREE_VALUE (arglist);
1481 tree val = TREE_VALUE (TREE_CHAIN (arglist));
1482 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1484 int dest_align
1485 = get_pointer_alignment (dest, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1486 rtx dest_mem, dest_addr, len_rtx;
1488 /* If DEST is not a pointer type, don't do this
1489 operation in-line. */
1490 if (dest_align == 0)
1491 return 0;
1493 /* If the arguments have side-effects, then we can only evaluate
1494 them at most once. The following code evaluates them twice if
1495 they are not constants because we break out to expand_call
1496 in that case. They can't be constants if they have side-effects
1497 so we can check for that first. Alternatively, we could call
1498 save_expr to make multiple evaluation safe. */
1499 if (TREE_SIDE_EFFECTS (val) || TREE_SIDE_EFFECTS (len))
1500 return 0;
1502 /* If VAL is not 0, don't do this operation in-line. */
1503 if (expand_expr (val, NULL_RTX, VOIDmode, 0) != const0_rtx)
1504 return 0;
1506 /* If LEN does not expand to a constant, don't do this
1507 operation in-line. */
1508 len_rtx = expand_expr (len, NULL_RTX, VOIDmode, 0);
1509 if (GET_CODE (len_rtx) != CONST_INT)
1510 return 0;
1512 dest_mem = get_memory_rtx (dest);
1514 /* Just check DST is writable and mark it as readable. */
1515 if (current_function_check_memory_usage)
1516 emit_library_call (chkr_check_addr_libfunc, 1, VOIDmode, 3,
1517 XEXP (dest_mem, 0), Pmode,
1518 len_rtx, TYPE_MODE (sizetype),
1519 GEN_INT (MEMORY_USE_WO),
1520 TYPE_MODE (integer_type_node));
1523 dest_addr = clear_storage (dest_mem, len_rtx, dest_align);
1525 if (dest_addr == 0)
1526 dest_addr = force_operand (XEXP (dest_mem, 0), NULL_RTX);
1528 return dest_addr;
1532 #ifdef HAVE_cmpstrsi
1533 /* Expand expression EXP, which is a call to the memcmp or the strcmp builtin.
1534 ARGLIST is the argument list for this call. Return 0 if we failed and the
1535 caller should emit a normal call, otherwise try to get the result in
1536 TARGET, if convenient. */
1537 static rtx
1538 expand_builtin_memcmp (exp, arglist, target)
1539 tree exp;
1540 tree arglist;
1541 rtx target;
1543 /* If we need to check memory accesses, call the library function. */
1544 if (current_function_check_memory_usage)
1545 return 0;
1547 if (arglist == 0
1548 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1549 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1550 || TREE_CHAIN (arglist) == 0
1551 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
1552 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
1553 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
1554 return 0;
1555 else if (!HAVE_cmpstrsi)
1556 return 0;
1559 enum machine_mode mode;
1560 tree arg1 = TREE_VALUE (arglist);
1561 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1562 tree len = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
1563 rtx result;
1565 int arg1_align
1566 = get_pointer_alignment (arg1, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1567 int arg2_align
1568 = get_pointer_alignment (arg2, BIGGEST_ALIGNMENT) / BITS_PER_UNIT;
1569 enum machine_mode insn_mode
1570 = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
1572 /* If we don't have POINTER_TYPE, call the function. */
1573 if (arg1_align == 0 || arg2_align == 0)
1574 return 0;
1576 /* Make a place to write the result of the instruction. */
1577 result = target;
1578 if (! (result != 0
1579 && GET_CODE (result) == REG && GET_MODE (result) == insn_mode
1580 && REGNO (result) >= FIRST_PSEUDO_REGISTER))
1581 result = gen_reg_rtx (insn_mode);
1583 emit_insn (gen_cmpstrsi (result, get_memory_rtx (arg1),
1584 get_memory_rtx (arg2),
1585 expand_expr (len, NULL_RTX, VOIDmode, 0),
1586 GEN_INT (MIN (arg1_align, arg2_align))));
1588 /* Return the value in the proper mode for this function. */
1589 mode = TYPE_MODE (TREE_TYPE (exp));
1590 if (GET_MODE (result) == mode)
1591 return result;
1592 else if (target != 0)
1594 convert_move (target, result, 0);
1595 return target;
1597 else
1598 return convert_to_mode (mode, result, 0);
1602 /* Expand expression EXP, which is a call to the strcmp builtin. Return 0
1603 if we failed the caller should emit a normal call, otherwise try to get
1604 the result in TARGET, if convenient. */
1605 static rtx
1606 expand_builtin_strcmp (exp, target)
1607 tree exp;
1608 rtx target;
1610 tree arglist = TREE_OPERAND (exp, 1);
1612 /* If we need to check memory accesses, call the library function. */
1613 if (current_function_check_memory_usage)
1614 return 0;
1616 if (arglist == 0
1617 /* Arg could be non-pointer if user redeclared this fcn wrong. */
1618 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE
1619 || TREE_CHAIN (arglist) == 0
1620 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE)
1621 return 0;
1622 else if (!HAVE_cmpstrsi)
1623 return 0;
1625 tree arg1 = TREE_VALUE (arglist);
1626 tree arg2 = TREE_VALUE (TREE_CHAIN (arglist));
1627 tree len, len2;
1628 rtx result;
1629 len = c_strlen (arg1);
1630 if (len)
1631 len = size_binop (PLUS_EXPR, integer_one_node, len);
1632 len2 = c_strlen (arg2);
1633 if (len2)
1634 len2 = size_binop (PLUS_EXPR, integer_one_node, len2);
1636 /* If we don't have a constant length for the first, use the length
1637 of the second, if we know it. We don't require a constant for
1638 this case; some cost analysis could be done if both are available
1639 but neither is constant. For now, assume they're equally cheap.
1641 If both strings have constant lengths, use the smaller. This
1642 could arise if optimization results in strcpy being called with
1643 two fixed strings, or if the code was machine-generated. We should
1644 add some code to the `memcmp' handler below to deal with such
1645 situations, someday. */
1646 if (!len || TREE_CODE (len) != INTEGER_CST)
1648 if (len2)
1649 len = len2;
1650 else if (len == 0)
1651 return 0;
1653 else if (len2 && TREE_CODE (len2) == INTEGER_CST)
1655 if (tree_int_cst_lt (len2, len))
1656 len = len2;
1659 chainon (arglist, build_tree_list (NULL_TREE, len));
1660 result = expand_builtin_memcmp (exp, arglist, target);
1661 if (! result)
1662 TREE_CHAIN (TREE_CHAIN (arglist)) = 0;
1663 return result;
1666 #endif
1668 /* Expand a call to __builtin_saveregs, generating the result in TARGET,
1669 if that's convenient. */
1671 expand_builtin_saveregs ()
1673 rtx val, seq;
1675 /* Don't do __builtin_saveregs more than once in a function.
1676 Save the result of the first call and reuse it. */
1677 if (saveregs_value != 0)
1678 return saveregs_value;
1680 /* When this function is called, it means that registers must be
1681 saved on entry to this function. So we migrate the call to the
1682 first insn of this function. */
1684 start_sequence ();
1686 #ifdef EXPAND_BUILTIN_SAVEREGS
1687 /* Do whatever the machine needs done in this case. */
1688 val = EXPAND_BUILTIN_SAVEREGS ();
1689 #else
1690 /* ??? We used to try and build up a call to the out of line function,
1691 guessing about what registers needed saving etc. This became much
1692 harder with __builtin_va_start, since we don't have a tree for a
1693 call to __builtin_saveregs to fall back on. There was exactly one
1694 port (i860) that used this code, and I'm unconvinced it could actually
1695 handle the general case. So we no longer try to handle anything
1696 weird and make the backend absorb the evil. */
1698 error ("__builtin_saveregs not supported by this target");
1699 val = const0_rtx;
1700 #endif
1702 seq = get_insns ();
1703 end_sequence ();
1705 saveregs_value = val;
1707 /* Put the sequence after the NOTE that starts the function. If this
1708 is inside a SEQUENCE, make the outer-level insn chain current, so
1709 the code is placed at the start of the function. */
1710 push_topmost_sequence ();
1711 emit_insns_after (seq, get_insns ());
1712 pop_topmost_sequence ();
1714 return val;
1717 /* __builtin_args_info (N) returns word N of the arg space info
1718 for the current function. The number and meanings of words
1719 is controlled by the definition of CUMULATIVE_ARGS. */
1720 static rtx
1721 expand_builtin_args_info (exp)
1722 tree exp;
1724 tree arglist = TREE_OPERAND (exp, 1);
1725 int nwords = sizeof (CUMULATIVE_ARGS) / sizeof (int);
1726 int *word_ptr = (int *) &current_function_args_info;
1727 #if 0
1728 /* These are used by the code below that is if 0'ed away */
1729 int i;
1730 tree type, elts, result;
1731 #endif
1733 if (sizeof (CUMULATIVE_ARGS) % sizeof (int) != 0)
1734 abort ();
1736 if (arglist != 0)
1738 tree arg = TREE_VALUE (arglist);
1739 if (TREE_CODE (arg) != INTEGER_CST)
1740 error ("argument of `__builtin_args_info' must be constant");
1741 else
1743 int wordnum = TREE_INT_CST_LOW (arg);
1745 if (wordnum < 0 || wordnum >= nwords || TREE_INT_CST_HIGH (arg))
1746 error ("argument of `__builtin_args_info' out of range");
1747 else
1748 return GEN_INT (word_ptr[wordnum]);
1751 else
1752 error ("missing argument in `__builtin_args_info'");
1754 return const0_rtx;
1756 #if 0
1757 for (i = 0; i < nwords; i++)
1758 elts = tree_cons (NULL_TREE, build_int_2 (word_ptr[i], 0));
1760 type = build_array_type (integer_type_node,
1761 build_index_type (build_int_2 (nwords, 0)));
1762 result = build (CONSTRUCTOR, type, NULL_TREE, nreverse (elts));
1763 TREE_CONSTANT (result) = 1;
1764 TREE_STATIC (result) = 1;
1765 result = build1 (INDIRECT_REF, build_pointer_type (type), result);
1766 TREE_CONSTANT (result) = 1;
1767 return expand_expr (result, NULL_RTX, VOIDmode, EXPAND_MEMORY_USE_BAD);
1768 #endif
1771 /* Expand ARGLIST, from a call to __builtin_next_arg. */
1772 static rtx
1773 expand_builtin_next_arg (arglist)
1774 tree arglist;
1776 tree fntype = TREE_TYPE (current_function_decl);
1778 if ((TYPE_ARG_TYPES (fntype) == 0
1779 || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
1780 == void_type_node))
1781 && ! current_function_varargs)
1783 error ("`va_start' used in function with fixed args");
1784 return const0_rtx;
1787 if (arglist)
1789 tree last_parm = tree_last (DECL_ARGUMENTS (current_function_decl));
1790 tree arg = TREE_VALUE (arglist);
1792 /* Strip off all nops for the sake of the comparison. This
1793 is not quite the same as STRIP_NOPS. It does more.
1794 We must also strip off INDIRECT_EXPR for C++ reference
1795 parameters. */
1796 while (TREE_CODE (arg) == NOP_EXPR
1797 || TREE_CODE (arg) == CONVERT_EXPR
1798 || TREE_CODE (arg) == NON_LVALUE_EXPR
1799 || TREE_CODE (arg) == INDIRECT_REF)
1800 arg = TREE_OPERAND (arg, 0);
1801 if (arg != last_parm)
1802 warning ("second parameter of `va_start' not last named argument");
1804 else if (! current_function_varargs)
1805 /* Evidently an out of date version of <stdarg.h>; can't validate
1806 va_start's second argument, but can still work as intended. */
1807 warning ("`__builtin_next_arg' called without an argument");
1809 return expand_binop (Pmode, add_optab,
1810 current_function_internal_arg_pointer,
1811 current_function_arg_offset_rtx,
1812 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1815 /* Make it easier for the backends by protecting the valist argument
1816 from multiple evaluations. */
1818 static tree
1819 stabilize_va_list (valist, was_ptr)
1820 tree valist;
1821 int was_ptr;
1823 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
1825 /* If stdarg.h took the address of an array-type valist that was passed
1826 as a parameter, we'll have taken the address of the parameter itself
1827 rather than the array as we'd intended. Undo this mistake. */
1829 if (was_ptr)
1831 STRIP_NOPS (valist);
1833 /* Two cases: either &array, which decomposed to
1834 <ptr <array <record> valist>>
1835 or &ptr, which turned into
1836 <ptr <ptr <record>>>
1837 In the first case we'll need to put the ADDR_EXPR back
1838 after frobbing the types as if &array[0]. */
1840 if (TREE_CODE (valist) != ADDR_EXPR)
1841 abort ();
1842 valist = TREE_OPERAND (valist, 0);
1845 if (TYPE_MAIN_VARIANT (TREE_TYPE (valist))
1846 == TYPE_MAIN_VARIANT (va_list_type_node))
1848 tree pt = build_pointer_type (TREE_TYPE (va_list_type_node));
1849 valist = build1 (ADDR_EXPR, pt, valist);
1850 TREE_SIDE_EFFECTS (valist)
1851 = TREE_SIDE_EFFECTS (TREE_OPERAND (valist, 0));
1853 else
1855 if (! POINTER_TYPE_P (TREE_TYPE (valist))
1856 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (valist)))
1857 != TYPE_MAIN_VARIANT (TREE_TYPE (va_list_type_node))))
1858 abort ();
1861 if (TREE_SIDE_EFFECTS (valist))
1862 valist = save_expr (valist);
1864 else
1866 if (! was_ptr)
1868 tree pt;
1870 if (! TREE_SIDE_EFFECTS (valist))
1871 return valist;
1873 pt = build_pointer_type (va_list_type_node);
1874 valist = fold (build1 (ADDR_EXPR, pt, valist));
1875 TREE_SIDE_EFFECTS (valist) = 1;
1877 if (TREE_SIDE_EFFECTS (valist))
1878 valist = save_expr (valist);
1879 valist = fold (build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)),
1880 valist));
1883 return valist;
1886 /* The "standard" implementation of va_start: just assign `nextarg' to
1887 the variable. */
1888 void
1889 std_expand_builtin_va_start (stdarg_p, valist, nextarg)
1890 int stdarg_p ATTRIBUTE_UNUSED;
1891 tree valist;
1892 rtx nextarg;
1894 tree t;
1896 if (!stdarg_p)
1897 nextarg = plus_constant (nextarg, -UNITS_PER_WORD);
1899 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
1900 make_tree (ptr_type_node, nextarg));
1901 TREE_SIDE_EFFECTS (t) = 1;
1903 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1906 /* Expand ARGLIST, which from a call to __builtin_stdarg_va_start or
1907 __builtin_varargs_va_start, depending on STDARG_P. */
1908 static rtx
1909 expand_builtin_va_start (stdarg_p, arglist)
1910 int stdarg_p;
1911 tree arglist;
1913 rtx nextarg;
1914 tree chain = arglist, valist;
1916 if (stdarg_p)
1917 nextarg = expand_builtin_next_arg (chain = TREE_CHAIN (arglist));
1918 else
1919 nextarg = expand_builtin_next_arg (NULL_TREE);
1921 if (TREE_CHAIN (chain))
1922 error ("too many arguments to function `va_start'");
1924 valist = stabilize_va_list (TREE_VALUE (arglist), 1);
1926 #ifdef EXPAND_BUILTIN_VA_START
1927 EXPAND_BUILTIN_VA_START (stdarg_p, valist, nextarg);
1928 #else
1929 std_expand_builtin_va_start (stdarg_p, valist, nextarg);
1930 #endif
1932 return const0_rtx;
1935 /* Allocate an alias set for use in storing and reading from the varargs
1936 spill area. */
1938 get_varargs_alias_set ()
1940 static int set = -1;
1941 if (set == -1)
1942 set = new_alias_set ();
1943 return set;
1946 /* The "standard" implementation of va_arg: read the value from the
1947 current (padded) address and increment by the (padded) size. */
1949 std_expand_builtin_va_arg (valist, type)
1950 tree valist, type;
1952 tree addr_tree, t;
1953 HOST_WIDE_INT align;
1954 HOST_WIDE_INT rounded_size;
1955 rtx addr;
1957 /* Compute the rounded size of the type. */
1958 align = PARM_BOUNDARY / BITS_PER_UNIT;
1959 rounded_size = (((int_size_in_bytes (type) + align - 1) / align) * align);
1961 /* Get AP. */
1962 addr_tree = valist;
1963 if (BYTES_BIG_ENDIAN)
1965 /* Small args are padded downward. */
1967 HOST_WIDE_INT adj;
1968 adj = TREE_INT_CST_LOW (TYPE_SIZE (type)) / BITS_PER_UNIT;
1969 if (rounded_size > align)
1970 adj = rounded_size;
1972 addr_tree = build (PLUS_EXPR, TREE_TYPE (addr_tree), addr_tree,
1973 build_int_2 (rounded_size - adj, 0));
1976 addr = expand_expr (addr_tree, NULL_RTX, Pmode, EXPAND_NORMAL);
1977 addr = copy_to_reg (addr);
1979 /* Compute new value for AP. */
1980 t = build (MODIFY_EXPR, TREE_TYPE (valist), valist,
1981 build (PLUS_EXPR, TREE_TYPE (valist), valist,
1982 build_int_2 (rounded_size, 0)));
1983 TREE_SIDE_EFFECTS (t) = 1;
1984 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1986 return addr;
1989 /* Expand __builtin_va_arg, which is not really a builtin function, but
1990 a very special sort of operator. */
1992 expand_builtin_va_arg (valist, type)
1993 tree valist, type;
1995 rtx addr, result;
1996 tree promoted_type, want_va_type, have_va_type;
1998 /* Verify that valist is of the proper type. */
2000 want_va_type = va_list_type_node;
2001 have_va_type = TREE_TYPE (valist);
2002 if (TREE_CODE (want_va_type) == ARRAY_TYPE)
2004 /* If va_list is an array type, the argument may have decayed
2005 to a pointer type, e.g. by being passed to another function.
2006 In that case, unwrap both types so that we can compare the
2007 underlying records. */
2008 if (TREE_CODE (have_va_type) == ARRAY_TYPE
2009 || TREE_CODE (have_va_type) == POINTER_TYPE)
2011 want_va_type = TREE_TYPE (want_va_type);
2012 have_va_type = TREE_TYPE (have_va_type);
2015 if (TYPE_MAIN_VARIANT (want_va_type) != TYPE_MAIN_VARIANT (have_va_type))
2017 error ("first argument to `va_arg' not of type `va_list'");
2018 addr = const0_rtx;
2021 /* Generate a diagnostic for requesting data of a type that cannot
2022 be passed through `...' due to type promotion at the call site. */
2023 else if ((promoted_type = (*lang_type_promotes_to) (type)) != NULL_TREE)
2025 const char *name = "<anonymous type>", *pname = 0;
2026 static int gave_help;
2028 if (TYPE_NAME (type))
2030 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
2031 name = IDENTIFIER_POINTER (TYPE_NAME (type));
2032 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
2033 && DECL_NAME (TYPE_NAME (type)))
2034 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
2036 if (TYPE_NAME (promoted_type))
2038 if (TREE_CODE (TYPE_NAME (promoted_type)) == IDENTIFIER_NODE)
2039 pname = IDENTIFIER_POINTER (TYPE_NAME (promoted_type));
2040 else if (TREE_CODE (TYPE_NAME (promoted_type)) == TYPE_DECL
2041 && DECL_NAME (TYPE_NAME (promoted_type)))
2042 pname = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (promoted_type)));
2045 error ("`%s' is promoted to `%s' when passed through `...'", name, pname);
2046 if (! gave_help)
2048 gave_help = 1;
2049 error ("(so you should pass `%s' not `%s' to `va_arg')", pname, name);
2052 addr = const0_rtx;
2054 else
2056 /* Make it easier for the backends by protecting the valist argument
2057 from multiple evaluations. */
2058 valist = stabilize_va_list (valist, 0);
2060 #ifdef EXPAND_BUILTIN_VA_ARG
2061 addr = EXPAND_BUILTIN_VA_ARG (valist, type);
2062 #else
2063 addr = std_expand_builtin_va_arg (valist, type);
2064 #endif
2067 result = gen_rtx_MEM (TYPE_MODE (type), addr);
2068 MEM_ALIAS_SET (result) = get_varargs_alias_set ();
2070 return result;
2073 /* Expand ARGLIST, from a call to __builtin_va_end. */
2074 static rtx
2075 expand_builtin_va_end (arglist)
2076 tree arglist;
2078 tree valist = TREE_VALUE (arglist);
2080 #ifdef EXPAND_BUILTIN_VA_END
2081 valist = stabilize_va_list (valist, 0);
2082 EXPAND_BUILTIN_VA_END(arglist);
2083 #else
2084 /* Evaluate for side effects, if needed. I hate macros that don't
2085 do that. */
2086 if (TREE_SIDE_EFFECTS (valist))
2087 expand_expr (valist, const0_rtx, VOIDmode, EXPAND_NORMAL);
2088 #endif
2090 return const0_rtx;
2093 /* Expand ARGLIST, from a call to __builtin_va_copy. We do this as a
2094 builtin rather than just as an assignment in stdarg.h because of the
2095 nastiness of array-type va_list types. */
2096 static rtx
2097 expand_builtin_va_copy (arglist)
2098 tree arglist;
2100 tree dst, src, t;
2102 dst = TREE_VALUE (arglist);
2103 src = TREE_VALUE (TREE_CHAIN (arglist));
2105 dst = stabilize_va_list (dst, 1);
2106 src = stabilize_va_list (src, 0);
2108 if (TREE_CODE (va_list_type_node) != ARRAY_TYPE)
2110 t = build (MODIFY_EXPR, va_list_type_node, dst, src);
2111 TREE_SIDE_EFFECTS (t) = 1;
2112 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
2114 else
2116 rtx dstb, srcb, size;
2118 /* Evaluate to pointers. */
2119 dstb = expand_expr (dst, NULL_RTX, Pmode, EXPAND_NORMAL);
2120 srcb = expand_expr (src, NULL_RTX, Pmode, EXPAND_NORMAL);
2121 size = expand_expr (TYPE_SIZE_UNIT (va_list_type_node), NULL_RTX,
2122 VOIDmode, EXPAND_NORMAL);
2124 /* "Dereference" to BLKmode memories. */
2125 dstb = gen_rtx_MEM (BLKmode, dstb);
2126 MEM_ALIAS_SET (dstb) = get_alias_set (TREE_TYPE (TREE_TYPE (dst)));
2127 srcb = gen_rtx_MEM (BLKmode, srcb);
2128 MEM_ALIAS_SET (srcb) = get_alias_set (TREE_TYPE (TREE_TYPE (src)));
2130 /* Copy. */
2131 emit_block_move (dstb, srcb, size,
2132 TYPE_ALIGN (va_list_type_node) / BITS_PER_UNIT);
2135 return const0_rtx;
2138 /* Expand a call to one of the builtin functions __builtin_frame_address or
2139 __builtin_return_address. */
2140 static rtx
2141 expand_builtin_frame_address (exp)
2142 tree exp;
2144 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2145 tree arglist = TREE_OPERAND (exp, 1);
2147 /* The argument must be a nonnegative integer constant.
2148 It counts the number of frames to scan up the stack.
2149 The value is the return address saved in that frame. */
2150 if (arglist == 0)
2151 /* Warning about missing arg was already issued. */
2152 return const0_rtx;
2153 else if (TREE_CODE (TREE_VALUE (arglist)) != INTEGER_CST
2154 || tree_int_cst_sgn (TREE_VALUE (arglist)) < 0)
2156 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2157 error ("invalid arg to `__builtin_frame_address'");
2158 else
2159 error ("invalid arg to `__builtin_return_address'");
2160 return const0_rtx;
2162 else
2164 rtx tem = expand_builtin_return_addr (DECL_FUNCTION_CODE (fndecl),
2165 TREE_INT_CST_LOW (TREE_VALUE (arglist)),
2166 hard_frame_pointer_rtx);
2168 /* Some ports cannot access arbitrary stack frames. */
2169 if (tem == NULL)
2171 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2172 warning ("unsupported arg to `__builtin_frame_address'");
2173 else
2174 warning ("unsupported arg to `__builtin_return_address'");
2175 return const0_rtx;
2178 /* For __builtin_frame_address, return what we've got. */
2179 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FRAME_ADDRESS)
2180 return tem;
2182 if (GET_CODE (tem) != REG
2183 && ! CONSTANT_P (tem))
2184 tem = copy_to_mode_reg (Pmode, tem);
2185 return tem;
2189 /* Expand a call to the alloca builtin, with arguments ARGLIST. Return 0 if
2190 we failed and the caller should emit a normal call, otherwise try to get
2191 the result in TARGET, if convenient. */
2192 static rtx
2193 expand_builtin_alloca (arglist, target)
2194 tree arglist;
2195 rtx target;
2197 rtx op0;
2199 if (arglist == 0
2200 /* Arg could be non-integer if user redeclared this fcn wrong. */
2201 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2202 return 0;
2204 /* Compute the argument. */
2205 op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
2207 /* Allocate the desired space. */
2208 return allocate_dynamic_stack_space (op0, target, BITS_PER_UNIT);
2211 /* Expand a call to the ffs builtin. The arguments are in ARGLIST.
2212 Return 0 if a normal call should be emitted rather than expanding the
2213 function in-line. If convenient, the result should be placed in TARGET.
2214 SUBTARGET may be used as the target for computing one of EXP's operands. */
2215 static rtx
2216 expand_builtin_ffs (arglist, target, subtarget)
2217 tree arglist;
2218 rtx target, subtarget;
2220 rtx op0;
2221 if (arglist == 0
2222 /* Arg could be non-integer if user redeclared this fcn wrong. */
2223 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != INTEGER_TYPE)
2224 return 0;
2226 /* Compute the argument. */
2227 op0 = expand_expr (TREE_VALUE (arglist), subtarget, VOIDmode, 0);
2228 /* Compute ffs, into TARGET if possible.
2229 Set TARGET to wherever the result comes back. */
2230 target = expand_unop (TYPE_MODE (TREE_TYPE (TREE_VALUE (arglist))),
2231 ffs_optab, op0, target, 1);
2232 if (target == 0)
2233 abort ();
2234 return target;
2237 /* Expand an expression EXP that calls a built-in function,
2238 with result going to TARGET if that's convenient
2239 (and in mode MODE if that's convenient).
2240 SUBTARGET may be used as the target for computing one of EXP's operands.
2241 IGNORE is nonzero if the value is to be ignored. */
2244 expand_builtin (exp, target, subtarget, mode, ignore)
2245 tree exp;
2246 rtx target;
2247 rtx subtarget;
2248 enum machine_mode mode;
2249 int ignore;
2251 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
2252 tree arglist = TREE_OPERAND (exp, 1);
2253 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
2255 #ifdef MD_EXPAND_BUILTIN
2256 if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
2257 return MD_EXPAND_BUILTIN (exp, target, subtarget, mode, ignore);
2258 #endif
2260 /* When not optimizing, generate calls to library functions for a certain
2261 set of builtins. */
2262 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl)
2263 && (fcode == BUILT_IN_SIN || fcode == BUILT_IN_COS
2264 || fcode == BUILT_IN_FSQRT || fcode == BUILT_IN_MEMSET
2265 || fcode == BUILT_IN_MEMCPY || fcode == BUILT_IN_MEMCMP
2266 || fcode == BUILT_IN_STRLEN || fcode == BUILT_IN_STRCPY
2267 || fcode == BUILT_IN_STRCMP || fcode == BUILT_IN_FFS))
2268 return expand_call (exp, target, ignore);
2270 switch (fcode)
2272 case BUILT_IN_ABS:
2273 case BUILT_IN_LABS:
2274 case BUILT_IN_FABS:
2275 /* build_function_call changes these into ABS_EXPR. */
2276 abort ();
2278 case BUILT_IN_SIN:
2279 case BUILT_IN_COS:
2280 /* Treat these like sqrt, but only if the user asks for them. */
2281 if (! flag_fast_math)
2282 break;
2283 case BUILT_IN_FSQRT:
2284 target = expand_builtin_mathfn (exp, target, subtarget);
2285 if (target)
2286 return target;
2287 break;
2289 case BUILT_IN_FMOD:
2290 break;
2292 case BUILT_IN_APPLY_ARGS:
2293 return expand_builtin_apply_args ();
2295 /* __builtin_apply (FUNCTION, ARGUMENTS, ARGSIZE) invokes
2296 FUNCTION with a copy of the parameters described by
2297 ARGUMENTS, and ARGSIZE. It returns a block of memory
2298 allocated on the stack into which is stored all the registers
2299 that might possibly be used for returning the result of a
2300 function. ARGUMENTS is the value returned by
2301 __builtin_apply_args. ARGSIZE is the number of bytes of
2302 arguments that must be copied. ??? How should this value be
2303 computed? We'll also need a safe worst case value for varargs
2304 functions. */
2305 case BUILT_IN_APPLY:
2306 if (arglist == 0
2307 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2308 || ! POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (arglist)))
2309 || TREE_CHAIN (arglist) == 0
2310 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) != POINTER_TYPE
2311 || TREE_CHAIN (TREE_CHAIN (arglist)) == 0
2312 || TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))))) != INTEGER_TYPE)
2313 return const0_rtx;
2314 else
2316 int i;
2317 tree t;
2318 rtx ops[3];
2320 for (t = arglist, i = 0; t; t = TREE_CHAIN (t), i++)
2321 ops[i] = expand_expr (TREE_VALUE (t), NULL_RTX, VOIDmode, 0);
2323 return expand_builtin_apply (ops[0], ops[1], ops[2]);
2326 /* __builtin_return (RESULT) causes the function to return the
2327 value described by RESULT. RESULT is address of the block of
2328 memory returned by __builtin_apply. */
2329 case BUILT_IN_RETURN:
2330 if (arglist
2331 /* Arg could be non-pointer if user redeclared this fcn wrong. */
2332 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE)
2333 expand_builtin_return (expand_expr (TREE_VALUE (arglist),
2334 NULL_RTX, VOIDmode, 0));
2335 return const0_rtx;
2337 case BUILT_IN_SAVEREGS:
2338 return expand_builtin_saveregs ();
2340 case BUILT_IN_ARGS_INFO:
2341 return expand_builtin_args_info (exp);
2343 /* Return the address of the first anonymous stack arg. */
2344 case BUILT_IN_NEXT_ARG:
2345 return expand_builtin_next_arg (arglist);
2347 case BUILT_IN_CLASSIFY_TYPE:
2348 return expand_builtin_classify_type (arglist);
2350 case BUILT_IN_CONSTANT_P:
2351 return expand_builtin_constant_p (exp);
2353 case BUILT_IN_FRAME_ADDRESS:
2354 case BUILT_IN_RETURN_ADDRESS:
2355 return expand_builtin_frame_address (exp);
2357 /* Returns the address of the area where the structure is returned.
2358 0 otherwise. */
2359 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
2360 if (arglist != 0
2361 || ! AGGREGATE_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
2362 || GET_CODE (DECL_RTL (DECL_RESULT (current_function_decl))) != MEM)
2363 return const0_rtx;
2364 else
2365 return XEXP (DECL_RTL (DECL_RESULT (current_function_decl)), 0);
2367 case BUILT_IN_ALLOCA:
2368 target = expand_builtin_alloca (arglist, target);
2369 if (target)
2370 return target;
2371 break;
2373 case BUILT_IN_FFS:
2374 target = expand_builtin_ffs (arglist, target, subtarget);
2375 if (target)
2376 return target;
2377 break;
2379 case BUILT_IN_STRLEN:
2380 target = expand_builtin_strlen (exp, target, mode);
2381 if (target)
2382 return target;
2383 break;
2385 case BUILT_IN_STRCPY:
2386 target = expand_builtin_strcpy (exp);
2387 if (target)
2388 return target;
2389 break;
2391 case BUILT_IN_MEMCPY:
2392 target = expand_builtin_memcpy (arglist);
2393 if (target)
2394 return target;
2395 break;
2397 case BUILT_IN_MEMSET:
2398 target = expand_builtin_memset (exp);
2399 if (target)
2400 return target;
2401 break;
2403 /* These comparison functions need an instruction that returns an actual
2404 index. An ordinary compare that just sets the condition codes
2405 is not enough. */
2406 #ifdef HAVE_cmpstrsi
2407 case BUILT_IN_STRCMP:
2408 target = expand_builtin_strcmp (exp, target);
2409 if (target)
2410 return target;
2411 break;
2413 case BUILT_IN_MEMCMP:
2414 target = expand_builtin_memcmp (exp, arglist, target);
2415 if (target)
2416 return target;
2417 break;
2418 #else
2419 case BUILT_IN_STRCMP:
2420 case BUILT_IN_MEMCMP:
2421 break;
2422 #endif
2424 case BUILT_IN_SETJMP:
2425 if (arglist == 0
2426 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2427 break;
2428 else
2430 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2431 VOIDmode, 0);
2432 rtx lab = gen_label_rtx ();
2433 rtx ret = expand_builtin_setjmp (buf_addr, target, lab, lab);
2434 emit_label (lab);
2435 return ret;
2438 /* __builtin_longjmp is passed a pointer to an array of five words.
2439 It's similar to the C library longjmp function but works with
2440 __builtin_setjmp above. */
2441 case BUILT_IN_LONGJMP:
2442 if (arglist == 0 || TREE_CHAIN (arglist) == 0
2443 || TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
2444 break;
2445 else
2447 rtx buf_addr = expand_expr (TREE_VALUE (arglist), subtarget,
2448 VOIDmode, 0);
2449 rtx value = expand_expr (TREE_VALUE (TREE_CHAIN (arglist)),
2450 NULL_RTX, VOIDmode, 0);
2452 if (value != const1_rtx)
2454 error ("__builtin_longjmp second argument must be 1");
2455 return const0_rtx;
2458 expand_builtin_longjmp (buf_addr, value);
2459 return const0_rtx;
2462 case BUILT_IN_TRAP:
2463 #ifdef HAVE_trap
2464 if (HAVE_trap)
2465 emit_insn (gen_trap ());
2466 else
2467 #endif
2468 error ("__builtin_trap not supported by this target");
2469 emit_barrier ();
2470 return const0_rtx;
2472 /* Various hooks for the DWARF 2 __throw routine. */
2473 case BUILT_IN_UNWIND_INIT:
2474 expand_builtin_unwind_init ();
2475 return const0_rtx;
2476 case BUILT_IN_DWARF_CFA:
2477 return virtual_cfa_rtx;
2478 #ifdef DWARF2_UNWIND_INFO
2479 case BUILT_IN_DWARF_FP_REGNUM:
2480 return expand_builtin_dwarf_fp_regnum ();
2481 case BUILT_IN_INIT_DWARF_REG_SIZES:
2482 expand_builtin_init_dwarf_reg_sizes (TREE_VALUE (arglist));
2483 return const0_rtx;
2484 #endif
2485 case BUILT_IN_FROB_RETURN_ADDR:
2486 return expand_builtin_frob_return_addr (TREE_VALUE (arglist));
2487 case BUILT_IN_EXTRACT_RETURN_ADDR:
2488 return expand_builtin_extract_return_addr (TREE_VALUE (arglist));
2489 case BUILT_IN_EH_RETURN:
2490 expand_builtin_eh_return (TREE_VALUE (arglist),
2491 TREE_VALUE (TREE_CHAIN (arglist)),
2492 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))));
2493 return const0_rtx;
2494 case BUILT_IN_VARARGS_START:
2495 return expand_builtin_va_start (0, arglist);
2496 case BUILT_IN_STDARG_START:
2497 return expand_builtin_va_start (1, arglist);
2498 case BUILT_IN_VA_END:
2499 return expand_builtin_va_end (arglist);
2500 case BUILT_IN_VA_COPY:
2501 return expand_builtin_va_copy (arglist);
2503 default: /* just do library call, if unknown builtin */
2504 error ("built-in function `%s' not currently supported",
2505 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
2508 /* The switch statement above can drop through to cause the function
2509 to be called normally. */
2510 return expand_call (exp, target, ignore);