remove quotes because we are passing the function through
[soc.git] / src / soc / decoder / pseudo / pywriter.py
1 # python code-writer for OpenPOWER ISA pseudo-code parsing
2
3 import os
4 from soc.decoder.pseudo.pagereader import ISA
5 from soc.decoder.power_pseudo import convert_to_python
6
7 def get_isasrc_dir():
8 fdir = os.path.abspath(os.path.dirname(__file__))
9 fdir = os.path.split(fdir)[0]
10 return os.path.join(fdir, "isa")
11
12 def create_args(reglist, extra=None):
13 args = set()
14 for reg in reglist:
15 args.add(reg)
16 args = list(args)
17 if extra:
18 args = [extra] + args
19 return ', '.join(args)
20
21
22 class PyISAWriter(ISA):
23 def __init__(self):
24 ISA.__init__(self)
25
26 def write_pysource(self, pagename):
27 instrs = isa.page[pagename]
28 isadir = get_isasrc_dir()
29 fname = os.path.join(isadir, "%s.py" % pagename)
30 with open(fname, "w") as f:
31 iinf = ''
32 f.write("from soc.decoder.isa import ISACaller\n\n")
33 f.write("class %s(ISACaller):\n" % pagename)
34 for page in instrs:
35 d = self.instr[page]
36 print (fname, d.opcode)
37 pcode = '\n'.join(d.pcode) + '\n'
38 print (pcode)
39 pycode, rused = convert_to_python(pcode)
40 # create list of arguments to call
41 regs = rused['read_regs'] + rused['uninit_regs']
42 args = create_args(regs, 'self')
43 # create list of arguments to return
44 retargs = create_args(rused['write_regs'])
45 f.write(" def %s(%s):\n" % (page.replace(".", "_"), args))
46 pycode = pycode.split("\n")
47 pycode = '\n'.join(map(lambda x: " %s" % x, pycode))
48 pycode = pycode.rstrip()
49 f.write(pycode + '\n')
50 if retargs:
51 f.write(" return (%s,)\n\n" % retargs)
52 else:
53 f.write("\n")
54 # cumulate the instruction info
55 iinfo = "(%s, %s, %s, %s)" % \
56 (pagename, rused['read_regs'],
57 rused['uninit_regs'], rused['write_regs'])
58 iinf += " instrs['%s'] = %s\n" % (pagename, iinfo)
59 f.write(" instrs = {}\n")
60 f.write(iinf)
61
62 if __name__ == '__main__':
63 isa = PyISAWriter()
64 isa.write_pysource('comparefixed')
65 isa.write_pysource('fixedarith')