add function _prtable_lookup and unit test
[soc.git] / src / soc / decoder / pseudo / pagereader.py
1 # Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa
2 """OpenPOWER ISA page parser
3
4 returns an OrderedDict of namedtuple "Ops" containing details of all
5 instructions listed in markdown files.
6
7 format must be strictly as follows (no optional sections) including whitespace:
8
9 # Compare Logical
10
11 X-Form
12
13 * cmpl BF,L,RA,RB
14
15 if L = 0 then a <- [0]*32 || (RA)[32:63]
16 b <- [0]*32 || (RB)[32:63]
17 else a <- (RA)
18 b <- (RB)
19 if a <u b then c <- 0b100
20 else if a >u b then c <- 0b010
21 else c <- 0b001
22 CR[4*BF+32:4*BF+35] <- c || XER[SO]
23
24 Special Registers Altered:
25
26 CR field BF
27 Another field
28
29 this translates to:
30
31 # heading
32 blank
33 Some-Form
34 blank
35 * instruction registerlist
36 * instruction registerlist
37 blank
38 4-space-indented pseudo-code
39 4-space-indented pseudo-code
40 blank
41 Special Registers Altered:
42 4-space-indented register description
43 blank
44 blank(s) (optional for convenience at end-of-page)
45 """
46
47 from collections import namedtuple, OrderedDict
48 from copy import copy
49 import os
50
51 opfields = ("desc", "form", "opcode", "regs", "pcode", "sregs", "page")
52 Ops = namedtuple("Ops", opfields)
53
54
55 def get_isa_dir():
56 fdir = os.path.abspath(os.path.dirname(__file__))
57 fdir = os.path.split(fdir)[0]
58 fdir = os.path.split(fdir)[0]
59 fdir = os.path.split(fdir)[0]
60 fdir = os.path.split(fdir)[0]
61 return os.path.join(fdir, "libreriscv", "openpower", "isa")
62
63
64 class ISA:
65
66 def __init__(self):
67 self.instr = OrderedDict()
68 self.forms = {}
69 self.page = {}
70 for pth in os.listdir(os.path.join(get_isa_dir())):
71 print(get_isa_dir(), pth)
72 if "swp" in pth:
73 continue
74 assert pth.endswith(".mdwn"), "only %s in isa dir" % pth
75 self.read_file(pth)
76 continue
77 # code which helped add in the keyword "Pseudo-code:" automatically
78 rewrite = self.read_file_for_rewrite(pth)
79 name = os.path.join("/tmp", pth)
80 with open(name, "w") as f:
81 f.write('\n'.join(rewrite) + '\n')
82
83 def read_file_for_rewrite(self, fname):
84 pagename = fname.split('.')[0]
85 fname = os.path.join(get_isa_dir(), fname)
86 with open(fname) as f:
87 lines = f.readlines()
88 rewrite = []
89
90 l = lines.pop(0).rstrip() # get first line
91 rewrite.append(l)
92 while lines:
93 print(l)
94 # look for HTML comment, if starting, skip line.
95 # XXX this is braindead! it doesn't look for the end
96 # so please put ending of comments on one line:
97 # <!-- line 1 comment -->
98 # <!-- line 2 comment -->
99 if l.startswith('<!--'):
100 print ("skipping comment", l)
101 continue
102 # expect get heading
103 assert l.startswith('#'), ("# not found in line %s" % l)
104
105 # whitespace expected
106 l = lines.pop(0).strip()
107 print(repr(l))
108 assert len(l) == 0, ("blank line not found %s" % l)
109 rewrite.append(l)
110
111 # Form expected
112 l = lines.pop(0).strip()
113 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
114 rewrite.append(l)
115
116 # whitespace expected
117 l = lines.pop(0).strip()
118 assert len(l) == 0, ("blank line not found %s" % l)
119 rewrite.append(l)
120
121 # get list of opcodes
122 while True:
123 l = lines.pop(0).strip()
124 rewrite.append(l)
125 if len(l) == 0:
126 break
127 assert l.startswith('*'), ("* not found in line %s" % l)
128
129 rewrite.append("Pseudo-code:")
130 rewrite.append("")
131 # get pseudocode
132 while True:
133 l = lines.pop(0).rstrip()
134 rewrite.append(l)
135 if len(l) == 0:
136 break
137 assert l.startswith(' '), ("4spcs not found in line %s" % l)
138
139 # "Special Registers Altered" expected
140 l = lines.pop(0).rstrip()
141 assert l.startswith("Special"), ("special not found %s" % l)
142 rewrite.append(l)
143
144 # whitespace expected
145 l = lines.pop(0).strip()
146 assert len(l) == 0, ("blank line not found %s" % l)
147 rewrite.append(l)
148
149 # get special regs
150 while lines:
151 l = lines.pop(0).rstrip()
152 rewrite.append(l)
153 if len(l) == 0:
154 break
155 assert l.startswith(' '), ("4spcs not found in line %s" % l)
156
157 # expect and drop whitespace
158 while lines:
159 l = lines.pop(0).rstrip()
160 rewrite.append(l)
161 if len(l) != 0:
162 break
163
164 return rewrite
165
166 def read_file(self, fname):
167 pagename = fname.split('.')[0]
168 fname = os.path.join(get_isa_dir(), fname)
169 with open(fname) as f:
170 lines = f.readlines()
171
172 # set up dict with current page name
173 d = {'page': pagename}
174
175 # line-by-line lexer/parser, quite straightforward: pops one
176 # line off the list and checks it. nothing complicated needed,
177 # all sections are mandatory so no need for a full LALR parser.
178
179 l = lines.pop(0).rstrip() # get first line
180 while lines:
181 print(l)
182 # expect get heading
183 assert l.startswith('#'), ("# not found in line %s" % l)
184 d['desc'] = l[1:].strip()
185
186 # whitespace expected
187 l = lines.pop(0).strip()
188 print(repr(l))
189 assert len(l) == 0, ("blank line not found %s" % l)
190
191 # Form expected
192 l = lines.pop(0).strip()
193 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
194 d['form'] = l.split('-')[0]
195
196 # whitespace expected
197 l = lines.pop(0).strip()
198 assert len(l) == 0, ("blank line not found %s" % l)
199
200 # get list of opcodes
201 li = []
202 while True:
203 l = lines.pop(0).strip()
204 if len(l) == 0:
205 break
206 assert l.startswith('*'), ("* not found in line %s" % l)
207 l = l[1:].split(' ') # lose star
208 l = filter(lambda x: len(x) != 0, l) # strip blanks
209 li.append(list(l))
210 opcodes = li
211
212 # "Pseudocode" expected
213 l = lines.pop(0).rstrip()
214 assert l.startswith("Pseudo-code:"), ("pseudocode found %s" % l)
215
216 # whitespace expected
217 l = lines.pop(0).strip()
218 print(repr(l))
219 assert len(l) == 0, ("blank line not found %s" % l)
220
221 # get pseudocode
222 li = []
223 while True:
224 l = lines.pop(0).rstrip()
225 if len(l) == 0:
226 break
227 assert l.startswith(' '), ("4spcs not found in line %s" % l)
228 l = l[4:] # lose 4 spaces
229 li.append(l)
230 d['pcode'] = li
231
232 # "Special Registers Altered" expected
233 l = lines.pop(0).rstrip()
234 assert l.startswith("Special"), ("special not found %s" % l)
235
236 # whitespace expected
237 l = lines.pop(0).strip()
238 assert len(l) == 0, ("blank line not found %s" % l)
239
240 # get special regs
241 li = []
242 while lines:
243 l = lines.pop(0).rstrip()
244 if len(l) == 0:
245 break
246 assert l.startswith(' '), ("4spcs not found in line %s" % l)
247 l = l[4:] # lose 4 spaces
248 li.append(l)
249 d['sregs'] = li
250
251 # add in opcode
252 for o in opcodes:
253 self.add_op(o, d)
254
255 # expect and drop whitespace
256 while lines:
257 l = lines.pop(0).rstrip()
258 if len(l) != 0:
259 break
260
261 def add_op(self, o, d):
262 opcode, regs = o[0], o[1:]
263 op = copy(d)
264 op['regs'] = regs
265 if len(regs) != 0:
266 regs[0] = regs[0].split(",")
267 op['opcode'] = opcode
268 self.instr[opcode] = Ops(**op)
269
270 # create list of instructions by form
271 form = op['form']
272 fl = self.forms.get(form, [])
273 self.forms[form] = fl + [opcode]
274
275 # create list of instructions by page
276 page = op['page']
277 pl = self.page.get(page, [])
278 self.page[page] = pl + [opcode]
279
280 def pprint_ops(self):
281 for k, v in self.instr.items():
282 print("# %s %s" % (v.opcode, v.desc))
283 print("Form: %s Regs: %s" % (v.form, v.regs))
284 print('\n'.join(map(lambda x: " %s" % x, v.pcode)))
285 print("Specials")
286 print('\n'.join(map(lambda x: " %s" % x, v.sregs)))
287 print()
288 for k, v in isa.forms.items():
289 print(k, v)
290
291
292 if __name__ == '__main__':
293 isa = ISA()
294 isa.pprint_ops()
295 # example on how to access cmp regs:
296 print ("cmp regs:", isa.instr["cmp"].regs)