hack-add @inject() into pyfnwriter, also take the
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 21 May 2023 14:02:44 +0000 (15:02 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 2 Jun 2023 18:51:18 +0000 (19:51 +0100)
opportunity to stop doing "import ISACallerFnHelper as something"
by doing a hack-substitute on the class name

src/openpower/decoder/pseudo/pyfnwriter.py

index 3560f782d39ceaa68341079e5e1be9cbf2e63ea1..417706a4df0af58f1be1af6c285998a3862a607e 100644 (file)
@@ -18,6 +18,7 @@ def get_isafn_src_dir():
 header = """\
 # auto-generated by pyfnwriter.py, do not edit or commit
 
+from openpower.decoder.isa.caller import inject
 from openpower.decoder.helpers import (ISACallerHelper,
                                  ne, eq, gt, ge, lt, le, ltu, gtu, length,
                                  trunc_divs, trunc_rems, copy_assign_rhs,
@@ -55,6 +56,17 @@ class PyISAFnWriter(ISAFunctions):
                 filename = os.path.join(get_isafn_dir(), pagename + ".mdwn")
                 pycode = convert_to_pure_python(pcode, True, filename=filename)
                 sourcecache[phash] = pycode
+            # sigh ok, anything as a function needs an "@inject()" in front
+            # but it is too late here: the code has already been written out.
+            # therefore scan lines looking for "def ..." at the front. it works.
+            res = []
+            for line in pycode.split('\n'):
+                if line.startswith("class ISACallerFnHelper("):
+                    line = line.replace("(", "_%s(" % pagename)
+                elif line.startswith("    def "):
+                    res.append("    @inject()")
+                res.append(line)
+            pycode = '\n'.join(res)
             f.write(pycode)
 
     def write_isa_class(self):
@@ -63,16 +75,17 @@ class PyISAFnWriter(ISAFunctions):
 
         with open(fname, "w") as f:
             helpers = []
-            f.write('# auto-generated by pyfnwriter.py: do not edit or commit\n')
+            f.write('# auto-generated by pyfnwriter.py do not edit or commit\n')
             f.write('from openpower.decoder.helpers import ISACallerHelper\n')
             for page in self.pages_written:
                 module = 'openpower.decoder.isafunctions.' + page
                 helper = 'ISACallerFnHelper_' + page
                 helpers.append(helper)
-                f.write('from %s import ISACallerFnHelper as %s\n' % (module, helper))
+                f.write('from %s import ISACallerFnHelper_%s\n' % \
+                         (module, page))
             f.write('\n')
             f.write('\n')
-            f.write('class ISACallerFnHelper(%s):\n' % ', '.join(helpers + ['ISACallerHelper']))
+            f.write('class ISACallerFnHelper(%s):\n' % ', '.join(helpers))
             f.write('    pass\n')