add pagereader for openpower/isa/*.mdwn
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Apr 2020 13:49:11 +0000 (14:49 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Apr 2020 13:49:11 +0000 (14:49 +0100)
libreriscv
src/soc/decoder/pseudo/pagereader.py [new file with mode: 0644]

index 5a043872cb68bd99203e7a9854533fde5537fe7e..b93437fe4b0db7e3d9d066445c9528dc236dc4ed 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 5a043872cb68bd99203e7a9854533fde5537fe7e
+Subproject commit b93437fe4b0db7e3d9d066445c9528dc236dc4ed
diff --git a/src/soc/decoder/pseudo/pagereader.py b/src/soc/decoder/pseudo/pagereader.py
new file mode 100644 (file)
index 0000000..14d6f82
--- /dev/null
@@ -0,0 +1,110 @@
+# Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa
+from collections import namedtuple, OrderedDict
+from copy import copy
+import os
+
+op = namedtuple("Ops", ("desc", "form", "opcode", "regs", "pcode", "sregs"))
+
+class ISA:
+
+    def __init__(self):
+        self.instr = OrderedDict()
+
+    def read_file(self, fname):
+        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)
+        fname = os.path.join(fdir, "libreriscv", "openpower", "isa", fname)
+        with open(fname) as f:
+            lines = f.readlines()
+        
+        d = {}
+        l = lines.pop(0).rstrip() # get first line
+        while lines:
+            print (l)
+            # expect get heading
+            assert l.startswith('#'), ("# not found in line %s" % l)
+            d['desc'] = l[1:].strip()
+
+            # whitespace expected
+            l = lines.pop(0).strip()
+            print (repr(l))
+            assert len(l) == 0, ("blank line not found %s" % l)
+
+            # Form expected
+            l = lines.pop(0).strip()
+            assert l.endswith('-Form'), ("line with -Form expected %s" % l)
+            d['form'] = l.split('-')[0]
+
+            # whitespace expected
+            l = lines.pop(0).strip()
+            assert len(l) == 0, ("blank line not found %s" % l)
+
+            # get list of opcodes
+            li = []
+            while True:
+                l = lines.pop(0).strip()
+                if len(l) == 0: break
+                assert l.startswith('*'), ("* not found in line %s" % l)
+                l = l[1:].split(' ') # lose star
+                l = filter(lambda x: len(x) != 0, l) # strip blanks
+                li.append(list(l))
+            opcodes = li
+
+            # get pseudocode
+            li = []
+            while True:
+                l = lines.pop(0).rstrip()
+                if len(l) == 0: break
+                assert l.startswith('    '), ("4spcs not found in line %s" % l)
+                l = l[4:] # lose 4 spaces
+                li.append(l)
+            d['pcode'] = li
+
+            # "Special Registers Altered" expected
+            l = lines.pop(0).rstrip()
+            assert l.startswith("Special"), ("special not found %s" % l)
+
+            # whitespace expected
+            l = lines.pop(0).strip()
+            assert len(l) == 0, ("blank line not found %s" % l)
+
+            # get special regs
+            li = []
+            while True:
+                l = lines.pop(0).rstrip()
+                if len(l) == 0: break
+                assert l.startswith('    '), ("4spcs not found in line %s" % l)
+                l = l[4:] # lose 4 spaces
+                li.append(l)
+            d['sregs'] = li
+
+            # add in opcode
+            for o in opcodes:
+                opcode, regs = o[0], o[1:]
+                op = copy(d)
+                op['regs'] = regs
+                op['opcode'] = opcode
+                self.instr[opcode] = op
+
+            # expect and drop whitespace
+            while lines:
+                l = lines.pop(0).rstrip()
+                if len(l) != 0: break
+
+    def pprint_ops(self):
+        for k, v in self.instr.items():
+            print ("# %s %s" % (v['opcode'], v['desc']))
+            print ("Form: %s Regs: %s" % (v['form'], v['regs']))
+            print ('\n'.join(map(lambda x: "    %s" % x, v['pcode'])))
+            print ("Specials")
+            print ('\n'.join(map(lambda x: "    %s" % x, v['sregs'])))
+            print ()
+
+if __name__ == '__main__':
+    isa = ISA()
+    isa.read_file("fixedlogical.mdwn")
+    isa.pprint_ops()