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