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
9 def __getitem__(self
, subscript
):
10 if isinstance(subscript
, slice):
11 return list(self
)[subscript
]
13 return 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
="fields.txt"):
113 self
.bitargs
= bitargs
114 self
.fname
= download_wiki_file(fname
)
116 def create_specs(self
):
117 self
.forms
, self
.instrs
= self
.decode_fields()
118 self
.form_names
= forms
= self
.instrs
.keys()
119 #print ("specs", self.forms, forms)
121 fields
= self
.instrs
[form
]
123 Fields
= namedtuple("Fields", fk
)
124 instr
= Fields(**fields
)
125 setattr(self
, "Form%s" % form
, instr
)
126 # now add in some commonly-used fields (should be done automatically)
127 # note that these should only be ones which are the same on all Forms
128 # note: these are from microwatt insn_helpers.vhdl
129 self
.common_fields
= {
137 "SH32": self
.FormM
.SH
,
138 "sh": self
.FormMD
.sh
,
139 "MB32": self
.FormM
.MB
,
140 "ME32": self
.FormM
.ME
,
145 "OE": self
.FormXO
.Rc
,
148 "CR": self
.FormXL
.XO
,
149 "BB": self
.FormXL
.BB
,
150 "BA": self
.FormXL
.BA
,
151 "BT": self
.FormXL
.BT
,
152 "FXM": self
.FormXFX
.FXM
,
153 "BO": self
.FormXL
.BO
,
154 "BI": self
.FormXL
.BI
,
155 "BH": self
.FormXL
.BH
,
157 "DS": self
.FormDS
.DS
,
163 "SPR": self
.FormXFX
.SPR
}
164 for k
, v
in self
.common_fields
.items():
167 def decode_fields(self
):
168 with
open(self
.fname
) as f
:
170 #print ("decode", txt)
181 forms
[heading
].append(l
)
184 heading
= l
[1:].strip()
185 # if heading.startswith('1.6.28'): # skip instr fields for now
187 heading
= heading
.split(' ')[-1]
194 for hdr
, form
in forms
.items():
195 if heading
== 'Fields':
196 i
= decode_instructions(form
)
197 for form
, field
in i
.items():
198 inst
[form
] = self
.decode_instruction_fields(field
)
200 # res[hdr] = decode_form(form)
203 def decode_instruction_fields(self
, fields
):
206 f
, spec
= field
.strip().split(" ")
207 d
= self
.bitkls(*self
.bitargs
)
209 for s
in spec
[1:-1].split(","):
221 f
= f
.replace(",", "_")
222 unique
= find_unique(res
, f
)
228 if __name__
== '__main__':
231 forms
, instrs
= dec
.forms
, dec
.instrs