From 0ce272f39f87b030d83f6965c011bb35725125d6 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Thu, 2 Apr 2020 14:49:11 +0100 Subject: [PATCH] add pagereader for openpower/isa/*.mdwn --- libreriscv | 2 +- src/soc/decoder/pseudo/pagereader.py | 110 +++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/soc/decoder/pseudo/pagereader.py diff --git a/libreriscv b/libreriscv index 5a043872..b93437fe 160000 --- a/libreriscv +++ b/libreriscv @@ -1 +1 @@ -Subproject commit 5a043872cb68bd99203e7a9854533fde5537fe7e +Subproject commit b93437fe4b0db7e3d9d066445c9528dc236dc4ed diff --git a/src/soc/decoder/pseudo/pagereader.py b/src/soc/decoder/pseudo/pagereader.py new file mode 100644 index 00000000..14d6f823 --- /dev/null +++ b/src/soc/decoder/pseudo/pagereader.py @@ -0,0 +1,110 @@ +# Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa +from collections import namedtuple, OrderedDict +from copy import copy +import os + +op = namedtuple("Ops", ("desc", "form", "opcode", "regs", "pcode", "sregs")) + +class ISA: + + def __init__(self): + self.instr = OrderedDict() + + def read_file(self, fname): + fdir = os.path.abspath(os.path.dirname(__file__)) + fdir = os.path.split(fdir)[0] + fdir = os.path.split(fdir)[0] + fdir = os.path.split(fdir)[0] + fdir = os.path.split(fdir)[0] + print (fdir) + fname = os.path.join(fdir, "libreriscv", "openpower", "isa", fname) + with open(fname) as f: + lines = f.readlines() + + d = {} + l = lines.pop(0).rstrip() # get first line + while lines: + print (l) + # expect get heading + assert l.startswith('#'), ("# not found in line %s" % l) + d['desc'] = l[1:].strip() + + # whitespace expected + l = lines.pop(0).strip() + print (repr(l)) + assert len(l) == 0, ("blank line not found %s" % l) + + # Form expected + l = lines.pop(0).strip() + assert l.endswith('-Form'), ("line with -Form expected %s" % l) + d['form'] = l.split('-')[0] + + # whitespace expected + l = lines.pop(0).strip() + assert len(l) == 0, ("blank line not found %s" % l) + + # get list of opcodes + li = [] + while True: + l = lines.pop(0).strip() + if len(l) == 0: break + assert l.startswith('*'), ("* not found in line %s" % l) + l = l[1:].split(' ') # lose star + l = filter(lambda x: len(x) != 0, l) # strip blanks + li.append(list(l)) + opcodes = li + + # get pseudocode + li = [] + while True: + l = lines.pop(0).rstrip() + if len(l) == 0: break + assert l.startswith(' '), ("4spcs not found in line %s" % l) + l = l[4:] # lose 4 spaces + li.append(l) + d['pcode'] = li + + # "Special Registers Altered" expected + l = lines.pop(0).rstrip() + assert l.startswith("Special"), ("special not found %s" % l) + + # whitespace expected + l = lines.pop(0).strip() + assert len(l) == 0, ("blank line not found %s" % l) + + # get special regs + li = [] + while True: + l = lines.pop(0).rstrip() + if len(l) == 0: break + assert l.startswith(' '), ("4spcs not found in line %s" % l) + l = l[4:] # lose 4 spaces + li.append(l) + d['sregs'] = li + + # add in opcode + for o in opcodes: + opcode, regs = o[0], o[1:] + op = copy(d) + op['regs'] = regs + op['opcode'] = opcode + self.instr[opcode] = op + + # expect and drop whitespace + while lines: + l = lines.pop(0).rstrip() + if len(l) != 0: break + + def pprint_ops(self): + for k, v in self.instr.items(): + print ("# %s %s" % (v['opcode'], v['desc'])) + print ("Form: %s Regs: %s" % (v['form'], v['regs'])) + print ('\n'.join(map(lambda x: " %s" % x, v['pcode']))) + print ("Specials") + print ('\n'.join(map(lambda x: " %s" % x, v['sregs']))) + print () + +if __name__ == '__main__': + isa = ISA() + isa.read_file("fixedlogical.mdwn") + isa.pprint_ops() -- 2.30.2