bit of a mess. getting carry recognised and output for shiftrot
[soc.git] / src / soc / decoder / pseudo / pywriter.py
1 # python code-writer for OpenPOWER ISA pseudo-code parsing
2
3 import os
4 import sys
5 import shutil
6 import subprocess
7 from soc.decoder.pseudo.pagereader import ISA
8 from soc.decoder.power_pseudo import convert_to_python
9 from soc.decoder.orderedset import OrderedSet
10 from soc.decoder.isa.caller import create_args
11
12 def get_isasrc_dir():
13 fdir = os.path.abspath(os.path.dirname(__file__))
14 fdir = os.path.split(fdir)[0]
15 return os.path.join(fdir, "isa")
16
17
18 header = """\
19 # auto-generated by pywriter.py, do not edit or commit
20
21 from soc.decoder.isa.caller import inject, instruction_info
22 from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
23 ne, eq, gt, ge, lt, le, length)
24 from soc.decoder.selectable_int import SelectableInt
25 from soc.decoder.selectable_int import selectconcat as concat
26 from soc.decoder.orderedset import OrderedSet
27
28 class %s:
29
30 """
31
32 iinfo_template = """instruction_info(func=%s,
33 read_regs=%s,
34 uninit_regs=%s, write_regs=%s,
35 special_regs=%s, op_fields=%s,
36 form='%s',
37 asmregs=%s)"""
38
39 class PyISAWriter(ISA):
40 def __init__(self):
41 ISA.__init__(self)
42 self.pages_written = []
43
44 def write_pysource(self, pagename):
45 self.pages_written.append(pagename)
46 instrs = isa.page[pagename]
47 isadir = get_isasrc_dir()
48 fname = os.path.join(isadir, "%s.py" % pagename)
49 with open(fname, "w") as f:
50 iinf = ''
51 f.write(header % pagename) # write out header
52 # go through all instructions
53 for page in instrs:
54 d = self.instr[page]
55 print (fname, d.opcode)
56 pcode = '\n'.join(d.pcode) + '\n'
57 print (pcode)
58 incl_carry = page == 'fixedshift'
59 pycode, rused = convert_to_python(pcode, d.form, incl_carry)
60 # create list of arguments to call
61 regs = list(rused['read_regs']) + list(rused['uninit_regs'])
62 regs += list(rused['special_regs'])
63 args = ', '.join(create_args(regs, 'self'))
64 # create list of arguments to return
65 retargs = ', '.join(create_args(rused['write_regs']))
66 # write out function. pre-pend "op_" because some instrs are
67 # also python keywords (cmp). also replace "." with "_"
68 op_fname ="op_%s" % page.replace(".", "_")
69 f.write(" @inject()\n")
70 f.write(" def %s(%s):\n" % (op_fname, args))
71 if 'NIA' in pycode: # HACK - TODO fix
72 f.write(" global NIA\n")
73 pycode = pycode.split("\n")
74 pycode = '\n'.join(map(lambda x: " %s" % x, pycode))
75 pycode = pycode.rstrip()
76 f.write(pycode + '\n')
77 if retargs:
78 f.write(" return (%s,)\n\n" % retargs)
79 else:
80 f.write("\n")
81 # accumulate the instruction info
82 ops = repr(rused['op_fields'])
83 iinfo = iinfo_template % (op_fname, rused['read_regs'],
84 rused['uninit_regs'],
85 rused['write_regs'],
86 rused['special_regs'],
87 ops, d.form, d.regs)
88 iinf += " %s_instrs['%s'] = %s\n" % (pagename, page, iinfo)
89 # write out initialisation of info, for ISACaller to use
90 f.write(" %s_instrs = {}\n" % pagename)
91 f.write(iinf)
92
93 def patch_if_needed(self, source):
94 isadir = get_isasrc_dir()
95 fname = os.path.join(isadir, "%s.py" % source)
96 patchname = os.path.join(isadir, "%s.patch" % source)
97
98 try:
99 with open(patchname, 'r') as patch:
100 newfname = fname + '.orig'
101 shutil.copyfile(fname, newfname)
102 subprocess.check_call(['patch', fname],
103 stdin=patch)
104 except:
105 pass
106
107
108 def write_isa_class(self):
109 isadir = get_isasrc_dir()
110 fname = os.path.join(isadir, "all.py")
111
112 with open(fname, "w") as f:
113 f.write('from soc.decoder.isa.caller import ISACaller\n')
114 for page in self.pages_written:
115 f.write('from soc.decoder.isa.%s import %s\n' % (page, page))
116 f.write('\n')
117
118 classes = ', '.join(['ISACaller'] + self.pages_written)
119 f.write('class ISA(%s):\n' % classes)
120 f.write(' def __init__(self, *args, **kwargs):\n')
121 f.write(' super().__init__(*args, **kwargs)\n')
122 f.write(' self.instrs = {\n')
123 for page in self.pages_written:
124 f.write(' **self.%s_instrs,\n' % page)
125 f.write(' }\n')
126
127
128 if __name__ == '__main__':
129 isa = PyISAWriter()
130 if len(sys.argv) == 1: # quick way to do it
131 print (dir(isa))
132 sources = isa.page.keys()
133 else:
134 sources = sys.argv[1:]
135 for source in sources:
136 isa.write_pysource(source)
137 isa.patch_if_needed(source)
138 isa.write_isa_class()