Skip to content

Commit 751e0f4

Browse files
wanda-phiwhitequark
authored andcommitted
ir: kill Fragment.ports
1 parent a725282 commit 751e0f4

File tree

9 files changed

+545
-635
lines changed

9 files changed

+545
-635
lines changed

amaranth/back/rtlil.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def emit_port_wires(self):
401401
port_id=port_id, port_kind=flow.value,
402402
name=name, attrs=self.value_attrs.get(value, {}))
403403
self.sigport_wires[name] = (wire, value)
404-
if flow == _nir.ModuleNetFlow.OUTPUT:
404+
if flow == _nir.ModuleNetFlow.Output:
405405
continue
406406
# If we just emitted an input or inout port, it is driving the value.
407407
self.driven_sigports.add(name)
@@ -463,7 +463,7 @@ def emit_submodule_wires(self):
463463
for submodule_idx in self.module.submodules:
464464
submodule = self.netlist.modules[submodule_idx]
465465
for _name, (value, flow) in submodule.ports.items():
466-
if flow == _nir.ModuleNetFlow.OUTPUT:
466+
if flow == _nir.ModuleNetFlow.Output:
467467
self.emit_driven_wire(value)
468468

469469
def sigspec(self, *parts: '_nir.Net | Iterable[_nir.Net]'):
@@ -989,10 +989,10 @@ def is_empty(self, module_idx):
989989
return module_idx in self.empty
990990

991991

992-
def convert_fragment(fragment, name="top", *, emit_src=True):
992+
def convert_fragment(fragment, ports, name="top", *, emit_src=True, **kwargs):
993993
assert isinstance(fragment, _ir.Fragment)
994994
name_map = _ast.SignalDict()
995-
netlist = _ir.build_netlist(fragment, name=name)
995+
netlist = _ir.build_netlist(fragment, ports=ports, name=name, **kwargs)
996996
empty_checker = EmptyModuleChecker(netlist)
997997
builder = _Builder(emit_src=emit_src)
998998
for module_idx, module in enumerate(netlist.modules):
@@ -1011,14 +1011,18 @@ def convert(elaboratable, name="top", platform=None, *, ports=None, emit_src=Tru
10111011
if (ports is None and
10121012
hasattr(elaboratable, "signature") and
10131013
isinstance(elaboratable.signature, wiring.Signature)):
1014-
ports = []
1015-
for _path, _member, value in elaboratable.signature.flatten(elaboratable):
1014+
ports = {}
1015+
for path, member, value in elaboratable.signature.flatten(elaboratable):
10161016
if isinstance(value, _ast.ValueCastable):
10171017
value = value.as_value()
10181018
if isinstance(value, _ast.Value):
1019-
ports.append(value)
1019+
if member.flow == wiring.In:
1020+
dir = _ir.PortDirection.Input
1021+
else:
1022+
dir = _ir.PortDirection.Output
1023+
ports["__".join(path)] = (value, dir)
10201024
elif ports is None:
10211025
raise TypeError("The `convert()` function requires a `ports=` argument")
1022-
fragment = _ir.Fragment.get(elaboratable, platform).prepare(ports=ports, **kwargs)
1023-
il_text, _name_map = convert_fragment(fragment, name, emit_src=emit_src)
1026+
fragment = _ir.Fragment.get(elaboratable, platform)
1027+
il_text, _name_map = convert_fragment(fragment, ports, name, emit_src=emit_src, **kwargs)
10241028
return il_text

amaranth/back/verilog.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ def convert(elaboratable, name="top", platform=None, *, ports=None, emit_src=Tru
4545
if (ports is None and
4646
hasattr(elaboratable, "signature") and
4747
isinstance(elaboratable.signature, wiring.Signature)):
48-
ports = []
48+
ports = {}
4949
for path, member, value in elaboratable.signature.flatten(elaboratable):
5050
if isinstance(value, _ast.ValueCastable):
5151
value = value.as_value()
5252
if isinstance(value, _ast.Value):
53-
ports.append(value)
53+
if member.flow == wiring.In:
54+
dir = _ir.PortDirection.Input
55+
else:
56+
dir = _ir.PortDirection.Output
57+
ports["__".join(path)] = (value, dir)
5458
elif ports is None:
5559
raise TypeError("The `convert()` function requires a `ports=` argument")
56-
fragment = _ir.Fragment.get(elaboratable, platform).prepare(ports=ports, **kwargs)
57-
verilog_text, name_map = convert_fragment(fragment, name, emit_src=emit_src, strip_internal_attrs=strip_internal_attrs)
60+
fragment = _ir.Fragment.get(elaboratable, platform)
61+
verilog_text, name_map = convert_fragment(fragment, ports, name, emit_src=emit_src, strip_internal_attrs=strip_internal_attrs, **kwargs)
5862
return verilog_text

amaranth/build/plat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,11 @@ def add_pin_fragment(pin, pin_fragment):
163163
if pin.dir == "io":
164164
add_pin_fragment(pin, self.get_diff_input_output(pin, port, attrs, invert))
165165

166-
fragment._propagate_ports(ports=self.iter_ports(), all_undef_as_ports=False)
167-
return self.toolchain_prepare(fragment, name, **kwargs)
166+
ports = list(self.iter_ports())
167+
return self.toolchain_prepare(fragment, ports, name, **kwargs)
168168

169169
@abstractmethod
170-
def toolchain_prepare(self, fragment, name, **kwargs):
170+
def toolchain_prepare(self, fragment, ports, name, **kwargs):
171171
"""
172172
Convert the ``fragment`` and constraints recorded in this :class:`Platform` into
173173
a :class:`BuildPlan`.
@@ -290,7 +290,7 @@ def iter_clock_constraints(self):
290290
continue
291291
yield net_signal, port_signal, frequency
292292

293-
def toolchain_prepare(self, fragment, name, *, emit_src=True, **kwargs):
293+
def toolchain_prepare(self, fragment, ports, name, *, emit_src=True, **kwargs):
294294
# Restrict the name of the design to a strict alphanumeric character set. Platforms will
295295
# interpolate the name of the design in many different contexts: filesystem paths, Python
296296
# scripts, Tcl scripts, ad-hoc constraint files, and so on. It is not practical to add
@@ -306,7 +306,8 @@ def toolchain_prepare(self, fragment, name, *, emit_src=True, **kwargs):
306306
# and to incorporate the Amaranth version into generated code.
307307
autogenerated = f"Automatically generated by Amaranth {__version__}. Do not edit."
308308

309-
rtlil_text, self._name_map = rtlil.convert_fragment(fragment, name=name, emit_src=emit_src)
309+
rtlil_text, self._name_map = rtlil.convert_fragment(fragment, ports=ports, name=name,
310+
emit_src=emit_src)
310311

311312
# Retrieve an override specified in either the environment or as a kwarg.
312313
# expected_type parameter is used to assert the type of kwargs, passing `None` will disable

0 commit comments

Comments
 (0)