From f741f31667e653d556d7ca02012b0ed4bdccccef Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sun, 5 Apr 2020 20:45:25 +0100 Subject: [PATCH] add Form name parsing --- src/soc/decoder/power_pseudo.py | 8 ++++--- src/soc/decoder/pseudo/parser.py | 35 ++++++++++++++++++++---------- src/soc/decoder/pseudo/pywriter.py | 12 +++++----- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index f6d67529..3e6e064e 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -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 diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index d8d0969c..6ccbe96c 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -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=""): tree = self.parser.parse(code) diff --git a/src/soc/decoder/pseudo/pywriter.py b/src/soc/decoder/pseudo/pywriter.py index a1629aaa..aaadee50 100644 --- a/src/soc/decoder/pseudo/pywriter.py +++ b/src/soc/decoder/pseudo/pywriter.py @@ -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) -- 2.30.2