@@ -63,51 +63,27 @@ def wire_transform(transform_func, select_types=WireVector,
63
63
:param select_types: Type or Tuple of types of WireVectors to replace
64
64
:param exclude_types: Type or Tuple of types of WireVectors to exclude from replacement
65
65
:param block: The Block to replace wires on
66
+
67
+ Note that if both new_src and new_dst don't equal orig_wire, orig_wire will
68
+ be removed from the block entirely.
66
69
"""
67
70
block = working_block (block )
71
+ src_nets , dst_nets = block .net_connections (include_virtual_nodes = False )
68
72
for orig_wire in block .wirevector_subset (select_types , exclude_types ):
69
73
new_src , new_dst = transform_func (orig_wire )
70
- replace_wire (orig_wire , new_src , new_dst , block )
74
+ replace_wire_fast (orig_wire , new_src , new_dst , src_nets , dst_nets , block )
71
75
72
76
73
77
def all_wires (transform_func ):
74
- """Decorator that wraps a wire transform function"""
78
+ """ Decorator that wraps a wire transform function. """
75
79
@functools .wraps (transform_func )
76
80
def t_res (** kwargs ):
77
81
wire_transform (transform_func , ** kwargs )
78
82
return t_res
79
83
80
84
81
- def replace_wire (orig_wire , new_src , new_dst , block = None ):
82
- block = working_block (block )
83
- if new_src is not orig_wire :
84
- # don't need to add the new_src and new_dst because they were made added at creation
85
- for net in block .logic :
86
- for wire in net .dests : # problem is that tuples use the == operator when using 'in'
87
- if wire is orig_wire :
88
- new_net = LogicNet (
89
- op = net .op , op_param = net .op_param , args = net .args ,
90
- dests = tuple (new_src if w is orig_wire else w for w in net .dests ))
91
- block .add_net (new_net )
92
- block .logic .remove (net )
93
- break
94
-
95
- if new_dst is not orig_wire :
96
- for net in block .logic :
97
- for wire in set (net .args ):
98
- if wire is orig_wire :
99
- new_net = LogicNet (
100
- op = net .op , op_param = net .op_param , dests = net .dests ,
101
- args = tuple (new_src if w is orig_wire else w for w in net .args ))
102
- block .add_net (new_net )
103
- block .logic .remove (net )
104
-
105
- if new_dst is not orig_wire and new_src is not orig_wire :
106
- block .remove_wirevector (orig_wire )
107
-
108
-
109
85
def replace_wires (wire_map , block = None ):
110
- """ Quickly replace all wires in a block.
86
+ """ Replace all wires in a block.
111
87
112
88
:param {old_wire: new_wire} wire_map: mapping of old wires to new wires
113
89
:param block: block to operate over (defaults to working block)
@@ -119,11 +95,7 @@ def replace_wires(wire_map, block=None):
119
95
120
96
121
97
def replace_wire_fast (orig_wire , new_src , new_dst , src_nets , dst_nets , block = None ):
122
- """
123
- Replace orig_wire with new_src and/or new_dst. The net that orig_wire originates from
124
- (its source net) will now feed into new_src as its destination, and the nets that
125
- orig_wire went to (its destination nets) will be fed from new_dst as
126
- their respective arguments.
98
+ """ Replace orig_wire with new_src and/or new_dst.
127
99
128
100
:param WireVector orig_wire: Wire to be replaced
129
101
:param WireVector new_src: Wire to replace orig_wire, anywhere orig_wire is the
@@ -134,10 +106,12 @@ def replace_wire_fast(orig_wire, new_src, new_dst, src_nets, dst_nets, block=Non
134
106
:param {WireVector: List[LogicNet]} dst_nets: Maps a wire to list of nets where it is an arg
135
107
:param Block block: The block on which to operate (defaults to working block)
136
108
137
- new_src will now originate from orig_wire's source net (meaning new_src will be that net's
138
- destination). new_dst will be now
109
+ The net that orig_wire originates from (its source net) will use new_src as its
110
+ destination wire. The nets that orig_wire went to (its destination nets) will now
111
+ have new_dst as one of their argument wires instead.
139
112
140
- This *updates* the src_nets and dst_nets maps that are passed in, such that:
113
+ This removes and/or adds nets to the block's logic set. This also *updates* the
114
+ src_nets and dst_nets maps that are passed in, such that the following hold:
141
115
142
116
```
143
117
old_src_net = src_nets[orig_wire]
@@ -149,8 +123,6 @@ def replace_wire_fast(orig_wire, new_src, new_dst, src_nets, dst_nets, block=Non
149
123
dst_nets[new_dst] = [old_dst_net (where old_dst_net.args replaces orig_wire with new_dst) foreach old_dst_net] # noqa
150
124
```
151
125
152
- This also removes and/or adds nets to the block's logic set.
153
-
154
126
For example, given the graph on left, `replace_wire_fast(w1, w4, w1, ...)` produces on right:
155
127
156
128
```
0 commit comments