Better dictionary...
This commit is contained in:
parent
fd78ec3960
commit
1cc788c9a1
51
main.py
51
main.py
|
@ -119,18 +119,41 @@ def parse_registers():
|
||||||
next(it)
|
next(it)
|
||||||
return it
|
return it
|
||||||
|
|
||||||
|
def interpret_field(field):
|
||||||
|
bit_pattern = re.compile(r'(\d+)(:(\d+))?')
|
||||||
|
m = bit_pattern.fullmatch(field['bits'][0])
|
||||||
|
if m is None:
|
||||||
|
raise ValueError(field['bits'])
|
||||||
|
high = int(m.group(1))
|
||||||
|
low = high
|
||||||
|
if m.group(3) is not None:
|
||||||
|
low = int(m.group(3))
|
||||||
|
result = {}
|
||||||
|
result['name'] = field['name'][0]
|
||||||
|
result['bits'] = [low, high]
|
||||||
|
result['type'] = field['type'][0]
|
||||||
|
return result
|
||||||
|
|
||||||
def interpret(reg):
|
def interpret(reg):
|
||||||
pattern = re.compile(r'(.+\w)\d+')
|
pattern = re.compile(r'(.+\w)\d+')
|
||||||
|
width_pattern = re.compile(r'(\d+)\s?bits')
|
||||||
result = {}
|
result = {}
|
||||||
name_pattern = None
|
name_pattern = None
|
||||||
expected = ['Name', 'Relative Address', 'Absolute Address', 'Width',
|
expected = [
|
||||||
'Access Type', 'Reset Value', 'Description']
|
['Name', 'name', lambda v: ''.join(v)],
|
||||||
|
['Relative Address', 'rel', lambda v: int(v[0], 16)],
|
||||||
|
['Absolute Address', 'abs', lambda v: int(v[0], 16)],
|
||||||
|
['Width', 'width', lambda v: int(width_pattern.fullmatch(v[0]).group(1))],
|
||||||
|
['Access Type', 'access', lambda v: v[0]],
|
||||||
|
['Reset Value', 'reset', lambda v: v[0]],
|
||||||
|
['Description', 'description', lambda v: ' '.join(v)]
|
||||||
|
]
|
||||||
for v in reg['def']:
|
for v in reg['def']:
|
||||||
a = v['key']
|
a = v['key']
|
||||||
b = v['value']
|
b = v['value']
|
||||||
key = a[0].strip()
|
key = a[0].strip()
|
||||||
if len(expected) > 0 and key == expected[0]:
|
if len(expected) > 0 and key == expected[0][0]:
|
||||||
result[key] = b
|
result[expected[0][1]] = expected[0][2](b)
|
||||||
expected = expected[1:]
|
expected = expected[1:]
|
||||||
if key == 'Name':
|
if key == 'Name':
|
||||||
m = pattern.fullmatch(b[0].strip())
|
m = pattern.fullmatch(b[0].strip())
|
||||||
|
@ -139,10 +162,24 @@ def interpret(reg):
|
||||||
elif name_pattern != None and key.startswith(name_pattern):
|
elif name_pattern != None and key.startswith(name_pattern):
|
||||||
if 'similar' not in result:
|
if 'similar' not in result:
|
||||||
result['similar'] = []
|
result['similar'] = []
|
||||||
result['similar'].append({'name': key, 'address': b[0].strip()})
|
result['similar'].append({'name': key, 'abs': int(b[0], 16)})
|
||||||
result['fields'] = reg['fields']
|
result['fields'] = [interpret_field(f) for f in reg['fields']]
|
||||||
|
assert len(expected) == 0
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def snake_to_camel(name: str):
|
||||||
|
result = []
|
||||||
|
start = True
|
||||||
|
for c in name:
|
||||||
|
if c == '_':
|
||||||
|
start = True
|
||||||
|
elif start:
|
||||||
|
start = False
|
||||||
|
result.append(c.upper())
|
||||||
|
else:
|
||||||
|
result.append(c.lower())
|
||||||
|
return ''.join(result)
|
||||||
|
|
||||||
def emit_rust(base_addr, ending_addr, registers):
|
def emit_rust(base_addr, ending_addr, registers):
|
||||||
current_addr = base_addr
|
current_addr = base_addr
|
||||||
reserved_id = 0
|
reserved_id = 0
|
||||||
|
@ -188,4 +225,4 @@ for line in sys.stdin:
|
||||||
v = end_iterator(parser)
|
v = end_iterator(parser)
|
||||||
for reg in v:
|
for reg in v:
|
||||||
reg = interpret(reg)
|
reg = interpret(reg)
|
||||||
print(json.dumps(reg, indent = 4))
|
print(reg)
|
||||||
|
|
Loading…
Reference in New Issue