1
0
mirror of https://github.com/YosysHQ/nextpnr.git synced 2026-01-11 23:53:21 +00:00

Rust cleanup (#1573)

* rust: formatting cleanup

* rust: explicitly mark as ISC license

* rust: use std::ffi C types instead of libc dependency
This commit is contained in:
Lofty 2025-10-10 15:04:19 +01:00 committed by GitHub
parent 4b00f58af5
commit 35810c9f87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 34 deletions

View File

@ -8,7 +8,9 @@ pub extern "C" fn rust_example_printnets(ctx: &mut Context) {
let driver = net.driver(); let driver = net.driver();
println!(" {}:", ctx.name_of(name).to_str().unwrap()); println!(" {}:", ctx.name_of(name).to_str().unwrap());
if let Some(driver) = driver { if let Some(driver) = driver {
let cell = driver.cell().map_or("(no cell)", |cell| ctx.name_of(cell.name()).to_str().unwrap()); let cell = driver.cell().map_or("(no cell)", |cell| {
ctx.name_of(cell.name()).to_str().unwrap()
});
let port = ctx.name_of(driver.port()).to_str().unwrap(); let port = ctx.name_of(driver.port()).to_str().unwrap();
println!(" driver: {cell}.{port}"); println!(" driver: {cell}.{port}");
} else { } else {

View File

@ -2,6 +2,7 @@
name = "nextpnr" name = "nextpnr"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
license = "ISC"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -11,4 +12,3 @@ path = "src/lib.rs"
crate-type = ["rlib"] crate-type = ["rlib"]
[dependencies] [dependencies]
libc = "0.2"

View File

@ -1,6 +1,4 @@
use std::{ffi::CStr, marker::PhantomData, sync::Mutex}; use std::{ffi::{c_char, c_int, CStr, CString}, marker::PhantomData, sync::Mutex};
use libc::c_char;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
@ -53,9 +51,9 @@ impl NetInfo {
#[must_use] #[must_use]
pub fn users(&self) -> NetUserIter<'_> { pub fn users(&self) -> NetUserIter<'_> {
NetUserIter { NetUserIter {
iter: unsafe { npnr_context_net_user_iter(self) }, iter: unsafe { npnr_context_net_user_iter(self) },
phantom_data: PhantomData phantom_data: PhantomData,
} }
} }
} }
@ -91,7 +89,7 @@ impl PortRef {
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)] #[repr(transparent)]
pub struct IdString(libc::c_int); pub struct IdString(c_int);
/// A type representing a bel name. /// A type representing a bel name.
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
@ -163,9 +161,9 @@ impl WireId {
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Loc { pub struct Loc {
pub x: libc::c_int, pub x: c_int,
pub y: libc::c_int, pub y: c_int,
pub z: libc::c_int, pub z: c_int,
} }
impl From<(i32, i32)> for Loc { impl From<(i32, i32)> for Loc {
@ -297,19 +295,28 @@ impl Context {
#[must_use] #[must_use]
pub fn bels(&self) -> BelIter<'_> { pub fn bels(&self) -> BelIter<'_> {
let iter = unsafe { npnr_context_get_bels(self) }; let iter = unsafe { npnr_context_get_bels(self) };
BelIter { iter, phantom_data: PhantomData } BelIter {
iter,
phantom_data: PhantomData,
}
} }
#[must_use] #[must_use]
pub fn pips(&self) -> PipIter<'_> { pub fn pips(&self) -> PipIter<'_> {
let iter = unsafe { npnr_context_get_pips(self) }; let iter = unsafe { npnr_context_get_pips(self) };
PipIter { iter, phantom_data: PhantomData } PipIter {
iter,
phantom_data: PhantomData,
}
} }
#[must_use] #[must_use]
pub fn wires(&self) -> WireIter<'_> { pub fn wires(&self) -> WireIter<'_> {
let iter = unsafe { npnr_context_get_wires(self) }; let iter = unsafe { npnr_context_get_wires(self) };
WireIter { iter, phantom_data: PhantomData } WireIter {
iter,
phantom_data: PhantomData,
}
} }
#[must_use] #[must_use]
@ -337,8 +344,8 @@ impl Context {
#[must_use] #[must_use]
pub fn pip_direction(&self, pip: PipId) -> Loc { pub fn pip_direction(&self, pip: PipId) -> Loc {
let mut src = Loc{x: 0, y: 0, z: 0}; let mut src = Loc { x: 0, y: 0, z: 0 };
let mut dst = Loc{x: 0, y: 0, z: 0}; let mut dst = Loc { x: 0, y: 0, z: 0 };
let mut pips = 0; let mut pips = 0;
for pip in self.get_uphill_pips(self.pip_src_wire(pip)) { for pip in self.get_uphill_pips(self.pip_src_wire(pip)) {
@ -364,7 +371,11 @@ impl Context {
dst.y /= pips; dst.y /= pips;
} }
Loc{x: dst.x - src.x, y: dst.y - src.y, z: 0} Loc {
x: dst.x - src.x,
y: dst.y - src.y,
z: 0,
}
} }
#[must_use] #[must_use]
@ -383,7 +394,7 @@ impl Context {
#[must_use] #[must_use]
pub fn id(&self, s: &str) -> IdString { pub fn id(&self, s: &str) -> IdString {
let s = std::ffi::CString::new(s).unwrap(); let s = CString::new(s).unwrap();
unsafe { npnr_context_id(self, s.as_ptr()) } unsafe { npnr_context_id(self, s.as_ptr()) }
} }
@ -424,8 +435,8 @@ unsafe extern "C" {
safe fn npnr_wireid_null() -> WireId; safe fn npnr_wireid_null() -> WireId;
safe fn npnr_pipid_null() -> PipId; safe fn npnr_pipid_null() -> PipId;
fn npnr_context_get_grid_dim_x(ctx: &Context) -> libc::c_int; fn npnr_context_get_grid_dim_x(ctx: &Context) -> c_int;
fn npnr_context_get_grid_dim_y(ctx: &Context) -> libc::c_int; fn npnr_context_get_grid_dim_y(ctx: &Context) -> c_int;
fn npnr_context_bind_bel( fn npnr_context_bind_bel(
ctx: &mut Context, ctx: &mut Context,
bel: BelId, bel: BelId,
@ -455,18 +466,14 @@ unsafe extern "C" {
fn npnr_context_get_pip_delay(ctx: &Context, pip: PipId) -> 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_wire_delay(ctx: &Context, wire: WireId) -> f32;
fn npnr_context_get_pip_location(ctx: &Context, pip: PipId) -> Loc; fn npnr_context_get_pip_location(ctx: &Context, pip: PipId) -> Loc;
fn npnr_context_check_pip_avail_for_net( fn npnr_context_check_pip_avail_for_net(ctx: &Context, pip: PipId, net: &NetInfo) -> bool;
ctx: &Context,
pip: PipId,
net: &NetInfo,
) -> bool;
fn npnr_context_check(ctx: &Context); fn npnr_context_check(ctx: &Context);
fn npnr_context_debug(ctx: &Context) -> bool; fn npnr_context_debug(ctx: &Context) -> bool;
fn npnr_context_id(ctx: &Context, s: *const c_char) -> IdString; fn npnr_context_id(ctx: &Context, s: *const c_char) -> IdString;
fn npnr_context_name_of(ctx: &Context, s: IdString) -> *const libc::c_char; fn npnr_context_name_of(ctx: &Context, s: IdString) -> *const c_char;
fn npnr_context_name_of_pip(ctx: &Context, pip: PipId) -> *const libc::c_char; fn npnr_context_name_of_pip(ctx: &Context, pip: PipId) -> *const c_char;
fn npnr_context_name_of_wire(ctx: &Context, wire: WireId) -> *const libc::c_char; fn npnr_context_name_of_wire(ctx: &Context, wire: WireId) -> *const c_char;
fn npnr_context_verbose(ctx: &Context) -> bool; fn npnr_context_verbose(ctx: &Context) -> bool;
fn npnr_context_get_netinfo_source_wire(ctx: &Context, net: &NetInfo) -> WireId; fn npnr_context_get_netinfo_source_wire(ctx: &Context, net: &NetInfo) -> WireId;
@ -483,9 +490,9 @@ unsafe extern "C" {
fn npnr_netinfo_udata_set(net: &mut NetInfo, value: NetIndex); fn npnr_netinfo_udata_set(net: &mut NetInfo, value: NetIndex);
fn npnr_portref_cell(port: &PortRef) -> Option<&CellInfo>; fn npnr_portref_cell(port: &PortRef) -> Option<&CellInfo>;
fn npnr_portref_port(port: &PortRef) -> libc::c_int; fn npnr_portref_port(port: &PortRef) -> c_int;
fn npnr_cellinfo_get_location(cell: &CellInfo) -> Loc; fn npnr_cellinfo_get_location(cell: &CellInfo) -> Loc;
fn npnr_cellinfo_name(cell: &CellInfo) -> libc::c_int; fn npnr_cellinfo_name(cell: &CellInfo) -> c_int;
fn npnr_context_get_pips_downhill(ctx: &Context, wire: WireId) -> &mut RawDownhillIter; fn npnr_context_get_pips_downhill(ctx: &Context, wire: WireId) -> &mut RawDownhillIter;
fn npnr_delete_downhill_iter(iter: &mut RawDownhillIter); fn npnr_delete_downhill_iter(iter: &mut RawDownhillIter);
@ -520,14 +527,14 @@ unsafe extern "C" {
fn npnr_context_net_iter(ctx: &Context) -> &mut RawNetIter; fn npnr_context_net_iter(ctx: &Context) -> &mut RawNetIter;
fn npnr_delete_net_iter(iter: &mut RawNetIter); fn npnr_delete_net_iter(iter: &mut RawNetIter);
fn npnr_inc_net_iter(iter: &mut RawNetIter); fn npnr_inc_net_iter(iter: &mut RawNetIter);
fn npnr_deref_net_iter_first(iter: &RawNetIter) -> libc::c_int; fn npnr_deref_net_iter_first(iter: &RawNetIter) -> c_int;
fn npnr_deref_net_iter_second(iter: &RawNetIter) -> &mut NetInfo; fn npnr_deref_net_iter_second(iter: &RawNetIter) -> &mut NetInfo;
fn npnr_is_net_iter_done(iter: &RawNetIter) -> bool; fn npnr_is_net_iter_done(iter: &RawNetIter) -> bool;
fn npnr_context_cell_iter(ctx: &Context) -> &mut RawCellIter; fn npnr_context_cell_iter(ctx: &Context) -> &mut RawCellIter;
fn npnr_delete_cell_iter(iter: &mut RawCellIter); fn npnr_delete_cell_iter(iter: &mut RawCellIter);
fn npnr_inc_cell_iter(iter: &mut RawCellIter); fn npnr_inc_cell_iter(iter: &mut RawCellIter);
fn npnr_deref_cell_iter_first(iter: &mut RawCellIter) -> libc::c_int; fn npnr_deref_cell_iter_first(iter: &mut RawCellIter) -> c_int;
fn npnr_deref_cell_iter_second(iter: &mut RawCellIter) -> &mut CellInfo; fn npnr_deref_cell_iter_second(iter: &mut RawCellIter) -> &mut CellInfo;
fn npnr_is_cell_iter_done(iter: &mut RawCellIter) -> bool; fn npnr_is_cell_iter_done(iter: &mut RawCellIter) -> bool;
@ -535,7 +542,7 @@ unsafe extern "C" {
fn npnr_delete_net_user_iter(iter: &mut RawNetUserIter); fn npnr_delete_net_user_iter(iter: &mut RawNetUserIter);
fn npnr_inc_net_user_iter(iter: &mut RawNetUserIter); fn npnr_inc_net_user_iter(iter: &mut RawNetUserIter);
fn npnr_deref_net_user_iter_cell(iter: &mut RawNetUserIter) -> &mut CellInfo; fn npnr_deref_net_user_iter_cell(iter: &mut RawNetUserIter) -> &mut CellInfo;
fn npnr_deref_net_user_iter_port(iter: &mut RawNetUserIter) -> libc::c_int; fn npnr_deref_net_user_iter_port(iter: &mut RawNetUserIter) -> c_int;
fn npnr_is_net_user_iter_done(iter: &mut RawNetUserIter) -> bool; fn npnr_is_net_user_iter_done(iter: &mut RawNetUserIter) -> bool;
} }
@ -851,4 +858,3 @@ macro_rules! log_error {
unsafe { crate::npnr::npnr_log_error(s.as_ptr()); } unsafe { crate::npnr::npnr_log_error(s.as_ptr()); }
}; };
} }