Skip to content

dump: add --sorted flag #5003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions backends/rtlil/rtlil_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,30 @@ void RTLIL_BACKEND::dump_conn(std::ostream &f, std::string indent, const RTLIL::
f << stringf("\n");
}

void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Module *module, RTLIL::Design *design, bool only_selected, bool flag_m, bool flag_n)
void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Module *module, RTLIL::Design *design, bool only_selected, bool flag_m, bool flag_n, bool flag_sorted)
{
bool print_header = flag_m || design->selected_whole_module(module->name);
bool print_body = !flag_n || !design->selected_whole_module(module->name);

std::unique_ptr<Design> d(new Design);
if (flag_sorted) {
Module* new_module = d->addModule(module->name);
module->cloneInto(new_module);
module = new_module;
module->sort();
for (auto& [lhs, rhs] : module->connections_) {
if (std::string(log_signal(lhs)) < std::string(log_signal(rhs))) {
std::swap(lhs, rhs);
}
}
std::sort(module->connections_.begin(), module->connections_.end(), [](const auto &a, const auto &b) {
if (std::string(log_signal(a.first)) != std::string(log_signal(b.first))) {
return std::string(log_signal(a.first)) < std::string(log_signal(b.first));
}
return std::string(log_signal(a.second)) < std::string(log_signal(b.second));
});
}

if (print_header)
{
for (auto it = module->attributes.begin(); it != module->attributes.end(); ++it) {
Expand Down Expand Up @@ -386,9 +405,10 @@ void RTLIL_BACKEND::dump_module(std::ostream &f, std::string indent, RTLIL::Modu

if (print_header)
f << stringf("%s" "end\n", indent.c_str());

}

void RTLIL_BACKEND::dump_design(std::ostream &f, RTLIL::Design *design, bool only_selected, bool flag_m, bool flag_n)
void RTLIL_BACKEND::dump_design(std::ostream &f, RTLIL::Design *design, bool only_selected, bool flag_m, bool flag_n, bool flag_sorted)
{
int init_autoidx = autoidx;

Expand All @@ -414,7 +434,7 @@ void RTLIL_BACKEND::dump_design(std::ostream &f, RTLIL::Design *design, bool onl
if (!only_selected || design->selected(module)) {
if (only_selected)
f << stringf("\n");
dump_module(f, "", module, design, only_selected, flag_m, flag_n);
dump_module(f, "", module, design, only_selected, flag_m, flag_n, flag_sorted);
}
}

Expand Down Expand Up @@ -488,11 +508,14 @@ struct DumpPass : public Pass {
log(" -a <filename>\n");
log(" like -outfile but append instead of overwrite\n");
log("\n");
log(" --sorted\n");
log(" dump sorted representation for nicer diffs. Doesn't modify design\n");
log("\n");
}
void execute(std::vector<std::string> args, RTLIL::Design *design) override
{
std::string filename;
bool flag_m = false, flag_n = false, append = false;
bool flag_m = false, flag_n = false, append = false, flag_sorted = false;

size_t argidx;
for (argidx = 1; argidx < args.size(); argidx++)
Expand All @@ -516,6 +539,10 @@ struct DumpPass : public Pass {
flag_n = true;
continue;
}
if (arg == "--sorted") {
flag_sorted = true;
continue;
}
break;
}
extra_args(args, argidx, design);
Expand All @@ -537,7 +564,7 @@ struct DumpPass : public Pass {
f = &buf;
}

RTLIL_BACKEND::dump_design(*f, design, true, flag_m, flag_n);
RTLIL_BACKEND::dump_design(*f, design, true, flag_m, flag_n, flag_sorted);

if (!empty) {
delete f;
Expand Down
4 changes: 2 additions & 2 deletions backends/rtlil/rtlil_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace RTLIL_BACKEND {
void dump_proc_sync(std::ostream &f, std::string indent, const RTLIL::SyncRule *sy);
void dump_proc(std::ostream &f, std::string indent, const RTLIL::Process *proc);
void dump_conn(std::ostream &f, std::string indent, const RTLIL::SigSpec &left, const RTLIL::SigSpec &right);
void dump_module(std::ostream &f, std::string indent, RTLIL::Module *module, RTLIL::Design *design, bool only_selected, bool flag_m = true, bool flag_n = false);
void dump_design(std::ostream &f, RTLIL::Design *design, bool only_selected, bool flag_m = true, bool flag_n = false);
void dump_module(std::ostream &f, std::string indent, RTLIL::Module *module, RTLIL::Design *design, bool only_selected, bool flag_m = true, bool flag_n = false, bool flag_sorted = false);
void dump_design(std::ostream &f, RTLIL::Design *design, bool only_selected, bool flag_m = true, bool flag_n = false, bool flag_sorted = false);
}

YOSYS_NAMESPACE_END
Expand Down
Loading