FileCheck [9/12]: Add support for matching formats
Summary:
This patch is part of a patch series to add support for FileCheck
numeric expressions. This specific patch adds support for selecting a
matching format to match a numeric value against (ie. decimal, hex lower
case letters or hex upper case letters).
This commit allows to select what format a numeric value should be
matched against. The following formats are supported: decimal value,
lower case hex value and upper case hex value. Matching formats impact
both the format of numeric value to be matched as well as the format of
accepted numbers in a definition with empty numeric expression
constraint.
Default for absence of format is decimal value unless the numeric
expression constraint is non null and use a variable in which case the
format is the one used to define that variable. Conclict of format in
case of several variable being used is diagnosed and forces the user to
select a matching format explicitely.
This commit also enables immediates in numeric expressions to be in any
radix known to StringRef's GetAsInteger method, except for legacy
numeric expressions (ie [[@LINE+<offset>]] which only support decimal
immediates.
Copyright:
- Linaro (changes up to diff 183612 of revision D55940)
- GraphCore (changes in later versions of revision D55940 and
in new revision created off D55940)
Reviewers: jhenderson, chandlerc, jdenny, probinson, grimar, arichardson
Reviewed By: jhenderson, arichardson
Subscribers: daltenty, MaskRay, hiraditya, llvm-commits, probinson, dblaikie, grimar, arichardson, kristina, hfinkel, rogfer01, JonChesterfield
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D60389
diff --git a/llvm/docs/CommandGuide/FileCheck.rst b/llvm/docs/CommandGuide/FileCheck.rst
index 0072c9c..49db571 100644
--- a/llvm/docs/CommandGuide/FileCheck.rst
+++ b/llvm/docs/CommandGuide/FileCheck.rst
@@ -112,10 +112,11 @@
Sets a filecheck pattern variable ``VAR`` with value ``VALUE`` that can be
used in ``CHECK:`` lines.
-.. option:: -D#<NUMVAR>=<NUMERIC EXPRESSION>
+.. option:: -D#<FMT>,<NUMVAR>=<NUMERIC EXPRESSION>
- Sets a filecheck numeric variable ``NUMVAR`` to the result of evaluating
- ``<NUMERIC EXPRESSION>`` that can be used in ``CHECK:`` lines. See section
+ Sets a filecheck numeric variable ``NUMVAR`` of matching format ``FMT`` to
+ the result of evaluating ``<NUMERIC EXPRESSION>`` that can be used in
+ ``CHECK:`` lines. See section
``FileCheck Numeric Variables and Expressions`` for details on supported
numeric expressions.
@@ -588,27 +589,44 @@
substitution. This allows ``CHECK:`` directives to verify a numeric relation
between two numbers, such as the need for consecutive registers to be used.
-The syntax to define a numeric variable is ``[[#<NUMVAR>:]]`` where
-``<NUMVAR>`` is the name of the numeric variable to define to the matching
-value.
+The syntax to define a numeric variable is ``[[#%<fmtspec>,<NUMVAR>:]]`` where:
+
+* ``%<fmtspec>`` is an optional scanf-style matching format specifier to
+ indicate what number format to match (e.g. hex number). Currently accepted
+ format specifiers are ``%u``, ``%x`` and ``%X``. If absent, the format
+ specifier defaults to ``%u``.
+
+* ``<NUMVAR>`` is the name of the numeric variable to define to the matching
+ value.
For example:
.. code-block:: llvm
- ; CHECK: mov r[[#REG:]], 42
+ ; CHECK: mov r[[#REG:]], 0x[[#%X,IMM:]]
-would match ``mov r5, 42`` and set ``REG`` to the value ``5``.
+would match ``mov r5, 0xF0F0`` and set ``REG`` to the value ``5`` and ``IMM``
+to the value ``0xF0F0``.
-The syntax of a numeric substitution is ``[[#<expr>]]`` where ``<expr>`` is an
-expression. An expression is recursively defined as:
+The syntax of a numeric substitution is ``[[#%<fmtspec>,<expr>]]`` where:
-* a numeric operand, or
-* an expression followed by an operator and a numeric operand.
+* ``%<fmtspec>`` is the same matching format specifier as for defining numeric
+ variables but acting as a printf-style format to indicate how a numeric
+ expression value should be matched against. If absent, the format specifier
+ is inferred from the matching format of the numeric variable(s) used by the
+ expression constraint if any, and defaults to ``%u`` if no numeric variable
+ is used. In case of conflict between matching formats of several numeric
+ variables the format specifier is mandatory.
-A numeric operand is a previously defined numeric variable, or an integer
-literal. The supported operators are ``+`` and ``-``. Spaces are accepted
-before, after and between any of these elements.
+* ``<expr>`` is an expression. An expression is in turn recursively defined
+ as:
+
+ * a numeric operand, or
+ * an expression followed by an operator and a numeric operand.
+
+ A numeric operand is a previously defined numeric variable, or an integer
+ literal. The supported operators are ``+`` and ``-``. Spaces are accepted
+ before, after and between any of these elements.
For example:
@@ -616,6 +634,7 @@
; CHECK: load r[[#REG:]], [r0]
; CHECK: load r[[#REG+1]], [r1]
+ ; CHECK: Loading from 0x[[#%x,ADDR:] to 0x[[#ADDR + 7]]
The above example would match the text:
@@ -623,6 +642,7 @@
load r5, [r0]
load r6, [r1]
+ Loading from 0xa0463440 to 0xa0463447
but would not match the text:
@@ -630,8 +650,10 @@
load r5, [r0]
load r7, [r1]
+ Loading from 0xa0463440 to 0xa0463443
-due to ``7`` being unequal to ``5 + 1``.
+Due to ``7`` being unequal to ``5 + 1`` and ``a0463443`` being unequal to
+``a0463440 + 7``.
The syntax also supports an empty expression, equivalent to writing {{[0-9]+}},
for cases where the input must contain a numeric value but the value itself
@@ -646,8 +668,21 @@
A numeric variable can also be defined to the result of a numeric expression,
in which case the numeric expression is checked and if verified the variable is
assigned to the value. The unified syntax for both defining numeric variables
-and checking a numeric expression is thus ``[[#<NUMVAR>: <expr>]]`` with each
-element as described previously.
+and checking a numeric expression is thus ``[[#%<fmtspec>,<NUMVAR>: <expr>]]``
+with each element as described previously. One can use this syntax to make a
+testcase more self-describing by using variables instead of values:
+
+.. code-block:: gas
+
+ ; CHECK: mov r[[#REG_OFFSET:]], 0x[[#%X,FIELD_OFFSET:12]]
+ ; CHECK-NEXT: load r[[#]], [r[[#REG_BASE:]], r[[#REG_OFFSET]]]
+
+which would match:
+
+.. code-block:: gas
+
+ mov r4, 0xC
+ load r6, [r5, r4]
The ``--enable-var-scope`` option has the same effect on numeric variables as
on string variables.