add Form name parsing
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 19:45:25 +0000 (20:45 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Sun, 5 Apr 2020 19:45:28 +0000 (20:45 +0100)
src/soc/decoder/power_pseudo.py
src/soc/decoder/pseudo/parser.py
src/soc/decoder/pseudo/pywriter.py

index f6d675294b31e1c844f7ecc7b1ae3dfd9c0205a2..3e6e064e3a59ea21d061e3a270d5475dafbabb82 100644 (file)
@@ -211,15 +211,17 @@ def tolist(num):
 def get_reg_hex(reg):
     return hex(reg.value)
 
-def convert_to_python(pcode):
+def convert_to_python(pcode, form):
 
-    gsc = GardenSnakeCompiler()
+    print ("form", form)
+    gsc = GardenSnakeCompiler(form=form)
 
     tree = gsc.compile(pcode, mode="exec", filename="string")
     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}
+                'uninit_regs': gsc.parser.uninit_regs,
+                'op_fields': gsc.parser.op_fields }
     return astor.to_source(tree), regsused
 
 
index d8d0969c0a32bcd3acd457bfb6799d756df972f9..6ccbe96c94b9fcfbe323391bd28547d54966aaf0 100644 (file)
@@ -226,10 +226,20 @@ class PowerParser:
         ("left", "INVERT"),
     )
 
-    def __init__(self):
+    def __init__(self, form):
         self.gprs = {}
+        form = self.sd.sigforms[form]
+        print (form)
+        formkeys = form._asdict().keys()
         for rname in ['RA', 'RB', 'RC', 'RT', 'RS']:
             self.gprs[rname] = None
+        self.available_op_fields = set()
+        for k in formkeys:
+            if k not in self.gprs:
+                if k == 'SPR': # sigh, lower-case to not conflict
+                    k = k.lower()
+                self.available_op_fields.add(k)
+        self.op_fields = OrderedSet()
         self.read_regs = OrderedSet()
         self.uninit_regs = OrderedSet()
         self.write_regs = OrderedSet()
@@ -591,7 +601,10 @@ class PowerParser:
 
     def p_atom_name(self, p):
         """atom : NAME"""
-        p[0] = ast.Name(id=p[1], ctx=ast.Load())
+        name = p[1]
+        if name in self.available_op_fields:
+            self.op_fields.add(name)
+        p[0] = ast.Name(id=name, ctx=ast.Load())
 
     def p_atom_number(self, p):
         """atom : BINARY
@@ -622,9 +635,10 @@ class PowerParser:
         print(astor.dump_tree(p[2]))
 
         if isinstance(p[2], ast.Name):
-            print("tuple name", p[2].id)
-            if p[2].id in self.gprs:
-                self.read_regs.add(p[2].id)  # add to list of regs to read
+            name = p[2].id
+            print("tuple name", name)
+            if name in self.gprs:
+                self.read_regs.add(name)  # add to list of regs to read
                 #p[0] = ast.Subscript(ast.Name("GPR"), ast.Str(p[2].id))
                 # return
             p[0] = p[2]
@@ -748,8 +762,9 @@ class PowerParser:
 
 
 class GardenSnakeParser(PowerParser):
-    def __init__(self, lexer=None, debug=False):
-        PowerParser.__init__(self)
+    def __init__(self, lexer=None, debug=False, form=None):
+        self.sd = create_pdecode()
+        PowerParser.__init__(self, form)
         self.debug = debug
         if lexer is None:
             lexer = IndentLexer(debug=0)
@@ -758,8 +773,6 @@ class GardenSnakeParser(PowerParser):
         self.parser = yacc.yacc(module=self, start="file_input_end",
                                 debug=debug, write_tables=False)
 
-        self.sd = create_pdecode()
-
     def parse(self, code):
         # self.lexer.input(code)
         result = self.parser.parse(code, lexer=self.lexer, debug=self.debug)
@@ -771,8 +784,8 @@ class GardenSnakeParser(PowerParser):
 #from compiler import misc, syntax, pycodegen
 
 class GardenSnakeCompiler(object):
-    def __init__(self, debug=False):
-        self.parser = GardenSnakeParser(debug=debug)
+    def __init__(self, debug=False, form=None):
+        self.parser = GardenSnakeParser(debug=debug, form=form)
 
     def compile(self, code, mode="exec", filename="<string>"):
         tree = self.parser.parse(code)
index a1629aaab0c10e72958eb534b0b4ef8e98ff241d..aaadee5041d0e1cb76d13bc7d4b515eaf758795e 100644 (file)
@@ -44,7 +44,7 @@ class PyISAWriter(ISA):
                 print (fname, d.opcode)
                 pcode = '\n'.join(d.pcode) + '\n'
                 print (pcode)
-                pycode, rused = convert_to_python(pcode)
+                pycode, rused = convert_to_python(pcode, d.form)
                 # create list of arguments to call
                 regs = list(rused['read_regs']) + list(rused['uninit_regs'])
                 args = ', '.join(create_args(regs, 'self'))
@@ -64,10 +64,12 @@ class PyISAWriter(ISA):
                 else:
                     f.write("\n")
                 # accumulate the instruction info
-                iinfo = "(%s, %s,\n                %s, %s, '%s')" % \
-                            (op_fname, rused['read_regs'],
-                            rused['uninit_regs'], rused['write_regs'],
-                            d.form)
+                ops = repr(rused['op_fields'])
+                iinfo = """(%s, %s,
+                %s, %s,
+                %s, '%s')""" % (op_fname, rused['read_regs'],
+                                rused['uninit_regs'], rused['write_regs'],
+                                ops, d.form)
                 iinf += "    %s_instrs['%s'] = %s\n" % (pagename, page, iinfo)
             # write out initialisation of info, for ISACaller to use
             f.write("    %s_instrs = {}\n" % pagename)