1 from collections
import OrderedDict
5 with
open("fields.txt") as f
:
18 forms
[heading
].append(l
)
21 heading
= l
[1:].strip()
22 #if heading.startswith('1.6.28'): # skip instruction fields for now
24 heading
= heading
.split(' ')[-1]
25 print ("heading", heading
)
32 for hdr
, form
in forms
.items():
33 print ("heading", hdr
)
34 if heading
== 'Fields':
35 i
= decode_instructions(form
)
36 for form
, field
in i
.items():
37 inst
[form
] = decode_instruction_fields(field
)
39 # res[hdr] = decode_form(form)
42 class BitRange(OrderedDict
):
43 """BitRange: remaps from straight indices (0,1,2..) to bit numbers
45 def __getitem__(self
, subscript
):
46 if isinstance(subscript
, slice):
47 return list(self
)[subscript
]
49 return self
[subscript
]
52 def decode_instruction_fields(fields
):
55 f
, spec
= field
.strip().split(" ")
58 for s
in spec
[1:-1].split(","):
74 def decode_instructions(form
):
78 if l
.strip().startswith("Formats"):
79 l
= l
.strip().split(":")[-1]
80 l
= l
.replace(" ", "")
86 res
[fmt
].append(accum
[0])
89 accum
.append(l
.strip())
92 def decode_form_header(hdr
):
96 print (hdr
.split('|'))
97 for f
in hdr
.split("|"):
101 idx
= int(f
.strip().split(' ')[0])
106 def find_unique(d
, key
):
110 while "%s_%d" % (key
, idx
) in d
:
112 return "%s_%d" % (key
, idx
)
115 def decode_line(header
, line
):
120 prev_fieldname
= None
121 for f
in line
.split("|"):
124 end
= count
+ len(f
) + 1
125 fieldname
= f
.strip()
126 if not fieldname
or fieldname
.startswith('/'):
127 if prev_fieldname
is not None:
128 res
[prev_fieldname
] = (res
[prev_fieldname
], header
[count
])
129 prev_fieldname
= None
132 bitstart
= header
[count
]
133 if prev_fieldname
is not None:
134 res
[prev_fieldname
] = (res
[prev_fieldname
], bitstart
)
135 res
[fieldname
] = bitstart
137 prev_fieldname
= fieldname
138 res
[prev_fieldname
] = (bitstart
, 32)
142 def decode_form(form
):
143 header
= decode_form_header(form
[0])
145 print ("header", header
)
146 for line
in form
[1:]:
147 dec
= decode_line(header
, line
)
153 for k
, (start
,end
) in l
.items():
155 if (start
, end
) == fields
[k
]:
156 continue # already in and matching for this Form
158 alternate
= "%s_%d" % (k
, falternate
[k
])
159 if (start
, end
) == fields
[alternate
]:
161 falternate
[k
] = fidx
= falternate
.get(k
, 0) + 1
162 fields
["%s_%d" % (k
, fidx
)] = (start
, end
)
164 fields
[k
] = (start
, end
)
168 if __name__
== '__main__':
169 forms
, instrs
= decode_fields()
170 for hdr
, form
in forms
.items():
173 for k
, v
in form
.items():
175 #for k, v in l.items():
176 print ("%s: %d-%d" % (k
, v
[0], v
[1]))
177 for form
, field
in instrs
.items():
180 for f
, vals
in field
.items():