simulator WIP: use petgraph for topological sort over assignments

This commit is contained in:
Jacob Lifshay 2024-11-13 04:14:04 -08:00
parent 3106a6fff6
commit a6e40839ac
Signed by: programmerjake
SSH key fingerprint: SHA256:HnFTLGpSm4Q4Fj502oCFisjZSoakwEuTsJJMSke63RQ
5 changed files with 887 additions and 145 deletions

17
Cargo.lock generated
View file

@ -315,6 +315,7 @@ dependencies = [
"num-bigint", "num-bigint",
"num-traits", "num-traits",
"os_pipe", "os_pipe",
"petgraph",
"serde", "serde",
"serde_json", "serde_json",
"tempfile", "tempfile",
@ -358,6 +359,12 @@ dependencies = [
"thiserror", "thiserror",
] ]
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]] [[package]]
name = "funty" name = "funty"
version = "2.0.0" version = "2.0.0"
@ -515,6 +522,16 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "petgraph"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]] [[package]]
name = "prettyplease" name = "prettyplease"
version = "0.2.20" version = "0.2.20"

View file

@ -29,6 +29,7 @@ jobslot = "0.2.19"
num-bigint = "0.4.6" num-bigint = "0.4.6"
num-traits = "0.2.16" num-traits = "0.2.16"
os_pipe = "1.2.1" os_pipe = "1.2.1"
petgraph = "0.6.5"
prettyplease = "0.2.20" prettyplease = "0.2.20"
proc-macro2 = "1.0.83" proc-macro2 = "1.0.83"
quote = "1.0.36" quote = "1.0.36"

View file

@ -25,6 +25,7 @@ jobslot.workspace = true
num-bigint.workspace = true num-bigint.workspace = true
num-traits.workspace = true num-traits.workspace = true
os_pipe.workspace = true os_pipe.workspace = true
petgraph.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde.workspace = true serde.workspace = true
tempfile.workspace = true tempfile.workspace = true

File diff suppressed because it is too large Load diff

View file

@ -362,6 +362,12 @@ pub(crate) struct Insns<BK: InsnsBuildingKind> {
state_layout: StateLayout<BK>, state_layout: StateLayout<BK>,
} }
impl<BK: InsnsBuildingKind> Insns<BK> {
pub(crate) fn state_layout(&self) -> &StateLayout<BK> {
&self.state_layout
}
}
struct InsnsDebug<'a> { struct InsnsDebug<'a> {
insns: &'a [Insn], insns: &'a [Insn],
insn_source_locations: &'a [SourceLocation], insn_source_locations: &'a [SourceLocation],
@ -564,13 +570,13 @@ macro_rules! make_state_part_kinds {
impl Copy for StateLayout<InsnsBuildingDone> {} impl Copy for StateLayout<InsnsBuildingDone> {}
impl<BK: InsnsBuildingKind> StateLayout<BK> { impl<BK: InsnsBuildingKind> StateLayout<BK> {
pub(crate) fn len(self) -> StateLen { pub(crate) fn len(&self) -> StateLen {
StateLen { StateLen {
ty: self.ty.len(), ty: self.ty.len(),
$($state_field: self.$state_field.len(),)* $($state_field: self.$state_field.len(),)*
} }
} }
pub(crate) fn is_empty(self) -> bool { pub(crate) fn is_empty(&self) -> bool {
self.ty.is_empty() $(&& self.$state_field.is_empty())* self.ty.is_empty() $(&& self.$state_field.is_empty())*
} }
pub(crate) fn empty() -> Self { pub(crate) fn empty() -> Self {
@ -652,12 +658,12 @@ macro_rules! make_state_part_kinds {
impl Copy for TypeLayout<InsnsBuildingDone> {} impl Copy for TypeLayout<InsnsBuildingDone> {}
impl<BK: InsnsBuildingKind> TypeLayout<BK> { impl<BK: InsnsBuildingKind> TypeLayout<BK> {
pub(crate) fn len(self) -> TypeLen { pub(crate) fn len(&self) -> TypeLen {
TypeLen { TypeLen {
$($type_field: self.$type_field.len(),)* $($type_field: self.$type_field.len(),)*
} }
} }
pub(crate) fn is_empty(self) -> bool { pub(crate) fn is_empty(&self) -> bool {
$(self.$type_field.is_empty())&&+ $(self.$type_field.is_empty())&&+
} }
pub(crate) fn empty() -> Self { pub(crate) fn empty() -> Self {
@ -762,6 +768,14 @@ macro_rules! make_state_part_kinds {
} }
impl State { impl State {
pub(crate) fn new(insns: Interned<Insns<InsnsBuildingDone>>) -> Self {
Self {
insns,
pc: 0,
$($state_field: StatePart::new(insns.state_layout.$state_field.len()),)*
$($type_field: StatePart::new(insns.state_layout.ty.$type_field.len()),)*
}
}
pub(crate) fn borrow(&mut self) -> BorrowedState<'_> { pub(crate) fn borrow(&mut self) -> BorrowedState<'_> {
BorrowedState { BorrowedState {
orig_insns: self.insns, orig_insns: self.insns,
@ -1494,6 +1508,13 @@ impl<K: StatePartKind, V: fmt::Debug> fmt::Debug for StatePartIndexMap<K, V> {
} }
} }
impl<K: StatePartKind, V> Extend<(StatePartIndex<K>, V)> for StatePartIndexMap<K, V> {
fn extend<T: IntoIterator<Item = (StatePartIndex<K>, V)>>(&mut self, iter: T) {
self.map
.extend(iter.into_iter().map(|(k, v)| (k.as_usize(), v)));
}
}
impl<K: StatePartKind, V> StatePartIndexMap<K, V> { impl<K: StatePartKind, V> StatePartIndexMap<K, V> {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
@ -1834,6 +1855,17 @@ impl_insns! {
} }
next!(); next!();
} }
CastBigToArrayIndex {
#[kind = Output]
dest: StatePartIndex<StatePartKindSmallSlots>,
#[kind = Input]
src: StatePartIndex<StatePartKindBigSlots>,
} => {
let src = &state.big_slots[src];
let value = src.try_into().unwrap_or(SmallUInt::MAX);
state.small_slots[dest] = value;
next!();
}
CastToSInt { CastToSInt {
#[kind = Output] #[kind = Output]
dest: StatePartIndex<StatePartKindBigSlots>, dest: StatePartIndex<StatePartKindBigSlots>,
@ -2055,6 +2087,20 @@ impl_insns! {
state.big_slots[dest] = value; state.big_slots[dest] = value;
next!(); next!();
} }
SliceInt {
#[kind = Output]
dest: StatePartIndex<StatePartKindBigSlots>,
#[kind = Input]
src: StatePartIndex<StatePartKindBigSlots>,
#[kind = Immediate]
start: usize,
#[kind = Immediate]
len: usize,
} => {
let value = &state.big_slots[src] >> start;
state.big_slots[dest] = value & &*bigint_mask(len);
next!();
}
CmpEq { CmpEq {
#[kind = Output] #[kind = Output]
dest: StatePartIndex<StatePartKindBigSlots>, dest: StatePartIndex<StatePartKindBigSlots>,