back: return name map from convert_fragment().
authorwhitequark <cz@m-labs.hk>
Wed, 11 Sep 2019 23:14:00 +0000 (23:14 +0000)
committerwhitequark <cz@m-labs.hk>
Wed, 11 Sep 2019 23:22:12 +0000 (23:22 +0000)
nmigen/back/rtlil.py
nmigen/back/verilog.py
nmigen/build/plat.py

index 09788aa50c29ef8f00b370b2bd013396a7ae54b1..470bd120abf6b0f263edf706828fa3d3accb339b 100644 (file)
@@ -716,7 +716,7 @@ class _StatementCompiler(xfrm.StatementVisitor):
             self.on_statement(stmt)
 
 
-def _convert_fragment(builder, fragment, hierarchy):
+def _convert_fragment(builder, fragment, name_map, hierarchy):
     if isinstance(fragment, ir.Instance):
         port_map = OrderedDict()
         for port_name, (value, dir) in fragment.named_ports.items():
@@ -803,7 +803,8 @@ def _convert_fragment(builder, fragment, hierarchy):
                     sub_params[param_name] = param_value
 
             sub_type, sub_port_map = \
-                _convert_fragment(builder, subfragment, hierarchy=hierarchy + (sub_name,))
+                _convert_fragment(builder, subfragment, name_map,
+                                  hierarchy=hierarchy + (sub_name,))
 
             sub_ports = OrderedDict()
             for port, value in sub_port_map.items():
@@ -924,23 +925,33 @@ def _convert_fragment(builder, fragment, hierarchy):
             wire_curr, _ = compiler_state.wires[wire]
             module.connect(wire_curr, rhs_compiler(ast.Const(wire.reset, wire.nbits)))
 
-    # Finally, collect the names we've given to our ports in RTLIL, and correlate these with
-    # the signals represented by these ports. If we are a submodule, this will be necessary
-    # to create a cell for us in the parent module.
+    # Collect the names we've given to our ports in RTLIL, and correlate these with the signals
+    # represented by these ports. If we are a submodule, this will be necessary to create a cell
+    # for us in the parent module.
     port_map = OrderedDict()
     for signal in fragment.ports:
         port_map[compiler_state.resolve_curr(signal)] = signal
 
+    # Finally, collect tha names we've given to each wire in RTLIL, and provide these to
+    # the caller, to allow manipulating them in the toolchain.
+    for signal in compiler_state.wires:
+        wire_name = compiler_state.resolve_curr(signal)
+        if wire_name.startswith("\\"):
+            wire_name = wire_name[1:]
+        name_map[signal] = hierarchy + (wire_name,)
+
     return module.name, port_map
 
 
 def convert_fragment(fragment, name="top"):
     assert isinstance(fragment, ir.Fragment)
     builder = _Builder()
-    _convert_fragment(builder, fragment, hierarchy=(name,))
-    return str(builder)
+    name_map = ast.SignalDict()
+    _convert_fragment(builder, fragment, name_map, hierarchy=(name,))
+    return str(builder), name_map
 
 
 def convert(elaboratable, name="top", platform=None, **kwargs):
     fragment = ir.Fragment.get(elaboratable, platform).prepare(**kwargs)
-    return convert_fragment(fragment, name)
+    il_text, name_map = convert_fragment(fragment, name)
+    return il_text
index d1a403226f0d9fb216c4a0da0a49e69ab3041b82..a70a35b29dd68fea9ba5ac07d9746e4221df69d0 100644 (file)
@@ -59,8 +59,8 @@ write_verilog -norename
 
 
 def convert_fragment(*args, strip_src=False, **kwargs):
-    il_text = rtlil.convert_fragment(*args, **kwargs)
-    return _convert_il_text(il_text, strip_src)
+    il_text, name_map = rtlil.convert_fragment(*args, **kwargs)
+    return _convert_il_text(il_text, strip_src), name_map
 
 
 def convert(*args, strip_src=False, **kwargs):
index 1c35cf60df4f9d38d839b5f427185387618a0a1a..f6aac5482f1ce318890ef1939a1d771fcbf5d65c 100644 (file)
@@ -254,9 +254,12 @@ class TemplatedPlatform(Platform):
         # and to incorporate the nMigen version into generated code.
         autogenerated = "Automatically generated by nMigen {}. Do not edit.".format(__version__)
 
+        name_map = None
         def emit_design(backend):
+            nonlocal name_map
             backend_mod = {"rtlil": rtlil, "verilog": verilog}[backend]
-            return backend_mod.convert_fragment(fragment, name=name)
+            design_text, name_map = backend_mod.convert_fragment(fragment, name=name)
+            return design_text
 
         def emit_commands(format):
             commands = []