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