1 from collections
import OrderedDict
, namedtuple
2 from soc
.decoder
.power_enums
import find_wiki_file
5 class BitRange(OrderedDict
):
6 """BitRange: remaps from straight indices (0,1,2..) to bit numbers
9 def __getitem__(self
, subscript
):
10 if isinstance(subscript
, slice):
11 return list(self
.values())[subscript
]
13 return OrderedDict
.__getitem
__(self
, subscript
)
16 def decode_instructions(form
):
20 if l
.strip().startswith("Formats"):
21 l
= l
.strip().split(":")[-1]
22 l
= l
.replace(" ", "")
28 res
[fmt
].append(accum
[0])
31 accum
.append(l
.strip())
35 def decode_form_header(hdr
):
39 for f
in hdr
.split("|"):
43 idx
= int(f
.strip().split(' ')[0])
49 def find_unique(d
, key
):
53 while "%s_%d" % (key
, idx
) in d
:
55 return "%s_%d" % (key
, idx
)
58 def decode_line(header
, line
):
63 for f
in line
.split("|"):
66 end
= count
+ len(f
) + 1
68 if not fieldname
or fieldname
.startswith('/'):
69 if prev_fieldname
is not None:
70 res
[prev_fieldname
] = (res
[prev_fieldname
], header
[count
])
74 bitstart
= header
[count
]
75 if prev_fieldname
is not None:
76 res
[prev_fieldname
] = (res
[prev_fieldname
], bitstart
)
77 res
[fieldname
] = bitstart
79 prev_fieldname
= fieldname
80 res
[prev_fieldname
] = (bitstart
, 32)
84 def decode_form(form
):
85 header
= decode_form_header(form
[0])
88 dec
= decode_line(header
, line
)
94 for k
, (start
, end
) in l
.items():
96 if (start
, end
) == fields
[k
]:
97 continue # already in and matching for this Form
99 alternate
= "%s_%d" % (k
, falternate
[k
])
100 if (start
, end
) == fields
[alternate
]:
102 falternate
[k
] = fidx
= falternate
.get(k
, 0) + 1
103 fields
["%s_%d" % (k
, fidx
)] = (start
, end
)
105 fields
[k
] = (start
, end
)
111 def __init__(self
, bitkls
=BitRange
, bitargs
=(), fname
=None,
114 self
.bitargs
= bitargs
116 assert name_on_wiki
is None
118 name_on_wiki
= "fields.text"
119 self
.fname
= find_wiki_file(name_on_wiki
)
121 def create_specs(self
):
122 self
.forms
, self
.instrs
= self
.decode_fields()
123 self
.form_names
= forms
= self
.instrs
.keys()
124 #print ("specs", self.forms, forms)
126 fields
= self
.instrs
[form
]
128 Fields
= namedtuple("Fields", fk
)
129 instr
= Fields(**fields
)
130 setattr(self
, "Form%s" % form
, instr
)
131 # now add in some commonly-used fields (should be done automatically)
132 # note that these should only be ones which are the same on all Forms
133 # note: these are from microwatt insn_helpers.vhdl
134 self
.common_fields
= {
142 "SH32": self
.FormM
.SH
,
143 "sh": self
.FormMD
.sh
,
144 "MB32": self
.FormM
.MB
,
145 "ME32": self
.FormM
.ME
,
150 "OE": self
.FormXO
.Rc
,
153 "CR": self
.FormXL
.XO
,
154 "BB": self
.FormXL
.BB
,
155 "BA": self
.FormXL
.BA
,
156 "BT": self
.FormXL
.BT
,
157 "FXM": self
.FormXFX
.FXM
,
158 "BO": self
.FormXL
.BO
,
159 "BI": self
.FormXL
.BI
,
160 "BH": self
.FormXL
.BH
,
162 "DS": self
.FormDS
.DS
,
168 "SPR": self
.FormXFX
.SPR
}
169 for k
, v
in self
.common_fields
.items():
172 def decode_fields(self
):
173 with
open(self
.fname
) as f
:
175 #print ("decode", txt)
186 forms
[heading
].append(l
)
189 heading
= l
[1:].strip()
190 # if heading.startswith('1.6.28'): # skip instr fields for now
192 heading
= heading
.split(' ')[-1]
199 for hdr
, form
in forms
.items():
200 if heading
== 'Fields':
201 i
= decode_instructions(form
)
202 for form
, field
in i
.items():
203 inst
[form
] = self
.decode_instruction_fields(field
)
205 # res[hdr] = decode_form(form)
208 def decode_instruction_fields(self
, fields
):
211 f
, spec
= field
.strip().split(" ")
212 ss
= spec
[1:-1].split(",")
215 individualfields
= []
216 for f0
, s0
in zip(fs
, ss
):
217 txt
= "%s (%s)" % (f0
, s0
)
218 individualfields
.append(txt
)
220 res
.update(self
.decode_instruction_fields(individualfields
))
221 d
= self
.bitkls(*self
.bitargs
)
235 f
= f
.replace(",", "_")
236 unique
= find_unique(res
, f
)
242 if __name__
== '__main__':
245 forms
, instrs
= dec
.forms
, dec
.instrs
246 for form
, fields
in instrs
.items():
248 for field
, bits
in fields
.items():
249 print ("\tfield", field
, bits
)