Skip to content

Commit

Permalink
start fleshing out Epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronKutch committed Oct 2, 2023
1 parent aaad772 commit 500713a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 39 deletions.
61 changes: 31 additions & 30 deletions starlight/src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
/// An epoch management struct used for tests and examples.
use std::{
cell::RefCell,
mem,
num::{NonZeroU64, NonZeroUsize},
thread::panicking,
};
use std::{cell::RefCell, mem, num::NonZeroUsize, thread::panicking};

use awint::{
awint_dag::{
epoch::{EpochCallback, EpochKey},
triple_arena::Arena,
Location, Op, PNote, PState,
Lineage, Location, Op, PState,
},
dag,
bw, dag,
};

use crate::{PBack, TDag};
use crate::TDag;

#[derive(Debug, Clone)]
pub struct Assertions {
pub bits: Vec<PNote>,
pub bits: Vec<PState>,
}

impl Assertions {
Expand All @@ -38,8 +32,8 @@ impl Default for Assertions {
struct EpochData {
key: EpochKey,
assertions: Assertions,
/// Backref to a `Referent::State`
states: Arena<PState, PBack>,
/// All states associated with this epoch
states: Vec<PState>,
}

struct TopEpochData {
Expand Down Expand Up @@ -74,18 +68,30 @@ pub fn _callback() -> EpochCallback {
fn new_pstate(nzbw: NonZeroUsize, op: Op<PState>, location: Option<Location>) -> PState {
EPOCH_DATA_TOP.with(|top| {
let mut top = top.borrow_mut();
let backref = todo!(); //top.tdag.make_state(nzbw, op, location);
top.data.states.insert(backref)
let p_state = top.tdag.make_state(nzbw, op, location);
top.data.states.push(p_state);
p_state
})
}
fn register_assertion_bit(bit: dag::bool, location: Location) {
todo!()
// need a new bit to attach location data to
let new_bit = new_pstate(bw(1), Op::Copy([bit.state()]), Some(location));
EPOCH_DATA_TOP.with(|top| {
let mut top = top.borrow_mut();
top.data.assertions.bits.push(new_bit);
})
}
fn get_nzbw(p_state: PState) -> NonZeroUsize {
todo!()
EPOCH_DATA_TOP.with(|top| {
let top = top.borrow();
top.tdag.states.get(p_state).unwrap().nzbw
})
}
fn get_op(p_state: PState) -> Op<PState> {
todo!()
EPOCH_DATA_TOP.with(|top| {
let top = top.borrow();
top.tdag.states.get(p_state).unwrap().op.clone()
})
}
EpochCallback {
new_pstate,
Expand All @@ -106,15 +112,14 @@ impl Drop for Epoch {
if !panicking() {
// unregister callback
self.key.pop_off_epoch_stack();
todo!();
/*EPOCH_DATA_TOP.with(|top| {
EPOCH_DATA_TOP.with(|top| {
let mut top = top.borrow_mut();
// remove all the states associated with this epoch
let mut last_state = top.data.prev_in_epoch;
while let Some(p_state) = last_state {
let state = top.arena.remove(p_state).unwrap();
last_state = state.prev_in_epoch;
for _p_state in top.data.states.iter() {
// TODO
//top.tdag.states.remove(*p_state).unwrap();
}
top.tdag = TDag::new();
// move the top of the stack to the new top
let new_top = EPOCH_DATA_STACK.with(|stack| {
let mut stack = stack.borrow_mut();
Expand All @@ -125,13 +130,9 @@ impl Drop for Epoch {
} else {
top.active = false;
top.data = EpochData::default();
// if there is considerable capacity, clear it (else we do not want to incur
// allocations for rapid state epoch creation)
if top.arena.capacity() > 64 {
top.arena.clear_and_shrink();
}
// TODO capacity clearing?
}
});*/
});
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions starlight/src/t_dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ pub enum Referent {
/// Represents the state resulting from a mimicking operation
#[derive(Debug, Clone)]
pub struct State {
pub p_self_bits: SmallVec<[PBack; 4]>,
/// Bitwidth
pub nzbw: NonZeroUsize,
/// This either has zero length or has a length equal to `nzbw`
pub p_self_bits: SmallVec<[PBack; 4]>,
/// Operation
pub op: Op<PState>,
/// Location where this state is derived from
Expand Down Expand Up @@ -358,14 +358,20 @@ impl TDag {
Ok(())
}

/*pub fn make_state(&mut self, nzbw: NonZeroUsize, op: Op<PState>, location: Option<Location>) -> PBack {
self.backrefs.insert_with(|p_self_equiv| {
(
Referent::State(p_state),
Equiv::new(p_self_equiv, V)
)
pub fn make_state(
&mut self,
nzbw: NonZeroUsize,
op: Op<PState>,
location: Option<Location>,
) -> PState {
self.states.insert(State {
nzbw,
p_self_bits: SmallVec::new(),
op,
location,
visit: NonZeroU64::new(2).unwrap(),
})
}*/
}

/// Inserts a `TNode` with `lit` value and returns a `PBack` to it
pub fn make_literal(&mut self, lit: Option<bool>) -> PBack {
Expand Down
30 changes: 30 additions & 0 deletions testcrate/tests/epoch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use starlight::{dag::*, Epoch};

#[test]
#[should_panic]
fn state_epoch_unregistered0() {
let _x = ExtAwi::zero(bw(1));
}

#[test]
#[should_panic]
fn state_epoch_unregistered1() {
let _x: u8 = 7.into();
}

#[test]
#[should_panic]
fn state_epoch_unregistered2() {
let epoch0 = Epoch::new();
drop(epoch0);
let _x: inlawi_ty!(1) = InlAwi::zero();
}

#[test]
#[should_panic]
fn state_epoch_fail() {
let epoch0 = Epoch::new();
let epoch1 = Epoch::new();
drop(epoch0);
drop(epoch1);
}

0 comments on commit 500713a

Please sign in to comment.