rust: add getBels() binding

This commit is contained in:
Lofty 2025-03-01 12:15:40 +00:00
parent e2307a37b2
commit f4640b9aee
2 changed files with 29 additions and 7 deletions

View File

@ -239,10 +239,10 @@ impl Context {
v
}
pub fn wires_leaking(&self) -> &[WireId] {
let mut wires = std::ptr::null_mut();
let len = unsafe { npnr_context_get_wires_leak(self, &mut wires as *mut *mut WireId) };
unsafe { std::slice::from_raw_parts(wires, len as usize) }
pub fn bels_leaking(&self) -> &[BelId] {
let mut bels = std::ptr::null_mut();
let len = unsafe { npnr_context_get_bels_leak(self, &mut bels as *mut *mut BelId) };
unsafe { std::slice::from_raw_parts(bels, len as usize) }
}
pub fn pips_leaking(&self) -> &[PipId] {
@ -251,6 +251,12 @@ impl Context {
unsafe { std::slice::from_raw_parts(pips, len as usize) }
}
pub fn wires_leaking(&self) -> &[WireId] {
let mut wires = std::ptr::null_mut();
let len = unsafe { npnr_context_get_wires_leak(self, &mut wires as *mut *mut WireId) };
unsafe { std::slice::from_raw_parts(wires, len as usize) }
}
pub fn get_downhill_pips(&self, wire: WireId) -> DownhillPipsIter {
let iter = unsafe { npnr_context_get_pips_downhill(self, wire) };
DownhillPipsIter {
@ -377,8 +383,9 @@ extern "C" {
fn npnr_context_delay_epsilon(ctx: &Context) -> f32;
fn npnr_context_get_pip_delay(ctx: &Context, pip: PipId) -> f32;
fn npnr_context_get_wire_delay(ctx: &Context, wire: WireId) -> f32;
fn npnr_context_get_wires_leak(ctx: &Context, wires: *mut *mut WireId) -> u64;
fn npnr_context_get_bels_leak(ctx: &Context, bels: *mut *mut BelId) -> u64;
fn npnr_context_get_pips_leak(ctx: &Context, pips: *mut *mut PipId) -> u64;
fn npnr_context_get_wires_leak(ctx: &Context, wires: *mut *mut WireId) -> u64;
fn npnr_context_get_pip_location(ctx: &Context, pip: PipId) -> Loc;
fn npnr_context_check_pip_avail_for_net(
ctx: &Context,

View File

@ -129,6 +129,21 @@ bool npnr_context_check_pip_avail_for_net(const Context *ctx, uint64_t pip, NetI
return ctx->checkPipAvailForNet(unwrap_pip(pip), net);
}
uint64_t npnr_context_get_bels_leak(const Context *ctx, uint64_t **const bels)
{
const auto ctx_bels{ctx->getBels()};
const auto size{std::accumulate(ctx_bels.begin(), ctx_bels.end(), /*initial value*/ size_t{},
[](size_t value, const auto & /*bel*/) { return value + 1U; })};
*bels = new uint64_t[size];
auto idx = 0;
for (const auto &bel : ctx_bels) {
(*bels)[idx] = wrap(bel);
idx++;
}
// Yes, by never deleting ctx_bels, we leak memory.
return size;
}
uint64_t npnr_context_get_pips_leak(const Context *ctx, uint64_t **const pips)
{
const auto ctx_pips{ctx->getPips()};
@ -140,7 +155,7 @@ uint64_t npnr_context_get_pips_leak(const Context *ctx, uint64_t **const pips)
(*pips)[idx] = wrap(pip);
idx++;
}
// Yes, by never deleting pip_vec, we leak memory.
// Yes, by never deleting ctx_pips, we leak memory.
return size;
}
@ -155,7 +170,7 @@ uint64_t npnr_context_get_wires_leak(const Context *ctx, uint64_t **const wires)
(*wires)[idx] = wrap(wire);
idx++;
}
// Yes, by never deleting wires, we leak memory.
// Yes, by never deleting ctx_wires, we leak memory.
return size;
}