pywriter: introduce C mode
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 3 Dec 2023 11:48:39 +0000 (14:48 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sun, 3 Dec 2023 11:51:34 +0000 (14:51 +0300)
src/openpower/decoder/pseudo/pywriter.py

index 0bf918c84521ccc303c32cf512342aa2485eb65a..89eca0a052d21578e8cad033d3c8f088f21199c6 100644 (file)
@@ -62,12 +62,12 @@ class PyISAWriter(ISA):
         ISA.__init__(self)
         self.pages_written = []
 
-    def check_for_old_generated_files(self, pagenames):
+    def check_for_old_generated_files(self, pagenames, ext):
         isadir = get_isasrc_dir()
         found = []
         first_fname = None
         for pagename in sorted(pagenames):
-            fbasename = pagename + ".py"
+            fbasename = (pagename + ext)
             if fbasename in OLD_GENERATED_FILES_EXCEPTIONS:
                 continue
             old_fname = os.path.join(isadir, fbasename)
@@ -91,11 +91,11 @@ class PyISAWriter(ISA):
                 "\n".join(lines)
             )
 
-    def write_pysource(self, pagename):
+    def write_code(self, pagename, ext):
         self.pages_written.append(pagename)
         instrs = self.page[pagename]
         isadir = get_isasrc_dir()
-        fname = os.path.join(isadir, "generated", pagename + ".py")
+        fname = os.path.join(isadir, "generated", (pagename + ext))
         check_in_gitignore(fname)
         with open(fname, "w") as f:
             iinf = ''
@@ -156,9 +156,11 @@ class PyISAWriter(ISA):
             f.write("    %s_instrs = {}\n" % pagename)
             f.write(iinf)
 
-    def patch_if_needed(self, source):
+    def patch_if_needed(self, source, ext):
         isadir = get_isasrc_dir()
-        fname = os.path.join(isadir, "generated", "%s.py" % source)
+        fname = os.path.join(isadir, "generated", (source + ext))
+
+        # FIXME: differ between modes
         patchname = os.path.join(isadir, "%s.patch" % source)
 
         try:
@@ -200,19 +202,26 @@ class PyISAWriter(ISA):
 def pywriter():
     isa = PyISAWriter()
     write_isa_class = True
-    if len(sys.argv) == 1:  # quick way to do it
+    args = dict.fromkeys(sys.argv[1:])
+    ext = ".py"
+    if "--c" in args:
+        ext = ".c"
+        del args["--c"]
+    if len(args) == 0:  # quick way to do it
         log(dir(isa))
         sources = isa.page.keys()
     else:
-        sources = sys.argv[1:]
+        sources = list(args)
         if sources[0] == "noall": # don't rewrite all.py
             write_isa_class = False
             sources.pop(0)
+    if ext == ".c":
+        raise NotImplementedError("C mode is not supported yet")
     log("sources", write_isa_class, sources)
-    isa.check_for_old_generated_files(sources)
+    isa.check_for_old_generated_files(pagenames=sources, ext=ext)
     for source in sources:
-        isa.write_pysource(source)
-        isa.patch_if_needed(source)
+        isa.write_code(pagename=source, ext=ext)
+        isa.patch_if_needed(source=source, ext=ext)
     if write_isa_class:
         isa.write_isa_class()