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
)
103 assert l
.startswith('#'), ("# not found in line %s" % l
)
105 # whitespace expected
106 l
= lines
.pop(0).strip()
108 assert len(l
) == 0, ("blank line not found %s" % l
)
112 l
= lines
.pop(0).strip()
113 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
116 # whitespace expected
117 l
= lines
.pop(0).strip()
118 assert len(l
) == 0, ("blank line not found %s" % l
)
121 # get list of opcodes
123 l
= lines
.pop(0).strip()
127 assert l
.startswith('*'), ("* not found in line %s" % l
)
129 rewrite
.append("Pseudo-code:")
133 l
= lines
.pop(0).rstrip()
137 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
139 # "Special Registers Altered" expected
140 l
= lines
.pop(0).rstrip()
141 assert l
.startswith("Special"), ("special not found %s" % l
)
144 # whitespace expected
145 l
= lines
.pop(0).strip()
146 assert len(l
) == 0, ("blank line not found %s" % l
)
151 l
= lines
.pop(0).rstrip()
155 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
157 # expect and drop whitespace
159 l
= lines
.pop(0).rstrip()
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()
172 # set up dict with current page name
173 d
= {'page': pagename
}
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.
179 l
= lines
.pop(0).rstrip() # get first line
183 assert l
.startswith('#'), ("# not found in line %s" % l
)
184 d
['desc'] = l
[1:].strip()
186 # whitespace expected
187 l
= lines
.pop(0).strip()
189 assert len(l
) == 0, ("blank line not found %s" % l
)
192 l
= lines
.pop(0).strip()
193 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
194 d
['form'] = l
.split('-')[0]
196 # whitespace expected
197 l
= lines
.pop(0).strip()
198 assert len(l
) == 0, ("blank line not found %s" % l
)
200 # get list of opcodes
203 l
= lines
.pop(0).strip()
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
212 # "Pseudocode" expected
213 l
= lines
.pop(0).rstrip()
214 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
216 # whitespace expected
217 l
= lines
.pop(0).strip()
219 assert len(l
) == 0, ("blank line not found %s" % l
)
224 l
= lines
.pop(0).rstrip()
227 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
228 l
= l
[4:] # lose 4 spaces
232 # "Special Registers Altered" expected
233 l
= lines
.pop(0).rstrip()
234 assert l
.startswith("Special"), ("special not found %s" % l
)
236 # whitespace expected
237 l
= lines
.pop(0).strip()
238 assert len(l
) == 0, ("blank line not found %s" % l
)
243 l
= lines
.pop(0).rstrip()
246 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
247 l
= l
[4:] # lose 4 spaces
255 # expect and drop whitespace
257 l
= lines
.pop(0).rstrip()
261 def add_op(self
, o
, d
):
262 opcode
, regs
= o
[0], o
[1:]
266 regs
[0] = regs
[0].split(",")
267 op
['opcode'] = opcode
268 self
.instr
[opcode
] = Ops(**op
)
270 # create list of instructions by form
272 fl
= self
.forms
.get(form
, [])
273 self
.forms
[form
] = fl
+ [opcode
]
275 # create list of instructions by page
277 pl
= self
.page
.get(page
, [])
278 self
.page
[page
] = pl
+ [opcode
]
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
)))
286 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
288 for k
, v
in isa
.forms
.items():
292 if __name__
== '__main__':
295 # example on how to access cmp regs:
296 print ("cmp regs:", isa
.instr
["cmp"].regs
)