add docstring for pagereader module
[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 (optional)
45
46 """
47
48 from collections import namedtuple, OrderedDict
49 from copy import copy
50 import os
51
52 op = namedtuple("Ops", ("desc", "form", "opcode", "regs", "pcode", "sregs"))
53
54 def get_isa_dir():
55 fdir = os.path.abspath(os.path.dirname(__file__))
56 fdir = os.path.split(fdir)[0]
57 fdir = os.path.split(fdir)[0]
58 fdir = os.path.split(fdir)[0]
59 fdir = os.path.split(fdir)[0]
60 return os.path.join(fdir, "libreriscv", "openpower", "isa")
61
62 class ISA:
63
64 def __init__(self):
65 self.instr = OrderedDict()
66
67 def read_file(self, fname):
68 fname = os.path.join(get_isa_dir(), fname)
69 with open(fname) as f:
70 lines = f.readlines()
71
72 d = {}
73 l = lines.pop(0).rstrip() # get first line
74 while lines:
75 print (l)
76 # expect get heading
77 assert l.startswith('#'), ("# not found in line %s" % l)
78 d['desc'] = l[1:].strip()
79
80 # whitespace expected
81 l = lines.pop(0).strip()
82 print (repr(l))
83 assert len(l) == 0, ("blank line not found %s" % l)
84
85 # Form expected
86 l = lines.pop(0).strip()
87 assert l.endswith('-Form'), ("line with -Form expected %s" % l)
88 d['form'] = l.split('-')[0]
89
90 # whitespace expected
91 l = lines.pop(0).strip()
92 assert len(l) == 0, ("blank line not found %s" % l)
93
94 # get list of opcodes
95 li = []
96 while True:
97 l = lines.pop(0).strip()
98 if len(l) == 0: break
99 assert l.startswith('*'), ("* not found in line %s" % l)
100 l = l[1:].split(' ') # lose star
101 l = filter(lambda x: len(x) != 0, l) # strip blanks
102 li.append(list(l))
103 opcodes = li
104
105 # get pseudocode
106 li = []
107 while True:
108 l = lines.pop(0).rstrip()
109 if len(l) == 0: break
110 assert l.startswith(' '), ("4spcs not found in line %s" % l)
111 l = l[4:] # lose 4 spaces
112 li.append(l)
113 d['pcode'] = li
114
115 # "Special Registers Altered" expected
116 l = lines.pop(0).rstrip()
117 assert l.startswith("Special"), ("special not found %s" % l)
118
119 # whitespace expected
120 l = lines.pop(0).strip()
121 assert len(l) == 0, ("blank line not found %s" % l)
122
123 # get special regs
124 li = []
125 while lines:
126 l = lines.pop(0).rstrip()
127 if len(l) == 0: break
128 assert l.startswith(' '), ("4spcs not found in line %s" % l)
129 l = l[4:] # lose 4 spaces
130 li.append(l)
131 d['sregs'] = li
132
133 # add in opcode
134 for o in opcodes:
135 opcode, regs = o[0], o[1:]
136 op = copy(d)
137 op['regs'] = regs
138 op['opcode'] = opcode
139 self.instr[opcode] = op
140
141 # expect and drop whitespace
142 while lines:
143 l = lines.pop(0).rstrip()
144 if len(l) != 0: break
145
146 def pprint_ops(self):
147 for k, v in self.instr.items():
148 print ("# %s %s" % (v['opcode'], v['desc']))
149 print ("Form: %s Regs: %s" % (v['form'], v['regs']))
150 print ('\n'.join(map(lambda x: " %s" % x, v['pcode'])))
151 print ("Specials")
152 print ('\n'.join(map(lambda x: " %s" % x, v['sregs'])))
153 print ()
154
155 if __name__ == '__main__':
156 isa = ISA()
157 for pth in os.listdir(os.path.join(get_isa_dir())):
158 print (get_isa_dir(), pth)
159 assert pth.endswith(".mdwn"), "only %s in isa dir" % pth
160 isa.read_file(pth)
161
162 isa.pprint_ops()