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