power_pseudo: unify Python code generation cpseudo
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 3 Dec 2023 12:49:24 +0000 (15:49 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sun, 3 Dec 2023 18:38:53 +0000 (21:38 +0300)
src/openpower/decoder/power_pseudo.py

index e9dcbd550d5157067172b841f5e389bf29c991c3..836b6f81e108874a07025191f6ff40e3bc55703b 100644 (file)
@@ -259,27 +259,34 @@ def get_reg_hex(reg):
     return hex(reg.value)
 
 
-def convert_to_pure_python(pcode, helper=False, filename="string"):
-
-    gsc = GardenSnakeCompiler(form=None, incl_carry=False, helper=helper)
-
+def compile(pcode, form, helper, incl_carry, filename):
+    fields = {
+        "read_regs",
+        "write_regs",
+        "uninit_regs",
+        "special_regs",
+        "op_fields",
+    }
+    gsc = GardenSnakeCompiler(form=form, incl_carry=incl_carry, helper=helper)
     tree = gsc.compile(pcode, mode="exec", filename=filename)
     tree = ast.fix_missing_locations(tree)
-    return astor.to_source(tree)
+    regs = {field:getattr(gsc.parser, field) for field in fields}
+
+    return (tree, regs)
 
 
 def convert_to_python(pcode, form, incl_carry, helper=False, filename="string"):
-    log("form", form)
-    gsc = GardenSnakeCompiler(form=form, incl_carry=incl_carry, helper=helper)
+    (tree, regs) = compile(pcode=pcode,
+        form=form, helper=helper,
+        incl_carry=incl_carry, filename=filename)
+    return (astor.to_source(tree), regs)
 
-    tree = gsc.compile(pcode, mode="exec", filename=filename)
-    tree = ast.fix_missing_locations(tree)
-    regsused = {'read_regs': gsc.parser.read_regs,
-                'write_regs': gsc.parser.write_regs,
-                'uninit_regs': gsc.parser.uninit_regs,
-                'special_regs': gsc.parser.special_regs,
-                'op_fields': gsc.parser.op_fields}
-    return astor.to_source(tree), regsused
+
+def convert_to_pure_python(pcode, helper=False, filename="string"):
+    (tree, _) = compile(form=None,
+        pcode=pcode, helper=helper,
+        incl_carry=False, filename=filename)
+    return astor.to_source(tree)
 
 
 def check_in_gitignore(output_file):