2013-06-10 05:37:22 -05:00
|
|
|
/*
|
|
|
|
* yosys -- Yosys Open SYnthesis Suite
|
|
|
|
*
|
2021-06-07 17:39:36 -05:00
|
|
|
* Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-06-10 05:37:22 -05:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2015-07-02 04:14:30 -05:00
|
|
|
*
|
2013-06-10 05:37:22 -05:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel/register.h"
|
|
|
|
#include "kernel/rtlil.h"
|
|
|
|
#include "kernel/log.h"
|
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
USING_YOSYS_NAMESPACE
|
|
|
|
PRIVATE_NAMESPACE_BEGIN
|
|
|
|
|
2019-03-27 07:47:42 -05:00
|
|
|
static void rename_in_module(RTLIL::Module *module, std::string from_name, std::string to_name, bool flag_output)
|
2013-06-10 05:37:22 -05:00
|
|
|
{
|
2013-06-19 09:55:43 -05:00
|
|
|
from_name = RTLIL::escape_id(from_name);
|
|
|
|
to_name = RTLIL::escape_id(to_name);
|
|
|
|
|
|
|
|
if (module->count_id(to_name))
|
|
|
|
log_cmd_error("There is already an object `%s' in module `%s'.\n", to_name.c_str(), module->name.c_str());
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
RTLIL::Wire *wire_to_rename = module->wire(from_name);
|
|
|
|
RTLIL::Cell *cell_to_rename = module->cell(from_name);
|
2013-06-19 09:55:43 -05:00
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
if (wire_to_rename != nullptr) {
|
|
|
|
log("Renaming wire %s to %s in module %s.\n", log_id(wire_to_rename), log_id(to_name), log_id(module));
|
|
|
|
module->rename(wire_to_rename, to_name);
|
|
|
|
if (wire_to_rename->port_id || flag_output) {
|
2019-03-27 07:47:42 -05:00
|
|
|
if (flag_output)
|
2020-04-08 02:22:10 -05:00
|
|
|
wire_to_rename->port_output = true;
|
|
|
|
module->fixup_ports();
|
2013-06-19 09:55:43 -05:00
|
|
|
}
|
2020-04-08 02:22:10 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell_to_rename != nullptr) {
|
|
|
|
if (flag_output)
|
|
|
|
log_cmd_error("Called with -output but the specified object is a cell.\n");
|
|
|
|
log("Renaming cell %s to %s in module %s.\n", log_id(cell_to_rename), log_id(to_name), log_id(module));
|
|
|
|
module->rename(cell_to_rename, to_name);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-19 09:55:43 -05:00
|
|
|
|
|
|
|
log_cmd_error("Object `%s' not found!\n", from_name.c_str());
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
|
|
|
|
2018-12-05 14:34:53 -06:00
|
|
|
static std::string derive_name_from_src(const std::string &src, int counter)
|
|
|
|
{
|
|
|
|
std::string src_base = src.substr(0, src.find('|'));
|
|
|
|
if (src_base.empty())
|
|
|
|
return stringf("$%d", counter);
|
|
|
|
else
|
|
|
|
return stringf("\\%s$%d", src_base.c_str(), counter);
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:05:13 -06:00
|
|
|
static IdString derive_name_from_cell_output_wire(const RTLIL::Cell *cell, string suffix)
|
2019-01-05 19:40:10 -06:00
|
|
|
{
|
|
|
|
// Find output
|
|
|
|
const SigSpec *output = nullptr;
|
|
|
|
int num_outputs = 0;
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto &connection : cell->connections()) {
|
|
|
|
if (cell->output(connection.first)) {
|
2019-01-05 19:40:10 -06:00
|
|
|
output = &connection.second;
|
|
|
|
num_outputs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_outputs != 1) // Skip cells thad drive multiple outputs
|
2020-04-08 02:22:10 -05:00
|
|
|
return cell->name;
|
2019-01-05 19:40:10 -06:00
|
|
|
|
|
|
|
std::string name = "";
|
|
|
|
for (auto &chunk : output->chunks()) {
|
|
|
|
// Skip cells that drive privately named wires
|
|
|
|
if (!chunk.wire || chunk.wire->name.str()[0] == '$')
|
2020-04-08 02:22:10 -05:00
|
|
|
return cell->name;
|
2019-01-05 19:40:10 -06:00
|
|
|
|
|
|
|
if (name != "")
|
|
|
|
name += "$";
|
|
|
|
|
|
|
|
name += chunk.wire->name.str();
|
|
|
|
if (chunk.wire->width != chunk.width) {
|
|
|
|
name += "[";
|
|
|
|
if (chunk.width != 1)
|
|
|
|
name += std::to_string(chunk.offset + chunk.width) + ":";
|
|
|
|
name += std::to_string(chunk.offset) + "]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:05:13 -06:00
|
|
|
if (suffix.empty()) {
|
|
|
|
suffix = cell->type.str();
|
|
|
|
}
|
|
|
|
return name + suffix;
|
2019-01-05 19:40:10 -06:00
|
|
|
}
|
|
|
|
|
2013-06-10 05:37:22 -05:00
|
|
|
struct RenamePass : public Pass {
|
|
|
|
RenamePass() : Pass("rename", "rename object in the design") { }
|
2020-06-18 18:34:52 -05:00
|
|
|
void help() override
|
2013-06-10 05:37:22 -05:00
|
|
|
{
|
|
|
|
// |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
|
|
|
|
log("\n");
|
|
|
|
log(" rename old_name new_name\n");
|
|
|
|
log("\n");
|
|
|
|
log("Rename the specified object. Note that selection patterns are not supported\n");
|
|
|
|
log("by this command.\n");
|
|
|
|
log("\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2019-03-27 07:47:42 -05:00
|
|
|
log("\n");
|
|
|
|
log(" rename -output old_name new_name\n");
|
|
|
|
log("\n");
|
|
|
|
log("Like above, but also make the wire an output. This will fail if the object is\n");
|
|
|
|
log("not a wire.\n");
|
|
|
|
log("\n");
|
|
|
|
log("\n");
|
2018-12-05 14:34:53 -06:00
|
|
|
log(" rename -src [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Assign names auto-generated from the src attribute to all selected wires and\n");
|
|
|
|
log("cells with private names.\n");
|
2013-08-07 11:39:49 -05:00
|
|
|
log("\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2022-02-10 17:05:13 -06:00
|
|
|
log(" rename -wire [selection] [-suffix <suffix>]\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2019-01-05 19:40:10 -06:00
|
|
|
log("Assign auto-generated names based on the wires they drive to all selected\n");
|
|
|
|
log("cells with private names. Ignores cells driving privatly named wires.\n");
|
2022-06-13 02:35:10 -05:00
|
|
|
log("By default, the cell is named after the wire with the cell type as suffix.\n");
|
|
|
|
log("The -suffix option can be used to set the suffix to the given string instead.\n");
|
2019-01-05 19:40:10 -06:00
|
|
|
log("\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2014-08-26 05:51:08 -05:00
|
|
|
log(" rename -enumerate [-pattern <pattern>] [selection]\n");
|
2013-08-07 11:39:49 -05:00
|
|
|
log("\n");
|
|
|
|
log("Assign short auto-generated names to all selected wires and cells with private\n");
|
2014-08-26 05:51:08 -05:00
|
|
|
log("names. The -pattern option can be used to set the pattern for the new names.\n");
|
|
|
|
log("The character %% in the pattern is replaced with a integer number. The default\n");
|
|
|
|
log("pattern is '_%%_'.\n");
|
2013-08-07 11:39:49 -05:00
|
|
|
log("\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2014-01-02 13:23:34 -06:00
|
|
|
log(" rename -hide [selection]\n");
|
|
|
|
log("\n");
|
|
|
|
log("Assign private names (the ones with $-prefix) to all selected wires and cells\n");
|
|
|
|
log("with public names. This ignores all selected ports.\n");
|
|
|
|
log("\n");
|
2019-03-27 07:33:26 -05:00
|
|
|
log("\n");
|
2015-06-17 02:38:56 -05:00
|
|
|
log(" rename -top new_name\n");
|
|
|
|
log("\n");
|
|
|
|
log("Rename top module.\n");
|
|
|
|
log("\n");
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
2020-06-18 18:34:52 -05:00
|
|
|
void execute(std::vector<std::string> args, RTLIL::Design *design) override
|
2013-06-10 05:37:22 -05:00
|
|
|
{
|
2014-08-26 05:51:08 -05:00
|
|
|
std::string pattern_prefix = "_", pattern_suffix = "_";
|
2022-02-10 17:05:13 -06:00
|
|
|
std::string cell_suffix = "";
|
2018-12-05 14:34:53 -06:00
|
|
|
bool flag_src = false;
|
2019-01-05 19:40:10 -06:00
|
|
|
bool flag_wire = false;
|
2013-06-10 05:37:22 -05:00
|
|
|
bool flag_enumerate = false;
|
2014-01-02 13:23:34 -06:00
|
|
|
bool flag_hide = false;
|
2015-06-17 02:38:56 -05:00
|
|
|
bool flag_top = false;
|
2019-03-27 07:47:42 -05:00
|
|
|
bool flag_output = false;
|
2014-01-02 13:23:34 -06:00
|
|
|
bool got_mode = false;
|
2013-06-10 05:37:22 -05:00
|
|
|
|
|
|
|
size_t argidx;
|
|
|
|
for (argidx = 1; argidx < args.size(); argidx++)
|
|
|
|
{
|
|
|
|
std::string arg = args[argidx];
|
2018-12-05 14:34:53 -06:00
|
|
|
if (arg == "-src" && !got_mode) {
|
|
|
|
flag_src = true;
|
|
|
|
got_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-27 07:47:42 -05:00
|
|
|
if (arg == "-output" && !got_mode) {
|
|
|
|
flag_output = true;
|
|
|
|
got_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-05 19:40:10 -06:00
|
|
|
if (arg == "-wire" && !got_mode) {
|
|
|
|
flag_wire = true;
|
|
|
|
got_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-02 13:23:34 -06:00
|
|
|
if (arg == "-enumerate" && !got_mode) {
|
2013-08-07 11:39:49 -05:00
|
|
|
flag_enumerate = true;
|
2014-01-02 13:23:34 -06:00
|
|
|
got_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg == "-hide" && !got_mode) {
|
|
|
|
flag_hide = true;
|
|
|
|
got_mode = true;
|
2013-08-07 11:39:49 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-17 02:38:56 -05:00
|
|
|
if (arg == "-top" && !got_mode) {
|
|
|
|
flag_top = true;
|
|
|
|
got_mode = true;
|
|
|
|
continue;
|
|
|
|
}
|
2014-08-26 05:51:08 -05:00
|
|
|
if (arg == "-pattern" && argidx+1 < args.size() && args[argidx+1].find('%') != std::string::npos) {
|
|
|
|
int pos = args[++argidx].find('%');
|
|
|
|
pattern_prefix = args[argidx].substr(0, pos);
|
|
|
|
pattern_suffix = args[argidx].substr(pos+1);
|
|
|
|
continue;
|
|
|
|
}
|
2022-02-10 17:05:13 -06:00
|
|
|
if (arg == "-suffix" && argidx + 1 < args.size()) {
|
|
|
|
cell_suffix = args[++argidx];
|
|
|
|
}
|
2013-06-10 05:37:22 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:34:53 -06:00
|
|
|
if (flag_src)
|
|
|
|
{
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto module : design->selected_modules())
|
2018-12-05 14:34:53 -06:00
|
|
|
{
|
|
|
|
int counter = 0;
|
2020-04-13 19:35:47 -05:00
|
|
|
dict<RTLIL::Wire *, IdString> new_wire_names;
|
|
|
|
dict<RTLIL::Cell *, IdString> new_cell_names;
|
2020-04-08 02:22:10 -05:00
|
|
|
|
|
|
|
for (auto wire : module->selected_wires())
|
|
|
|
if (wire->name[0] == '$')
|
2020-04-13 19:35:47 -05:00
|
|
|
new_wire_names.emplace(wire, derive_name_from_src(wire->get_src_attribute(), counter++));
|
2020-04-08 02:22:10 -05:00
|
|
|
|
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
if (cell->name[0] == '$')
|
2020-04-13 19:35:47 -05:00
|
|
|
new_cell_names.emplace(cell, derive_name_from_src(cell->get_src_attribute(), counter++));
|
2018-12-05 14:34:53 -06:00
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto &it : new_wire_names)
|
|
|
|
module->rename(it.first, it.second);
|
|
|
|
|
|
|
|
for (auto &it : new_cell_names)
|
|
|
|
module->rename(it.first, it.second);
|
2018-12-05 14:34:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-01-05 19:40:10 -06:00
|
|
|
if (flag_wire)
|
|
|
|
{
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto module : design->selected_modules()) {
|
2020-04-13 19:35:47 -05:00
|
|
|
dict<RTLIL::Cell *, IdString> new_cell_names;
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
if (cell->name[0] == '$')
|
2022-02-10 17:05:13 -06:00
|
|
|
new_cell_names[cell] = derive_name_from_cell_output_wire(cell, cell_suffix);
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto &it : new_cell_names)
|
|
|
|
module->rename(it.first, it.second);
|
2019-01-05 19:40:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-06-10 05:37:22 -05:00
|
|
|
if (flag_enumerate)
|
|
|
|
{
|
|
|
|
extra_args(args, argidx, design);
|
2013-08-07 11:39:49 -05:00
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto module : design->selected_modules())
|
2013-08-07 11:39:49 -05:00
|
|
|
{
|
|
|
|
int counter = 0;
|
2020-04-13 19:35:47 -05:00
|
|
|
dict<RTLIL::Wire *, IdString> new_wire_names;
|
|
|
|
dict<RTLIL::Cell *, IdString> new_cell_names;
|
2020-04-08 02:22:10 -05:00
|
|
|
|
|
|
|
for (auto wire : module->selected_wires())
|
|
|
|
if (wire->name[0] == '$') {
|
2020-04-09 14:31:12 -05:00
|
|
|
RTLIL::IdString buf;
|
|
|
|
do buf = stringf("\\%s%d%s", pattern_prefix.c_str(), counter++, pattern_suffix.c_str());
|
|
|
|
while (module->wire(buf) != nullptr);
|
|
|
|
new_wire_names[wire] = buf;
|
2020-04-08 02:22:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto cell : module->selected_cells())
|
|
|
|
if (cell->name[0] == '$') {
|
2020-04-09 14:31:12 -05:00
|
|
|
RTLIL::IdString buf;
|
|
|
|
do buf = stringf("\\%s%d%s", pattern_prefix.c_str(), counter++, pattern_suffix.c_str());
|
|
|
|
while (module->cell(buf) != nullptr);
|
|
|
|
new_cell_names[cell] = buf;
|
2020-04-08 02:22:10 -05:00
|
|
|
}
|
2013-08-07 11:39:49 -05:00
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto &it : new_wire_names)
|
|
|
|
module->rename(it.first, it.second);
|
|
|
|
|
|
|
|
for (auto &it : new_cell_names)
|
|
|
|
module->rename(it.first, it.second);
|
2014-01-02 13:23:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (flag_hide)
|
|
|
|
{
|
|
|
|
extra_args(args, argidx, design);
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
for (auto module : design->selected_modules())
|
2014-01-02 13:23:34 -06:00
|
|
|
{
|
2020-04-13 19:35:47 -05:00
|
|
|
dict<RTLIL::Wire *, IdString> new_wire_names;
|
|
|
|
dict<RTLIL::Cell *, IdString> new_cell_names;
|
2020-04-08 02:22:10 -05:00
|
|
|
|
|
|
|
for (auto wire : module->selected_wires())
|
2020-09-14 05:43:18 -05:00
|
|
|
if (wire->name.isPublic() && wire->port_id == 0)
|
2020-04-08 02:22:10 -05:00
|
|
|
new_wire_names[wire] = NEW_ID;
|
|
|
|
|
|
|
|
for (auto cell : module->selected_cells())
|
2020-09-14 05:43:18 -05:00
|
|
|
if (cell->name.isPublic())
|
2020-04-08 02:22:10 -05:00
|
|
|
new_cell_names[cell] = NEW_ID;
|
|
|
|
|
|
|
|
for (auto &it : new_wire_names)
|
|
|
|
module->rename(it.first, it.second);
|
|
|
|
|
|
|
|
for (auto &it : new_cell_names)
|
|
|
|
module->rename(it.first, it.second);
|
2013-08-07 11:39:49 -05:00
|
|
|
}
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
|
|
|
else
|
2015-06-17 02:38:56 -05:00
|
|
|
if (flag_top)
|
|
|
|
{
|
|
|
|
if (argidx+1 != args.size())
|
|
|
|
log_cmd_error("Invalid number of arguments!\n");
|
|
|
|
|
|
|
|
IdString new_name = RTLIL::escape_id(args[argidx]);
|
|
|
|
RTLIL::Module *module = design->top_module();
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
if (module == nullptr)
|
2015-06-17 02:38:56 -05:00
|
|
|
log_cmd_error("No top module found!\n");
|
|
|
|
|
|
|
|
log("Renaming module %s to %s.\n", log_id(module), log_id(new_name));
|
2015-06-29 18:37:59 -05:00
|
|
|
design->rename(module, new_name);
|
2015-06-17 02:38:56 -05:00
|
|
|
}
|
|
|
|
else
|
2013-06-10 05:37:22 -05:00
|
|
|
{
|
|
|
|
if (argidx+2 != args.size())
|
|
|
|
log_cmd_error("Invalid number of arguments!\n");
|
|
|
|
|
|
|
|
std::string from_name = args[argidx++];
|
|
|
|
std::string to_name = args[argidx++];
|
|
|
|
|
|
|
|
if (!design->selected_active_module.empty())
|
|
|
|
{
|
2020-04-08 02:22:10 -05:00
|
|
|
if (design->module(design->selected_active_module) != nullptr)
|
|
|
|
rename_in_module(design->module(design->selected_active_module), from_name, to_name, flag_output);
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-27 07:47:42 -05:00
|
|
|
if (flag_output)
|
|
|
|
log_cmd_error("Mode -output requires that there is an active module selected.\n");
|
2020-04-08 02:22:10 -05:00
|
|
|
|
|
|
|
RTLIL::Module *module_to_rename = nullptr;
|
|
|
|
for (auto module : design->modules())
|
|
|
|
if (module->name == from_name || RTLIL::unescape_id(module->name) == from_name) {
|
|
|
|
module_to_rename = module;
|
|
|
|
break;
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:22:10 -05:00
|
|
|
if (module_to_rename != nullptr) {
|
|
|
|
to_name = RTLIL::escape_id(to_name);
|
|
|
|
log("Renaming module %s to %s.\n", module_to_rename->name.c_str(), to_name.c_str());
|
|
|
|
design->rename(module_to_rename, to_name);
|
|
|
|
} else
|
|
|
|
log_cmd_error("Object `%s' not found!\n", from_name.c_str());
|
2013-06-10 05:37:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} RenamePass;
|
2015-07-02 04:14:30 -05:00
|
|
|
|
2014-09-27 09:17:53 -05:00
|
|
|
PRIVATE_NAMESPACE_END
|