add power isa python-writer
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Apr 2020 17:25:12 +0000 (18:25 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Thu, 2 Apr 2020 17:25:12 +0000 (18:25 +0100)
src/soc/decoder/pseudo/pywriter.py [new file with mode: 0644]

diff --git a/src/soc/decoder/pseudo/pywriter.py b/src/soc/decoder/pseudo/pywriter.py
new file mode 100644 (file)
index 0000000..3ecc2f8
--- /dev/null
@@ -0,0 +1,36 @@
+# python code-writer for OpenPOWER ISA pseudo-code parsing
+
+import os
+from soc.decoder.pseudo.pagereader import ISA
+from soc.decoder.power_pseudo import convert_to_python
+
+def get_isasrc_dir():
+    fdir = os.path.abspath(os.path.dirname(__file__))
+    fdir = os.path.split(fdir)[0]
+    return os.path.join(fdir, "isa")
+
+
+class PyISAWriter(ISA):
+    def __init__(self):
+        ISA.__init__(self)
+
+    def write_pysource(self, pagename):
+        instrs = isa.page[pagename]
+        isadir = get_isasrc_dir()
+        fname = os.path.join(isadir, "%s.py" % pagename)
+        with open(fname, "w") as f:
+            for page in instrs:
+                d = self.instr[page]
+                print (fname, d.opcode)
+                f.write("def %s():\n" % page.replace(".", "_"))
+                pcode = '\n'.join(d.pcode) + '\n'
+                print (pcode)
+                pycode = convert_to_python(pcode)
+                pycode = pycode.split("\n")
+                pycode = '\n'.join(map(lambda x: "    %s" % x, pycode))
+                f.write("%s\n\n" % pycode)
+
+
+if __name__ == '__main__':
+    isa = PyISAWriter()
+    isa.write_pysource('comparefixed')