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
95 assert l
.startswith('#'), ("# not found in line %s" % l
)
98 l
= lines
.pop(0).strip()
100 assert len(l
) == 0, ("blank line not found %s" % l
)
104 l
= lines
.pop(0).strip()
105 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
108 # whitespace expected
109 l
= lines
.pop(0).strip()
110 assert len(l
) == 0, ("blank line not found %s" % l
)
113 # get list of opcodes
115 l
= lines
.pop(0).strip()
119 assert l
.startswith('*'), ("* not found in line %s" % l
)
121 rewrite
.append("Pseudo-code:")
125 l
= lines
.pop(0).rstrip()
129 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
131 # "Special Registers Altered" expected
132 l
= lines
.pop(0).rstrip()
133 assert l
.startswith("Special"), ("special not found %s" % l
)
136 # whitespace expected
137 l
= lines
.pop(0).strip()
138 assert len(l
) == 0, ("blank line not found %s" % l
)
143 l
= lines
.pop(0).rstrip()
147 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
149 # expect and drop whitespace
151 l
= lines
.pop(0).rstrip()
158 def read_file(self
, fname
):
159 pagename
= fname
.split('.')[0]
160 fname
= os
.path
.join(get_isa_dir(), fname
)
161 with
open(fname
) as f
:
162 lines
= f
.readlines()
164 # set up dict with current page name
165 d
= {'page': pagename
}
167 # line-by-line lexer/parser, quite straightforward: pops one
168 # line off the list and checks it. nothing complicated needed,
169 # all sections are mandatory so no need for a full LALR parser.
171 l
= lines
.pop(0).rstrip() # get first line
175 assert l
.startswith('#'), ("# not found in line %s" % l
)
176 d
['desc'] = l
[1:].strip()
178 # whitespace expected
179 l
= lines
.pop(0).strip()
181 assert len(l
) == 0, ("blank line not found %s" % l
)
184 l
= lines
.pop(0).strip()
185 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
186 d
['form'] = l
.split('-')[0]
188 # whitespace expected
189 l
= lines
.pop(0).strip()
190 assert len(l
) == 0, ("blank line not found %s" % l
)
192 # get list of opcodes
195 l
= lines
.pop(0).strip()
198 assert l
.startswith('*'), ("* not found in line %s" % l
)
199 l
= l
[1:].split(' ') # lose star
200 l
= filter(lambda x
: len(x
) != 0, l
) # strip blanks
204 # "Pseudocode" expected
205 l
= lines
.pop(0).rstrip()
206 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
208 # whitespace expected
209 l
= lines
.pop(0).strip()
211 assert len(l
) == 0, ("blank line not found %s" % l
)
216 l
= lines
.pop(0).rstrip()
219 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
220 l
= l
[4:] # lose 4 spaces
224 # "Special Registers Altered" expected
225 l
= lines
.pop(0).rstrip()
226 assert l
.startswith("Special"), ("special not found %s" % l
)
228 # whitespace expected
229 l
= lines
.pop(0).strip()
230 assert len(l
) == 0, ("blank line not found %s" % l
)
235 l
= lines
.pop(0).rstrip()
238 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
239 l
= l
[4:] # lose 4 spaces
247 # expect and drop whitespace
249 l
= lines
.pop(0).rstrip()
253 def add_op(self
, o
, d
):
254 opcode
, regs
= o
[0], o
[1:]
258 regs
[0] = regs
[0].split(",")
259 op
['opcode'] = opcode
260 self
.instr
[opcode
] = Ops(**op
)
262 # create list of instructions by form
264 fl
= self
.forms
.get(form
, [])
265 self
.forms
[form
] = fl
+ [opcode
]
267 # create list of instructions by page
269 pl
= self
.page
.get(page
, [])
270 self
.page
[page
] = pl
+ [opcode
]
272 def pprint_ops(self
):
273 for k
, v
in self
.instr
.items():
274 print("# %s %s" % (v
.opcode
, v
.desc
))
275 print("Form: %s Regs: %s" % (v
.form
, v
.regs
))
276 print('\n'.join(map(lambda x
: " %s" % x
, v
.pcode
)))
278 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
280 for k
, v
in isa
.forms
.items():
284 if __name__
== '__main__':
287 # example on how to access cmp regs:
288 print ("cmp regs:", isa
.instr
["cmp"].regs
)