add function reader for appendix and other v3.0B pseudocode
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 16 May 2021 12:58:52 +0000 (13:58 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 16 May 2021 12:58:52 +0000 (13:58 +0100)
src/openpower/decoder/pseudo/functionreader.py [new file with mode: 0644]
src/openpower/decoder/pseudo/pagereader.py

diff --git a/src/openpower/decoder/pseudo/functionreader.py b/src/openpower/decoder/pseudo/functionreader.py
new file mode 100644 (file)
index 0000000..4447fde
--- /dev/null
@@ -0,0 +1,102 @@
+# Reads OpenPOWER ISA pages from http://libre-soc.org/openpower/isafunctions
+"""OpenPOWER ISA function reader
+
+reads markdown files looking for indented code blocks
+"""
+
+from collections import OrderedDict
+from copy import copy
+import os
+
+
+def get_isafn_dir():
+    fdir = os.path.abspath(os.path.dirname(__file__))
+    fdir = os.path.split(fdir)[0]
+    fdir = os.path.split(fdir)[0]
+    fdir = os.path.split(fdir)[0]
+    fdir = os.path.split(fdir)[0]
+    print (fdir)
+    return os.path.join(fdir, "openpower", "isafunctions")
+
+
+class ISAFunctions:
+
+    def __init__(self):
+        self.fns = OrderedDict()
+        for pth in os.listdir(os.path.join(get_isafn_dir())):
+            print("examining", get_isafn_dir(), pth)
+            if "swp" in pth:
+                continue
+            if not pth.endswith(".mdwn"):
+                print ("warning, file not .mdwn, skipping", pth)
+                continue
+            self.read_file(pth)
+
+    def read_file(self, fname):
+        pagename = fname.split('.')[0]
+        fname = os.path.join(get_isafn_dir(), fname)
+        with open(fname) as f:
+            lines = f.readlines()
+
+        # set up dict with current page name
+        d = {'page': pagename}
+
+        # line-by-line lexer/parser, quite straightforward: pops one
+        # line off the list and checks it.  nothing complicated needed,
+        # all sections are mandatory so no need for a full LALR parser.
+
+        l = lines.pop(0).rstrip()  # get first line
+        while lines:
+            print(l)
+            # look for HTML comment, if starting, skip line.
+            # XXX this is braindead!  it doesn't look for the end
+            # so please put ending of comments on one line:
+            # <!-- line 1 comment -->
+            # <!-- line 2 comment -->
+            if l.startswith('<!--'):
+                # print ("skipping comment", l)
+                l = lines.pop(0).rstrip()  # get next line
+                continue
+
+            # Ignore blank lines before the first #
+            if len(l) == 0:
+                l = lines.pop(0).rstrip()  # get next line
+                continue
+
+            # expect get heading
+            assert l.startswith('#'), ("# not found in line '%s'" % l)
+            d['desc'] = l[1:].strip()
+
+            # any lines not starting with space, ignore
+            while True:
+                l = lines.pop(0).rstrip()
+                print ("examining", repr(l))
+                if l.startswith("    "):
+                    break
+
+            # get pseudocode
+            li = [l[4:]] # first line detected with 4-space
+            while lines:
+                l = lines.pop(0).rstrip()
+                print ("examining", repr(l))
+                if len(l) == 0:
+                    li.append(l)
+                    continue
+                assert l.startswith('    '), ("4spcs not found in line %s" % l)
+                l = l[4:]  # lose 4 spaces
+                li.append(l)
+            d['pcode'] = '\n'.join(li)
+            break
+
+        self.fns[pagename] = d
+
+    def pprint(self):
+        for k, v in self.fns.items():
+            print("# %s %s" % (k, v['desc']))
+            print(v['pcode'])
+            print()
+
+
+if __name__ == '__main__':
+    isa = ISAFunctions()
+    isa.pprint()
index dabfec2141ef45a169942c5ad77b41f77dbb16b4..d023ab57ea571fbd750eddbdee7cd2319582f07e 100644 (file)
@@ -1,4 +1,4 @@
-# Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa
+# Reads OpenPOWER ISA pages from http://libre-soc.org/openpower/isa
 """OpenPOWER ISA page parser
 
 returns an OrderedDict of namedtuple "Ops" containing details of all