Skip to content

Commit 7be57c8

Browse files
committed
Improve Verilog identifier naming, fix two bugs, add tests, refactor duplicated
code: Update `_NameSanitizer` to try cleaning up an invalid name before giving up and auto-generating a new name. The cleaning process just replaces non-word characters `[A-Za-z0-9_]` with underscores, and also tries adding a prefix underscore. This makes the generated Verilog output easier to read, especially when using `wire_struct` or `wire_matrix`, which both add non-word characters to wire names. Remove `_NameSanitizer`'s `map_valid_vals` and `allow_duplicates` options, which don't seem to be used. Bugs fixed: - `output_verilog_testbench` should not re-initialize RomBlocks. This appears to be an existing bug. - Consts inputs to bit-slices must be declared. Add tests for these bugs. Also move the Verilog export code to a new `class _VerilogOutputter`. This lets `output_to_verilog` and `output_verilog_testbench` share more code. Also remove `OutputToVerilog`. It is deprecated, and the forwarding function was broken, so it's pretty clear nobody uses this.
1 parent c94b341 commit 7be57c8

4 files changed

Lines changed: 655 additions & 653 deletions

File tree

pyrtl/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
from .importexport import (
104104
input_from_verilog,
105105
output_to_verilog,
106-
OutputToVerilog,
107106
output_verilog_testbench,
108107
input_from_blif,
109108
output_to_firrtl,
@@ -242,7 +241,6 @@
242241
# importexport
243242
"input_from_verilog",
244243
"output_to_verilog",
245-
"OutputToVerilog",
246244
"output_verilog_testbench",
247245
"input_from_blif",
248246
"output_to_firrtl",

pyrtl/core.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,40 +1206,43 @@ def __init__(
12061206
self,
12071207
identifier_regex_str,
12081208
internal_prefix="_sani_temp",
1209-
map_valid_vals=True,
12101209
extra_checks=lambda _string: True,
1211-
allow_duplicates=False,
12121210
):
12131211
if identifier_regex_str[-1] != "$":
12141212
identifier_regex_str += "$"
12151213
self.identifier = re.compile(identifier_regex_str)
12161214
self.val_map = {}
1217-
self.map_valid = map_valid_vals
12181215
self.extra_checks = extra_checks
1219-
self.allow_dups = allow_duplicates
12201216
super().__init__(internal_prefix)
12211217

12221218
def __getitem__(self, item):
12231219
"""Get a value from the sanitizer"""
1224-
if not self.map_valid and self.is_valid_str(item):
1225-
return item
12261220
return self.val_map[item]
12271221

12281222
def is_valid_str(self, string):
12291223
return self.identifier.match(string) and self.extra_checks(string)
12301224

12311225
def make_valid_string(self, string=""):
12321226
"""Inputting a value for the first time."""
1233-
if not self.is_valid_str(string):
1234-
if string in self.val_map and not self.allow_dups:
1235-
msg = f"Value {string} has already been given to the sanitizer"
1236-
raise IndexError(msg)
1237-
internal_name = super().make_valid_string()
1238-
self.val_map[string] = internal_name
1239-
return internal_name
1240-
if self.map_valid:
1227+
if self.is_valid_str(string):
12411228
self.val_map[string] = string
1242-
return string
1229+
return string
1230+
1231+
if string in self.val_map:
1232+
msg = f"Value {string} has already been given to the sanitizer"
1233+
raise IndexError(msg)
1234+
# Try replacing non-word characters with ``_``.
1235+
internal_name = re.sub(r"\W", "_", string)
1236+
1237+
if not self.is_valid_str(internal_name) or internal_name in self.val_map:
1238+
# If that didn't work, try prepending ``_``.
1239+
internal_name = f"_{internal_name}"
1240+
while not self.is_valid_str(internal_name) or internal_name in self.val_map:
1241+
# If that didn't work, generate names starting with ``internal_prefix``
1242+
# until we find something acceptable.
1243+
internal_name = super().make_valid_string()
1244+
self.val_map[string] = internal_name
1245+
return internal_name
12431246

12441247

12451248
class _PythonSanitizer(_NameSanitizer):

0 commit comments

Comments
 (0)