Skip to content

Commit fa6968d

Browse files
committed
rename: add -move-to-cell option in -wire mode
1 parent c4b5190 commit fa6968d

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

passes/cmds/rename.cc

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static std::string derive_name_from_src(const std::string &src, int counter)
6666
return stringf("\\%s$%d", src_base.c_str(), counter);
6767
}
6868

69-
static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, string suffix)
69+
static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, string suffix, bool move_to_cell)
7070
{
7171
// Find output
7272
const SigSpec *output = nullptr;
@@ -92,13 +92,23 @@ static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, strin
9292

9393
name += chunk.wire->name.str();
9494
if (chunk.wire->width != chunk.width) {
95-
name += "[";
96-
if (chunk.width != 1)
97-
name += std::to_string(chunk.offset + chunk.width) + ":";
98-
name += std::to_string(chunk.offset) + "]";
95+
int lhs = chunk.wire->to_hdl_index(chunk.offset + chunk.width - 1);
96+
int rhs = chunk.wire->to_hdl_index(chunk.offset);
97+
if (chunk.wire->upto)
98+
std::swap(lhs, rhs);
99+
100+
if (lhs != rhs)
101+
name += stringf("[%d:%d]", lhs, rhs);
102+
else
103+
name += stringf("[%d]", lhs);
99104
}
100105
}
101106

107+
RTLIL::Wire *wire;
108+
109+
if (move_to_cell && (!(wire = cell->module->wire(name)) || !(wire->port_input || wire->port_output)))
110+
return name;
111+
102112
if (suffix.empty()) {
103113
suffix = cell->type.str();
104114
}
@@ -211,13 +221,17 @@ struct RenamePass : public Pass {
211221
log("cells with private names.\n");
212222
log("\n");
213223
log("\n");
214-
log(" rename -wire [selection] [-suffix <suffix>]\n");
224+
log(" rename -wire [selection] [-move-to-cell] [-suffix <suffix>]\n");
215225
log("\n");
216226
log("Assign auto-generated names based on the wires they drive to all selected\n");
217227
log("cells with private names. Ignores cells driving privatly named wires.\n");
218228
log("By default, the cell is named after the wire with the cell type as suffix.\n");
219229
log("The -suffix option can be used to set the suffix to the given string instead.\n");
220230
log("\n");
231+
log("The -move-to-cell option can be used to name the cell after the wire without\n");
232+
log("any suffix. If this would lead to conflicts, the suffix is added to the wire\n");
233+
log("instead. For cells driving ports, the -move-to-cell option is ignored.\n");
234+
log("\n");
221235
log("\n");
222236
log(" rename -enumerate [-pattern <pattern>] [selection]\n");
223237
log("\n");
@@ -259,6 +273,7 @@ struct RenamePass : public Pass {
259273
std::string cell_suffix = "";
260274
bool flag_src = false;
261275
bool flag_wire = false;
276+
bool flag_move_to_cell = false;
262277
bool flag_enumerate = false;
263278
bool flag_witness = false;
264279
bool flag_hide = false;
@@ -312,6 +327,10 @@ struct RenamePass : public Pass {
312327
got_mode = true;
313328
continue;
314329
}
330+
if (arg == "-move-to-cell" && flag_wire && !flag_move_to_cell) {
331+
flag_move_to_cell = true;
332+
continue;
333+
}
315334
if (arg == "-pattern" && argidx+1 < args.size() && args[argidx+1].find('%') != std::string::npos) {
316335
int pos = args[++argidx].find('%');
317336
pattern_prefix = args[argidx].substr(0, pos);
@@ -363,9 +382,26 @@ struct RenamePass : public Pass {
363382
dict<RTLIL::Cell *, IdString> new_cell_names;
364383
for (auto cell : module->selected_cells())
365384
if (cell->name[0] == '$')
366-
new_cell_names[cell] = derive_name_from_cell_output_wire(cell, cell_suffix);
367-
for (auto &it : new_cell_names)
368-
module->rename(it.first, it.second);
385+
new_cell_names[cell] = derive_name_from_cell_output_wire(cell, cell_suffix, flag_move_to_cell);
386+
for (auto &[cell, new_name] : new_cell_names) {
387+
if (flag_move_to_cell) {
388+
RTLIL::Wire *found_wire = module->wire(new_name);
389+
if (found_wire) {
390+
std::string wire_suffix = cell_suffix;
391+
if (wire_suffix.empty()) {
392+
for (auto const &[port, _] : cell->connections()) {
393+
if (cell->output(port)) {
394+
wire_suffix += stringf("%s.%s", cell->type.c_str(), port.c_str() + 1);
395+
break;
396+
}
397+
}
398+
}
399+
IdString new_wire_name = found_wire->name.str() + wire_suffix;
400+
module->rename(found_wire, new_wire_name);
401+
}
402+
}
403+
module->rename(cell, new_name);
404+
}
369405
}
370406
}
371407
else
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
read_verilog <<EOF
2+
module top(input clk, rst, input [7:0] din, output [7:0] dout, input bin, output bout);
3+
reg [7:0] dq;
4+
reg bq;
5+
6+
always @(posedge clk, posedge rst) begin
7+
if (rst) dq <= '0;
8+
else dq <= din;
9+
end
10+
11+
always @(posedge clk) bq <= bin;
12+
13+
assign dout = dq;
14+
assign bout = bq;
15+
endmodule
16+
EOF
17+
18+
proc
19+
hierarchy -top top
20+
21+
select -assert-count 1 t:$dff
22+
select -assert-count 1 t:$adff
23+
select -assert-count 0 t:$dff n:bq %i
24+
select -assert-count 0 t:$adff n:dq %i
25+
select -assert-count 1 w:bq
26+
select -assert-count 1 w:dq
27+
28+
rename -wire -move-to-cell
29+
30+
select -assert-count 1 t:$dff
31+
select -assert-count 1 t:$adff
32+
select -assert-count 1 t:$dff n:bq %i
33+
select -assert-count 1 t:$adff n:dq %i
34+
select -assert-count 0 w:bq
35+
select -assert-count 0 w:dq

0 commit comments

Comments
 (0)