unpack tuple with new op_fields
[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 from soc.decoder.orderedset import OrderedSet
7 from soc.decoder.isa.caller import create_args
8
9 def get_isasrc_dir():
10 fdir = os.path.abspath(os.path.dirname(__file__))
11 fdir = os.path.split(fdir)[0]
12 return os.path.join(fdir, "isa")
13
14
15 header = """\
16 # auto-generated by pywriter.py, do not edit or commit
17
18 from soc.decoder.isa.caller import inject
19 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
20 from soc.decoder.selectable_int import SelectableInt
21 from soc.decoder.selectable_int import selectconcat as concat
22 from soc.decoder.orderedset import OrderedSet
23
24 class %s:
25
26 """
27
28 class PyISAWriter(ISA):
29 def __init__(self):
30 ISA.__init__(self)
31 self.pages_written = []
32
33 def write_pysource(self, pagename):
34 self.pages_written.append(pagename)
35 instrs = isa.page[pagename]
36 isadir = get_isasrc_dir()
37 fname = os.path.join(isadir, "%s.py" % pagename)
38 with open(fname, "w") as f:
39 iinf = ''
40 f.write(header % pagename) # write out header
41 # go through all instructions
42 for page in instrs:
43 d = self.instr[page]
44 print (fname, d.opcode)
45 pcode = '\n'.join(d.pcode) + '\n'
46 print (pcode)
47 pycode, rused = convert_to_python(pcode, d.form)
48 # create list of arguments to call
49 regs = list(rused['read_regs']) + list(rused['uninit_regs'])
50 args = ', '.join(create_args(regs, 'self'))
51 # create list of arguments to return
52 retargs = ', '.join(create_args(rused['write_regs']))
53 # write out function. pre-pend "op_" because some instrs are
54 # also python keywords (cmp). also replace "." with "_"
55 op_fname ="op_%s" % page.replace(".", "_")
56 f.write(" @inject()\n")
57 f.write(" def %s(%s):\n" % (op_fname, args))
58 pycode = pycode.split("\n")
59 pycode = '\n'.join(map(lambda x: " %s" % x, pycode))
60 pycode = pycode.rstrip()
61 f.write(pycode + '\n')
62 if retargs:
63 f.write(" return (%s,)\n\n" % retargs)
64 else:
65 f.write("\n")
66 # accumulate the instruction info
67 ops = repr(rused['op_fields'])
68 iinfo = """(%s, %s,
69 %s, %s,
70 %s, '%s')""" % (op_fname, rused['read_regs'],
71 rused['uninit_regs'], rused['write_regs'],
72 ops, d.form)
73 iinf += " %s_instrs['%s'] = %s\n" % (pagename, page, iinfo)
74 # write out initialisation of info, for ISACaller to use
75 f.write(" %s_instrs = {}\n" % pagename)
76 f.write(iinf)
77
78 def write_isa_class(self):
79 isadir = get_isasrc_dir()
80 fname = os.path.join(isadir, "all.py")
81
82 with open(fname, "w") as f:
83 f.write('from soc.decoder.isa.caller import ISACaller\n')
84 for page in self.pages_written:
85 f.write('from soc.decoder.isa.%s import %s\n' % (page, page))
86 f.write('\n')
87
88 classes = ', '.join(['ISACaller'] + self.pages_written)
89 f.write('class ISA(%s):\n' % classes)
90 f.write(' def __init__(self, dec, regs):\n')
91 f.write(' super().__init__(dec, regs)\n')
92 f.write(' self.instrs = {\n')
93 for page in self.pages_written:
94 f.write(' **self.%s_instrs,\n' % page)
95 f.write(' }\n')
96
97
98
99
100
101 if __name__ == '__main__':
102 isa = PyISAWriter()
103 isa.write_pysource('fixedarith')
104 isa.write_pysource('sprset')
105 #isa.write_pysource('system')
106 isa.write_isa_class()
107 exit(0)
108 isa.write_pysource('stringldst')
109 isa.write_pysource('fixedshift')
110 isa.write_pysource('condition')
111 isa.write_pysource('fixedtrap')
112 isa.write_pysource('branch')
113 isa.write_pysource('fixedlogical')
114 isa.write_pysource('fixedstore')
115 isa.write_pysource('fixedload')
116 isa.write_pysource('comparefixed')