add support for pseudocode being a [[!inline]] directive
authorJacob Lifshay <programmerjake@gmail.com>
Tue, 1 Aug 2023 02:44:00 +0000 (19:44 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Tue, 1 Aug 2023 02:44:00 +0000 (19:44 -0700)
src/openpower/decoder/pseudo/pagereader.py
src/openpower/decoder/pseudo/pywriter.py

index b2fca28e8b030b59a131797f545abbc3d81e090e..a3db0ffbe5f32b8f13ee813b7abe4f06b90324b8 100644 (file)
@@ -52,7 +52,7 @@ import os
 import re
 
 opfields = ("desc", "form", "opcode", "regs", "pcode", "sregs", "page",
-            "extra_uninit_regs")
+            "extra_uninit_regs", "pcode_fname")
 Ops = namedtuple("Ops", opfields)
 
 
@@ -222,6 +222,7 @@ class ISA:
         l = lines.pop(0).rstrip()  # get first line
         prefix_lines = 0
         while lines:
+            pcode_fname = fname
             if self.verbose:
                 print(l)
             # look for HTML comment, if starting, skip line.
@@ -310,8 +311,7 @@ class ISA:
 
             # fix parser line numbers by prepending the right number of
             # blank lines to the parser input
-            li = [""] * prefix_lines
-            li += [l[4:]]  # first line detected with 4-space
+            li = [""] * (prefix_lines + 1)
             while True:
                 l = lines.pop(0).rstrip()
                 prefix_lines += 1
@@ -325,13 +325,50 @@ class ISA:
                             extra_uninit_regs.add(i)
                     li.append("")
                     continue
+                if l.startswith("[[!inline "):
+                    li.append(l)
+                    continue
                 if l.strip().startswith('<!--'):
                     li.append("")
                     continue
                 assert l.startswith('    '), ("4spcs not found in line %s" % l)
                 l = l[4:]  # lose 4 spaces
                 li.append(l)
+            inline_line = None
+            other = False
+            for l in li:
+                if l.startswith("[[!inline "):
+                    assert inline_line is None, \
+                        "can't use multiple [[!inline]] directives"
+                    inline_line = l
+                elif l != "":
+                    other = True
+            if inline_line is not None:
+                assert not other, \
+                    "can't use [[!inline]] directive with other content"
+
+                re_match = re.fullmatch(
+                    r'\[\[!inline pagenames="openpower/isa/([^" ]*[^"/ ])" '
+                    r'raw="yes"]]', inline_line)
+                assert re_match, (
+                    'invalid [[!inline]] directive, must be of the form:\n'
+                    '[[!inline pagenames="openpower/isa/foo/bar" '
+                    'raw="yes"]]')
+                pcode_fname = re_match[1] + ".mdwn"
+                pcode_fname = os.path.join(get_isa_dir(), pcode_fname)
+                with open(pcode_fname) as f:
+                    li = f.readlines()
+                for i, l in enumerate(li):
+                    l = l.rstrip()
+                    if l.startswith("<!--"):
+                        l = ""
+                    elif l != "":
+                        assert l.startswith("    "), \
+                            "line must start with 4 spaces"
+                        l = l[4:]
+                    li[i] = l
             d['pcode'] = li
+            d['pcode_fname'] = pcode_fname
             d['extra_uninit_regs'] = extra_uninit_regs
 
             # "Special Registers Altered" expected
index 6d436eef2f8f9877322bd6e91d2022f6c36a09b9..bb92e92d31c79daca6adfb818f1b70f7bb277e34 100644 (file)
@@ -112,9 +112,8 @@ class PyISAWriter(ISA):
                 pcode = '\n'.join(d.pcode) + '\n'
                 print(pcode)
                 incl_carry = pagename == 'fixedshift'
-                filename = os.path.join(get_isa_dir(), pagename + ".mdwn")
                 pycode, rused = convert_to_python(pcode, d.form, incl_carry,
-                                                  filename=filename)
+                                                  filename=d.pcode_fname)
                 rused['uninit_regs'] |= d.extra_uninit_regs
                 # create list of arguments to call
                 regs = list(rused['read_regs']) + list(rused['uninit_regs'])