]> gcc.gnu.org Git - gcc.git/commitdiff
builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.
authorJakub Jelinek <jakub@redhat.com>
Tue, 11 Sep 2007 13:40:14 +0000 (15:40 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 11 Sep 2007 13:40:14 +0000 (15:40 +0200)
* builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.
* builtins.c (expand_builtin) <case BUILT_IN_VA_ARG_PACK_LEN>: Issue
error if __builtin_va_arg_pack_len () wasn't optimized out during
inlining.
* tree-inline.c (copy_bb): Replace __builtin_va_arg_pack_len ()
with the number of inline's anonymous arguments.
* doc/extend.texi: Document __builtin_va_arg_pack_len ().

* gcc.dg/va-arg-pack-len-1.c: New test.
* g++.dg/va-arg-pack-len-1.C: New test.

From-SVN: r128376

gcc/ChangeLog
gcc/builtins.c
gcc/builtins.def
gcc/doc/extend.texi
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/va-arg-pack-len-1.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/va-arg-pack-len-1.c [new file with mode: 0644]
gcc/tree-inline.c

index 8e4e37ef8df3a4af1cec172827dec5a9593d72e0..3e45698a6246247672f12a8a35cb03445e62ffb0 100644 (file)
@@ -1,3 +1,13 @@
+2007-09-11  Jakub Jelinek  <jakub@redhat.com>
+
+       * builtins.def (BUILT_IN_VA_ARG_PACK_LEN): New builtin.
+       * builtins.c (expand_builtin) <case BUILT_IN_VA_ARG_PACK_LEN>: Issue
+       error if __builtin_va_arg_pack_len () wasn't optimized out during
+       inlining.
+       * tree-inline.c (copy_bb): Replace __builtin_va_arg_pack_len ()
+       with the number of inline's anonymous arguments.
+       * doc/extend.texi: Document __builtin_va_arg_pack_len ().
+
 2007-09-11  Zdenek Dvorak  <ook@ucw.cz>
 
        * fold-const.c (extract_muldiv_1): Do not simplify
index a0b1e2f441a594484abff9146949ed6daf58ea33..50d4b6211e118910f3f522b9443257567cd187f0 100644 (file)
@@ -6276,6 +6276,12 @@ expand_builtin (tree exp, rtx target, rtx subtarget, enum machine_mode mode,
       error ("invalid use of %<__builtin_va_arg_pack ()%>");
       return const0_rtx;
 
+    case BUILT_IN_VA_ARG_PACK_LEN:
+      /* All valid uses of __builtin_va_arg_pack_len () are removed during
+        inlining.  */
+      error ("invalid use of %<__builtin_va_arg_pack_len ()%>");
+      return const0_rtx;
+
       /* Return the address of the first anonymous stack arg.  */
     case BUILT_IN_NEXT_ARG:
       if (fold_builtin_next_arg (exp, false))
index e925a571f518388a97bfde769a7f56a17689ec93..b8b739b6bc556d0448f44febcb7993569a134ee7 100644 (file)
@@ -702,6 +702,7 @@ DEF_GCC_BUILTIN        (BUILT_IN_VA_COPY, "va_copy", BT_FN_VOID_VALIST_REF_VALIS
 DEF_GCC_BUILTIN        (BUILT_IN_VA_END, "va_end", BT_FN_VOID_VALIST_REF, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_VA_START, "va_start", BT_FN_VOID_VALIST_REF_VAR, ATTR_NULL)
 DEF_GCC_BUILTIN        (BUILT_IN_VA_ARG_PACK, "va_arg_pack", BT_FN_INT, ATTR_PURE_NOTHROW_LIST)
+DEF_GCC_BUILTIN        (BUILT_IN_VA_ARG_PACK_LEN, "va_arg_pack_len", BT_FN_INT, ATTR_PURE_NOTHROW_LIST)
 DEF_EXT_LIB_BUILTIN    (BUILT_IN__EXIT, "_exit", BT_FN_VOID_INT, ATTR_NORETURN_NOTHROW_LIST)
 DEF_C99_BUILTIN        (BUILT_IN__EXIT2, "_Exit", BT_FN_VOID_INT, ATTR_NORETURN_NOTHROW_LIST)
 
index 730940a49ddb2d01b5c3a725a29f58dd8003c60f..633913bb88d00d741954ecfc806b67f32b01fab3 100644 (file)
@@ -583,6 +583,41 @@ myprintf (FILE *f, const char *format, ...)
 @end smallexample
 @end deftypefn
 
+@deftypefn {Built-in Function} __builtin_va_arg_pack_len ()
+This built-in function returns the number of anonymous arguments of
+an inline function.  It can be used only in inline functions which
+will be always inlined, never compiled as a separate function, such
+as those using @code{__attribute__ ((__always_inline__))} or
+@code{__attribute__ ((__gnu_inline__))} extern inline functions.
+For example following will do link or runtime checking of open
+arguments for optimized code:
+@smallexample
+#ifdef __OPTIMIZE__
+extern inline __attribute__((__gnu_inline__)) int
+myopen (const char *path, int oflag, ...)
+@{
+  if (__builtin_va_arg_pack_len () > 1)
+    warn_open_too_many_arguments ();
+
+  if (__builtin_constant_p (oflag))
+    @{
+      if ((oflag & O_CREAT) != 0 && __builtin_va_arg_pack_len () < 1)
+        @{
+          warn_open_missing_mode ();
+          return __open_2 (path, oflag);
+        @}
+      return open (path, oflag, __builtin_va_arg_pack ());
+    @}
+    
+  if (__builtin_va_arg_pack_len () < 1)
+    return __open_2 (path, oflag);
+
+  return open (path, oflag, __builtin_va_arg_pack ());
+@}
+#endif
+@end smallexample
+@end deftypefn
+
 @node Typeof
 @section Referring to a Type with @code{typeof}
 @findex typeof
index e28d0a93fc05392a72d5c372c8e06ba6a214879e..953e6aab619b059672ac4fe7f519dcba7b18b1f7 100644 (file)
@@ -1,3 +1,8 @@
+2007-09-11  Jakub Jelinek  <jakub@redhat.com>
+
+       * gcc.dg/va-arg-pack-len-1.c: New test.
+       * g++.dg/va-arg-pack-len-1.C: New test.
+
 2007-09-11  Michael Matz  <matz@suse.de>
 
        * gcc.dg/tree-ssa/loadpre11.c: Add -fno-tree-cselim to flags.
diff --git a/gcc/testsuite/g++.dg/ext/va-arg-pack-len-1.C b/gcc/testsuite/g++.dg/ext/va-arg-pack-len-1.C
new file mode 100644 (file)
index 0000000..36104cf
--- /dev/null
@@ -0,0 +1,120 @@
+// { dg-do run }
+// { dg-options "-O2" }
+
+#include <stdarg.h>
+
+extern "C" int warn_open_missing_mode (void);
+extern "C" int warn_open_too_many_arguments (void);
+extern "C" void abort (void);
+
+char expected_char;
+
+__attribute__((noinline)) int
+myopen2 (const char *path, int oflag)
+{
+  if (expected_char++ != path[0] || path[1] != '\0')
+    abort ();
+  switch (path[0])
+    {
+    case 'f':
+      if (oflag != 0x2)
+       abort ();
+      break;
+    case 'g':
+      if (oflag != 0x43)
+       abort ();
+      // In real __open_2 this would terminate the program:
+      // open with O_CREAT without third argument.
+      return -6;
+    default:
+      abort ();
+    }
+  return 0;
+}
+
+__attribute__((noinline)) int
+myopenva (const char *path, int oflag, ...)
+{
+  int mode = 0;
+  va_list ap;
+  if ((oflag & 0x40) != 0)
+    {
+      va_start (ap, oflag);
+      mode = va_arg (ap, int);
+      va_end (ap);
+    }
+  if (expected_char++ != path[0] || path[1] != '\0')
+    abort ();
+  switch (path[0])
+    {
+    case 'a':
+      if (oflag != 0x43 || mode != 0644)
+       abort ();
+      break;
+    case 'b':
+      if (oflag != 0x3)
+       abort ();
+      break;
+    case 'c':
+      if (oflag != 0x2)
+       abort ();
+      break;
+    case 'd':
+      if (oflag != 0x43 || mode != 0600)
+       abort ();
+      break;
+    case 'e':
+      if (oflag != 0x3)
+       abort ();
+      break;
+    default:
+      abort ();
+    }
+  return 0;
+}
+
+extern inline __attribute__((always_inline, gnu_inline)) int
+myopen (const char *path, int oflag, ...)
+{
+  if (__builtin_va_arg_pack_len () > 1)
+    warn_open_too_many_arguments ();
+
+  if (__builtin_constant_p (oflag))
+    {
+      if ((oflag & 0x40) != 0 && __builtin_va_arg_pack_len () < 1)
+       {
+         warn_open_missing_mode ();
+         return myopen2 (path, oflag);
+       }
+      return myopenva (path, oflag, __builtin_va_arg_pack ());
+    }
+
+  if (__builtin_va_arg_pack_len () < 1)
+    return myopen2 (path, oflag);
+
+  return myopenva (path, oflag, __builtin_va_arg_pack ());
+}
+
+volatile int l0;
+
+int
+main (void)
+{
+  expected_char = 'a';
+  if (myopen ("a", 0x43, 0644))
+    abort ();
+  if (myopen ("b", 0x3, 0755))
+    abort ();
+  if (myopen ("c", 0x2))
+    abort ();
+  if (myopen ("d", l0 + 0x43, 0600))
+    abort ();
+  if (myopen ("e", l0 + 0x3, 0700))
+    abort ();
+  if (myopen ("f", l0 + 0x2))
+    abort ();
+  // Invalid use of myopen, but only detectable at runtime.
+  if (myopen ("g", l0 + 0x43) != -6)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/va-arg-pack-len-1.c b/gcc/testsuite/gcc.dg/va-arg-pack-len-1.c
new file mode 100644 (file)
index 0000000..7df6380
--- /dev/null
@@ -0,0 +1,120 @@
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#include <stdarg.h>
+
+extern int warn_open_missing_mode (void);
+extern int warn_open_too_many_arguments (void);
+extern void abort (void);
+
+char expected_char;
+
+__attribute__((noinline)) int
+myopen2 (const char *path, int oflag)
+{
+  if (expected_char++ != path[0] || path[1] != '\0')
+    abort ();
+  switch (path[0])
+    {
+    case 'f':
+      if (oflag != 0x2)
+       abort ();
+      break;
+    case 'g':
+      if (oflag != 0x43)
+       abort ();
+      /* In real __open_2 this would terminate the program:
+        open with O_CREAT without third argument.  */
+      return -6;
+    default:
+      abort ();
+    }
+  return 0;
+}
+
+__attribute__((noinline)) int
+myopenva (const char *path, int oflag, ...)
+{
+  int mode = 0;
+  va_list ap;
+  if ((oflag & 0x40) != 0)
+    {
+      va_start (ap, oflag);
+      mode = va_arg (ap, int);
+      va_end (ap);
+    }
+  if (expected_char++ != path[0] || path[1] != '\0')
+    abort ();
+  switch (path[0])
+    {
+    case 'a':
+      if (oflag != 0x43 || mode != 0644)
+       abort ();
+      break;
+    case 'b':
+      if (oflag != 0x3)
+       abort ();
+      break;
+    case 'c':
+      if (oflag != 0x2)
+       abort ();
+      break;
+    case 'd':
+      if (oflag != 0x43 || mode != 0600)
+       abort ();
+      break;
+    case 'e':
+      if (oflag != 0x3)
+       abort ();
+      break;
+    default:
+      abort ();
+    }
+  return 0;
+}
+
+extern inline __attribute__((always_inline, gnu_inline)) int
+myopen (const char *path, int oflag, ...)
+{
+  if (__builtin_va_arg_pack_len () > 1)
+    warn_open_too_many_arguments ();
+
+  if (__builtin_constant_p (oflag))
+    {
+      if ((oflag & 0x40) != 0 && __builtin_va_arg_pack_len () < 1)
+       {
+         warn_open_missing_mode ();
+         return myopen2 (path, oflag);
+       }
+      return myopenva (path, oflag, __builtin_va_arg_pack ());
+    }
+
+  if (__builtin_va_arg_pack_len () < 1)
+    return myopen2 (path, oflag);
+
+  return myopenva (path, oflag, __builtin_va_arg_pack ());
+}
+
+volatile int l0;
+
+int
+main (void)
+{
+  expected_char = 'a';
+  if (myopen ("a", 0x43, 0644))
+    abort ();
+  if (myopen ("b", 0x3, 0755))
+    abort ();
+  if (myopen ("c", 0x2))
+    abort ();
+  if (myopen ("d", l0 + 0x43, 0600))
+    abort ();
+  if (myopen ("e", l0 + 0x3, 0700))
+    abort ();
+  if (myopen ("f", l0 + 0x2))
+    abort ();
+  /* Invalid use of myopen, but only detectable at runtime.  */
+  if (myopen ("g", l0 + 0x43) != -6)
+    abort ();
+  return 0;
+}
index d49c3c8c49055b855090e48c09840fbbb92d634c..d50b2c754efd37b07facda7e0e1bf5d74a65b659 100644 (file)
@@ -867,6 +867,33 @@ copy_bb (copy_body_data *id, basic_block bb, int frequency_scale, int count_scal
                  stmt = *stmtp;
                  update_stmt (stmt);
                }
+             else if (call
+                      && id->call_expr
+                      && (decl = get_callee_fndecl (call))
+                      && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
+                      && DECL_FUNCTION_CODE (decl)
+                         == BUILT_IN_VA_ARG_PACK_LEN)
+               {
+                 /* __builtin_va_arg_pack_len () should be replaced by
+                    the number of anonymous arguments.  */
+                 int nargs = call_expr_nargs (id->call_expr);
+                 tree count, *call_ptr, p;
+
+                 for (p = DECL_ARGUMENTS (id->src_fn); p; p = TREE_CHAIN (p))
+                   nargs--;
+
+                 count = build_int_cst (integer_type_node, nargs);
+                 call_ptr = stmtp;
+                 if (TREE_CODE (*call_ptr) == GIMPLE_MODIFY_STMT)
+                   call_ptr = &GIMPLE_STMT_OPERAND (*call_ptr, 1);
+                 if (TREE_CODE (*call_ptr) == WITH_SIZE_EXPR)
+                   call_ptr = &TREE_OPERAND (*call_ptr, 0);
+                 gcc_assert (*call_ptr == call && call_ptr != stmtp);
+                 *call_ptr = count;
+                 stmt = *stmtp;
+                 update_stmt (stmt);
+                 call = NULL_TREE;
+               }
 
              /* Statements produced by inlining can be unfolded, especially
                 when we constant propagated some operands.  We can't fold
This page took 0.144826 seconds and 5 git commands to generate.