aabf591af34a0eee7a6aa697fd89b6ffe998dcb5
1 from collections
import OrderedDict
, namedtuple
2 from soc
.decoder
.power_enums
import download_wiki_file
5 class BitRange(OrderedDict
):
6 """BitRange: remaps from straight indices (0,1,2..) to bit numbers
8 def __getitem__(self
, subscript
):
9 if isinstance(subscript
, slice):
10 return list(self
)[subscript
]
12 return self
[subscript
]
14 def decode_instructions(form
):
18 if l
.strip().startswith("Formats"):
19 l
= l
.strip().split(":")[-1]
20 l
= l
.replace(" ", "")
26 res
[fmt
].append(accum
[0])
29 accum
.append(l
.strip())
32 def decode_form_header(hdr
):
36 print (hdr
.split('|'))
37 for f
in hdr
.split("|"):
41 idx
= int(f
.strip().split(' ')[0])
46 def find_unique(d
, key
):
50 while "%s_%d" % (key
, idx
) in d
:
52 return "%s_%d" % (key
, idx
)
55 def decode_line(header
, line
):
61 for f
in line
.split("|"):
64 end
= count
+ len(f
) + 1
66 if not fieldname
or fieldname
.startswith('/'):
67 if prev_fieldname
is not None:
68 res
[prev_fieldname
] = (res
[prev_fieldname
], header
[count
])
72 bitstart
= header
[count
]
73 if prev_fieldname
is not None:
74 res
[prev_fieldname
] = (res
[prev_fieldname
], bitstart
)
75 res
[fieldname
] = bitstart
77 prev_fieldname
= fieldname
78 res
[prev_fieldname
] = (bitstart
, 32)
82 def decode_form(form
):
83 header
= decode_form_header(form
[0])
85 print ("header", header
)
87 dec
= decode_line(header
, line
)
93 for k
, (start
,end
) in l
.items():
95 if (start
, end
) == fields
[k
]:
96 continue # already in and matching for this Form
98 alternate
= "%s_%d" % (k
, falternate
[k
])
99 if (start
, end
) == fields
[alternate
]:
101 falternate
[k
] = fidx
= falternate
.get(k
, 0) + 1
102 fields
["%s_%d" % (k
, fidx
)] = (start
, end
)
104 fields
[k
] = (start
, end
)
110 def __init__(self
, bitkls
=BitRange
, bitargs
=(), fname
="fields.text"):
112 self
.bitargs
= bitargs
113 self
.fname
= download_wiki_file(fname
)
115 def create_specs(self
):
116 self
.forms
, self
.instrs
= self
.decode_fields()
117 self
.form_names
= forms
= self
.instrs
.keys()
119 fields
= self
.instrs
[form
]
121 Fields
= namedtuple("Fields", fk
)
122 instr
= Fields(**fields
)
123 setattr(self
, "Form%s" % form
, instr
)
124 # now add in some commonly-used fields (should be done automatically)
125 # note that these should only be ones which are the same on all Forms
126 # note: these are from microwatt insn_helpers.vhdl
127 self
.RS
= self
.FormX
.RS
128 self
.RT
= self
.FormX
.RT
129 self
.RA
= self
.FormX
.RA
130 self
.RB
= self
.FormX
.RB
131 self
.SI
= self
.FormD
.SI
132 self
.UI
= self
.FormD
.UI
133 self
.L
= self
.FormD
.L
134 self
.SH32
= self
.FormM
.SH
135 self
.sh
= self
.FormMD
.sh
136 self
.MB32
= self
.FormM
.MB
137 self
.ME32
= self
.FormM
.ME
138 self
.LI
= self
.FormI
.LI
139 self
.LK
= self
.FormI
.LK
140 self
.AA
= self
.FormB
.AA
141 self
.Rc
= self
.FormX
.Rc
142 self
.OE
= self
.FormXO
.Rc
143 self
.BD
= self
.FormB
.BD
144 self
.BF
= self
.FormX
.BF
145 self
.CR
= self
.FormXL
.XO
# used by further mcrf decoding
146 self
.BB
= self
.FormXL
.BB
147 self
.BA
= self
.FormXL
.BA
148 self
.BT
= self
.FormXL
.BT
149 self
.FXM
= self
.FormXFX
.FXM
150 self
.BO
= self
.FormXL
.BO
151 self
.BI
= self
.FormXL
.BI
152 self
.BH
= self
.FormXL
.BH
153 self
.D
= self
.FormD
.D
154 self
.DS
= self
.FormDS
.DS
155 self
.TO
= self
.FormX
.TO
156 self
.BC
= self
.FormA
.BC
157 self
.SH
= self
.FormX
.SH
158 self
.ME
= self
.FormM
.ME
159 self
.MB
= self
.FormM
.MB
160 self
.SPR
= self
.FormXFX
.SPR
162 def decode_fields(self
):
163 with
open(self
.fname
) as f
:
176 forms
[heading
].append(l
)
179 heading
= l
[1:].strip()
180 #if heading.startswith('1.6.28'): # skip instr fields for now
182 heading
= heading
.split(' ')[-1]
183 print ("heading", heading
)
190 for hdr
, form
in forms
.items():
191 print ("heading", hdr
)
192 if heading
== 'Fields':
193 i
= decode_instructions(form
)
194 for form
, field
in i
.items():
195 inst
[form
] = self
.decode_instruction_fields(field
)
197 # res[hdr] = decode_form(form)
200 def decode_instruction_fields(self
, fields
):
203 f
, spec
= field
.strip().split(" ")
204 d
= self
.bitkls(*self
.bitargs
)
206 for s
in spec
[1:-1].split(","):
218 f
= f
.replace(",", "_")
219 unique
= find_unique(res
, f
)
224 if __name__
== '__main__':
227 forms
, instrs
= dec
.forms
, dec
.instrs
228 for hdr
, form
in forms
.items():
231 for k
, v
in form
.items():
233 #for k, v in l.items():
234 print ("%s: %d-%d" % (k
, v
[0], v
[1]))
235 for form
, field
in instrs
.items():
238 for f
, vals
in field
.items():
242 print (dir(dec
.FormX
))
243 print (dec
.FormX
._fields
)