@@ -67,7 +67,7 @@ static std::string derive_name_from_src(const std::string &src, int counter)
67
67
return stringf (" \\ %s$%d" , src_base.c_str (), counter);
68
68
}
69
69
70
- static IdString derive_name_from_cell_output_wire (const RTLIL::Cell *cell, string suffix)
70
+ static IdString derive_name_from_cell_output_wire (const RTLIL::Cell *cell, string suffix, bool move_to_cell )
71
71
{
72
72
// Find output
73
73
const SigSpec *output = nullptr ;
@@ -93,13 +93,21 @@ static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, strin
93
93
94
94
name += chunk.wire ->name .str ();
95
95
if (chunk.wire ->width != chunk.width ) {
96
- name += " [" ;
97
- if (chunk.width != 1 )
98
- name += std::to_string (chunk.offset + chunk.width ) + " :" ;
99
- name += std::to_string (chunk.offset ) + " ]" ;
96
+ int lhs = chunk.wire ->to_hdl_index (chunk.offset + chunk.width - 1 );
97
+ int rhs = chunk.wire ->to_hdl_index (chunk.offset );
98
+
99
+ if (lhs != rhs)
100
+ name += stringf (" [%d:%d]" , lhs, rhs);
101
+ else
102
+ name += stringf (" [%d]" , lhs);
100
103
}
101
104
}
102
105
106
+ RTLIL::Wire *wire;
107
+
108
+ if (move_to_cell && (!(wire = cell->module ->wire (name)) || !(wire->port_input || wire->port_output )))
109
+ return name;
110
+
103
111
if (suffix.empty ()) {
104
112
suffix = cell->type .str ();
105
113
}
@@ -232,13 +240,17 @@ struct RenamePass : public Pass {
232
240
log (" cells with private names.\n " );
233
241
log (" \n " );
234
242
log (" \n " );
235
- log (" rename -wire [selection] [-suffix <suffix>]\n " );
243
+ log (" rename -wire [selection] [-move-to-cell] [- suffix <suffix>]\n " );
236
244
log (" \n " );
237
245
log (" Assign auto-generated names based on the wires they drive to all selected\n " );
238
246
log (" cells with private names. Ignores cells driving privatly named wires.\n " );
239
247
log (" By default, the cell is named after the wire with the cell type as suffix.\n " );
240
248
log (" The -suffix option can be used to set the suffix to the given string instead.\n " );
241
249
log (" \n " );
250
+ log (" The -move-to-cell option can be used to name the cell after the wire without\n " );
251
+ log (" any suffix. If this would lead to conflicts, the suffix is added to the wire\n " );
252
+ log (" instead. For cells driving ports, the -move-to-cell option is ignored.\n " );
253
+ log (" \n " );
242
254
log (" \n " );
243
255
log (" rename -enumerate [-pattern <pattern>] [selection]\n " );
244
256
log (" \n " );
@@ -286,6 +298,7 @@ struct RenamePass : public Pass {
286
298
std::string cell_suffix = " " ;
287
299
bool flag_src = false ;
288
300
bool flag_wire = false ;
301
+ bool flag_move_to_cell = false ;
289
302
bool flag_enumerate = false ;
290
303
bool flag_witness = false ;
291
304
bool flag_hide = false ;
@@ -345,6 +358,10 @@ struct RenamePass : public Pass {
345
358
got_mode = true ;
346
359
continue ;
347
360
}
361
+ if (arg == " -move-to-cell" && flag_wire && !flag_move_to_cell) {
362
+ flag_move_to_cell = true ;
363
+ continue ;
364
+ }
348
365
if (arg == " -pattern" && argidx+1 < args.size () && args[argidx+1 ].find (' %' ) != std::string::npos) {
349
366
int pos = args[++argidx].find (' %' );
350
367
pattern_prefix = args[argidx].substr (0 , pos);
@@ -396,9 +413,26 @@ struct RenamePass : public Pass {
396
413
dict<RTLIL::Cell *, IdString> new_cell_names;
397
414
for (auto cell : module ->selected_cells ())
398
415
if (cell->name [0 ] == ' $' )
399
- new_cell_names[cell] = derive_name_from_cell_output_wire (cell, cell_suffix);
400
- for (auto &it : new_cell_names)
401
- module ->rename (it.first , it.second );
416
+ new_cell_names[cell] = derive_name_from_cell_output_wire (cell, cell_suffix, flag_move_to_cell);
417
+ for (auto &[cell, new_name] : new_cell_names) {
418
+ if (flag_move_to_cell) {
419
+ RTLIL::Wire *found_wire = module ->wire (new_name);
420
+ if (found_wire) {
421
+ std::string wire_suffix = cell_suffix;
422
+ if (wire_suffix.empty ()) {
423
+ for (auto const &[port, _] : cell->connections ()) {
424
+ if (cell->output (port)) {
425
+ wire_suffix += stringf (" %s.%s" , cell->type .c_str (), port.c_str () + 1 );
426
+ break ;
427
+ }
428
+ }
429
+ }
430
+ IdString new_wire_name = found_wire->name .str () + wire_suffix;
431
+ module ->rename (found_wire, new_wire_name);
432
+ }
433
+ }
434
+ module ->rename (cell, new_name);
435
+ }
402
436
}
403
437
}
404
438
else
0 commit comments