forked from openhwgroup/cva6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_config.py
261 lines (188 loc) · 6.29 KB
/
user_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import sys
import re
# Public interface
def get_config(input_file, to_py=True):
"Get the contents of `cva6_cfg` as a Python `dict`."
lines = read_file(input_file)
params, config = parse(lines)
return {k: evaluate(v, params, to_py) for k, v in config.items()}
def derive_config(input_file, output_file, changes):
"""
Change variables in the configuration and write the result.
`changes` is a name->value dictionary. By default it looks in cva6_cfg. If
the value is a reference to a `localparam`, it finds the root parameter
recursively. To avoid this, prefix the name with `*` to only look in
cva6_cfg, or `+` to only update one specific `localparam`.
"""
lines = read_file(input_file)
params, config = parse(lines)
i_params, i_config = parse(lines, get_index=True)
for name, new_value in changes:
if name[0] == "*":
name = name[1:]
replace_cfg(lines, config, i_config, name, new_value)
elif name[0] == "+":
name = name[1:]
replace_param(lines, i_params, name, new_value)
else:
t, value = find_casting(config[name])
root = find_root_def(value, params)
if root:
name, _value = root
replace_param(lines, i_params, name, new_value)
else:
replace_cfg(lines, config, i_config, name, new_value)
write_file(output_file, lines)
def parse_derive_args(argv):
"Parse base config and changes from argv"
base = argv[0]
changes = list(map(lambda s: tuple(s.split("=")), argv[1:]))
return base, changes
# Internals
def parse(lines, get_index=False):
params = {}
config = {}
for index, line in enumerate(lines):
param = parse_param(line)
if param:
name, value = param
params[name] = index if get_index else value
cfg = parse_cfg(line)
if cfg:
name, value = cfg
config[name] = index if get_index else value
return params, config
def evaluate(value, params, to_py):
t, value = find_casting(value)
root = find_root_def(value, params)
if root:
_name, value = root
if to_py:
py_cast = to_py_casting(t)
if py_cast:
value = py_cast(value)
parsers = [array, number]
for parser in parsers:
if type(value) is str:
value = parser(value)
return value
def find_root_def(name, params):
if name in params:
value = params[name]
while value in params:
name = value
value = params[name]
return (name, value)
return None
def replace_cfg(lines, config, i_config, name, new_value):
t, _value = find_casting(config[name])
value = build_casting(t, new_value)
line = build_cfg(lines[i_config[name]], value)
lines[i_config[name]] = line
def replace_param(lines, i_params, name, new_value):
line = build_param(lines[i_params[name]], new_value)
lines[i_params[name]] = line
# Statement parsers
param_re = re.compile(
r"^(\s*localparam\b.*?\b)(?P<name>\w+)(\s*=\s*)(?P<value>[^;]+)(;.*)$"
)
def parse_param(line):
match = param_re.match(line)
if match:
name = match["name"]
value = match["value"].strip()
return name, value
else:
return None
def build_param(line, value):
assert param_re.match(line)
return param_re.sub(r"\g<1>\g<2>\g<3>" + value + r"\g<5>", line)
cfg_re = re.compile(r"^(\s*)(?P<name>\w+)(\s*:\s*)(?P<value>.*?)(,?\s*)$")
def parse_cfg(line):
match = cfg_re.match(line)
if match:
name = match["name"]
value = match["value"].strip()
return name, value
else:
return None
def build_cfg(line, value):
assert cfg_re.match(line)
return cfg_re.sub(r"\g<1>\g<2>\g<3>" + value + r"\g<5>", line)
# Expression parsers
type_re = re.compile(r"^(?P<type>\w+)\s*'\((?P<value>.*)\)$")
def find_casting(value):
match = type_re.match(value.strip())
if match:
t = match["type"]
value = match["value"].strip()
return (t, value)
else:
return (None, value)
def build_casting(t, value):
return f"{t}'({value})" if t else value
def to_py_casting(t):
if t == "bit":
return lambda x: bool(int(x))
elif t == "unsigned":
return int
else:
return None
number_re = re.compile(r"^\d*'s?(?P<base>.)(?P<digits>.*)$")
def number(value):
"Parse a <len>'<base><value> `str` to `int`."
match = number_re.match(value)
if match:
base = match["base"]
digits = match["digits"]
if base == "b":
return BasedNumber(digits, 2)
elif base == "o":
return BasedNumber(digits, 8)
elif base == "d":
return BasedNumber(digits, 10)
elif base == "h":
return BasedNumber(digits, 16)
else:
raise Exception(f"unknown base {base}")
else:
return value
class BasedNumber(int):
def __new__(cls, digits, base):
obj = int.__new__(cls, digits, base)
obj.base = base
return obj
def __repr__(self):
if self.base == 2:
return bin(self)
elif self.base == 8:
return oct(self)
elif self.base == 16:
return hex(self)
else:
return super().__repr__()
repeat_re = re.compile(r"^{\s*(?P<times>\d+)\s*{(?P<value>.*)}\s*}$")
array_re = re.compile(r"^{(?P<values>.*)}$")
def array(value):
"Parse `str` to `list`. Only arrays of literal integers are supported."
match = repeat_re.match(value)
if match:
return int(match["times"]) * [number(match["value"].strip())]
match = array_re.match(value)
if match:
values = match["values"].split(",")
return list(map(lambda value: number(value.strip()), values))
return value
# File handling
def read_file(path):
with open(path, "r") as f:
return f.readlines()
def write_file(path, lines):
with open(path, "w") as f:
f.writelines(lines)
# Command line interface
if __name__ == "__main__":
base, changes = parse_derive_args(sys.argv[1:])
input_file = f"core/include/{base}_config_pkg.sv"
output_file = "core/include/gen_config_pkg.sv"
derive_config(input_file, output_file, changes)