@@ -66,7 +66,7 @@ static std::string derive_name_from_src(const std::string &src, int counter)
66
66
return stringf (" \\ %s$%d" , src_base.c_str (), counter);
67
67
}
68
68
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 )
70
70
{
71
71
// Find output
72
72
const SigSpec *output = nullptr ;
@@ -92,13 +92,23 @@ static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, strin
92
92
93
93
name += chunk.wire ->name .str ();
94
94
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);
99
104
}
100
105
}
101
106
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
+
102
112
if (suffix.empty ()) {
103
113
suffix = cell->type .str ();
104
114
}
@@ -211,13 +221,17 @@ struct RenamePass : public Pass {
211
221
log (" cells with private names.\n " );
212
222
log (" \n " );
213
223
log (" \n " );
214
- log (" rename -wire [selection] [-suffix <suffix>]\n " );
224
+ log (" rename -wire [selection] [-move-to-cell] [- suffix <suffix>]\n " );
215
225
log (" \n " );
216
226
log (" Assign auto-generated names based on the wires they drive to all selected\n " );
217
227
log (" cells with private names. Ignores cells driving privatly named wires.\n " );
218
228
log (" By default, the cell is named after the wire with the cell type as suffix.\n " );
219
229
log (" The -suffix option can be used to set the suffix to the given string instead.\n " );
220
230
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 " );
221
235
log (" \n " );
222
236
log (" rename -enumerate [-pattern <pattern>] [selection]\n " );
223
237
log (" \n " );
@@ -259,6 +273,7 @@ struct RenamePass : public Pass {
259
273
std::string cell_suffix = " " ;
260
274
bool flag_src = false ;
261
275
bool flag_wire = false ;
276
+ bool flag_move_to_cell = false ;
262
277
bool flag_enumerate = false ;
263
278
bool flag_witness = false ;
264
279
bool flag_hide = false ;
@@ -312,6 +327,10 @@ struct RenamePass : public Pass {
312
327
got_mode = true ;
313
328
continue ;
314
329
}
330
+ if (arg == " -move-to-cell" && flag_wire && !flag_move_to_cell) {
331
+ flag_move_to_cell = true ;
332
+ continue ;
333
+ }
315
334
if (arg == " -pattern" && argidx+1 < args.size () && args[argidx+1 ].find (' %' ) != std::string::npos) {
316
335
int pos = args[++argidx].find (' %' );
317
336
pattern_prefix = args[argidx].substr (0 , pos);
@@ -363,9 +382,26 @@ struct RenamePass : public Pass {
363
382
dict<RTLIL::Cell *, IdString> new_cell_names;
364
383
for (auto cell : module ->selected_cells ())
365
384
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
+ }
369
405
}
370
406
}
371
407
else
0 commit comments