code-morph in StepLoop work towards splitting into iterators
[openpower-isa.git] / src / openpower / decoder / pseudo / functionreader.py
1 # Reads OpenPOWER ISA pages from http://libre-soc.org/openpower/isafunctions
2 """OpenPOWER ISA function reader
3
4 reads markdown files looking for indented code blocks
5 """
6
7 from collections import OrderedDict
8 from copy import copy
9 import os
10
11
12 def get_isafn_dir():
13 fdir = os.path.abspath(os.path.dirname(__file__))
14 fdir = os.path.split(fdir)[0]
15 fdir = os.path.split(fdir)[0]
16 fdir = os.path.split(fdir)[0]
17 fdir = os.path.split(fdir)[0]
18 print (fdir)
19 return os.path.join(fdir, "openpower", "isafunctions")
20
21
22 class ISAFunctions:
23
24 def __init__(self):
25 self.fns = OrderedDict()
26 for pth in os.listdir(os.path.join(get_isafn_dir())):
27 print("examining", get_isafn_dir(), pth)
28 if "swp" in pth:
29 continue
30 if not pth.endswith(".mdwn"):
31 print ("warning, file not .mdwn, skipping", pth)
32 continue
33 self.read_file(pth)
34
35 def read_file(self, fname):
36 pagename = fname.split('.')[0]
37 fname = os.path.join(get_isafn_dir(), fname)
38 with open(fname) as f:
39 lines = f.readlines()
40
41 # set up dict with current page name
42 d = {'page': pagename}
43
44 # line-by-line lexer/parser, quite straightforward: pops one
45 # line off the list and checks it. nothing complicated needed,
46 # all sections are mandatory so no need for a full LALR parser.
47
48 l = lines.pop(0).rstrip() # get first line
49 prefix_lines = 0
50 while lines:
51 print(l)
52 # look for HTML comment, if starting, skip line.
53 # XXX this is braindead! it doesn't look for the end
54 # so please put ending of comments on one line:
55 # <!-- line 1 comment -->
56 # <!-- line 2 comment -->
57 if l.startswith('<!--'):
58 # print ("skipping comment", l)
59 l = lines.pop(0).rstrip() # get next line
60 prefix_lines += 1
61 continue
62
63 # Ignore blank lines before the first #
64 if len(l) == 0:
65 l = lines.pop(0).rstrip() # get next line
66 prefix_lines += 1
67 continue
68
69 # expect get heading
70 assert l.startswith('#'), ("# not found in line '%s'" % l)
71 d['desc'] = l[1:].strip()
72
73 # any lines not starting with space, ignore
74 while True:
75 l = lines.pop(0).rstrip()
76 prefix_lines += 1
77 print ("examining", repr(l))
78 if l.startswith(" "):
79 break
80 if l.startswith('<!--'):
81 continue
82
83 # get pseudocode
84
85 # fix parser line numbers by prepending the right number of
86 # blank lines to the parser input
87 li = [""] * prefix_lines
88 li += [l[4:]] # first line detected with 4-space
89 while lines:
90 l = lines.pop(0).rstrip()
91 print ("examining", repr(l))
92 if len(l) == 0:
93 li.append(l)
94 continue
95 if l.startswith('<!--'):
96 li.append("")
97 continue
98 assert l.startswith(' '), ("4spcs not found in line %s" % l)
99 l = l[4:] # lose 4 spaces
100 li.append(l)
101 d['pcode'] = '\n'.join(li)
102 break
103
104 self.fns[pagename] = d
105
106 def pprint(self):
107 for k, v in self.fns.items():
108 print("# %s %s" % (k, v['desc']))
109 print(v['pcode'])
110 print()
111
112
113 if __name__ == '__main__':
114 isa = ISAFunctions()
115 isa.pprint()