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
)
72 assert pth
.endswith(".mdwn"), "only %s in isa dir" % pth
75 # code which helped add in the keyword "Pseudo-code:" automatically
76 rewrite
= self
.read_file_for_rewrite(pth
)
77 name
= os
.path
.join("/tmp", pth
)
78 with
open(name
, "w") as f
:
79 f
.write('\n'.join(rewrite
) + '\n')
81 def read_file_for_rewrite(self
, fname
):
82 pagename
= fname
.split('.')[0]
83 fname
= os
.path
.join(get_isa_dir(), fname
)
84 with
open(fname
) as f
:
88 l
= lines
.pop(0).rstrip() # get first line
93 assert l
.startswith('#'), ("# not found in line %s" % l
)
96 l
= lines
.pop(0).strip()
98 assert len(l
) == 0, ("blank line not found %s" % l
)
102 l
= lines
.pop(0).strip()
103 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
106 # whitespace expected
107 l
= lines
.pop(0).strip()
108 assert len(l
) == 0, ("blank line not found %s" % l
)
111 # get list of opcodes
113 l
= lines
.pop(0).strip()
117 assert l
.startswith('*'), ("* not found in line %s" % l
)
119 rewrite
.append("Pseudo-code:")
123 l
= lines
.pop(0).rstrip()
127 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
129 # "Special Registers Altered" expected
130 l
= lines
.pop(0).rstrip()
131 assert l
.startswith("Special"), ("special not found %s" % l
)
134 # whitespace expected
135 l
= lines
.pop(0).strip()
136 assert len(l
) == 0, ("blank line not found %s" % l
)
141 l
= lines
.pop(0).rstrip()
145 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
147 # expect and drop whitespace
149 l
= lines
.pop(0).rstrip()
156 def read_file(self
, fname
):
157 pagename
= fname
.split('.')[0]
158 fname
= os
.path
.join(get_isa_dir(), fname
)
159 with
open(fname
) as f
:
160 lines
= f
.readlines()
162 # set up dict with current page name
163 d
= {'page': pagename
}
165 # line-by-line lexer/parser, quite straightforward: pops one
166 # line off the list and checks it. nothing complicated needed,
167 # all sections are mandatory so no need for a full LALR parser.
169 l
= lines
.pop(0).rstrip() # get first line
173 assert l
.startswith('#'), ("# not found in line %s" % l
)
174 d
['desc'] = l
[1:].strip()
176 # whitespace expected
177 l
= lines
.pop(0).strip()
179 assert len(l
) == 0, ("blank line not found %s" % l
)
182 l
= lines
.pop(0).strip()
183 assert l
.endswith('-Form'), ("line with -Form expected %s" % l
)
184 d
['form'] = l
.split('-')[0]
186 # whitespace expected
187 l
= lines
.pop(0).strip()
188 assert len(l
) == 0, ("blank line not found %s" % l
)
190 # get list of opcodes
193 l
= lines
.pop(0).strip()
196 assert l
.startswith('*'), ("* not found in line %s" % l
)
197 l
= l
[1:].split(' ') # lose star
198 l
= filter(lambda x
: len(x
) != 0, l
) # strip blanks
202 # "Pseudocode" expected
203 l
= lines
.pop(0).rstrip()
204 assert l
.startswith("Pseudo-code:"), ("pseudocode found %s" % l
)
206 # whitespace expected
207 l
= lines
.pop(0).strip()
209 assert len(l
) == 0, ("blank line not found %s" % l
)
214 l
= lines
.pop(0).rstrip()
217 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
218 l
= l
[4:] # lose 4 spaces
222 # "Special Registers Altered" expected
223 l
= lines
.pop(0).rstrip()
224 assert l
.startswith("Special"), ("special not found %s" % l
)
226 # whitespace expected
227 l
= lines
.pop(0).strip()
228 assert len(l
) == 0, ("blank line not found %s" % l
)
233 l
= lines
.pop(0).rstrip()
236 assert l
.startswith(' '), ("4spcs not found in line %s" % l
)
237 l
= l
[4:] # lose 4 spaces
245 # expect and drop whitespace
247 l
= lines
.pop(0).rstrip()
251 def add_op(self
, o
, d
):
252 opcode
, regs
= o
[0], o
[1:]
255 regs
[0] = regs
[0].split(",")
256 op
['opcode'] = opcode
257 self
.instr
[opcode
] = Ops(**op
)
259 # create list of instructions by form
261 fl
= self
.forms
.get(form
, [])
262 self
.forms
[form
] = fl
+ [opcode
]
264 # create list of instructions by page
266 pl
= self
.page
.get(page
, [])
267 self
.page
[page
] = pl
+ [opcode
]
269 def pprint_ops(self
):
270 for k
, v
in self
.instr
.items():
271 print("# %s %s" % (v
.opcode
, v
.desc
))
272 print("Form: %s Regs: %s" % (v
.form
, v
.regs
))
273 print('\n'.join(map(lambda x
: " %s" % x
, v
.pcode
)))
275 print('\n'.join(map(lambda x
: " %s" % x
, v
.sregs
)))
277 for k
, v
in isa
.forms
.items():
281 if __name__
== '__main__':