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