b7e978dd561b3f267ba7d9acd1e9cf588889a07f
[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 assert pth.endswith(".mdwn"), "only %s in isa dir" % pth
73 self.read_file(pth)
74 continue
75 # code which helped add in the keyword "Pseudo-code:" automatically
76 rewrite = self.read_file_for_rewrite(pth)
77 name = os.path.join("/tmp", pth)
78 with open(name, "w") as f:
79 f.write('\n'.join(rewrite) + '\n')
80
81 def read_file_for_rewrite(self, fname):
82 pagename = fname.split('.')[0]
83 fname = os.path.join(get_isa_dir(), fname)
84 with open(fname) as f:
85 lines = f.readlines()
86 rewrite = []
87
88 l = lines.pop(0).rstrip() # get first line
89 rewrite.append(l)
90 while lines:
91 print (l)
92 # expect get heading
93 assert l.startswith('#'), ("# not found in line %s" % l)
94
95 # whitespace expected
96 l = lines.pop(0).strip()
97 print (repr(l))
98 assert len(l) == 0, ("blank line not found %s" % l)
99 rewrite.append(l)
100
101 # Form expected
102 l = lines.pop(0).strip()
103 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
104 rewrite.append(l)
105
106 # whitespace expected
107 l = lines.pop(0).strip()
108 assert len(l) == 0, ("blank line not found %s" % l)
109 rewrite.append(l)
110
111 # get list of opcodes
112 while True:
113 l = lines.pop(0).strip()
114 rewrite.append(l)
115 if len(l) == 0: break
116 assert l.startswith('*'), ("* not found in line %s" % l)
117
118 rewrite.append("Pseudo-code:")
119 rewrite.append("")
120 # get pseudocode
121 while True:
122 l = lines.pop(0).rstrip()
123 rewrite.append(l)
124 if len(l) == 0: break
125 assert l.startswith(' '), ("4spcs not found in line %s" % l)
126
127 # "Special Registers Altered" expected
128 l = lines.pop(0).rstrip()
129 assert l.startswith("Special"), ("special not found %s" % l)
130 rewrite.append(l)
131
132 # whitespace expected
133 l = lines.pop(0).strip()
134 assert len(l) == 0, ("blank line not found %s" % l)
135 rewrite.append(l)
136
137 # get special regs
138 while lines:
139 l = lines.pop(0).rstrip()
140 rewrite.append(l)
141 if len(l) == 0: break
142 assert l.startswith(' '), ("4spcs not found in line %s" % l)
143
144 # expect and drop whitespace
145 while lines:
146 l = lines.pop(0).rstrip()
147 rewrite.append(l)
148 if len(l) != 0: break
149
150 return rewrite
151
152 def read_file(self, fname):
153 pagename = fname.split('.')[0]
154 fname = os.path.join(get_isa_dir(), fname)
155 with open(fname) as f:
156 lines = f.readlines()
157
158 # set up dict with current page name
159 d = {'page': pagename}
160
161 # line-by-line lexer/parser, quite straightforward: pops one
162 # line off the list and checks it. nothing complicated needed,
163 # all sections are mandatory so no need for a full LALR parser.
164
165 l = lines.pop(0).rstrip() # get first line
166 while lines:
167 print (l)
168 # expect get heading
169 assert l.startswith('#'), ("# not found in line %s" % l)
170 d['desc'] = l[1:].strip()
171
172 # whitespace expected
173 l = lines.pop(0).strip()
174 print (repr(l))
175 assert len(l) == 0, ("blank line not found %s" % l)
176
177 # Form expected
178 l = lines.pop(0).strip()
179 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
180 d['form'] = l.split('-')[0]
181
182 # whitespace expected
183 l = lines.pop(0).strip()
184 assert len(l) == 0, ("blank line not found %s" % l)
185
186 # get list of opcodes
187 li = []
188 while True:
189 l = lines.pop(0).strip()
190 if len(l) == 0: break
191 assert l.startswith('*'), ("* not found in line %s" % l)
192 l = l[1:].split(' ') # lose star
193 l = filter(lambda x: len(x) != 0, l) # strip blanks
194 li.append(list(l))
195 opcodes = li
196
197 # "Pseudocode" expected
198 l = lines.pop(0).rstrip()
199 assert l.startswith("Pseudo-code:"), ("pseudocode found %s" % l)
200
201 # whitespace expected
202 l = lines.pop(0).strip()
203 print (repr(l))
204 assert len(l) == 0, ("blank line not found %s" % l)
205
206 # get pseudocode
207 li = []
208 while True:
209 l = lines.pop(0).rstrip()
210 if len(l) == 0: break
211 assert l.startswith(' '), ("4spcs not found in line %s" % l)
212 l = l[4:] # lose 4 spaces
213 li.append(l)
214 d['pcode'] = li
215
216 # "Special Registers Altered" expected
217 l = lines.pop(0).rstrip()
218 assert l.startswith("Special"), ("special not found %s" % l)
219
220 # whitespace expected
221 l = lines.pop(0).strip()
222 assert len(l) == 0, ("blank line not found %s" % l)
223
224 # get special regs
225 li = []
226 while lines:
227 l = lines.pop(0).rstrip()
228 if len(l) == 0: break
229 assert l.startswith(' '), ("4spcs not found in line %s" % l)
230 l = l[4:] # lose 4 spaces
231 li.append(l)
232 d['sregs'] = li
233
234 # add in opcode
235 for o in opcodes:
236 self.add_op(o, d)
237
238 # expect and drop whitespace
239 while lines:
240 l = lines.pop(0).rstrip()
241 if len(l) != 0: break
242
243 def add_op(self, o, d):
244 opcode, regs = o[0], o[1:]
245 op = copy(d)
246 op['regs'] = regs
247 regs[0] = regs[0].split(",")
248 op['opcode'] = opcode
249 self.instr[opcode] = Ops(**op)
250
251 # create list of instructions by form
252 form = op['form']
253 fl = self.forms.get(form, [])
254 self.forms[form] = fl + [opcode]
255
256 # create list of instructions by page
257 page = op['page']
258 pl = self.page.get(page, [])
259 self.page[page] = pl + [opcode]
260
261 def pprint_ops(self):
262 for k, v in self.instr.items():
263 print ("# %s %s" % (v.opcode, v.desc))
264 print ("Form: %s Regs: %s" % (v.form, v.regs))
265 print ('\n'.join(map(lambda x: " %s" % x, v.pcode)))
266 print ("Specials")
267 print ('\n'.join(map(lambda x: " %s" % x, v.sregs)))
268 print ()
269 for k, v in isa.forms.items():
270 print (k, v)
271
272 if __name__ == '__main__':
273 isa = ISA()
274 isa.pprint_ops()