add SelectableInt and selectconcat to imports
[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 header = """\
22 # auto-generated by pywriter.py, do not edit or commit
23
24 from soc.decoder.isa import ISACaller
25 from soc.decoder.helpers import (EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
26 from soc.decoder.selectable_int import SelectableInt
27 from soc.decoder.selectable_int import selectconcat as concat
28
29 class %s(ISACaller):
30
31 """
32
33 class PyISAWriter(ISA):
34 def __init__(self):
35 ISA.__init__(self)
36
37 def write_pysource(self, pagename):
38 instrs = isa.page[pagename]
39 isadir = get_isasrc_dir()
40 fname = os.path.join(isadir, "%s.py" % pagename)
41 with open(fname, "w") as f:
42 iinf = ''
43 f.write(header % pagename) # write out header
44 # go through all instructions
45 for page in instrs:
46 d = self.instr[page]
47 print (fname, d.opcode)
48 pcode = '\n'.join(d.pcode) + '\n'
49 print (pcode)
50 pycode, rused = convert_to_python(pcode)
51 # create list of arguments to call
52 regs = rused['read_regs'] + rused['uninit_regs']
53 args = create_args(regs, 'self')
54 # create list of arguments to return
55 retargs = create_args(rused['write_regs'])
56 # write out function. pre-pend "op_" because some instrs are
57 # also python keywords (cmp). also replace "." with "_"
58 op_fname ="op_%s" % page.replace(".", "_")
59 f.write(" def %s(%s):\n" % (op_fname, args))
60 pycode = pycode.split("\n")
61 pycode = '\n'.join(map(lambda x: " %s" % x, pycode))
62 pycode = pycode.rstrip()
63 f.write(pycode + '\n')
64 if retargs:
65 f.write(" return (%s,)\n\n" % retargs)
66 else:
67 f.write("\n")
68 # accumulate the instruction info
69 iinfo = "(%s, %s, %s, %s)" % \
70 (op_fname, rused['read_regs'],
71 rused['uninit_regs'], rused['write_regs'])
72 iinf += " instrs['%s'] = %s\n" % (page, iinfo)
73 # write out initialisation of info, for ISACaller to use
74 f.write(" instrs = {}\n")
75 f.write(iinf)
76
77 if __name__ == '__main__':
78 isa = PyISAWriter()
79 isa.write_pysource('comparefixed')
80 isa.write_pysource('fixedarith')