diff options
author | Andi Kleen <ak@linux.intel.com> | 2024-01-24 04:27:13 -0800 |
---|---|---|
committer | Andi Kleen <ak@linux.intel.com> | 2024-06-11 09:52:28 -0700 |
commit | 53ac88cedf9348b0139fa92c3257b877694f6194 (patch) | |
tree | 5525ad215ef852168853d3f5ffa76a710940c237 /libcpp/init.cc | |
parent | 6ef8c905e0064c4dfb7ca302355fc20cb96b147b (diff) | |
download | gcc-53ac88cedf9348b0139fa92c3257b877694f6194.zip gcc-53ac88cedf9348b0139fa92c3257b877694f6194.tar.gz gcc-53ac88cedf9348b0139fa92c3257b877694f6194.tar.bz2 |
C++: Support constexpr strings for asm statements
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));
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 __GXX_CONSTEXPR_ASM__
gcc/cp/ChangeLog:
* parser.cc (cp_parser_asm_string_expression): New function
to handle constexpr strings for asm.
(cp_parser_asm_definition): Use cp_parser_asm_string_expression.
(cp_parser_yield_expression): Dito.
(cp_parser_asm_specification_opt): Dito.
(cp_parser_asm_operand_list): Dito.
(cp_parser_asm_clobber_list): Dito.
gcc/ChangeLog:
* doc/extend.texi: Document constexpr asm.
gcc/testsuite/ChangeLog:
* g++.dg/ext/asm11.C: Adjust to new error message.
* g++.dg/ext/asm9.C: Dito.
* g++.dg/parse/asm1.C: Dito.
* g++.dg/parse/asm2.C: Dito.
* g++.dg/parse/asm3.C: Dito.
* g++.dg/cpp1z/constexpr-asm-1.C: New test.
* g++.dg/cpp1z/constexpr-asm-2.C: New test.
* g++.dg/cpp1z/constexpr-asm-3.C: New test.
Diffstat (limited to 'libcpp/init.cc')
0 files changed, 0 insertions, 0 deletions