diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 25c09aa5530..6f2dae16d87 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,16 @@ +2003-04-13 Roger Sayle + + * builtin-types.def (BT_FN_STRING_CONST_STRING): New builtin type. + (BT_FN_PTR_SIZE_SIZE): Likewise. + * builtins.def (BUILT_IN_MALLOC, BUILT_IN_CALLOC, BUILT_IN_STRDUP): + New built-in functions for malloc, calloc and strdup respectively. + * calls.c (special_function_p): No need to handle malloc-like + functions any longer. ECF_MALLOC is set via built-in attributes. + + * c-decl.c (duplicate_decls): Preserve pure and malloc attributes. + + * doc/extend.texi: Document these new built-in functions. + 2003-04-13 Kaveh R. Ghazi * builtins.c (expand_builtin_memcpy): Add `endp' argument, use it. diff --git a/gcc/builtin-types.def b/gcc/builtin-types.def index c095d07b609..a9db4a32be5 100644 --- a/gcc/builtin-types.def +++ b/gcc/builtin-types.def @@ -124,6 +124,7 @@ DEF_FUNCTION_TYPE_1 (BT_FN_FLOAT_CONST_STRING, BT_FLOAT, BT_CONST_STRING) DEF_FUNCTION_TYPE_1 (BT_FN_DOUBLE_CONST_STRING, BT_DOUBLE, BT_CONST_STRING) DEF_FUNCTION_TYPE_1 (BT_FN_LONG_DOUBLE_CONST_STRING, BT_LONG_DOUBLE, BT_CONST_STRING) +DEF_FUNCTION_TYPE_1 (BT_FN_STRING_CONST_STRING, BT_STRING, BT_CONST_STRING) DEF_FUNCTION_TYPE_2 (BT_FN_VOID_PTR_INT, BT_VOID, BT_PTR, BT_INT) DEF_FUNCTION_TYPE_2 (BT_FN_STRING_STRING_CONST_STRING, @@ -158,6 +159,8 @@ DEF_FUNCTION_TYPE_2 (BT_FN_LONG_DOUBLE_LONG_DOUBLE_LONG_DOUBLE, BT_LONG_DOUBLE, BT_LONG_DOUBLE, BT_LONG_DOUBLE) DEF_FUNCTION_TYPE_2 (BT_FN_INT_CONST_STRING_VALIST_ARG, BT_INT, BT_CONST_STRING, BT_VALIST_ARG) +DEF_FUNCTION_TYPE_2 (BT_FN_PTR_SIZE_SIZE, + BT_PTR, BT_SIZE, BT_SIZE) DEF_FUNCTION_TYPE_3 (BT_FN_STRING_STRING_CONST_STRING_SIZE, BT_STRING, BT_STRING, BT_CONST_STRING, BT_SIZE) diff --git a/gcc/builtins.def b/gcc/builtins.def index 1d456e9f201..f0d118b0dc3 100644 --- a/gcc/builtins.def +++ b/gcc/builtins.def @@ -936,3 +936,19 @@ DEF_C99_BUILTIN(BUILT_IN__EXIT2, BT_FN_VOID_INT, ATTR_NORETURN_NOTHROW_LIST) +/* Declare malloc, calloc and strdup. */ +DEF_LIB_BUILTIN(BUILT_IN_MALLOC, + "__builtin_malloc", + BT_FN_PTR_SIZE, + ATTR_MALLOC_NOTHROW_LIST) + +DEF_LIB_BUILTIN(BUILT_IN_CALLOC, + "__builtin_calloc", + BT_FN_PTR_SIZE_SIZE, + ATTR_MALLOC_NOTHROW_LIST) + +DEF_EXT_LIB_BUILTIN(BUILT_IN_STRDUP, + "__builtin_strdup", + BT_FN_STRING_CONST_STRING, + ATTR_MALLOC_NOTHROW_LIST) + diff --git a/gcc/c-decl.c b/gcc/c-decl.c index 4b2e6e13c8a..3fc1808fe67 100644 --- a/gcc/c-decl.c +++ b/gcc/c-decl.c @@ -1391,6 +1391,8 @@ duplicate_decls (newdecl, olddecl, different_binding_level) DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); + DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); + DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); } } /* If cannot merge, then use the new type and qualifiers, diff --git a/gcc/calls.c b/gcc/calls.c index ffb8a21f021..1dc5fc5cc75 100644 --- a/gcc/calls.c +++ b/gcc/calls.c @@ -665,9 +665,6 @@ emit_call_1 (funexp, fndecl, funtype, stack_size, rounded_stack_size, Similarly set LONGJMP for if the function is in the longjmp family. - Set MALLOC for any of the standard memory allocation functions which - allocate from the heap. - Set MAY_BE_ALLOCA for any memory allocation function that might allocate space from the stack such as alloca. */ @@ -747,19 +744,6 @@ special_function_p (fndecl, flags) || ((tname[5] == 'p' || tname[5] == 'e') && tname[6] == '\0')))) flags |= ECF_FORK_OR_EXEC; - - /* Do not add any more malloc-like functions to this list, - instead mark them as malloc functions using the malloc attribute. - Note, realloc is not suitable for attribute malloc since - it may return the same address across multiple calls. - C++ operator new is not suitable because it is not required - to return a unique pointer; indeed, the standard placement new - just returns its argument. */ - else if (TYPE_MODE (TREE_TYPE (TREE_TYPE (fndecl))) == Pmode - && (! strcmp (tname, "malloc") - || ! strcmp (tname, "calloc") - || ! strcmp (tname, "strdup"))) - flags |= ECF_MALLOC; } return flags; } diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index b520d2c1bb2..c6cec1fd780 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,7 @@ +2003-04-13 Roger Sayle + + * decl.c (duplicate_decls): Preserve pure and malloc attributes. + 2003-04-12 Mark Mitchell PR c++/10300 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index f5ee1a36c29..33881d94f5f 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -3359,8 +3359,9 @@ duplicate_decls (tree newdecl, tree olddecl) { DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); - DECL_NO_LIMIT_STACK (newdecl) - |= DECL_NO_LIMIT_STACK (olddecl); + DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); + DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); + DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); /* Keep the old RTL. */ COPY_DECL_RTL (olddecl, newdecl); } diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index b61a718b287..f5a2c110fbc 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -4554,6 +4554,7 @@ v4si f (v4si a, v4si b, v4si c) @findex atan2l @findex bcmp @findex bzero +@findex calloc @findex ceil @findex ceilf @findex ceill @@ -4596,6 +4597,7 @@ v4si f (v4si a, v4si b, v4si c) @findex log @findex logf @findex logl +@findex malloc @findex memcmp @findex memcpy @findex mempcpy @@ -4630,6 +4632,7 @@ v4si f (v4si a, v4si b, v4si c) @findex strcmp @findex strcpy @findex strcspn +@findex strdup @findex strlen @findex strncat @findex strncmp @@ -4670,7 +4673,8 @@ Outside strict ISO C mode (@option{-ansi}, @option{-std=c89} or @option{-std=c99}), the functions @code{alloca}, @code{bcmp}, @code{bzero}, @code{_exit}, @code{ffs}, @code{fprintf_unlocked}, @code{fputs_unlocked}, @code{index}, @code{mempcpy}, @code{printf_unlocked}, -@code{rindex}, and @code{stpcpy} may be handled as built-in functions. +@code{rindex}, @code{stpcpy} and @code{strdup} +may be handled as built-in functions. All these functions have corresponding versions prefixed with @code{__builtin_}, which may be used even in strict C89 mode. @@ -4695,10 +4699,10 @@ that are recognized in any mode since ISO C90 reserves these names for the purpose to which ISO C99 puts them. All these functions have corresponding versions prefixed with @code{__builtin_}. -The ISO C90 functions @code{abort}, @code{abs}, @code{atan2}, @code{ceil}, -@code{cos}, @code{exit}, +The ISO C90 functions @code{abort}, @code{abs}, @code{atan2}, +@code{calloc}, @code{ceil}, @code{cos}, @code{exit}, @code{exp}, @code{fabs}, @code{floor}, @code{fmod}, -@code{fprintf}, @code{fputs}, @code{labs}, @code{log}, +@code{fprintf}, @code{fputs}, @code{labs}, @code{log}, @code{malloc}, @code{memcmp}, @code{memcpy}, @code{memset}, @code{pow}, @code{printf}, @code{putchar}, @code{puts}, @code{scanf}, @code{sin}, @code{snprintf}, @code{sprintf}, @code{sqrt}, @code{sscanf}, diff --git a/gcc/f/ChangeLog b/gcc/f/ChangeLog index fc712ff0701..47240a4c654 100644 --- a/gcc/f/ChangeLog +++ b/gcc/f/ChangeLog @@ -1,3 +1,7 @@ +2003-04-13 Roger Sayle + + * com.c (duplicate_decls): Preserve pure and malloc attributes. + 2003-04-12 Zack Weinberg * com.c (ffecom_build_complex_constant_, ffecom_expr_) diff --git a/gcc/f/com.c b/gcc/f/com.c index 1ccaeb20dce..6c606444ac5 100644 --- a/gcc/f/com.c +++ b/gcc/f/com.c @@ -13335,6 +13335,8 @@ duplicate_decls (tree newdecl, tree olddecl) { DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); + DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); + DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); } } /* If cannot merge, then use the new type and qualifiers, diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3b8b5c51251..8bded5bed57 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2003-04-13 Roger Sayle + + * gcc.dg/builtins-12.c: New test case. + * gcc.dg/builtins-13.c: New test case. + 2003-04-13 Kaveh R. Ghazi * gcc.c-torture/execute/string-opt-18.c: New test. diff --git a/gcc/testsuite/gcc.dg/builtins-12.c b/gcc/testsuite/gcc.dg/builtins-12.c new file mode 100644 index 00000000000..22ba8ce3d31 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtins-12.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2003 Free Software Foundation. + + Verify that all the malloc-like __builtin_ allocation functions are + recognized by the compiler. + + Written by Roger Sayle, 12th April 2003. */ + +/* { dg-do compile } */ +/* { dg-final { scan-assembler-not "__builtin_" } } */ + +typedef __SIZE_TYPE__ size_t; + +void *test1(size_t n) +{ + return __builtin_malloc(n); +} + +void *test2(size_t n, size_t s) +{ + return __builtin_calloc(n,s); +} + +char *test3(const char *ptr) +{ + return __builtin_strdup(ptr); +} + diff --git a/gcc/testsuite/gcc.dg/builtins-13.c b/gcc/testsuite/gcc.dg/builtins-13.c new file mode 100644 index 00000000000..befcd3447ad --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtins-13.c @@ -0,0 +1,61 @@ +/* Copyright (C) 2003 Free Software Foundation. + + Verify that the malloc-like __builtin_ allocation functions are + correctly aliased by the compiler. + + Written by Roger Sayle, 12th April 2003. */ + +/* { dg-do link } */ + +typedef __SIZE_TYPE__ size_t; + +extern void abort (void); +extern void *malloc (size_t); +extern void *calloc (size_t,size_t); + +extern void link_error (void); + +static int x; + +void test1(void) +{ + int *ptr1, *ptr2; + + ptr1 = &x; + ptr2 = (int*) malloc (sizeof (int)); + + *ptr1 = 12; + *ptr2 = 8; + + if (*ptr1 != 12) + link_error(); +} + +void test2(void) +{ + int *ptr1, *ptr2; + + ptr1 = &x; + ptr2 = (int*) calloc (1, sizeof (int)); + + *ptr1 = 12; + *ptr2 = 8; + + if (*ptr1 != 12) + link_error (); +} + +int main() +{ + test1 (); + test2 (); + return 0; +} + +#ifndef __OPTIMIZE__ +void link_error (void) +{ + abort (); +} +#endif +