yosys/kernel/unstable/patch.cc

139 lines
3.4 KiB
C++
Raw Permalink Normal View History

2025-12-19 19:14:33 +01:00
#include "kernel/unstable/patch.h"
2025-12-31 17:46:27 +01:00
#include "kernel/celltypes.h"
2026-05-19 12:36:41 +02:00
#include "kernel/log.h"
2025-12-31 17:46:27 +01:00
#include "kernel/rtlil.h"
2025-12-19 19:14:33 +01:00
YOSYS_NAMESPACE_BEGIN
2025-12-31 17:59:24 +01:00
/**
* Notes
*
* If we want GC, we need more indices
* namely user count (and users?). This should be optional
*
*
2025-12-31 17:59:24 +01:00
*/
2025-12-19 19:14:33 +01:00
using namespace RTLIL;
template class CellAdderMixin<Patch>;
2025-12-19 19:14:33 +01:00
Cell* Patch::addCell(IdString name, IdString type) {
2026-05-19 12:36:41 +02:00
cells_.push_back(std::make_unique<Cell>(Cell::ConstructToken{}));
2026-05-14 17:43:46 +02:00
Cell* cell = cells_.back().get();
2026-05-19 12:36:41 +02:00
cell->name = name;
2026-05-14 17:43:46 +02:00
cell->type = type;
cell->module = nullptr;
2026-05-14 17:43:46 +02:00
return cell;
}
Wire* Patch::addWire(IdString name, int width) {
2026-05-19 15:33:41 +02:00
wires_.push_back(std::make_unique<Wire>(Wire::ConstructToken{}));
Wire* wire = wires_.back().get();
wire->name = name;
wire->width = width;
wire->module = nullptr;
2026-05-19 15:33:41 +02:00
return wire;
}
// TODO code golf
2026-05-19 15:57:10 +02:00
2026-05-19 15:33:41 +02:00
RTLIL::Wire *RTLIL::Patch::addWire(RTLIL::IdString name, const RTLIL::Wire *other)
{
RTLIL::Wire *wire = addWire(std::move(name));
wire->width = other->width;
wire->start_offset = other->start_offset;
wire->port_id = other->port_id;
wire->port_input = other->port_input;
wire->port_output = other->port_output;
wire->upto = other->upto;
wire->is_signed = other->is_signed;
wire->attributes = other->attributes;
return wire;
2025-12-19 19:14:33 +01:00
}
2026-05-19 19:31:16 +02:00
void Patch::collect_src(Cell* old_cell) {
src.insert(old_cell->get_src_attribute());
log("collect %s\n", old_cell->name);
std::vector<Cell*> inputs = {};
for (auto [port_name, sig] : old_cell->connections()) {
auto dir = old_cell->port_dir(port_name);
log_assert(dir != PD_UNKNOWN);
log_assert(!sig.size() || sig.is_wire());
if (dir == PD_INPUT || dir == PD_INOUT) {
Wire* in_wire = sig.as_wire();
if (!leaves.count(in_wire))
inputs.push_back(in_wire->driverCell());
}
}
for (auto input : inputs)
collect_src(input);
}
void Patch::gc(Cell* old_cell) {
log("gc %s\n", old_cell->name);
std::vector<Cell*> inputs = {};
for (auto [port_name, sig] : old_cell->connections()) {
auto dir = old_cell->port_dir(port_name);
log_assert(dir != PD_UNKNOWN);
log_assert(!sig.size() || sig.is_wire());
if (dir == PD_OUTPUT || dir == PD_INOUT) {
if (sig.size()) {
for (auto bit : sig) {
// Reject GC if used
if (!mod->fanout(bit).empty())
return;
}
}
}
if (dir == PD_INPUT || dir == PD_INOUT) {
Wire* in_wire = sig.as_wire();
if (!leaves.count(in_wire))
inputs.push_back(in_wire->driverCell());
}
}
for (auto input : inputs)
gc(input);
}
2026-05-19 12:36:41 +02:00
void Patch::patch(Cell* old_cell, Cell* new_cell) {
2026-05-19 19:31:16 +02:00
log_assert(!leaves.empty());
collect_src(old_cell);
std::string src_str = AttrObject::strpool_attribute_to_str(src);
2026-05-19 15:33:41 +02:00
for (auto& wire: wires_) {
wire->module = mod;
2026-05-19 15:33:41 +02:00
Wire* raw = wire.release();
mod->wires_[raw->name] = raw;
}
2026-05-19 12:36:41 +02:00
log("patching:\n");
log_cell(old_cell);
for (auto& cell: cells_) {
log_cell(cell.get());
2026-05-19 19:31:16 +02:00
cell->set_src_attribute(src_str);
2026-05-19 12:36:41 +02:00
Cell* raw = cell.release();
mod->cells_[raw->name] = raw;
for (auto [port_name, sig] : raw->connections()) {
auto dir = raw->port_dir(port_name);
log_assert(dir != PD_UNKNOWN);
if (dir == PD_OUTPUT || dir == PD_INOUT) {
if (raw == new_cell) {
2026-05-19 12:36:41 +02:00
// RAUW
auto yoink = old_cell->getPort(port_name);
log(">>>> RAUW %s to %s\n", port_name, log_signal(yoink));
new_cell->setPort(port_name, yoink);
old_cell->setPort(port_name, mod->addWire(NEW_ID, yoink.size()));
}
}
}
raw->module = mod;
raw->initIndex();
raw->fixup_parameters();
}
2026-05-19 12:36:41 +02:00
log_module(mod, "");
2026-05-19 19:31:16 +02:00
gc(old_cell);
2025-12-31 17:46:27 +01:00
}
2025-12-19 19:14:33 +01:00
YOSYS_NAMESPACE_END