pywriter: convert pcode to tree and back to pcode
authorDmitry Selyutin <ghostmansd@gmail.com>
Mon, 8 Jan 2024 08:34:02 +0000 (11:34 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Tue, 9 Jan 2024 14:34:32 +0000 (17:34 +0300)
src/openpower/decoder/pseudo/pywriter.py

index 16465822bc4858b2f8eee42f9af2ec3b26afe833..609727b741a51dfe8ea7f383385774b3abdb8009 100644 (file)
@@ -1,5 +1,5 @@
 # python code-writer for OpenPOWER ISA pseudo-code parsing
-
+import io
 import os
 import sys
 import shutil
@@ -11,6 +11,11 @@ from openpower.decoder.orderedset import OrderedSet
 from openpower.decoder.isa.caller import create_args, create_full_args
 
 
+import openpower.oppc.pc_lexer as pc_lexer
+import openpower.oppc.pc_parser as pc_parser
+import openpower.oppc.pc_util as pc_util
+
+
 def get_isasrc_dir():
     fdir = os.path.abspath(os.path.dirname(__file__))
     fdir = os.path.split(fdir)[0]
@@ -60,6 +65,17 @@ class PyISAWriter(ISA):
     def __init__(self):
         ISA.__init__(self)
         self.pages_written = []
+        self.pc_lexer = pc_lexer.IndentLexer()
+        self.pc_parser = pc_parser.Parser(lexer=self.pc_lexer)
+
+    def normalize(self, code):
+        tree = self.pc_parser.parse(code=code)
+        with io.StringIO() as stream:
+            for (level, line) in pc_util.pseudocode(tree):
+                print(f"{' ' * 4 * level}{line}", file=stream)
+            stream.seek(0)
+            return "\n".join(stream)
+
 
     def check_for_old_generated_files(self, pagenames):
         isadir = get_isasrc_dir()
@@ -113,6 +129,8 @@ class PyISAWriter(ISA):
                 print(pcode)
                 incl_carry = pagename == 'fixedshift'
                 filename = os.path.join(get_isa_dir(), pagename + ".mdwn")
+                pcode = self.normalize(code=pcode)
+                print(pcode)
                 pycode, rused = convert_to_python(pcode, d.form, incl_carry,
                                                   filename=filename)
                 rused['uninit_regs'] |= d.extra_uninit_regs