From b1d7d93e54dd5698628eafd8012e243593122671 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Mon, 8 Jan 2024 11:34:02 +0300 Subject: [PATCH] pywriter: convert pcode to tree and back to pcode --- src/openpower/decoder/pseudo/pywriter.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/openpower/decoder/pseudo/pywriter.py b/src/openpower/decoder/pseudo/pywriter.py index 16465822..609727b7 100644 --- a/src/openpower/decoder/pseudo/pywriter.py +++ b/src/openpower/decoder/pseudo/pywriter.py @@ -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 -- 2.30.2