On 6/2/24 23:45, Andi Kleen wrote:
Some programing styles use a lot of inline assembler, and it is common
to use very complex preprocessor macros to generate the assembler
strings for the asm statements. In C++ there would be a typesafe alternative
using templates and constexpr to generate the assembler strings, but
unfortunately the asm statement requires plain string literals, so this
doesn't work.
This patch modifies the C++ parser to accept strings generated by
constexpr instead of just plain strings. This requires new syntax
because e.g. asm("..." : "r" (expr)) would be ambigious with a function
call. I chose () to make it unique. For example now you can write
constexpr const char *genasm() { return "insn"; }
constexpr const char *genconstraint() { return "r"; }
asm(genasm() :: (genconstraint()) (input));
Looks plausible. What happens when someone forgets the parens, as seems
a likely mistake?
The constexpr strings are allowed for the asm template, the
constraints and the clobbers (every time current asm accepts a string)
This version allows the same constexprs as C++26 static_assert,
following Jakub's suggestion.
The drawback of this scheme is that the constexpr doesn't have
full control over the input/output/clobber lists, but that can be
usually handled with a switch statement. One could imagine
more flexible ways to handle that, for example supporting constexpr
vectors for the clobber list, or similar. But even without
that it is already useful.
Bootstrapped and full test on x86_64-linux.
gcc/c-family/ChangeLog:
* c-cppbuiltin.cc (c_cpp_builtins): Define
__GNU_CONSTEXPR_ASM__.
__GXX instead of __GNU would be more consistent with the other old
predefined macros.
Jason