This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[rfa] atomic builtins for c++
- From: Richard Henderson <rth at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org, mark at codesourcery dot com
- Date: Fri, 15 Apr 2005 17:35:23 -0700
- Subject: [rfa] atomic builtins for c++
It appears that this is much simpler than I thought it would be. All the
do-we-have-dependent-types-in-the-argument-list things are already taken
care of at the top of finish_call_expr. And as finish_call_expr is what's
called during template expansion, by putting the call to
resolve_overloaded_builtin after the template handling in finish_call_expr
means don't have to worry about templates at all.
The only thing that's a teeny bit confusing is that is_overloaded_fn is true
for *all* FUNCTION_DECLs. No way I'm touching that with a 100 foot pole.
Thus the somewhat curious placement of this bit of code.
Can you think of anything else I ought to be testing wrt templates?
r~
* semantics.c (finish_call_expr): Call resolve_overloaded_builtin.
Index: cp/semantics.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/semantics.c,v
retrieving revision 1.467
diff -u -p -d -r1.467 semantics.c
--- cp/semantics.c 9 Apr 2005 03:18:18 -0000 1.467
+++ cp/semantics.c 16 Apr 2005 00:26:28 -0000
@@ -1833,8 +1833,16 @@ finish_call_expr (tree fn, tree args, bo
? LOOKUP_NONVIRTUAL : 0));
}
else if (is_overloaded_fn (fn))
- /* A call to a namespace-scope function. */
- result = build_new_function_call (fn, args);
+ {
+ /* If the function is an overloaded builtin, resolve it. */
+ if (TREE_CODE (fn) == FUNCTION_DECL
+ && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
+ result = resolve_overloaded_builtin (fn, args);
+
+ if (!result)
+ /* A call to a namespace-scope function. */
+ result = build_new_function_call (fn, args);
+ }
else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
{
if (args)
@@ -1851,6 +1859,7 @@ finish_call_expr (tree fn, tree args, bo
have an overloaded `operator ()'. */
result = build_new_op (CALL_EXPR, LOOKUP_NORMAL, fn, args, NULL_TREE,
/*overloaded_p=*/NULL);
+
if (!result)
/* A call where the function is unknown. */
result = build_function_call (fn, args);
Index: testsuite/g++.dg/ext/sync-1.C
===================================================================
RCS file: testsuite/g++.dg/ext/sync-1.C
diff -N testsuite/g++.dg/ext/sync-1.C
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/g++.dg/ext/sync-1.C 16 Apr 2005 00:26:28 -0000
@@ -0,0 +1,40 @@
+// Validate that the __sync builtins are overloaded properly.
+// { dg-do compile }
+// { dg-options "-Werror" }
+
+#define TEST1(TYPE, BUILTIN) \
+void t_##TYPE##BUILTIN(TYPE *p) \
+{ \
+ __typeof(BUILTIN(p, 1)) *pp; \
+ pp = p; \
+}
+
+#define TEST2(BUILTIN) \
+ TEST1(int, BUILTIN) \
+ TEST1(long, BUILTIN)
+
+TEST2(__sync_fetch_and_add)
+TEST2(__sync_fetch_and_sub)
+TEST2(__sync_fetch_and_or)
+TEST2(__sync_fetch_and_and)
+TEST2(__sync_fetch_and_xor)
+TEST2(__sync_fetch_and_nand)
+
+TEST2(__sync_add_and_fetch)
+TEST2(__sync_sub_and_fetch)
+TEST2(__sync_or_and_fetch)
+TEST2(__sync_and_and_fetch)
+TEST2(__sync_xor_and_fetch)
+TEST2(__sync_nand_and_fetch)
+
+TEST2(__sync_lock_test_and_set)
+
+#define TEST3(TYPE) \
+void t_##TYPE##__sync_val_compare_and_swap(TYPE *p) \
+{ \
+ __typeof(__sync_val_compare_and_swap(p, 1, 2)) *pp; \
+ pp = p; \
+}
+
+TEST3(int)
+TEST3(long)
Index: testsuite/g++.dg/ext/sync-2.C
===================================================================
RCS file: testsuite/g++.dg/ext/sync-2.C
diff -N testsuite/g++.dg/ext/sync-2.C
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/g++.dg/ext/sync-2.C 16 Apr 2005 00:26:28 -0000
@@ -0,0 +1,58 @@
+// Validate that the __sync builtins are overloaded properly in templates.
+// { dg-do compile }
+// { dg-options "-Werror" }
+
+
+#define TEST1(BUILTIN) \
+template<typename T> \
+void f##BUILTIN(T *p) \
+{ \
+ __typeof(BUILTIN(p, 1)) *pp; \
+ pp = p; \
+}
+
+TEST1(__sync_fetch_and_add)
+TEST1(__sync_fetch_and_sub)
+TEST1(__sync_fetch_and_or)
+TEST1(__sync_fetch_and_and)
+TEST1(__sync_fetch_and_xor)
+TEST1(__sync_fetch_and_nand)
+
+TEST1(__sync_add_and_fetch)
+TEST1(__sync_sub_and_fetch)
+TEST1(__sync_or_and_fetch)
+TEST1(__sync_and_and_fetch)
+TEST1(__sync_xor_and_fetch)
+TEST1(__sync_nand_and_fetch)
+
+TEST1(__sync_lock_test_and_set)
+
+template<typename T>
+void f__sync_val_compare_and_swap(T *p)
+{
+ __typeof(__sync_val_compare_and_swap(p, 1, 2)) *pp;
+ pp = p;
+}
+
+#define TEST2(TYPE) \
+void h_##TYPE () \
+{ \
+ TYPE x; \
+ f__sync_fetch_and_add (&x); \
+ f__sync_fetch_and_sub (&x); \
+ f__sync_fetch_and_or (&x); \
+ f__sync_fetch_and_and (&x); \
+ f__sync_fetch_and_xor (&x); \
+ f__sync_fetch_and_nand (&x); \
+ f__sync_add_and_fetch (&x); \
+ f__sync_sub_and_fetch (&x); \
+ f__sync_or_and_fetch (&x); \
+ f__sync_and_and_fetch (&x); \
+ f__sync_xor_and_fetch (&x); \
+ f__sync_nand_and_fetch (&x); \
+ f__sync_lock_test_and_set (&x); \
+ f__sync_val_compare_and_swap (&x); \
+}
+
+TEST2(int)
+TEST2(long)