Today output operand modifiers are limited to a single character, as in %w0 or
%c1. That works, but it makes the modifier namespace very small and encourages
target ports either to use cryptic letters or to fall back to C output blocks
for cases that are really just target-specific printing.
This patch adds a longer spelling for the same mechanism:
%[name:0]
The bracketed spelling is intentionally familiar rather than completely new:
extended asm already uses %[Value] to refer to an operand with a symbolic name,
as in:
asm ("add %[out], %[in], %[imm]"
: [out] "=r" (out)
: [in] "r" (in), [imm] "I" (1));
That existing form keeps its meaning. A bare %[Value] is still resolved by the
front end as a symbolic operand reference. The new print-operand form puts the
modifier name before a colon and the operand number inside the brackets, as in
%[name:0]. The form %[0] also lets a template separate an operand number from
any literal digits that follow.
The modifier syntax can also be combined with symbolic operand names. In user
asm, %[modifier:Value] means "print operand Value using the multi-character
modifier named modifier". The front end resolves Value to an operand number, so
final output sees the same thing it would have seen for %[modifier:N]. For
example:
asm ("mov %[w:out], %[w:in]"
: [out] "=r" (out)
: [in] "r" (in));
is resolved as if the template had used %[w:0] and %[w:1].
GCC parses the bracketed modifier name and asks the new
TARGET_PRINT_OPERAND_MODIFIER hook for the integer code to pass to
PRINT_OPERAND. The target owns the names and codes, which must be outside the
single-character range. Old-style modifiers keep their existing values, so
existing target hooks continue to see 'w', 'c', etc. for those forms.
For example, a machine description can now write something like:
"extract\t%[lowpart:0], %1"
and the target's PRINT_OPERAND hook can handle "lowpart" as a named modifier.
The parser also allows comma-separated nonempty components in the brackets, but
deliberately leaves the interpretation of those components to the target.
A named modifier can also take several operands, as in %[name:0,1].
PRINT_OPERAND receives the first operand as usual, and can use
output_operand_modifier_operand (code, i) to get the others. All operands in
the list are recorded as referenced, including those that the target prints
through this helper. This requires the use of a temporary global variable.
current_modifier_opnums which is my least favorite part of this but I don't
see another way around it...
The new hook also receives a flag saying whether the modifier came from user asm
or an internal machine-description output template. Targets can reject
internal-only names in user-facing asm while still using the clearer names
internally. The front end checks named modifiers too, so invalid user asm is
rejected even if it would later be optimized away.
This addresses Richard Earnshaw's suggestion by keeping the target hook
interface as an int code and mapping the longer spelling onto values outside the
single-character range. The default hook rejects named modifiers, so targets
that do not opt in keep the existing syntax. Existing hard-coded character uses
continue to work.
It also addresses Richard Sandiford's feedback by giving the new %[...] syntax
its own namespace rather than automatically exposing every single-letter
modifier through the new spelling. The user-asm flag lets a target decide which
named modifiers are public and which are only for compiler-generated templates.
The comma-separated spelling is accepted as a target-defined modifier string, so
a port can later treat, say, %[gpr,zero:0] as a composition of target-specific
printing choices without requiring more middle-end syntax.
I also used a little bit of C++ to clean up the parsing code, the diff shows
a lot of movements but the final code is simpler.
I decided to support the multiple operands case now, as that allowed me to clean
up the CONST_INT cases and while doing so fixing the existing ambiguity with
%[foo]0 being able to resolve to %10.
Bootstrapped Regtested on aarch64-none-linux-gnu,
arm-none-linux-gnueabihf, x86_64-pc-linux-gnu
-m32, -m64 and no issues.
Changes were pre-approved by Richard S.
Pushed.
Thanks,
Tamar
gcc/ChangeLog:
PR target/122716
* doc/extend.texi: Document named asm operand modifiers.
* doc/md.texi: Document bracketed operands and operand lists.
* doc/tm.texi: Regenerate.
* doc/tm.texi.in (PRINT_OPERAND): Document named modifier codes and
output_operand_modifier_operand.
(TARGET_PRINT_OPERAND_MODIFIER): Add hook reference.
* final.cc: Include selftest.h.
(current_modifier_operands, current_modifier_opnums): New variables.
(output_operand_modifier_operand, output_bracketed_operand): New
functions.
(output_asm_insn): Handle bracketed operands.
(selftest::test_parse_bracketed_operand): New test.
(selftest::test_referenced_operands): Likewise.
(selftest::test_output_operand_modifier_operand): Likewise.
(selftest::final_cc_tests): Run them.
* output.h (PRINT_OPERAND_MODIFIER_BASE): New constant.
(output_operand_modifier_operand): Declare.
* recog.cc (parse_bracketed_operand): New function.
(get_referenced_operands): Handle bracketed operands and operand lists.
Handle %ccN and a trailing percent sign.
* recog.h (bracketed_operand): New structure.
(parse_bracketed_operand): Declare.
* selftest-run-tests.cc (selftest::run_tests): Call final_cc_tests and
stmt_cc_tests.
* selftest.h (final_cc_tests, stmt_cc_tests): Declare.
* stmt.cc: Include selftest.h when checking is enabled.
(resolve_operand_name_1): Remove.
(asm_operand_name_number, asm_obstack_grow_opnum): New functions.
(resolve_asm_operand_names): Resolve bracketed operand lists and check
named modifiers. Share bracket parsing and build strings from obstack
lengths.
(selftest::build_asm_operand, selftest::build_asm_label): New helpers.
(selftest::test_asm_operand_modifier): Likewise.
(selftest::test_resolve_asm_operand_names): New test.
(selftest::stmt_cc_tests): Run it.
* target.def (print_operand_modifier): New hook.
* targhooks.cc (default_print_operand_modifier): New function.
* targhooks.h (default_print_operand_modifier): Declare.
---
--
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index
961fa27ba0eb409f3c4cb4f5856148d84273dad6..d0468f43603ed8c423a00f7c0f475f0b12104c54
100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -12060,6 +12060,12 @@ on all targets. Other modifiers are hardware
dependent.
For example, the list of supported modifiers for x86 is found at
@ref{x86Operandmodifiers,x86 Operand modifiers}.
+Targets can also provide multi-character modifiers, written as
+@samp{%[@var{modifier}:2]}. If the operand has a symbolic name, write
+@samp{%[@var{modifier}:@var{asmSymbolicName}]}.
+@samp{%[@var{name}]} without a modifier separator continues to mean the
+operand named @var{name}.
+
If the C code that follows the @code{asm} makes no use of any of the output
operands, use @code{volatile} for the @code{asm} statement to prevent the
optimizers from discarding the @code{asm} statement as unneeded
diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index
a22365db6aafd2cc2d7f294bf0a1c7b39b88b191..3fbab7f846bca6bd5a3187b1110884749846d4c8
100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -524,6 +524,39 @@ alternate fashion. Four letters have standard, built-in
meanings described
below. The machine description macro @code{PRINT_OPERAND} can define
additional letters with nonstandard meanings.
+@samp{%[@var{n}]} is another way of writing operand @var{n}. The brackets
+separate the operand number from any literal digits that follow it.
+
+@samp{%[@var{name}:@var{n}]} is the same idea as
+@samp{%@var{letter}@var{n}}, but with a multi-character target-specific
+modifier named @var{name}. GCC parses the bracketed name and passes it to
+@code{PRINT_OPERAND} as an opaque integer code supplied by the target's
+@code{TARGET_PRINT_OPERAND_MODIFIER} hook. This form is
+useful when a single-letter modifier would be too cryptic, or when a target
+has used most of its single-letter namespace. The modifier name can include
+text substituted by the machine description reader, such as mode attributes.
+It can also contain comma-separated nonempty components; GCC records the
+full text before the colon and leaves any further interpretation to the
+target.
+
+The operand part can list more than one operand, as in
+@samp{%[@var{name}:@var{n},@var{m}]}. @code{PRINT_OPERAND} receives
+@var{n}; the target can use @code{output_operand_modifier_operand} to
+access either operand. Operands in a list must be numbered.
+
+For example, a target could write:
+
+@example
+(define_insn "example_extract"
+ [(set (match_operand:SI 0 "register_operand" "=r")
+ (match_operand:SI 1 "register_operand" "r"))]
+ ""
+ "extract\t%[lowpart:0], %1")
+@end example
+
+@noindent
+and then define @samp{lowpart} in its @code{PRINT_OPERAND} hook.
+
@samp{%c@var{digit}} can be used to substitute an operand that is a
constant value without the syntax that normally indicates an immediate
operand.
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index
09512a07a87c6bd09a239a6fe312765c007e62cf..1ced86f610a0f0c3e3c0315f52a742b25825dade
100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -9863,6 +9863,22 @@ the @samp{%} specification that was used to request
printing of the
operand. If the specification was just @samp{%@var{digit}} then
@var{code} is 0; if the specification was @samp{%@var{ltr}
@var{digit}} then @var{code} is the ASCII code for @var{ltr}.
+If the specification used the multi-character form
+@samp{%[@var{name}:@var{digit}]}, then @var{code} is the value
+returned by @code{TARGET_PRINT_OPERAND_MODIFIER}.
+User @code{asm} statements can combine this form with symbolic operand names
+by writing @samp{%[@var{name}:@var{asmSymbolicName}]}. The front end
+resolves the symbolic operand name before final output, so the
+@code{PRINT_OPERAND} implementation still sees the same @var{code} as it
+would for @samp{%[@var{name}:@var{digit}]}.
+
+@findex output_operand_modifier_operand
+For @samp{%[@var{name}:@var{n},@var{m}]}, use
+@code{output_operand_modifier_operand (@var{code}, @var{i})} to access
+operand @var{i} in the list, counting from zero, using the @var{code}
+passed to @code{PRINT_OPERAND}. It resolves @code{SUBREG}s and records
+symbol references as @code{output_operand} does. It returns null if the
+position is absent.
@findex reg_names
If @var{x} is a register, this macro should print the register's name.
@@ -9876,6 +9892,13 @@ with a null pointer for @var{x} and the punctuation
character for
@var{code}.
@end defmac
+@deftypefn {Target Hook} int TARGET_PRINT_OPERAND_MODIFIER (string_slice
@var{name}, bool @var{user_p})
+Return the code to pass to @code{TARGET_PRINT_OPERAND} for the named
+modifier @var{name}. @var{user_p} is true for a modifier in a user
+@code{asm}. Return zero if the modifier is unavailable in that context.
+Valid codes must be at least @code{PRINT_OPERAND_MODIFIER_BASE}.
+@end deftypefn
+
@defmac PRINT_OPERAND_PUNCT_VALID_P (@var{code})
A C expression which evaluates to true if @var{code} is a valid
punctuation character for use in the @code{PRINT_OPERAND} macro. If
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index
9faacb28781d6058992a547b2a8bba3888091fad..dc1b8efd8f1c03f7218ce88092123f3eb3622468
100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -6358,6 +6358,22 @@ the @samp{%} specification that was used to request
printing of the
operand. If the specification was just @samp{%@var{digit}} then
@var{code} is 0; if the specification was @samp{%@var{ltr}
@var{digit}} then @var{code} is the ASCII code for @var{ltr}.
+If the specification used the multi-character form
+@samp{%[@var{name}:@var{digit}]}, then @var{code} is the value
+returned by @code{TARGET_PRINT_OPERAND_MODIFIER}.
+User @code{asm} statements can combine this form with symbolic operand names
+by writing @samp{%[@var{name}:@var{asmSymbolicName}]}. The front end
+resolves the symbolic operand name before final output, so the
+@code{PRINT_OPERAND} implementation still sees the same @var{code} as it
+would for @samp{%[@var{name}:@var{digit}]}.
+
+@findex output_operand_modifier_operand
+For @samp{%[@var{name}:@var{n},@var{m}]}, use
+@code{output_operand_modifier_operand (@var{code}, @var{i})} to access
+operand @var{i} in the list, counting from zero, using the @var{code}
+passed to @code{PRINT_OPERAND}. It resolves @code{SUBREG}s and records
+symbol references as @code{output_operand} does. It returns null if the
+position is absent.
@findex reg_names
If @var{x} is a register, this macro should print the register's name.
@@ -6371,6 +6387,8 @@ with a null pointer for @var{x} and the punctuation
character for
@var{code}.
@end defmac
+@hook TARGET_PRINT_OPERAND_MODIFIER
+
@defmac PRINT_OPERAND_PUNCT_VALID_P (@var{code})
A C expression which evaluates to true if @var{code} is a valid
punctuation character for use in the @code{PRINT_OPERAND} macro. If
diff --git a/gcc/final.cc b/gcc/final.cc
index
ab7fba83176019af762d2e2356d22265a4f5da5a..ab06738c5a6acc2bba6312741dd676705c1eeb2b
100644
--- a/gcc/final.cc
+++ b/gcc/final.cc
@@ -84,6 +84,7 @@ along with GCC; see the file COPYING3. If not see
#include "common/common-target.h"
#include "diagnostic.h"
#include "diagnostics/file-cache.h"
+#include "selftest.h"
#include "dwarf2out.h"
@@ -3393,6 +3394,70 @@ do_assembler_dialects (const char *p, int *dialect)
}
#endif
+static rtx *current_modifier_operands;
+static const vec<unsigned long> *current_modifier_opnums;
+
+/* Return the Nth operand of the current multi-operand modifier. */
+
+rtx
+output_operand_modifier_operand (int code, unsigned int n)
+{
+ if (code < PRINT_OPERAND_MODIFIER_BASE
+ || !current_modifier_opnums
+ || n >= current_modifier_opnums->length ())
+ return nullptr;
+ rtx x = current_modifier_operands[(*current_modifier_opnums)[n]];
+ if (x && GET_CODE (x) == SUBREG)
+ x = alter_subreg (&x, true);
+ if (x)
+ mark_symbol_refs_as_used (x);
+ return x;
+}
+
+/* Output OPERAND, which was parsed from a %[...] reference. */
+
+static void
+output_bracketed_operand (bracketed_operand &operand, rtx *operands,
+ int *oporder, char *opoutput, int &ops)
+{
+ if (this_is_asm_operands)
+ for (unsigned long opnum : operand.opnums)
+ if (opnum >= insn_noperands)
+ {
+ output_operand_lossage ("operand number out of range");
+ return;
+ }
+ unsigned long opnum = operand.opnums[0];
+ if (!operand.modifier.empty ())
+ {
+ int code = targetm.asm_out.print_operand_modifier
+ (operand.modifier, this_is_asm_operands != NULL);
+ if (code < PRINT_OPERAND_MODIFIER_BASE)
+ {
+ output_operand_lossage ("invalid operand modifier '%.*s'",
+ (int) operand.modifier.size (),
+ operand.modifier.begin ());
+ return;
+ }
+ rtx *saved_operands = current_modifier_operands;
+ const vec<unsigned long> *saved_opnums = current_modifier_opnums;
+ current_modifier_operands = operands;
+ current_modifier_opnums = &operand.opnums;
+ output_operand (operands[opnum], code);
+ current_modifier_operands = saved_operands;
+ current_modifier_opnums = saved_opnums;
+ }
+ else
+ output_operand (operands[opnum], 0);
+
+ for (unsigned long number : operand.opnums)
+ {
+ if (!opoutput[number])
+ oporder[ops++] = number;
+ opoutput[number] = 1;
+ }
+}
+
/* Output text from TEMPLATE to the assembler output file,
obeying %-directions to substitute operands taken from
the vector OPERANDS.
@@ -3486,6 +3551,18 @@ output_asm_insn (const char *templ, rtx *operands)
p++;
fprintf (asm_out_file, "%d", insn_counter);
}
+ else if (*p == '[')
+ {
+ bracketed_operand operand;
+ if (!parse_bracketed_operand (&p, operand))
+ {
+ output_operand_lossage ("invalid %%[...] operand modifier");
+ break;
+ }
+ output_bracketed_operand (operand, operands, oporder, opoutput,
+ ops);
+ c = *p;
+ }
/* % followed by a letter and some digits
outputs an operand in a special way depending on the letter.
Letters `acln' are implemented directly.
@@ -4632,3 +4709,105 @@ collect_fn_hard_reg_usage (void)
node->function_used_regs = function_used_regs;
}
+
+#if CHECKING_P
+
+namespace selftest {
+
+static void
+test_parse_bracketed_operand ()
+{
+ const struct
+ {
+ const char *text;
+ const char *modifier;
+ unsigned long opnums[2];
+ unsigned int count;
+ } valid[] = {
+ { "[10]", "", { 10 }, 1 },
+ { "[fmt:11]", "fmt", { 11 }, 1 },
+ { "[gpr,zero:13]", "gpr,zero", { 13 }, 1 },
+ { "[fmt:13,14]", "fmt", { 13, 14 }, 2 },
+ { "[999999999999999999999999999999999999]", "", { ULONG_MAX }, 1 }
+ };
+ bracketed_operand operand;
+ for (const auto &test : valid)
+ {
+ const char *p = test.text;
+ ASSERT_TRUE (parse_bracketed_operand (&p, operand));
+ ASSERT_TRUE (operand.modifier == test.modifier);
+ ASSERT_EQ (test.count, operand.opnums.length ());
+ for (unsigned int i = 0; i < test.count; ++i)
+ ASSERT_EQ (test.opnums[i], operand.opnums[i]);
+ ASSERT_EQ ('\0', *p);
+ }
+ const char *invalid[] = {
+ "[fmt]12", "[fmt:1,]", "[fmt]", "[fmt:]", "[]0", "[0,1]",
+ "[fmt:12x]", "[fmt:1", "[:1]", "[,fmt:1]", "[fmt,:1]", "[fmt,,part:1]"
+ };
+ for (const char *text : invalid)
+ {
+ const char *p = text;
+ ASSERT_FALSE (parse_bracketed_operand (&p, operand));
+ }
+}
+
+/* The same syntax must be understood by the operand reference scanner. */
+
+static void
+test_referenced_operands ()
+{
+ const struct
+ {
+ const char *text;
+ unsigned int mask;
+ } tests[] = {
+ { "%[0] %[fmt:1,2]", 7 },
+ { "%%[0] %%[fmt:1,2]", 0 },
+ { "%[1]0 %2", 6 },
+ { "%0 %a1 %cc2", 7 },
+ { "%[fmt:2,2] %[99]", 4 },
+ { "%[fmt:1,]", 0 },
+ { "%", 0 }
+ };
+ for (const auto &test : tests)
+ {
+ bool used[3];
+ get_referenced_operands (test.text, used, 3);
+ for (unsigned int i = 0; i < 3; ++i)
+ ASSERT_EQ ((test.mask & (1U << i)) != 0, used[i]);
+ }
+}
+
+static void
+test_output_operand_modifier_operand ()
+{
+ rtx operands[] = { GEN_INT (10),
+ gen_rtx_SUBREG (QImode, gen_rtx_REG (HImode, 0), 0) };
+ auto_vec<unsigned long, 2> opnums;
+ opnums.safe_push (1);
+ opnums.safe_push (0);
+ current_modifier_operands = operands;
+ current_modifier_opnums = &opnums;
+ rtx x = output_operand_modifier_operand (PRINT_OPERAND_MODIFIER_BASE, 0);
+ ASSERT_TRUE (REG_P (x));
+ ASSERT_EQ (QImode, GET_MODE (x));
+ ASSERT_EQ (operands[0], output_operand_modifier_operand
+ (PRINT_OPERAND_MODIFIER_BASE, 1));
+ ASSERT_EQ (nullptr, output_operand_modifier_operand
+ (PRINT_OPERAND_MODIFIER_BASE, 2));
+ current_modifier_operands = nullptr;
+ current_modifier_opnums = nullptr;
+}
+
+void
+final_cc_tests ()
+{
+ test_parse_bracketed_operand ();
+ test_referenced_operands ();
+ test_output_operand_modifier_operand ();
+}
+
+} // namespace selftest
+
+#endif /* CHECKING_P */
diff --git a/gcc/output.h b/gcc/output.h
index
1f628ea2371aeca006d686e521a04d4f7ddb6e2d..e0fdc7bb73923d55614a8b7af41cd4be75ca0d0b
100644
--- a/gcc/output.h
+++ b/gcc/output.h
@@ -80,6 +80,8 @@ extern rtx alter_subreg (rtx *, bool);
/* Print an operand using machine-dependent assembler syntax. */
extern void output_operand (rtx, int);
+constexpr int PRINT_OPERAND_MODIFIER_BASE = UCHAR_MAX + 1;
+extern rtx output_operand_modifier_operand (int, unsigned int);
/* Report inconsistency between the assembler template and the operands.
In an `asm', it's the user's fault; otherwise, the compiler's fault. */
diff --git a/gcc/recog.cc b/gcc/recog.cc
index
ddfffdac0ffa23e5c890ebac897291ee1fb8c20a..388aeeac8ae6766084def082209dc0633ab8631d
100644
--- a/gcc/recog.cc
+++ b/gcc/recog.cc
@@ -2234,6 +2234,52 @@ decode_asm_operands (rtx body, rtx *operands, rtx
**operand_locs,
return ASM_OPERANDS_TEMPLATE (asmop);
}
+/* Parse an operand reference that starts with %[...].
+
+ The forms are %[N] and %[MODIFIER:N[,N...]]. */
+
+bool
+parse_bracketed_operand (const char **p, bracketed_operand &operand)
+{
+ const char *start = *p;
+ gcc_assert (*start == '[');
+ operand.opnums.truncate (0);
+
+ const char *end = strchr (start + 1, ']');
+ if (!end)
+ return false;
+
+ const char *contents = start + 1;
+ const char *colon = (const char *) memchr (contents, ':', end - contents);
+
+ if (colon)
+ {
+ if (colon == contents)
+ return false;
+ for (const char *q = contents; q != colon; ++q)
+ if (*q == ',' && (q == contents || q[-1] == ',' || q + 1 == colon))
+ return false;
+ }
+
+ const char *number = colon ? colon + 1 : contents;
+ char *next;
+ do
+ {
+ if (!ISDIGIT (*number))
+ return false;
+ unsigned long opnum = strtoul (number, &next, 10);
+ if (next != end && (!colon || *next != ','))
+ return false;
+ operand.opnums.safe_push (opnum);
+ number = next + 1;
+ }
+ while (next != end);
+
+ operand.modifier = string_slice (contents, colon ? colon : contents);
+ *p = end + 1;
+ return true;
+}
+
/* Parse inline assembly string STRING and determine which operands are
referenced by % markers. For the first NOPERANDS operands, set USED[I]
to true if operand I is referenced.
@@ -2257,7 +2303,20 @@ get_referenced_operands (const char *string, bool *used,
{
case '%':
p += 1;
+ if (*p == '[')
+ {
+ bracketed_operand operand;
+ if (parse_bracketed_operand (&p, operand))
+ {
+ for (unsigned long opnum : operand.opnums)
+ if (opnum < noperands)
+ used[opnum] = true;
+ break;
+ }
+ }
/* A letter followed by a digit indicates an operand number. */
+ if (p[0] == 'c' && p[1] == 'c')
+ p += 1;
if (ISALPHA (p[0]) && ISDIGIT (p[1]))
p += 1;
if (ISDIGIT (*p))
@@ -2268,7 +2327,7 @@ get_referenced_operands (const char *string, bool *used,
used[opnum] = true;
p = endptr;
}
- else
+ else if (*p)
p += 1;
break;
diff --git a/gcc/recog.h b/gcc/recog.h
index
70fa58910cacf411888470319430c2e678ee7a9b..7e805c718457f5abb2dda9007ca58cb64fe8615b
100644
--- a/gcc/recog.h
+++ b/gcc/recog.h
@@ -23,6 +23,15 @@ along with GCC; see the file COPYING3. If not see
/* For enum tree_code ERROR_MARK. */
#include "tree.h"
+/* An operand reference of the form %[N] or %[MODIFIER:N[,N...]]. */
+struct bracketed_operand
+{
+ string_slice modifier;
+ auto_vec<unsigned long, 2> opnums;
+};
+
+extern bool parse_bracketed_operand (const char **, bracketed_operand &);
+
/* Random number that should be large enough for all purposes. Also define
a type that has at least MAX_RECOG_ALTERNATIVES + 1 bits, with the extra
bit giving an invalid value that can be used to mean "uninitialized". */
diff --git a/gcc/selftest-run-tests.cc b/gcc/selftest-run-tests.cc
index
07eb984e918eeb56b7c832c0c87794647b47b5c6..53877889a0f5b3509004b061f8474e2c394e160b
100644
--- a/gcc/selftest-run-tests.cc
+++ b/gcc/selftest-run-tests.cc
@@ -106,11 +106,13 @@ selftest::run_tests ()
/* Higher-level tests, or for components that other selftests don't
rely on. */
fold_const_cc_tests ();
+ final_cc_tests ();
spellcheck_cc_tests ();
spellcheck_tree_cc_tests ();
tree_cfg_cc_tests ();
simple_diagnostic_path_cc_tests ();
attribs_cc_tests ();
+ stmt_cc_tests ();
path_coverage_cc_tests ();
/* This one relies on most of the above. */
diff --git a/gcc/selftest.h b/gcc/selftest.h
index
acfc4a46e49b42cb1a1dde7043f94b22bc134181..3e47ac77d1ed1a65059e2bfcd4f7f52dc654dd7c
100644
--- a/gcc/selftest.h
+++ b/gcc/selftest.h
@@ -225,6 +225,7 @@ extern void digraph_cc_tests ();
extern void dumpfile_cc_tests ();
extern void et_forest_cc_tests ();
extern void fibonacci_heap_cc_tests ();
+extern void final_cc_tests ();
extern void fold_const_cc_tests ();
extern void function_tests_cc_tests ();
extern void gcc_attribute_urlifier_cc_tests ();
@@ -264,6 +265,7 @@ extern void spellcheck_cc_tests ();
extern void spellcheck_tree_cc_tests ();
extern void splay_tree_cc_tests ();
extern void sreal_cc_tests ();
+extern void stmt_cc_tests ();
extern void store_merging_cc_tests ();
extern void tree_cc_tests ();
extern void tree_cfg_cc_tests ();
diff --git a/gcc/stmt.cc b/gcc/stmt.cc
index
c3f8b7ce3f20f27c8c679ea9a3299d182c6e080d..78fe35b14e82463083a453751f8ffbc84da23921
100644
--- a/gcc/stmt.cc
+++ b/gcc/stmt.cc
@@ -56,6 +56,10 @@ along with GCC; see the file COPYING3. If not see
#include "builtins.h"
#include "cfgexpand.h"
+#if CHECKING_P
+#include "selftest.h"
+#endif
+
/* Functions and data structures for expanding case statements. */
@@ -86,7 +90,6 @@ public:
};
static bool check_unique_operand_names (tree, tree, tree);
-static char *resolve_operand_name_1 (char *, tree, tree, tree);
/* Return the rtx-label that corresponds to a LABEL_DECL,
creating it if necessary. */
@@ -869,149 +872,255 @@ check_unique_operand_names (tree outputs, tree inputs,
tree labels)
return false;
}
-/* Resolve the names of the operands in *POUTPUTS and *PINPUTS to numbers,
- and replace the name expansions in STRING and in the constraints to
- those numbers. This is generally done in the front end while creating
- the ASM_EXPR generic tree that eventually becomes the GIMPLE_ASM. */
+/* Find the operand named by [START, END), including in/out operands and
+ labels in the numbering. */
+
+static int
+asm_operand_name_number (const char *start, const char *end,
+ tree outputs, tree inputs, tree labels)
+{
+ string_slice operand_name (start, end);
+ int op = 0;
+ int op_inout = 0;
+
+ /* Outputs come first. Count read/write outputs as we go: each one
+ also creates an implicit input, which affects the label numbers. */
+ for (tree t = outputs; t; t = TREE_CHAIN (t), op++)
+ {
+ tree name = TREE_PURPOSE (TREE_PURPOSE (t));
+ if (name && operand_name == TREE_STRING_POINTER (name))
+ return op;
+ tree constraint = TREE_VALUE (TREE_PURPOSE (t));
+ if (constraint && strchr (TREE_STRING_POINTER (constraint), '+'))
+ op_inout++;
+ }
+ /* Explicit inputs follow the outputs, before the implicit inputs. */
+ for (tree t = inputs; t; t = TREE_CHAIN (t), op++)
+ {
+ tree name = TREE_PURPOSE (TREE_PURPOSE (t));
+ if (name && operand_name == TREE_STRING_POINTER (name))
+ return op;
+ }
+ /* Labels follow all inputs, including those from read/write outputs.
+ Their names are stored directly in TREE_PURPOSE. */
+ op += op_inout;
+ for (tree t = labels; t; t = TREE_CHAIN (t), op++)
+ {
+ tree name = TREE_PURPOSE (t);
+ if (name && operand_name == TREE_STRING_POINTER (name))
+ return op;
+ }
+
+ /* Only the diagnostic needs a null-terminated copy of the name. */
+ char *name = xstrndup (start, end - start);
+ error ("undefined named operand %qs",
+ identifier_to_locale (name));
+ free (name);
+ return 0;
+}
+
+/* Append OPNUM in decimal form to OB. */
+
+static void
+asm_obstack_grow_opnum (struct obstack *ob, int opnum)
+{
+ char number[HOST_BITS_PER_INT * 3 / 10 + 3];
+ int n = sprintf (number, "%d", opnum);
+ obstack_grow (ob, number, n);
+}
+
+/* Resolve names in an asm template and its input constraints. This is
+ normally done while the front end builds the ASM_EXPR. */
tree
resolve_asm_operand_names (tree string, tree outputs, tree inputs, tree labels)
{
- char *buffer;
- char *p;
- const char *c;
- tree t;
-
check_unique_operand_names (outputs, inputs, labels);
- /* Substitute [<name>] in input constraint strings. There should be no
- named operands in output constraints. */
- for (t = inputs; t ; t = TREE_CHAIN (t))
+ /* Substitute names in input constraints, removing their brackets. */
+ for (tree t = inputs; t; t = TREE_CHAIN (t))
{
- c = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
- if (strchr (c, '[') != NULL)
+ const char *input = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
+ if (!strchr (input, '['))
+ continue;
+
+ struct obstack ob;
+ gcc_obstack_init (&ob);
+ const char *p = input;
+ while (const char *open = strchr (p, '['))
{
- p = buffer = xstrdup (c);
- while ((p = strchr (p, '[')) != NULL)
- p = resolve_operand_name_1 (p, outputs, inputs, NULL);
- TREE_VALUE (TREE_PURPOSE (t))
- = build_string (strlen (buffer), buffer);
- free (buffer);
+ obstack_grow (&ob, p, open - p);
+ const char *close = strchr (open + 1, ']');
+ if (!close)
+ {
+ error ("missing close brace for named operand");
+ p = open;
+ break;
+ }
+ asm_obstack_grow_opnum (&ob,
+ asm_operand_name_number (open + 1, close,
+ outputs, inputs,
+ nullptr));
+ p = close + 1;
}
+ obstack_grow (&ob, p, strlen (p));
+ TREE_VALUE (TREE_PURPOSE (t))
+ = build_string (obstack_object_size (&ob), (char *) obstack_base (&ob));
+ obstack_free (&ob, nullptr);
}
- /* Now check for any needed substitutions in the template. */
- c = TREE_STRING_POINTER (string);
- while ((c = strchr (c, '%')) != NULL)
+ const char *input = TREE_STRING_POINTER (string);
+ if (!strchr (input, '%'))
+ return string;
+
+ struct obstack ob;
+ gcc_obstack_init (&ob);
+ const char *p = input;
+ while (const char *percent = strchr (p, '%'))
{
- if (c[1] == '[')
- break;
- else if (ISALPHA (c[1])
- && (c[2] == '[' || (ISALPHA (c[2]) && c[3] == '[')))
- break;
- else
+ obstack_grow (&ob, p, percent - p);
+ p = percent;
+ const char *open = p + 1;
+ if (ISALPHA (*open))
+ {
+ ++open;
+ if (ISALPHA (*open))
+ ++open;
+ }
+ if (*open != '[')
{
- c += 1 + (c[1] == '%');
+ int len = 1 + (p[1] == '%');
+ obstack_grow (&ob, p, len);
+ p += len;
continue;
}
- }
- if (c)
- {
- /* OK, we need to make a copy so we can perform the substitutions.
- Assume that we will not need extra space--we get to remove '['
- and ']', which means we cannot have a problem until we have more
- than 999 operands. */
- buffer = xstrdup (TREE_STRING_POINTER (string));
- p = buffer + (c - TREE_STRING_POINTER (string));
-
- while ((p = strchr (p, '%')) != NULL)
+ bool remove_brackets = open != p + 1;
+ const char *name_start = open + 1;
+ const char *close = strchr (name_start, ']');
+ if (!close)
{
- if (p[1] == '[')
- p += 1;
- else if (ISALPHA (p[1]) && p[2] == '[')
- p += 2;
- else if (ISALPHA (p[1]) && ISALPHA (p[2]) && p[3] == '[')
- p += 3;
- else
+ error ("missing close brace for named operand");
+ break;
+ }
+ if (!remove_brackets)
+ {
+ if (const char *colon
+ = (const char *) memchr (name_start, ':', close - name_start))
{
- p += 1 + (p[1] == '%');
+ /* Validate modifiers even in asms that are optimized away. */
+ string_slice name (name_start, colon);
+ if (targetm.asm_out.print_operand_modifier (name, true)
+ < PRINT_OPERAND_MODIFIER_BASE)
+ error ("invalid operand modifier %qB", &name);
+ name_start = colon + 1;
+ }
+ /* Symbolic operand names cannot start with a digit. Leave
+ numbered operands and lists for final output to parse. */
+ if (ISDIGIT (*name_start))
+ {
+ obstack_grow (&ob, p, close + 1 - p);
+ p = close + 1;
continue;
}
-
- p = resolve_operand_name_1 (p, outputs, inputs, labels);
}
-
- string = build_string (strlen (buffer), buffer);
- free (buffer);
+ /* Preserve brackets in %[name] and %[modifier:name], so following
+ literal digits cannot become part of the operand number. */
+ obstack_grow (&ob, p, name_start - p - remove_brackets);
+ asm_obstack_grow_opnum (&ob,
+ asm_operand_name_number (name_start, close,
+ outputs, inputs,
+ labels));
+ p = close + remove_brackets;
}
-
+ obstack_grow (&ob, p, strlen (p));
+ string = build_string (obstack_object_size (&ob), (char *) obstack_base
(&ob));
+ obstack_free (&ob, nullptr);
return string;
}
-/* A subroutine of resolve_operand_names. P points to the '[' for a
- potential named operand of the form [<name>]. In place, replace
- the name and brackets with a number. Return a pointer to the
- balance of the string after substitution. */
+#if CHECKING_P
+
+namespace selftest {
-static char *
-resolve_operand_name_1 (char *p, tree outputs, tree inputs, tree labels)
+static tree
+build_asm_operand (const char *name, const char *constraint)
{
- char *q;
- int op, op_inout;
- tree t;
+ tree name_tree = name ? build_string (strlen (name), name) : NULL_TREE;
+ tree constraint_tree = build_string (strlen (constraint), constraint);
+ return tree_cons (tree_cons (name_tree, constraint_tree, NULL_TREE),
+ integer_zero_node, NULL_TREE);
+}
- /* Collect the operand name. */
- q = strchr (++p, ']');
- if (!q)
- {
- error ("missing close brace for named operand");
- return strchr (p, '\0');
- }
- *q = '\0';
+static tree
+build_asm_label (const char *name)
+{
+ return tree_cons (build_string (strlen (name), name), integer_zero_node,
+ NULL_TREE);
+}
- /* Resolve the name to a number. */
- for (op_inout = op = 0, t = outputs; t ; t = TREE_CHAIN (t), op++)
- {
- tree name = TREE_PURPOSE (TREE_PURPOSE (t));
- if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
- goto found;
- tree constraint = TREE_VALUE (TREE_PURPOSE (t));
- if (constraint && strchr (TREE_STRING_POINTER (constraint), '+') != NULL)
- op_inout++;
- }
- for (t = inputs; t ; t = TREE_CHAIN (t), op++)
- {
- tree name = TREE_PURPOSE (TREE_PURPOSE (t));
- if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
- goto found;
- }
- op += op_inout;
- for (t = labels; t ; t = TREE_CHAIN (t), op++)
- {
- tree name = TREE_PURPOSE (t);
- if (name && strcmp (TREE_STRING_POINTER (name), p) == 0)
- goto found;
- }
+/* Keep these tests independent of the target's modifier namespace. */
- error ("undefined named operand %qs", identifier_to_locale (p));
- op = 0;
+static int
+test_asm_operand_modifier (string_slice name, bool user_p)
+{
+ ASSERT_TRUE (user_p);
+ return name == "w" || name == "wide" ? PRINT_OPERAND_MODIFIER_BASE : 0;
+}
- found:
- /* Replace the name with the number. Unfortunately, not all libraries
- get the return value of sprintf correct, so search for the end of the
- generated string by hand. */
- sprintf (--p, "%d", op);
- p = strchr (p, '\0');
+static void
+test_resolve_asm_operand_names ()
+{
+ tree outputs = build_asm_operand ("out", "=r");
+ tree inputs = build_asm_operand ("in", "[out]");
+ tree labels = build_asm_label ("lab");
+ auto saved_hook = targetm.asm_out.print_operand_modifier;
+ targetm.asm_out.print_operand_modifier = test_asm_operand_modifier;
+
+ const char *tests[][2] = {
+ { "%[out] %w[out] %cc[in] %l[lab]", "%[0] %w0 %cc1 %l2" },
+ { "%[out]0 %[in]1", "%[0]0 %[1]1" },
+ { "%[10] %[0]", "%[10] %[0]" },
+ { "%[w:out] %[w:in]", "%[w:0] %[w:1]" },
+ { "%[wide:out] %[wide:0,1]", "%[wide:0] %[wide:0,1]" },
+ { "%%[out] %%[w:in] %[w:0,1]", "%%[out] %%[w:in] %[w:0,1]" }
+ };
+ for (const auto &test : tests)
+ {
+ tree string = build_string (strlen (test[0]), test[0]);
+ tree result = resolve_asm_operand_names (string, outputs, inputs,
labels);
+ ASSERT_STREQ (test[1], TREE_STRING_POINTER (result));
+ }
+ ASSERT_STREQ ("0", TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (inputs))));
+
+ /* Unlike the old %[name] syntax, %[modifier:name] can grow when a
+ short name is replaced by a two-digit operand number. */
+ tree many_outputs = build_asm_operand ("x", "=r");
+ for (int i = 0; i < 10; ++i)
+ many_outputs = chainon (build_asm_operand (nullptr, "=r"), many_outputs);
+ const char *templ = "%[w:x]";
+ tree string = build_string (strlen (templ), templ);
+ tree result = resolve_asm_operand_names (string, many_outputs, nullptr,
+ nullptr);
+ ASSERT_STREQ ("%[w:10]", TREE_STRING_POINTER (result));
+
+ TREE_VALUE (TREE_PURPOSE (outputs)) = build_string (2, "+r");
+ templ = "%l[lab]";
+ string = build_string (strlen (templ), templ);
+ result = resolve_asm_operand_names (string, outputs, inputs, labels);
+ ASSERT_STREQ ("%l3", TREE_STRING_POINTER (result));
+ targetm.asm_out.print_operand_modifier = saved_hook;
+}
- /* Verify the no extra buffer space assumption. */
- gcc_assert (p <= q);
+void
+stmt_cc_tests ()
+{
+ test_resolve_asm_operand_names ();
+}
- /* Shift the rest of the buffer down to fill the gap. */
- memmove (p, q + 1, strlen (q + 1) + 1);
+} // namespace selftest
- return p;
-}
+#endif /* CHECKING_P */
/* Generate RTL to return directly from the current function.
diff --git a/gcc/target.def b/gcc/target.def
index
20c7f111295d414fb7c62826550ab96ac1452e78..fc2671c29540a9ff218dcf64b316b2949db71c43
100644
--- a/gcc/target.def
+++ b/gcc/target.def
@@ -895,6 +895,16 @@ DEFHOOK_UNDOC
void, (FILE *file, rtx x, int code),
default_print_operand)
+/* Validate a multi-character operand modifier and return its target code. */
+DEFHOOK
+(print_operand_modifier,
+ "Return the code to pass to @code{TARGET_PRINT_OPERAND} for the named\n\
+modifier @var{name}. @var{user_p} is true for a modifier in a user\n\
+@code{asm}. Return zero if the modifier is unavailable in that context.\n\
+Valid codes must be at least @code{PRINT_OPERAND_MODIFIER_BASE}.",
+ int, (string_slice name, bool user_p),
+ default_print_operand_modifier)
+
/* Emit a machine-specific memory address. */
/* ??? tm.texi only documents the old macro PRINT_OPERAND_ADDRESS,
not this hook, and uses different argument names. */
diff --git a/gcc/targhooks.cc b/gcc/targhooks.cc
index
88582e4fa516f2d5b6b854a38e23294baf448333..727a0f2ba326c5439f08035d22c237f5e3d2ae26
100644
--- a/gcc/targhooks.cc
+++ b/gcc/targhooks.cc
@@ -398,6 +398,14 @@ default_print_operand (FILE *stream ATTRIBUTE_UNUSED, rtx
x ATTRIBUTE_UNUSED,
#endif
}
+/* No named operand modifiers are supported by default. */
+
+int
+default_print_operand_modifier (string_slice, bool)
+{
+ return 0;
+}
+
/* Emit to STREAM the assembler syntax for an insn operand whose memory
address is X. */
diff --git a/gcc/targhooks.h b/gcc/targhooks.h
index
4b029bbe40536d7c1396c2d61856a9fbf65a2de0..d5f4548fc7c1bdb58a9eb5f84bb18f7ba8507ed3
100644
--- a/gcc/targhooks.h
+++ b/gcc/targhooks.h
@@ -75,6 +75,7 @@ extern bool hook_callee_copies_named
(cumulative_args_t ca, const function_arg_info &);
extern void default_print_operand (FILE *, rtx, int);
+extern int default_print_operand_modifier (string_slice, bool);
extern void default_print_operand_address (FILE *, machine_mode, rtx);
extern bool default_print_operand_punct_valid_p (unsigned char);
extern tree default_mangle_assembler_name (const char *);