debeccec3623e48bc28b03f9d84b1d69070f2fec
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 collections
import namedtuple
, OrderedDict
53 opfields
= ("desc", "form", "opcode", "regs", "pcode", "sregs", "page")
54 Ops
= namedtuple("Ops", opfields
)
58 fdir
= os
.path
.abspath(os
.path
.dirname(__file__
))
59 fdir
= os
.path
.split(fdir
)[0]
60 fdir
= os
.path
.split(fdir
)[0]
61 fdir
= os
.path
.split(fdir
)[0]
62 fdir
= os
.path
.split(fdir
)[0]
64 return os
.path
.join(fdir
, "openpower", "isa")
67 pattern_opcode
= r
"[A-Za-z0-9_\.]+\.?"
68 pattern_dynamic
= r
"[A-Za-z0-9_]+(?:\([A-Za-z0-9_]+\))*"
69 pattern_static
= r
"[A-Za-z0-9]+\=[01]"
70 regex_opcode
= re
.compile(f
"^{pattern_opcode}$")
71 regex_dynamic
= re
.compile(f
"^{pattern_dynamic}(?:,{pattern_dynamic})*$")
72 regex_static
= re
.compile(f
"^\({pattern_static}(?:\s{pattern_static})*\)$")
75 def operands(opcode
, desc
):
78 desc
= desc
.replace("(", "")
79 desc
= desc
.replace(")", "")
80 desc
= desc
.replace(",", " ")
81 for operand
in desc
.split(" "):
82 operand
= operand
.strip()
89 self
.instr
= OrderedDict()
93 for pth
in os
.listdir(os
.path
.join(get_isa_dir())):
95 print("examining", get_isa_dir(), pth
)
98 if not pth
.endswith(".mdwn"):
99 log ("warning, file not .mdwn, skipping", pth
)
103 # code which helped add in the keyword "Pseudo-code:" automatically
104 rewrite
= self
.read_file_for_rewrite(pth
)
105 name
= os
.path
.join("/tmp", pth
)
106 with
open(name
, "w") as f
:
107 f
.write('\n'.join(rewrite
) + '\n')
110 yield from self
.instr
.items()
112 def read_file_for_rewrite(self
, fname
):
113 pagename
= fname
.split('.')[0]
114 fname
= os
.path
.join(get_isa_dir(), fname
)
115 with
open(fname
) as f
:
116 lines
= f
.readlines()
119 l
= lines
.pop(0).rstrip() # get first line
124 # look for HTML comment, if starting, skip line.
125 # XXX this is braindead! it doesn't look for the end
126 # so please put ending of comments on one line:
127 # <!-- line 1 comment -->
128 # {some whitespace}<!-- line 2 comment -->
129 if l
.strip().startswith('<!--'):
130 # print ("skipping comment", l)
131 l
= lines
.pop(0).rstrip() # get first line
134 # Ignore blank lines before the first #
135 if len(l
.strip()) == 0:
139 assert l
.startswith('#'), ("# not found in line %s" % l
)
141 # whitespace expected
142 l
= lines
.pop(0).strip()
145 assert len(l
) == 0, ("blank line not found %s" % l
)
149 l
= lines
.pop(0).strip()
150 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
153 # whitespace expected
154 l
= lines
.pop(0).strip()
155 assert len(l
) == 0, ("blank line not found %s" % l
)
158 # get list of opcodes
160 l
= lines
.pop(0).strip()
164 assert l
.startswith('*'), ("* not found in line %s" % l
)
166 rewrite
.append("Pseudo-code:")
170 l
= lines
.pop(0).rstrip()
171 if l
.strip().startswith('<!--'):
172 # print ("skipping comment", l)
173 l
= lines
.pop(0).rstrip() # get first line
178 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
180 # "Special Registers Altered" expected
181 l
= lines
.pop(0).rstrip()
182 assert l
.startswith("Special"), ("special not found %s" % l
)
185 # whitespace expected
186 l
= lines
.pop(0).strip()
187 assert len(l
) == 0, ("blank line not found %s" % l
)
192 l
= lines
.pop(0).rstrip()
196 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
198 # expect and drop whitespace
200 l
= lines
.pop(0).rstrip()
202 if len(l
) != 0 and not l
.strip().startswith('<!--'):
207 def read_file(self
, fname
):
208 pagename
= fname
.split('.')[0]
209 fname
= os
.path
.join(get_isa_dir(), fname
)
210 with
open(fname
) as f
:
211 lines
= f
.readlines()
213 # set up dict with current page name
214 d
= {'page': pagename
}
216 # line-by-line lexer/parser, quite straightforward: pops one
217 # line off the list and checks it. nothing complicated needed,
218 # all sections are mandatory so no need for a full LALR parser.
220 l
= lines
.pop(0).rstrip() # get first line
224 # look for HTML comment, if starting, skip line.
225 # XXX this is braindead! it doesn't look for the end
226 # so please put ending of comments on one line:
227 # <!-- line 1 comment -->
228 # <!-- line 2 comment -->
229 if l
.strip().startswith('<!--'):
230 # print ("skipping comment", l)
231 l
= lines
.pop(0).rstrip() # get next line
234 # Ignore blank lines before the first #
236 l
= lines
.pop(0).rstrip() # get next line
240 assert l
.startswith('#'), ("# not found in line '%s'" % l
)
241 d
['desc'] = l
[1:].strip()
243 # whitespace expected
244 l
= lines
.pop(0).strip()
247 assert len(l
) == 0, ("blank line not found %s" % l
)
250 l
= lines
.pop(0).strip()
251 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
252 d
['form'] = l
.split('-')[0]
254 # whitespace expected
255 l
= lines
.pop(0).strip()
256 assert len(l
) == 0, ("blank line not found %s" % l
)
258 # get list of opcodes
261 l
= lines
.pop(0).strip()
264 assert l
.startswith('*'), ("* not found in line %s" % l
)
267 (opcode
, _
, rest
) = map(str.strip
, rest
.partition(" "))
268 if regex_opcode
.match(opcode
) is None:
269 raise IOError(repr(opcode
))
272 (dynamic
, _
, rest
) = map(str.strip
, rest
.partition(" "))
273 if regex_dynamic
.match(dynamic
) is None and dynamic
:
274 raise IOError(f
"{l!r}: {dynamic!r}")
276 opcode
.append(dynamic
.split(","))
279 if regex_static
.match(static
) is None and static
:
280 raise IOError(f
"{l!r}: {static!r}")
282 opcode
.extend(static
[1:-1].split(" "))
284 opcodes
.append(opcode
)
286 # "Pseudocode" expected
287 l
= lines
.pop(0).rstrip()
288 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
290 # whitespace expected
291 l
= lines
.pop(0).strip()
294 assert len(l
) == 0, ("blank line not found %s" % l
)
299 l
= lines
.pop(0).rstrip()
300 if l
.strip().startswith('<!--'):
304 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
305 l
= l
[4:] # lose 4 spaces
309 # "Special Registers Altered" expected
310 l
= lines
.pop(0).rstrip()
311 assert l
.startswith("Special"), ("special not found %s" % l
)
313 # whitespace expected
314 l
= lines
.pop(0).strip()
315 assert len(l
) == 0, ("blank line not found %s" % l
)
320 l
= lines
.pop(0).rstrip()
323 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
324 l
= l
[4:] # lose 4 spaces
332 # expect and drop whitespace and comments
334 l
= lines
.pop(0).rstrip()
335 if len(l
) != 0 and not l
.strip().startswith('<!--'):
338 def add_op(self
, o
, d
):
339 opcode
, regs
= o
[0], o
[1:]
342 op
['opcode'] = opcode
343 self
.instr
[opcode
] = Ops(**op
)
345 # create list of instructions by form
347 fl
= self
.forms
.get(form
, [])
348 self
.forms
[form
] = fl
+ [opcode
]
350 # create list of instructions by page
352 pl
= self
.page
.get(page
, [])
353 self
.page
[page
] = pl
+ [opcode
]
355 def pprint_ops(self
):
356 for k
, v
in self
.instr
.items():
357 print("# %s %s" % (v
.opcode
, v
.desc
))
358 print("Form: %s Regs: %s" % (v
.form
, v
.regs
))
359 print('\n'.join(map(lambda x
: " %s" % x
, v
.pcode
)))
361 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
363 for k
, v
in isa
.forms
.items():
367 if __name__
== '__main__':
370 # example on how to access cmp regs:
371 print ("cmp regs:", isa
.instr
["cmp"].regs
)