# python code-writer for OpenPOWER ISA pseudo-code parsing
import os
+import sys
+import shutil
+import subprocess
from soc.decoder.pseudo.pagereader import ISA
from soc.decoder.power_pseudo import convert_to_python
from soc.decoder.orderedset import OrderedSet
header = """\
# auto-generated by pywriter.py, do not edit or commit
-from soc.decoder.isa.caller import ISACaller, inject
-from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,)
+from soc.decoder.isa.caller import inject, instruction_info
+from soc.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, MASK,
+ ne, eq, gt, ge, lt, le, length,
+ trunc_div, trunc_rem)
from soc.decoder.selectable_int import SelectableInt
from soc.decoder.selectable_int import selectconcat as concat
from soc.decoder.orderedset import OrderedSet
-class %s(ISACaller):
+class %s:
"""
+iinfo_template = """instruction_info(func=%s,
+ read_regs=%s,
+ uninit_regs=%s, write_regs=%s,
+ special_regs=%s, op_fields=%s,
+ form='%s',
+ asmregs=%s)"""
+
class PyISAWriter(ISA):
def __init__(self):
ISA.__init__(self)
+ self.pages_written = []
def write_pysource(self, pagename):
+ self.pages_written.append(pagename)
instrs = isa.page[pagename]
isadir = get_isasrc_dir()
fname = os.path.join(isadir, "%s.py" % pagename)
# go through all instructions
for page in instrs:
d = self.instr[page]
- print (fname, d.opcode)
+ print ("page", pagename, page, fname, d.opcode)
pcode = '\n'.join(d.pcode) + '\n'
print (pcode)
- pycode, rused = convert_to_python(pcode)
+ incl_carry = pagename == 'fixedshift'
+ pycode, rused = convert_to_python(pcode, d.form, incl_carry)
# create list of arguments to call
regs = list(rused['read_regs']) + list(rused['uninit_regs'])
+ regs += list(rused['special_regs'])
args = ', '.join(create_args(regs, 'self'))
# create list of arguments to return
retargs = ', '.join(create_args(rused['write_regs']))
op_fname ="op_%s" % page.replace(".", "_")
f.write(" @inject()\n")
f.write(" def %s(%s):\n" % (op_fname, args))
+ if 'NIA' in pycode: # HACK - TODO fix
+ f.write(" global NIA\n")
pycode = pycode.split("\n")
pycode = '\n'.join(map(lambda x: " %s" % x, pycode))
pycode = pycode.rstrip()
else:
f.write("\n")
# accumulate the instruction info
- iinfo = "(%s, %s,\n %s, %s)" % \
- (op_fname, rused['read_regs'],
- rused['uninit_regs'], rused['write_regs'])
- iinf += " instrs['%s'] = %s\n" % (page, iinfo)
+ ops = repr(rused['op_fields'])
+ iinfo = iinfo_template % (op_fname, rused['read_regs'],
+ rused['uninit_regs'],
+ rused['write_regs'],
+ rused['special_regs'],
+ ops, d.form, d.regs)
+ iinf += " %s_instrs['%s'] = %s\n" % (pagename, page, iinfo)
# write out initialisation of info, for ISACaller to use
- f.write(" instrs = {}\n")
+ f.write(" %s_instrs = {}\n" % pagename)
f.write(iinf)
+ def patch_if_needed(self, source):
+ isadir = get_isasrc_dir()
+ fname = os.path.join(isadir, "%s.py" % source)
+ patchname = os.path.join(isadir, "%s.patch" % source)
+
+ try:
+ with open(patchname, 'r') as patch:
+ newfname = fname + '.orig'
+ shutil.copyfile(fname, newfname)
+ subprocess.check_call(['patch', fname],
+ stdin=patch)
+ except:
+ pass
+
+
+ def write_isa_class(self):
+ isadir = get_isasrc_dir()
+ fname = os.path.join(isadir, "all.py")
+
+ with open(fname, "w") as f:
+ f.write('from soc.decoder.isa.caller import ISACaller\n')
+ for page in self.pages_written:
+ f.write('from soc.decoder.isa.%s import %s\n' % (page, page))
+ f.write('\n')
+
+ classes = ', '.join(['ISACaller'] + self.pages_written)
+ f.write('class ISA(%s):\n' % classes)
+ f.write(' def __init__(self, *args, **kwargs):\n')
+ f.write(' super().__init__(*args, **kwargs)\n')
+ f.write(' self.instrs = {\n')
+ for page in self.pages_written:
+ f.write(' **self.%s_instrs,\n' % page)
+ f.write(' }\n')
+
+
if __name__ == '__main__':
isa = PyISAWriter()
- isa.write_pysource('system')
- exit(0)
- isa.write_pysource('stringldst')
- isa.write_pysource('fixedshift')
- isa.write_pysource('condition')
- isa.write_pysource('fixedtrap')
- isa.write_pysource('branch')
- isa.write_pysource('fixedlogical')
- isa.write_pysource('fixedstore')
- isa.write_pysource('fixedload')
- isa.write_pysource('comparefixed')
- isa.write_pysource('fixedarith')
+ if len(sys.argv) == 1: # quick way to do it
+ print (dir(isa))
+ sources = isa.page.keys()
+ else:
+ sources = sys.argv[1:]
+ for source in sources:
+ isa.write_pysource(source)
+ isa.patch_if_needed(source)
+ isa.write_isa_class()