@@ -34,6 +34,22 @@ static int evaluate_to_int_const_expr(struct expression *expr)
return 1;
}
+static int evaluate_pure_unop(struct expression *expr)
+{
+ struct expression *arg = first_expression(expr->args);
+ int flags = arg->flags;
+
+ /*
+ * Allow such functions with a constant integer expression
+ * argument to be treated as a *constant* integer.
+ * This allow us to use them in switch() { case ...:
+ */
+ flags |= (flags & CEF_ICE) ? CEF_SET_INT : 0;
+ expr->flags = flags;
+ return 1;
+}
+
+
static int evaluate_expect(struct expression *expr)
{
/* Should we evaluate it to return the type of the first argument? */
@@ -201,6 +217,7 @@ static int expand_bswap(struct expression *expr, int cost)
}
static struct symbol_op bswap_op = {
+ .evaluate = evaluate_pure_unop,
.expand = expand_bswap,
};
new file mode 100644
@@ -0,0 +1,23 @@
+// requires constant integer expressions
+static int bar[] = {
+ [__builtin_bswap16(0x1234)] = 0, // OK
+ [__builtin_bswap32(0x1234)] = 0, // OK
+ [__builtin_bswap64(0x1234)] = 0, // OK
+};
+
+// requires constant integers
+static int foo(unsigned long long a)
+{
+ switch (a) {
+ case __builtin_bswap16(1 << 8):
+ case __builtin_bswap32(2L << 24):
+ case __builtin_bswap64(3LL << 56):
+ return 0;
+ default:
+ return 1;
+ }
+}
+
+/*
+ * check-name: constness of pure/const builtins
+ */
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- builtin.c | 17 +++++++++++++++++ validation/constexpr-pure-builtin.c | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 validation/constexpr-pure-builtin.c