Skip to content

Commit 18a7492

Browse files
committed
first round of review fixes
1 parent f65f9ec commit 18a7492

File tree

3 files changed

+116
-168
lines changed

3 files changed

+116
-168
lines changed

Diff for: CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ option(USE_RUST "Enable Rust bindings" OFF)
3434
if (USE_RUST)
3535
add_subdirectory(3rdparty/corrosion)
3636
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml PROFILE "release" IMPORTED_CRATES RUST_CRATES)
37-
aux_source_directory(rust RUST_FILES)
3837
endif()
3938

4039
if (USE_IPO)

Diff for: rust/nextpnr/src/lib.rs

+54-13
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ impl Context {
157157
}
158158

159159
/// Bind a given bel to a given cell with the given strength.
160-
pub fn bind_bel(&mut self, bel: BelId, cell: *mut CellInfo, strength: PlaceStrength) {
160+
///
161+
/// # Safety
162+
/// `cell` must be valid and not null.
163+
pub unsafe fn bind_bel(&mut self, bel: BelId, cell: *mut CellInfo, strength: PlaceStrength) {
161164
unsafe { npnr_context_bind_bel(self, bel, cell, strength) }
162165
}
163166

@@ -172,7 +175,10 @@ impl Context {
172175
}
173176

174177
/// Bind a wire to a net. This method must be used when binding a wire that is driven by a bel pin. Use bindPip() when binding a wire that is driven by a pip.
175-
pub fn bind_wire(&mut self, wire: WireId, net: *mut NetInfo, strength: PlaceStrength) {
178+
///
179+
/// # Safety
180+
/// `net` must be valid and not null.
181+
pub unsafe fn bind_wire(&mut self, wire: WireId, net: *mut NetInfo, strength: PlaceStrength) {
176182
unsafe { npnr_context_bind_wire(self, wire, net, strength) }
177183
}
178184

@@ -181,8 +187,11 @@ impl Context {
181187
unsafe { npnr_context_unbind_wire(self, wire) }
182188
}
183189

184-
/// Bid a pip to a net. This also bind the destination wire of that pip.
185-
pub fn bind_pip(&mut self, pip: PipId, net: *mut NetInfo, strength: PlaceStrength) {
190+
/// Bind a pip to a net. This also binds the destination wire of that pip.
191+
///
192+
/// # Safety
193+
/// `net` must be valid and not null.
194+
pub unsafe fn bind_pip(&mut self, pip: PipId, net: *mut NetInfo, strength: PlaceStrength) {
186195
unsafe { npnr_context_bind_pip(self, pip, net, strength) }
187196
}
188197

@@ -218,11 +227,15 @@ impl Context {
218227
unsafe { npnr_context_delay_epsilon(self) }
219228
}
220229

221-
pub fn source_wire(&self, net: *const NetInfo) -> WireId {
230+
/// # Safety
231+
/// `net` must be valid and not null.
232+
pub unsafe fn source_wire(&self, net: *const NetInfo) -> WireId {
222233
unsafe { npnr_context_get_netinfo_source_wire(self, net) }
223234
}
224235

225-
pub fn sink_wires(&self, net: *const NetInfo, sink: *const PortRef) -> Vec<WireId> {
236+
/// # Safety
237+
/// `net` and `sink` must be valid and not null.
238+
pub unsafe fn sink_wires(&self, net: *const NetInfo, sink: *const PortRef) -> Vec<WireId> {
226239
let mut v = Vec::new();
227240
let mut n = 0;
228241
loop {
@@ -269,10 +282,39 @@ impl Context {
269282
}
270283

271284
pub fn pip_direction(&self, pip: PipId) -> Loc {
272-
unsafe { npnr_context_get_pip_direction(self, pip) }
285+
let mut src = Loc{x: 0, y: 0, z: 0};
286+
let mut dst = Loc{x: 0, y: 0, z: 0};
287+
288+
let mut pips = 0;
289+
for pip in self.get_uphill_pips(self.pip_src_wire(pip)) {
290+
let loc = self.pip_location(pip);
291+
src.x += loc.x;
292+
src.y += loc.y;
293+
pips += 1;
294+
}
295+
if pips != 0 {
296+
src.x /= pips;
297+
src.y /= pips;
298+
}
299+
300+
let mut pips = 0;
301+
for pip in self.get_downhill_pips(self.pip_dst_wire(pip)) {
302+
let loc = self.pip_location(pip);
303+
dst.x += loc.x;
304+
dst.y += loc.y;
305+
pips += 1;
306+
}
307+
if pips != 0 {
308+
dst.x /= pips;
309+
dst.y /= pips;
310+
}
311+
312+
Loc{x: dst.x - src.x, y: dst.y - src.y, z: 0}
273313
}
274314

275-
pub fn pip_avail_for_net(&self, pip: PipId, net: *mut NetInfo) -> bool {
315+
/// # Safety
316+
/// `net` must be valid and not null.
317+
pub unsafe fn pip_avail_for_net(&self, pip: PipId, net: *mut NetInfo) -> bool {
276318
unsafe { npnr_context_check_pip_avail_for_net(self, pip, net) }
277319
}
278320

@@ -349,7 +391,6 @@ extern "C" {
349391
fn npnr_context_get_wires_leak(ctx: *const Context, wires: *mut *mut WireId) -> u64;
350392
fn npnr_context_get_pips_leak(ctx: *const Context, pips: *mut *mut PipId) -> u64;
351393
fn npnr_context_get_pip_location(ctx: *const Context, pip: PipId) -> Loc;
352-
fn npnr_context_get_pip_direction(ctx: *const Context, pip: PipId) -> Loc;
353394
fn npnr_context_check_pip_avail_for_net(
354395
ctx: *const Context,
355396
pip: PipId,
@@ -404,7 +445,6 @@ pub struct Nets<'a> {
404445
nets: HashMap<IdString, *mut NetInfo>,
405446
users: HashMap<IdString, &'a [&'a mut PortRef]>,
406447
index_to_net: Vec<IdString>,
407-
net_to_index: HashMap<*mut NetInfo, i32>,
408448
_data: PhantomData<&'a Context>,
409449
}
410450

@@ -428,7 +468,6 @@ impl<'a> Nets<'a> {
428468
let mut nets = HashMap::new();
429469
let mut users = HashMap::new();
430470
let mut index_to_net = Vec::new();
431-
let mut net_to_index = HashMap::new();
432471
for i in 0..size {
433472
let name = unsafe { IdString(*names.add(i as usize)) };
434473
let net = unsafe { *nets_ptr.add(i as usize) };
@@ -443,7 +482,6 @@ impl<'a> Nets<'a> {
443482
users.insert(name, users_slice);
444483
let index = index_to_net.len() as i32;
445484
index_to_net.push(name);
446-
net_to_index.insert(net, index);
447485
unsafe {
448486
npnr_netinfo_udata_set(net, NetIndex(index));
449487
}
@@ -453,7 +491,6 @@ impl<'a> Nets<'a> {
453491
nets,
454492
users,
455493
index_to_net,
456-
net_to_index,
457494
_data: PhantomData,
458495
}
459496
}
@@ -468,6 +505,10 @@ impl<'a> Nets<'a> {
468505
self.nets.len()
469506
}
470507

508+
pub fn is_empty(&self) -> bool {
509+
self.nets.len() == 0
510+
}
511+
471512
pub fn name_from_index(&self, index: NetIndex) -> IdString {
472513
self.index_to_net[index.0 as usize]
473514
}

0 commit comments

Comments
 (0)