1 # Reads OpenPOWER ISA pages from http://libre-soc.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 openpower
.util
import log
48 from openpower
.decoder
.orderedset
import OrderedSet
49 from collections
import namedtuple
, OrderedDict
54 opfields
= ("desc", "form", "opcode", "regs", "pcode", "sregs", "page",
56 Ops
= namedtuple("Ops", opfields
)
60 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
61 fdir
= os
.path
.split(fdir
)[0]
62 fdir
= os
.path
.split(fdir
)[0]
63 fdir
= os
.path
.split(fdir
)[0]
64 fdir
= os
.path
.split(fdir
)[0]
66 return os
.path
.join(fdir
, "openpower", "isa")
69 pattern_opcode
= r
"[A-Za-z0-9_\.]+\.?"
70 pattern_dynamic
= r
"[A-Za-z0-9_]+(?:\([A-Za-z0-9_]+\))*"
71 pattern_static
= r
"[A-Za-z0-9]+\=[01]"
72 regex_opcode
= re
.compile(f
"^{pattern_opcode}$")
73 regex_dynamic
= re
.compile(f
"^{pattern_dynamic}(?:,{pattern_dynamic})*$")
74 regex_static
= re
.compile(f
"^\({pattern_static}(?:\s{pattern_static})*\)$")
77 def operands(opcode
, desc
):
80 desc
= desc
.replace("(", "")
81 desc
= desc
.replace(")", "")
82 desc
= desc
.replace(",", " ")
83 for operand
in desc
.split(" "):
84 operand
= operand
.strip()
91 self
.instr
= OrderedDict()
95 for pth
in os
.listdir(os
.path
.join(get_isa_dir())):
97 print("examining", get_isa_dir(), pth
)
100 if not pth
.endswith(".mdwn"):
101 log ("warning, file not .mdwn, skipping", pth
)
105 # code which helped add in the keyword "Pseudo-code:" automatically
106 rewrite
= self
.read_file_for_rewrite(pth
)
107 name
= os
.path
.join("/tmp", pth
)
108 with
open(name
, "w") as f
:
109 f
.write('\n'.join(rewrite
) + '\n')
112 yield from self
.instr
.items()
114 def read_file_for_rewrite(self
, fname
):
115 pagename
= fname
.split('.')[0]
116 fname
= os
.path
.join(get_isa_dir(), fname
)
117 with
open(fname
) as f
:
118 lines
= f
.readlines()
121 l
= lines
.pop(0).rstrip() # get first line
126 # look for HTML comment, if starting, skip line.
127 # XXX this is braindead! it doesn't look for the end
128 # so please put ending of comments on one line:
129 # <!-- line 1 comment -->
130 # {some whitespace}<!-- line 2 comment -->
131 if l
.strip().startswith('<!--'):
132 # print ("skipping comment", l)
133 l
= lines
.pop(0).rstrip() # get first line
136 # Ignore blank lines before the first #
137 if len(l
.strip()) == 0:
141 assert l
.startswith('#'), ("# not found in line %s" % l
)
143 # whitespace expected
144 l
= lines
.pop(0).strip()
147 assert len(l
) == 0, ("blank line not found %s" % l
)
151 l
= lines
.pop(0).strip()
152 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
155 # whitespace expected
156 l
= lines
.pop(0).strip()
157 assert len(l
) == 0, ("blank line not found %s" % l
)
160 # get list of opcodes
162 l
= lines
.pop(0).strip()
166 assert l
.startswith('*'), ("* not found in line %s" % l
)
168 rewrite
.append("Pseudo-code:")
172 l
= lines
.pop(0).rstrip()
173 if l
.strip().startswith('<!--'):
174 # print ("skipping comment", l)
175 l
= lines
.pop(0).rstrip() # get first line
180 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
182 # "Special Registers Altered" expected
183 l
= lines
.pop(0).rstrip()
184 assert l
.startswith("Special"), ("special not found %s" % l
)
187 # whitespace expected
188 l
= lines
.pop(0).strip()
189 assert len(l
) == 0, ("blank line not found %s" % l
)
194 l
= lines
.pop(0).rstrip()
198 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
200 # expect and drop whitespace
202 l
= lines
.pop(0).rstrip()
204 if len(l
) != 0 and not l
.strip().startswith('<!--'):
209 def read_file(self
, fname
):
210 pagename
= fname
.split('.')[0]
211 fname
= os
.path
.join(get_isa_dir(), fname
)
212 with
open(fname
) as f
:
213 lines
= f
.readlines()
215 # set up dict with current page name
216 d
= {'page': pagename
}
218 # line-by-line lexer/parser, quite straightforward: pops one
219 # line off the list and checks it. nothing complicated needed,
220 # all sections are mandatory so no need for a full LALR parser.
222 l
= lines
.pop(0).rstrip() # get first line
227 # look for HTML comment, if starting, skip line.
228 # XXX this is braindead! it doesn't look for the end
229 # so please put ending of comments on one line:
230 # <!-- line 1 comment -->
231 # <!-- line 2 comment -->
232 if l
.strip().startswith('<!--'):
233 # print ("skipping comment", l)
234 l
= lines
.pop(0).rstrip() # get next line
238 # Ignore blank lines before the first #
240 l
= lines
.pop(0).rstrip() # get next line
245 assert l
.startswith('#'), ("# not found in line '%s'" % l
)
246 d
['desc'] = l
[1:].strip()
248 # whitespace expected
249 l
= lines
.pop(0).strip()
253 assert len(l
) == 0, ("blank line not found %s" % l
)
256 l
= lines
.pop(0).strip()
258 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
259 d
['form'] = l
.split('-')[0]
261 # whitespace expected
262 l
= lines
.pop(0).strip()
264 assert len(l
) == 0, ("blank line not found %s" % l
)
266 # get list of opcodes
269 l
= lines
.pop(0).strip()
273 assert l
.startswith('*'), ("* not found in line %s" % l
)
276 (opcode
, _
, rest
) = map(str.strip
, rest
.partition(" "))
277 if regex_opcode
.match(opcode
) is None:
278 raise IOError(repr(opcode
))
281 (dynamic
, _
, rest
) = map(str.strip
, rest
.partition(" "))
282 if regex_dynamic
.match(dynamic
) is None and dynamic
:
283 raise IOError(f
"{l!r}: {dynamic!r}")
285 opcode
.append(dynamic
.split(","))
288 if regex_static
.match(static
) is None and static
:
289 raise IOError(f
"{l!r}: {static!r}")
291 opcode
.extend(static
[1:-1].split(" "))
293 opcodes
.append(opcode
)
295 # "Pseudocode" expected
296 l
= lines
.pop(0).rstrip()
298 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
300 # whitespace expected
301 l
= lines
.pop(0).strip()
305 assert len(l
) == 0, ("blank line not found %s" % l
)
307 extra_uninit_regs
= OrderedSet()
311 # fix parser line numbers by prepending the right number of
312 # blank lines to the parser input
313 li
= [""] * prefix_lines
314 li
+= [l
[4:]] # first line detected with 4-space
316 l
= lines
.pop(0).rstrip()
321 re_match
= re
.fullmatch(r
" *<!-- EXTRA_UNINIT_REGS:(.*)-->", l
)
323 for i
in re_match
[1].split(' '):
325 extra_uninit_regs
.add(i
)
328 if l
.strip().startswith('<!--'):
331 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
332 l
= l
[4:] # lose 4 spaces
335 d
['extra_uninit_regs'] = extra_uninit_regs
337 # "Special Registers Altered" expected
338 l
= lines
.pop(0).rstrip()
340 assert l
.startswith("Special"), ("special not found %s" % l
)
342 # whitespace expected
343 l
= lines
.pop(0).strip()
345 assert len(l
) == 0, ("blank line not found %s" % l
)
350 l
= lines
.pop(0).rstrip()
354 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
355 l
= l
[4:] # lose 4 spaces
363 # expect and drop whitespace and comments
365 l
= lines
.pop(0).rstrip()
367 if len(l
) != 0 and not l
.strip().startswith('<!--'):
370 def add_op(self
, o
, d
):
371 opcode
, regs
= o
[0], o
[1:]
374 op
['opcode'] = opcode
375 self
.instr
[opcode
] = Ops(**op
)
377 # create list of instructions by form
379 fl
= self
.forms
.get(form
, [])
380 self
.forms
[form
] = fl
+ [opcode
]
382 # create list of instructions by page
384 pl
= self
.page
.get(page
, [])
385 self
.page
[page
] = pl
+ [opcode
]
387 def pprint_ops(self
):
388 for k
, v
in self
.instr
.items():
389 print("# %s %s" % (v
.opcode
, v
.desc
))
390 print("Form: %s Regs: %s" % (v
.form
, v
.regs
))
391 print('\n'.join(map(lambda x
: " %s" % x
, v
.pcode
)))
393 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
395 for k
, v
in isa
.forms
.items():
399 if __name__
== '__main__':
402 # example on how to access cmp regs:
403 print ("cmp regs:", isa
.instr
["cmp"].regs
)