Skip to content

Commit

Permalink
Removed UUID::INVALID
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Dec 19, 2023
1 parent 7bd9167 commit 43b691b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 43 deletions.
27 changes: 4 additions & 23 deletions src/arena_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::{ops::{IndexMut, Index}, marker::PhantomData, iter::Enumerate, fmt};

use crate::block_vector::{BlockVec, BlockVecIterMut, BlockVecIter};


// TODO add custom niche for more efficient Options, wait until custom niches are stabilized (https://internals.rust-lang.org/t/nonmaxusize-and-niche-value-optimisation/19661)
// Maybe use NonZeroUsize (https://doc.rust-lang.org/std/num/struct.NonZeroUsize.html)
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct UUID<IndexMarker : UUIDMarker>(usize, PhantomData<IndexMarker>);

Expand All @@ -12,25 +15,11 @@ pub trait UUIDMarker {
impl<IndexMarker : UUIDMarker> fmt::Debug for UUID<IndexMarker> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(IndexMarker::DISPLAY_NAME)?;
if self.0 == Self::INVALID.0 {
f.write_str("INV")
} else {
self.0.fmt(f)
}
}
}

impl<IndexMarker : UUIDMarker> Default for UUID<IndexMarker> {
fn default() -> Self {
Self::INVALID
self.0.fmt(f)
}
}

const INVALID_UUID_VALUE : usize = usize::MAX;

impl<IndexMarker : UUIDMarker> UUID<IndexMarker> {
pub const INVALID : Self = UUID(INVALID_UUID_VALUE, PhantomData);

pub const fn from_hidden_value(v : usize) -> Self {
UUID(v, PhantomData)
}
Expand Down Expand Up @@ -97,15 +86,13 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ArenaAllocator<T,
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
assert!(self.data[uuid].is_some());
self.data[uuid].as_ref().unwrap()
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ArenaAllocator<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
assert!(self.data[uuid].is_some());
self.data[uuid].as_mut().unwrap()
}
Expand Down Expand Up @@ -208,14 +195,12 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ArenaVector<T, In
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
self.data[uuid].as_ref().unwrap()
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ArenaVector<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
self.data[uuid].as_mut().unwrap()
}
}
Expand Down Expand Up @@ -269,14 +254,12 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for ListAllocator<T,
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
&self.data[uuid]
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for ListAllocator<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
&mut self.data[uuid]
}
}
Expand Down Expand Up @@ -383,14 +366,12 @@ impl<T, IndexMarker : UUIDMarker> Index<UUID<IndexMarker>> for FlatAlloc<T, Inde
type Output = T;

fn index(&self, UUID(uuid, _): UUID<IndexMarker>) -> &Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index");
&self.data[uuid]
}
}

impl<T, IndexMarker : UUIDMarker> IndexMut<UUID<IndexMarker>> for FlatAlloc<T, IndexMarker> {
fn index_mut(&mut self, UUID(uuid, _): UUID<IndexMarker>) -> &mut Self::Output {
assert!(uuid != INVALID_UUID_VALUE, "Invalid UUID passed to index_mut");
&mut self.data[uuid]
}
}
Expand Down
30 changes: 14 additions & 16 deletions src/flattening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Instantiation {
}

struct FlatteningContext<'l, 'm, 'fl> {
decl_to_flat_map : FlatAlloc<FlatID, DeclIDMarker>,
decl_to_flat_map : FlatAlloc<Option<FlatID>, DeclIDMarker>,
instantiations : &'fl ListAllocator<Instantiation, FlatIDMarker>,
errors : &'fl ErrorCollector,

Expand Down Expand Up @@ -153,7 +153,7 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
let (name_expr, name_expr_span) = &func_and_args[0]; // Function name is always there
let func_instantiation_id = match name_expr {
Expression::Named(LocalOrGlobal::Local(l)) => {
self.decl_to_flat_map[*l]
self.decl_to_flat_map[*l].unwrap()
}
Expression::Named(LocalOrGlobal::Global(g)) => {
let module_ref = self.module.link_info.global_references[*g];
Expand Down Expand Up @@ -205,8 +205,7 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
fn flatten_single_expr(&self, (expr, expr_span) : &SpanExpression, condition : Option<FlatID>) -> Option<SpanFlatID> {
let single_connection_side = match expr {
Expression::Named(LocalOrGlobal::Local(l)) => {
assert!(self.decl_to_flat_map[*l] != UUID::INVALID);
self.decl_to_flat_map[*l]
self.decl_to_flat_map[*l].unwrap()
}
Expression::Named(LocalOrGlobal::Global(g)) => {
let r = self.module.link_info.global_references[*g];
Expand Down Expand Up @@ -260,7 +259,7 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
fn flatten_assignable_expr(&self, (expr, span) : &SpanAssignableExpression, condition : Option<FlatID>) -> Option<ConnectionWrite> {
Some(match expr {
AssignableExpression::Named{local_idx} => {
let root = self.decl_to_flat_map[*local_idx];
let root = self.decl_to_flat_map[*local_idx].unwrap();
let WireSource::NamedWire { read_only, identifier_type : _, decl_id : _ } = &self.instantiations[root].extract_wire().inst else {
unreachable!("Attempting to assign to a Instantiation::PlainWire")
};
Expand Down Expand Up @@ -300,14 +299,11 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
Statement::Declaration(decl_id) => {
let decl = &self.module.declarations[*decl_id];

let typ = self.map_to_type(&decl.typ.0, &self.module.link_info.global_references) else {continue;};
let typ = self.map_to_type(&decl.typ.0, &self.module.link_info.global_references);
let typ_span = decl.typ.1;

let decl_typ_root_reference = typ.get_root();
let inst = if decl_typ_root_reference == UUID::INVALID {
Instantiation::Error // Error's covered by linker
} else {
match &self.linker.links.globals[decl_typ_root_reference] {
let inst = if let Some(root_ref) = typ.get_root() {
match &self.linker.links.globals[root_ref] {
Named::Constant(c) => {
self.errors.error_basic(typ_span, format!("This should be the type of a declaration, but it refers to the constant '{}'", c.get_full_name()));
Instantiation::Error
Expand All @@ -326,10 +322,12 @@ impl<'l, 'm, 'fl> FlatteningContext<'l, 'm, 'fl> {
Instantiation::Wire(WireInstance{typ, span : typ_span, inst : WireSource::NamedWire{read_only : false, identifier_type : decl.identifier_type, decl_id : Some(*decl_id)}})
}
}
} else {
Instantiation::Error // Error's covered by linker
};

let wire_id = self.instantiations.alloc(inst);
self.decl_to_flat_map[*decl_id] = wire_id;
self.decl_to_flat_map[*decl_id] = Some(wire_id);
}
Statement::If{condition : condition_expr, then, els} => {
let Some(if_statement_condition) = self.flatten_single_expr(condition_expr, condition) else {continue;};
Expand Down Expand Up @@ -444,7 +442,7 @@ impl FlattenedModule {
Produces an initial FlattenedModule, in which the interface types have already been resolved.
Must be further processed by flatten, but this requires all modules to have been Initial Flattened for dependency resolution
*/
pub fn initialize_interfaces(linker : &Linker, module : &Module) -> (FlattenedInterface, FlattenedModule, FlatAlloc<FlatID, DeclIDMarker>) {
pub fn initialize_interfaces(linker : &Linker, module : &Module) -> (FlattenedInterface, FlattenedModule, FlatAlloc<Option<FlatID>, DeclIDMarker>) {
let mut interface = FlattenedInterface::new();

let flat_mod = FlattenedModule {
Expand All @@ -453,7 +451,7 @@ impl FlattenedModule {
};

let mut context = FlatteningContext{
decl_to_flat_map: module.declarations.iter().map(|_| UUID::INVALID).collect(),
decl_to_flat_map: module.declarations.iter().map(|_| None).collect(),
instantiations: &flat_mod.instantiations,
errors: &flat_mod.errors,
linker,
Expand All @@ -471,7 +469,7 @@ impl FlattenedModule {
let wire_id = context.instantiations.alloc(Instantiation::Wire(WireInstance{typ : typ.clone(), span : decl.typ.1, inst : WireSource::NamedWire{read_only: is_input, identifier_type : decl.identifier_type, decl_id : Some(decl_id)}}));

interface.interface_wires.push(FlattenedInterfacePort { wire_id, is_input, typ, port_name: decl.name.clone(), span: decl.span });
context.decl_to_flat_map[decl_id] = wire_id;
context.decl_to_flat_map[decl_id] = Some(wire_id);
}

let decl_to_flat_map = context.decl_to_flat_map;
Expand All @@ -484,7 +482,7 @@ impl FlattenedModule {
The Generating Structure of the code is not yet executed.
It is template-preserving
*/
pub fn flatten(&self, module : &Module, linker : &Linker, decl_to_flat_map : FlatAlloc<FlatID, DeclIDMarker>) {
pub fn flatten(&self, module : &Module, linker : &Linker, decl_to_flat_map : FlatAlloc<Option<FlatID>, DeclIDMarker>) {
let mut context = FlatteningContext {
decl_to_flat_map : decl_to_flat_map,
instantiations : &self.instantiations,
Expand Down
2 changes: 1 addition & 1 deletion src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ impl Linker {
// First create initial flattening for everything, to produce the necessary interfaces

let mut interface_vec : Vec<(NamedUUID, FlattenedInterface, FlattenedModule)> = Vec::new();
let mut initial_flat_vec : Vec<(NamedUUID, FlatAlloc<FlatID, DeclIDMarker>)> = Vec::new();
let mut initial_flat_vec : Vec<(NamedUUID, FlatAlloc<Option<FlatID>, DeclIDMarker>)> = Vec::new();
for (id, named_object) in &self.links.globals {
println!("Initializing Interface for {}", named_object.get_name());
if let Named::Module(md) = named_object {
Expand Down
6 changes: 3 additions & 3 deletions src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl Type {
Type::Array(sub) => sub.deref().0.to_string(linker) + "[]",
}
}
pub fn get_root(&self) -> NamedUUID {
pub fn get_root(&self) -> Option<NamedUUID> {
match self {
Type::Invalid => {unreachable!()}
Type::Named(name) => *name,
Type::Invalid => {None}
Type::Named(name) => Some(*name),
Type::Array(sub) => sub.0.get_root(),
}
}
Expand Down

0 comments on commit 43b691b

Please sign in to comment.