1 # Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa
2 """OpenPOWER ISA page parser
4 returns an OrderedDict of namedtuple "Ops" containing details of all
5 instructions listed in markdown files.
7 format must be strictly as follows (no optional sections) including whitespace:
15 if L = 0 then a <- [0]*32 || (RA)[32:63]
16 b <- [0]*32 || (RB)[32:63]
19 if a <u b then c <- 0b100
20 else if a >u b then c <- 0b010
22 CR[4*BF+32:4*BF+35] <- c || XER[SO]
24 Special Registers Altered:
35 * instruction registerlist
36 * instruction registerlist
38 4-space-indented pseudo-code
39 4-space-indented pseudo-code
41 Special Registers Altered:
42 4-space-indented register description
44 blank(s) (optional for convenience at end-of-page)
47 from collections
import namedtuple
, OrderedDict
51 opfields
= ("desc", "form", "opcode", "regs", "pcode", "sregs", "page")
52 Ops
= namedtuple("Ops", opfields
)
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")
67 self
.instr
= OrderedDict()
70 for pth
in os
.listdir(os
.path
.join(get_isa_dir())):
71 print(get_isa_dir(), pth
)
74 assert pth
.endswith(".mdwn"), "only %s in isa dir" % pth
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')
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
:
90 l
= lines
.pop(0).rstrip() # get first line
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 l
= lines
.pop(0).rstrip() # get first line
104 # Ignore blank lines before the first #
105 if len(l
.strip()) == 0:
109 assert l
.startswith('#'), ("# not found in line %s" % l
)
111 # whitespace expected
112 l
= lines
.pop(0).strip()
114 assert len(l
) == 0, ("blank line not found %s" % l
)
118 l
= lines
.pop(0).strip()
119 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
122 # whitespace expected
123 l
= lines
.pop(0).strip()
124 assert len(l
) == 0, ("blank line not found %s" % l
)
127 # get list of opcodes
129 l
= lines
.pop(0).strip()
133 assert l
.startswith('*'), ("* not found in line %s" % l
)
135 rewrite
.append("Pseudo-code:")
139 l
= lines
.pop(0).rstrip()
143 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
145 # "Special Registers Altered" expected
146 l
= lines
.pop(0).rstrip()
147 assert l
.startswith("Special"), ("special not found %s" % l
)
150 # whitespace expected
151 l
= lines
.pop(0).strip()
152 assert len(l
) == 0, ("blank line not found %s" % l
)
157 l
= lines
.pop(0).rstrip()
161 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
163 # expect and drop whitespace
165 l
= lines
.pop(0).rstrip()
167 if len(l
) != 0 and not l
.startswith('<!--'):
172 def read_file(self
, fname
):
173 pagename
= fname
.split('.')[0]
174 fname
= os
.path
.join(get_isa_dir(), fname
)
175 with
open(fname
) as f
:
176 lines
= f
.readlines()
178 # set up dict with current page name
179 d
= {'page': pagename
}
181 # line-by-line lexer/parser, quite straightforward: pops one
182 # line off the list and checks it. nothing complicated needed,
183 # all sections are mandatory so no need for a full LALR parser.
185 l
= lines
.pop(0).rstrip() # get first line
188 # look for HTML comment, if starting, skip line.
189 # XXX this is braindead! it doesn't look for the end
190 # so please put ending of comments on one line:
191 # <!-- line 1 comment -->
192 # <!-- line 2 comment -->
193 if l
.startswith('<!--'):
194 # print ("skipping comment", l)
195 l
= lines
.pop(0).rstrip() # get next line
198 # Ignore blank lines before the first #
200 l
= lines
.pop(0).rstrip() # get next line
204 assert l
.startswith('#'), ("# not found in line '%s'" % l
)
205 d
['desc'] = l
[1:].strip()
207 # whitespace expected
208 l
= lines
.pop(0).strip()
210 assert len(l
) == 0, ("blank line not found %s" % l
)
213 l
= lines
.pop(0).strip()
214 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
215 d
['form'] = l
.split('-')[0]
217 # whitespace expected
218 l
= lines
.pop(0).strip()
219 assert len(l
) == 0, ("blank line not found %s" % l
)
221 # get list of opcodes
224 l
= lines
.pop(0).strip()
227 assert l
.startswith('*'), ("* not found in line %s" % l
)
228 l
= l
[1:].split(' ') # lose star
229 l
= filter(lambda x
: len(x
) != 0, l
) # strip blanks
233 # "Pseudocode" expected
234 l
= lines
.pop(0).rstrip()
235 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
237 # whitespace expected
238 l
= lines
.pop(0).strip()
240 assert len(l
) == 0, ("blank line not found %s" % l
)
245 l
= lines
.pop(0).rstrip()
248 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
249 l
= l
[4:] # lose 4 spaces
253 # "Special Registers Altered" expected
254 l
= lines
.pop(0).rstrip()
255 assert l
.startswith("Special"), ("special not found %s" % l
)
257 # whitespace expected
258 l
= lines
.pop(0).strip()
259 assert len(l
) == 0, ("blank line not found %s" % l
)
264 l
= lines
.pop(0).rstrip()
267 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
268 l
= l
[4:] # lose 4 spaces
276 # expect and drop whitespace
278 l
= lines
.pop(0).rstrip()
279 if len(l
) != 0 and not l
.startswith('<!--'):
282 def add_op(self
, o
, d
):
283 opcode
, regs
= o
[0], o
[1:]
287 regs
[0] = regs
[0].split(",")
288 op
['opcode'] = opcode
289 self
.instr
[opcode
] = Ops(**op
)
291 # create list of instructions by form
293 fl
= self
.forms
.get(form
, [])
294 self
.forms
[form
] = fl
+ [opcode
]
296 # create list of instructions by page
298 pl
= self
.page
.get(page
, [])
299 self
.page
[page
] = pl
+ [opcode
]
301 def pprint_ops(self
):
302 for k
, v
in self
.instr
.items():
303 print("# %s %s" % (v
.opcode
, v
.desc
))
304 print("Form: %s Regs: %s" % (v
.form
, v
.regs
))
305 print('\n'.join(map(lambda x
: " %s" % x
, v
.pcode
)))
307 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
309 for k
, v
in isa
.forms
.items():
313 if __name__
== '__main__':
316 # example on how to access cmp regs:
317 print ("cmp regs:", isa
.instr
["cmp"].regs
)