Skip to contentSkip to conversation

decodetree enhacements

PATCH New Richard Henderson 1 participant 13 messages Mar 7, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover decodetree enhacements

Changes since v2:
* Add some additional tests, based on actual uncaught exceptions I saw while using pattern groups in an ARM32 conversion.
* Allow negative numbers in field=number.
* Push extraction functions into the namespace of the decoder.

I'll probably give a PR for this on the weekend.

r~

Bastian Koppelmann (1):
test/decode: Add tests for PatternGroups

Philippe Mathieu-Daudé (2):
MAINTAINERS: Add scripts/decodetree.py to the TCG section
decodetree: Ensure build_tree does not include values outside insnmask

Richard Henderson (9):
decodetree: Move documentation to docs/devel/decodetree.rst
decodetree: Document the usefulness of argument sets
decodetree: Do not unconditionaly return from Pattern.output_code
decodetree: Allow grouping of overlapping patterns
decodetree: Add --static-decode option
decodetree: Produce clean output for an empty input file
decodetree: Allow +- to begin a number initializing a field
decodetree: Prefix extract function names with decode_function
decodetree: Properly diagnose fields overflowing an insn

 MAINTAINERS                                   |   2 +
 docs/devel/decodetree.rst                     | 221 ++++++++++++
 docs/devel/index.rst                          |   2 +-
 scripts/decodetree.py                         | 330 +++++++++---------
 tests/decode/check.sh                         |   6 +
 tests/decode/err_pattern_group_empty.decode   |   6 +
 tests/decode/err_pattern_group_ident1.decode  |  10 +
 tests/decode/err_pattern_group_ident2.decode  |  11 +
 tests/decode/err_pattern_group_nest1.decode   |  13 +
 .../decode/err_pattern_group_overlap1.decode  |   6 +
 tests/decode/err_width1.decode                |   5 +
 tests/decode/err_width2.decode                |   5 +
 tests/decode/err_width3.decode                |   5 +
 tests/decode/err_width4.decode                |   5 +
 tests/decode/succ_pattern_group_nest1.decode  |  22 ++
 15 files changed, 480 insertions(+), 169 deletions(-)
 create mode 100644 docs/devel/decodetree.rst
 create mode 100644 tests/decode/err_pattern_group_empty.decode
 create mode 100644 tests/decode/err_pattern_group_ident1.decode
 create mode 100644 tests/decode/err_pattern_group_ident2.decode
 create mode 100644 tests/decode/err_pattern_group_nest1.decode
 create mode 100644 tests/decode/err_pattern_group_overlap1.decode
 create mode 100644 tests/decode/err_width1.decode
 create mode 100644 tests/decode/err_width2.decode
 create mode 100644 tests/decode/err_width3.decode
 create mode 100644 tests/decode/err_width4.decode
 create mode 100644 tests/decode/succ_pattern_group_nest1.decode
Signature · 1 line
2.17.2
#1 lore raw

PATCH v3 1/12 MAINTAINERS: Add scripts/decodetree.py to the TCG section

From: Philippe Mathieu-Daudé <[email protected]>

Message-Id: <[email protected]>

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 074ad46d47..af1df70be1 100644
@@ -117,6 +117,7 @@ F: cpus.c
 F: exec.c
 F: accel/tcg/
 F: accel/stubs/tcg-stub.c
+F: scripts/decodetree.py
 F: include/exec/cpu*.h
 F: include/exec/exec-all.h
 F: include/exec/helper*.h
Signature · 1 line
2.17.2
#2 lore raw

PATCH v3 2/12 decodetree: Move documentation to docs/devel/decodetree.rst

In reply to Richard Henderson

One great big block comment isn't the best way to document the syntax of a language.

---
 MAINTAINERS               |   1 +
 docs/devel/decodetree.rst | 156 ++++++++++++++++++++++++++++++++++++++
 docs/devel/index.rst      |   2 +-
 scripts/decodetree.py     | 134 +-------------------------------
 4 files changed, 159 insertions(+), 134 deletions(-)
 create mode 100644 docs/devel/decodetree.rst
MAINTAINERS +1 -0
index af1df70be1..820024a808 100644
@@ -118,6 +118,7 @@ F: exec.c
 F: accel/tcg/
 F: accel/stubs/tcg-stub.c
 F: scripts/decodetree.py
+F: docs/devel/decodetree.rst
 F: include/exec/cpu*.h
 F: include/exec/exec-all.h
 F: include/exec/helper*.h
docs/devel/decodetree.rst new file +156 -0

156 lines not on this page · raw message

docs/devel/index.rst +1 -1
index cd0fa6c9ba..6b9a6ba71c 100644
@@ -18,4 +18,4 @@ Contents:
    migration
    stable-process
    testing
-
+   decodetree
scripts/decodetree.py +1 -133

140 lines not on this page · raw message

Signature · 1 line
2.17.2
#3 lore raw

PATCH v3 3/12 decodetree: Document the usefulness of argument sets

In reply to Richard Henderson
---
 docs/devel/decodetree.rst | 7 +++++++
 1 file changed, 7 insertions(+)
docs/devel/decodetree.rst +7 -0
index d9be30b2db..62cb7f687c 100644
@@ -69,6 +69,13 @@ with each of the fields being one of the member arguments.
 If ``!extern`` is specified, the backing structure is assumed
 to have been already declared, typically via a second decoder.
 
+Argument sets are useful when one wants to define helper functions
+for the translator functions that can perform operations on a common
+set of arguments.  This can ensure, for instance, that the ``AND``
+pattern and the ``OR`` pattern put their operands into the same named
+structure, so that a common ``gen_logic_insn`` may be able to handle
+the operations common between the two.
+
 Argument set examples::
 
   &reg3       ra rb rc
Signature · 1 line
2.17.2
#4 lore raw

PATCH v3 4/12 decodetree: Ensure build_tree does not include values outside insnmask

In reply to Richard Henderson

From: Philippe Mathieu-Daudé <[email protected]>

Reproduced with "scripts/decodetree.py /dev/null".

---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +1 -1
index 33e32ee87f..e26d8253f2 100755
@@ -784,7 +784,7 @@ class Tree:
 
 def build_tree(pats, outerbits, outermask):
     # Find the intersection of all remaining fixedmask.
-    innermask = ~outermask
+    innermask = ~outermask & insnmask
     for i in pats:
         innermask &= i.fixedmask
 
Signature · 1 line
2.17.2
#5 lore raw

PATCH v3 5/12 decodetree: Do not unconditionaly return from Pattern.output_code

In reply to Richard Henderson

As a consequence, the 'return false' gets pushed up one level.

This will allow us to perform some other action when the translator returns failure.

---
 scripts/decodetree.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/decodetree.py +4 -3
index e26d8253f2..cc5fa1a8ab 100755
@@ -348,8 +348,8 @@ class Pattern(General):
             output(ind, self.base.extract_name(), '(&u.f_', arg, ', insn);\n')
         for n, f in self.fields.items():
             output(ind, 'u.f_', arg, '.', n, ' = ', f.str_extract(), ';\n')
-        output(ind, 'return ', translate_prefix, '_', self.name,
-               '(ctx, &u.f_', arg, ');\n')
+        output(ind, 'if (', translate_prefix, '_', self.name,
+               '(ctx, &u.f_', arg, ')) return true;\n')
 # end Pattern
 
 
@@ -777,8 +777,8 @@ class Tree:
             output(ind, '    /* ',
                    str_match_bits(innerbits, innermask), ' */\n')
             s.output_code(i + 4, extracted, innerbits, innermask)
+            output(ind, '    return false;\n')
         output(ind, '}\n')
-        output(ind, 'return false;\n')
 # end Tree
 
 
@@ -932,6 +932,7 @@ def main():
     output(i4, '} u;\n\n')
 
     t.output_code(4, False, 0, 0)
+    output(i4, 'return false;\n')
 
     output('}\n')
 
Signature · 1 line
2.17.2
#6 lore raw

PATCH v3 6/12 decodetree: Allow grouping of overlapping patterns

In reply to Richard Henderson
---
 docs/devel/decodetree.rst                     |  58 +++++++
 scripts/decodetree.py                         | 162 +++++++++++++++---
 .../decode/err_pattern_group_overlap1.decode  |   6 +
 3 files changed, 204 insertions(+), 22 deletions(-)
 create mode 100644 tests/decode/err_pattern_group_overlap1.decode
docs/devel/decodetree.rst +58 -0
index 62cb7f687c..44ac621ea8 100644
@@ -161,3 +161,61 @@ which will, in part, invoke::
 and::
 
   trans_addl_i(ctx, &arg_opi, insn)
+
+Pattern Groups
+==============
+
+Syntax::
+
+  group    := '{' ( pat_def | group )+ '}'
+
+A *group* begins with a lone open-brace, with all subsequent lines
+indented two spaces, and ending with a lone close-brace.  Groups
+may be nested, increasing the required indentation of the lines
+within the nested group to two spaces per nesting level.
+
+Unlike ungrouped patterns, grouped patterns are allowed to overlap.
+Conflicts are resolved by selecting the patterns in order.  If all
+of the fixedbits for a pattern match, its translate function will
+be called.  If the translate function returns false, then subsequent
+patterns within the group will be matched.
+
+The following example from PA-RISC shows specialization of the *or*
+instruction::
+
+  {
+    {
+      nop   000010 ----- ----- 0000 001001 0 00000
+      copy  000010 00000 r1:5  0000 001001 0 rt:5
+    }
+    or      000010 rt2:5 r1:5  cf:4 001001 0 rt:5
+  }
+
+When the *cf* field is zero, the instruction has no side effects,
+and may be specialized.  When the *rt* field is zero, the output
+is discarded and so the instruction has no effect.  When the *rt2*
+field is zero, the operation is ``reg[rt] | 0`` and so encodes
+the canonical register copy operation.
+
+The output from the generator might look like::
+
+  switch (insn & 0xfc000fe0) {
+  case 0x08000240:
+    /* 000010.. ........ ....0010 010..... */
+    if ((insn & 0x0000f000) == 0x00000000) {
+        /* 000010.. ........ 00000010 010..... */
+        if ((insn & 0x0000001f) == 0x00000000) {
+            /* 000010.. ........ 00000010 01000000 */
+            extract_decode_Fmt_0(&u.f_decode0, insn);
+            if (trans_nop(ctx, &u.f_decode0)) return true;
+        }
+        if ((insn & 0x03e00000) == 0x00000000) {
+            /* 00001000 000..... 00000010 010..... */
+            extract_decode_Fmt_1(&u.f_decode1, insn);
+            if (trans_copy(ctx, &u.f_decode1)) return true;
+        }
+    }
+    extract_decode_Fmt_2(&u.f_decode2, insn);
+    if (trans_or(ctx, &u.f_decode2)) return true;
+    return false;
+  }
scripts/decodetree.py +140 -22

249 lines not on this page · raw message

tests/decode/err_pattern_group_overlap1.decode new file +6 -0
new file mode 100644
index 0000000000..ebe3030d26
@@ -0,0 +1,6 @@
+one	00000000000000000000000000000000
+{
+  two	0000000000000000000000000000000 s:1
+  three 000000000000000000000000000000 s:1 0
+}
+
Signature · 1 line
2.17.2
#7 lore raw

PATCH v3 7/12 test/decode: Add tests for PatternGroups

In reply to Richard Henderson

From: Bastian Koppelmann <[email protected]>

This adds one test that supposed to succeed to test deep nesting of pattern groups which is rarely exercised by targets using decode tree. The remaining tests exercise various fail conditions.

Message-Id: <[email protected]>

---
 tests/decode/check.sh                        |  6 ++++++
 tests/decode/err_pattern_group_empty.decode  |  6 ++++++
 tests/decode/err_pattern_group_ident1.decode | 10 +++++++++
 tests/decode/err_pattern_group_ident2.decode | 11 ++++++++++
 tests/decode/err_pattern_group_nest1.decode  | 13 ++++++++++++
 tests/decode/succ_pattern_group_nest1.decode | 22 ++++++++++++++++++++
 6 files changed, 68 insertions(+)
 create mode 100644 tests/decode/err_pattern_group_empty.decode
 create mode 100644 tests/decode/err_pattern_group_ident1.decode
 create mode 100644 tests/decode/err_pattern_group_ident2.decode
 create mode 100644 tests/decode/err_pattern_group_nest1.decode
 create mode 100644 tests/decode/succ_pattern_group_nest1.decode
tests/decode/check.sh +6 -0
index 79a06c37cd..95445a0115 100755
@@ -15,4 +15,10 @@ for i in err_*.decode; do
     fi
 done
 
+for i in succ_*.decode; do
+    if ! $PYTHON $DECODETREE $i > /dev/null 2> /dev/null; then
+        echo FAIL:$i 1>&2
+    fi
+done
+
 exit $E
tests/decode/err_pattern_group_empty.decode new file +6 -0
new file mode 100644
index 0000000000..abbff6b528
@@ -0,0 +1,6 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+# empty groups are not allowed
+{
+}
tests/decode/err_pattern_group_ident1.decode new file +10 -0
new file mode 100644
index 0000000000..3e65fab2f9
@@ -0,0 +1,10 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+%sub1 0:8
+
+# Make sure that indentation is enforced
+{
+    top      00000000 00000000 00000000 00000000
+    sub1     00000000 00000000 00000000 ........ %sub1
+}
tests/decode/err_pattern_group_ident2.decode new file +11 -0
new file mode 100644
index 0000000000..bc859233b1
@@ -0,0 +1,11 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+%sub1 0:8
+
+# Make sure that indentation is enforced
+{
+  top      00000000 00000000 00000000 00000000
+  sub1     00000000 00000000 00000000 ........ %sub1
+# comments are suposed to be indented
+}
tests/decode/err_pattern_group_nest1.decode new file +13 -0
new file mode 100644
index 0000000000..92e971c3c5
@@ -0,0 +1,13 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+%sub1 0:8
+%sub2 8:8
+%sub3 16:8
+%sub4 24:8
+
+# Groups with no overlap are supposed to fail
+{
+  top  00000000 00000000 00000000 00000000
+  sub4 ........ ........ ........ ........ %sub1 %sub2 %sub3 %sub4
+}
tests/decode/succ_pattern_group_nest1.decode new file +22 -0
new file mode 100644
index 0000000000..77b0f48b49
@@ -0,0 +1,22 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+%sub1 0:8
+%sub2 8:8
+%sub3 16:8
+%sub4 24:7
+
+# Make sure deep netsting works, as few targets will actually exercise it
+{
+  top        00000000 00000000 00000000 00000000
+  {
+    sub1     00000000 00000000 00000000 ........ %sub1
+    {
+      sub2   00000000 00000000 ........ ........ %sub1 %sub2
+      {
+        sub3 00000000 ........ ........ ........ %sub1 %sub2 %sub3
+        sub4 0....... ........ ........ ........ %sub1 %sub2 %sub3 %sub4
+      }
+    }
+  }
+}
Signature · 1 line
2.17.2
#8 lore raw

PATCH v3 8/12 decodetree: Add --static-decode option

In reply to Richard Henderson

Like --decode, but do not drop 'static' qualifier.

---
 scripts/decodetree.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/decodetree.py +4 -1
index c4e8cb52f7..d6f94d902f 100755
@@ -976,7 +976,8 @@ def main():
 
     decode_scope = 'static '
 
-    long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=']
+    long_opts = ['decode=', 'translate=', 'output=', 'insnwidth=',
+                 'static-decode=']
     try:
         (opts, args) = getopt.getopt(sys.argv[1:], 'o:w:', long_opts)
     except getopt.GetoptError as err:
@@ -987,6 +988,8 @@ def main():
         elif o == '--decode':
             decode_function = a
             decode_scope = ''
+        elif o == '--static-decode':
+            decode_function = a
         elif o == '--translate':
             translate_prefix = a
             translate_scope = ''
Signature · 1 line
2.17.2
#9 lore raw

PATCH v3 9/12 decodetree: Produce clean output for an empty input file

In reply to Richard Henderson

This is interesting for bisection, where an output file is plumbed, but does not yet have patterns.

---
 scripts/decodetree.py | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
scripts/decodetree.py +8 -7
index d6f94d902f..57d4c9297e 100755
@@ -1046,15 +1046,16 @@ def main():
            '(DisasContext *ctx, ', insntype, ' insn)\n{\n')
 
     i4 = str_indent(4)
-    output(i4, 'union {\n')
-    for n in sorted(arguments.keys()):
-        f = arguments[n]
-        output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
-    output(i4, '} u;\n\n')
 
-    t.output_code(4, False, 0, 0)
+    if len(allpatterns) != 0:
+        output(i4, 'union {\n')
+        for n in sorted(arguments.keys()):
+            f = arguments[n]
+            output(i4, i4, f.struct_name(), ' f_', f.name, ';\n')
+        output(i4, '} u;\n\n')
+        t.output_code(4, False, 0, 0)
+
     output(i4, 'return false;\n')
-
     output('}\n')
 
     if output_file:
Signature · 1 line
2.17.2
#10 lore raw

PATCH v3 10/12 decodetree: Allow +- to begin a number initializing a field

In reply to Richard Henderson
---
 scripts/decodetree.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/decodetree.py +1 -1
index 57d4c9297e..eef32f695f 100755
@@ -589,7 +589,7 @@ def parse_generic(lineno, is_format, name, toks):
             continue
 
         # 'Foo=number' sets an argument field to a constant value
-        if re_fullmatch(re_ident + '=[0-9]+', t):
+        if re_fullmatch(re_ident + '=[+-]?[0-9]+', t):
             (fname, value) = t.split('=')
             value = int(value)
             flds = add_field(lineno, flds, fname, ConstField(value))
Signature · 1 line
2.17.2
#11 lore raw

PATCH v3 11/12 decodetree: Prefix extract function names with decode_function

In reply to Richard Henderson

This makes it easier to name Formats within multiple decode files.

---
 scripts/decodetree.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/decodetree.py +2 -1
index eef32f695f..f8df89e072 100755
@@ -312,7 +312,8 @@ class Format(General):
     """Class representing an instruction format"""
 
     def extract_name(self):
-        return 'extract_' + self.name
+        global decode_function
+        return decode_function + '_extract_' + self.name
 
     def output_extract(self):
         output('static void ', self.extract_name(), '(',
Signature · 1 line
2.17.2
#12 lore raw

PATCH v3 12/12 decodetree: Properly diagnose fields overflowing an insn

In reply to Richard Henderson

Previously this would result in an exception for shifting the field mask by a negative number.

---
 scripts/decodetree.py          | 2 ++
 tests/decode/err_width1.decode | 5 +++++
 tests/decode/err_width2.decode | 5 +++++
 tests/decode/err_width3.decode | 5 +++++
 tests/decode/err_width4.decode | 5 +++++
 5 files changed, 22 insertions(+)
 create mode 100644 tests/decode/err_width1.decode
 create mode 100644 tests/decode/err_width2.decode
 create mode 100644 tests/decode/err_width3.decode
 create mode 100644 tests/decode/err_width4.decode
scripts/decodetree.py +2 -0
index f8df89e072..7e890ecf1a 100755
@@ -622,6 +622,8 @@ def parse_generic(lineno, is_format, name, toks):
                 sign = True
                 flen = flen[1:]
             shift = int(flen, 10)
+            if shift + width > insnwidth:
+                error(lineno, 'field {0} exceeds insnwidth'.format(fname))
             f = Field(sign, insnwidth - width - shift, shift)
             flds = add_field(lineno, flds, fname, f)
             fixedbits <<= shift
tests/decode/err_width1.decode new file +5 -0
new file mode 100644
index 0000000000..0c14f6d73b
@@ -0,0 +1,5 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+# Diagnose too many bits (33 of 32)
+one	000000000000000000000000000000000
tests/decode/err_width2.decode new file +5 -0
new file mode 100644
index 0000000000..47f0acf322
@@ -0,0 +1,5 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+# Diagnose too few bits (31 of 32)
+one	0000000000000000000000000000000
tests/decode/err_width3.decode new file +5 -0
new file mode 100644
index 0000000000..c5fb6b3699
@@ -0,0 +1,5 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+# Diagnose too many bits (33 of 32)
+one	0 s:32
tests/decode/err_width4.decode new file +5 -0
new file mode 100644
index 0000000000..1588a63698
@@ -0,0 +1,5 @@
+# This work is licensed under the terms of the GNU LGPL, version 2 or later.
+# See the COPYING.LIB file in the top-level directory.
+
+# Diagnose too few bits (31 of 32)
+one	0 s:30
Signature · 1 line
2.17.2