add pagename to list
[soc.git] / src / soc / decoder / pseudo / pagereader.py
1 # Reads OpenPOWER ISA pages from http://libre-riscv.org/openpower/isa
2 """OpenPOWER ISA page parser
3
4 returns an OrderedDict of namedtuple "Ops" containing details of all
5 instructions listed in markdown files.
6
7 format must be strictly as follows (no optional sections) including whitespace:
8
9 # Compare Logical
10
11 X-Form
12
13 * cmpl BF,L,RA,RB
14
15 if L = 0 then a <- [0]*32 || (RA)[32:63]
16 b <- [0]*32 || (RB)[32:63]
17 else a <- (RA)
18 b <- (RB)
19 if a <u b then c <- 0b100
20 else if a >u b then c <- 0b010
21 else c <- 0b001
22 CR[4*BF+32:4*BF+35] <- c || XER[SO]
23
24 Special Registers Altered:
25
26 CR field BF
27 Another field
28
29 this translates to:
30
31 # heading
32 blank
33 Some-Form
34 blank
35 * instruction registerlist
36 * instruction registerlist
37 blank
38 4-space-indented pseudo-code
39 4-space-indented pseudo-code
40 blank
41 Special Registers Altered:
42 4-space-indented register description
43 blank
44 blank(s) (optional for convenience at end-of-page)
45 """
46
47 from collections import namedtuple, OrderedDict
48 from copy import copy
49 import os
50
51 opfields = ("desc", "form", "opcode", "regs", "pcode", "sregs", "page")
52 op = namedtuple("Ops", opfields)
53
54
55 def get_isa_dir():
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")
62
63
64 class ISA:
65
66 def __init__(self):
67 self.instr = OrderedDict()
68 self.forms = {}
69 for pth in os.listdir(os.path.join(get_isa_dir())):
70 print (get_isa_dir(), pth)
71 assert pth.endswith(".mdwn"), "only %s in isa dir" % pth
72 self.read_file(pth)
73
74 def read_file(self, fname):
75 pagename = fname.split('.')[0]
76 fname = os.path.join(get_isa_dir(), fname)
77 with open(fname) as f:
78 lines = f.readlines()
79
80 # set up dict with current page name
81 d = {'pagename': pagename}
82
83 l = lines.pop(0).rstrip() # get first line
84 while lines:
85 print (l)
86 # expect get heading
87 assert l.startswith('#'), ("# not found in line %s" % l)
88 d['desc'] = l[1:].strip()
89
90 # whitespace expected
91 l = lines.pop(0).strip()
92 print (repr(l))
93 assert len(l) == 0, ("blank line not found %s" % l)
94
95 # Form expected
96 l = lines.pop(0).strip()
97 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
98 d['form'] = l.split('-')[0]
99
100 # whitespace expected
101 l = lines.pop(0).strip()
102 assert len(l) == 0, ("blank line not found %s" % l)
103
104 # get list of opcodes
105 li = []
106 while True:
107 l = lines.pop(0).strip()
108 if len(l) == 0: break
109 assert l.startswith('*'), ("* not found in line %s" % l)
110 l = l[1:].split(' ') # lose star
111 l = filter(lambda x: len(x) != 0, l) # strip blanks
112 li.append(list(l))
113 opcodes = li
114
115 # get pseudocode
116 li = []
117 while True:
118 l = lines.pop(0).rstrip()
119 if len(l) == 0: break
120 assert l.startswith(' '), ("4spcs not found in line %s" % l)
121 l = l[4:] # lose 4 spaces
122 li.append(l)
123 d['pcode'] = li
124
125 # "Special Registers Altered" expected
126 l = lines.pop(0).rstrip()
127 assert l.startswith("Special"), ("special not found %s" % l)
128
129 # whitespace expected
130 l = lines.pop(0).strip()
131 assert len(l) == 0, ("blank line not found %s" % l)
132
133 # get special regs
134 li = []
135 while lines:
136 l = lines.pop(0).rstrip()
137 if len(l) == 0: break
138 assert l.startswith(' '), ("4spcs not found in line %s" % l)
139 l = l[4:] # lose 4 spaces
140 li.append(l)
141 d['sregs'] = li
142
143 # add in opcode
144 for o in opcodes:
145 self.add_op(o, d)
146
147 # expect and drop whitespace
148 while lines:
149 l = lines.pop(0).rstrip()
150 if len(l) != 0: break
151
152 def add_op(self, o, d):
153 opcode, regs = o[0], o[1:]
154 op = copy(d)
155 op['regs'] = regs
156 op['opcode'] = opcode
157 self.instr[opcode] = op
158
159 # create list of instructions by form
160 form = op['form']
161 fl = self.forms.get(form, [])
162 self.forms[form] = fl + [opcode]
163
164 def pprint_ops(self):
165 for k, v in self.instr.items():
166 print ("# %s %s" % (v['opcode'], v['desc']))
167 print ("Form: %s Regs: %s" % (v['form'], v['regs']))
168 print ('\n'.join(map(lambda x: " %s" % x, v['pcode'])))
169 print ("Specials")
170 print ('\n'.join(map(lambda x: " %s" % x, v['sregs'])))
171 print ()
172 for k, v in isa.forms.items():
173 print (k, v)
174
175 if __name__ == '__main__':
176 isa = ISA()
177 isa.pprint_ops()